diff options
author | Roman Birg <roman@cyngn.com> | 2015-02-16 17:28:02 -0800 |
---|---|---|
committer | Adnan Begovic <adnan@cyngn.com> | 2015-10-26 16:11:15 -0700 |
commit | f10a82bfca8032a3e4c4c4057aa873dddf6e4c7b (patch) | |
tree | bbdf9a34d068c13eed6e0c1715a34b25a4b70fb9 /src/com/android/settings/profiles | |
parent | 7c5caefa7ab469ea5eb1acb73e8afc1c2567d81f (diff) | |
download | packages_apps_Settings-f10a82bfca8032a3e4c4c4057aa873dddf6e4c7b.zip packages_apps_Settings-f10a82bfca8032a3e4c4c4057aa873dddf6e4c7b.tar.gz packages_apps_Settings-f10a82bfca8032a3e4c4c4057aa873dddf6e4c7b.tar.bz2 |
Settings: restore App groups with advanced mode
Change-Id: I2dc623ab969d881201b18346e492cf9ea5111587
Signed-off-by: Roman Birg <roman@cyngn.com>
Diffstat (limited to 'src/com/android/settings/profiles')
10 files changed, 997 insertions, 0 deletions
diff --git a/src/com/android/settings/profiles/AppGroupConfig.java b/src/com/android/settings/profiles/AppGroupConfig.java new file mode 100644 index 0000000..b140a10 --- /dev/null +++ b/src/com/android/settings/profiles/AppGroupConfig.java @@ -0,0 +1,318 @@ +/* + * Copyright (C) 2012 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.profiles; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.NotificationGroup; +import android.app.ProfileManager; +import android.content.Context; +import android.content.DialogInterface; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.PreferenceGroup; +import android.preference.PreferenceScreen; +import android.util.Log; +import android.view.ContextMenu; +import android.view.ContextMenu.ContextMenuInfo; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.AdapterView.AdapterContextMenuInfo; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.ListView; +import android.widget.Toast; + +import com.android.settings.R; +import com.android.settings.SettingsPreferenceFragment; +import com.android.settings.cyanogenmod.PackageListAdapter; +import com.android.settings.cyanogenmod.PackageListAdapter.PackageItem; + +public class AppGroupConfig extends SettingsPreferenceFragment + implements Preference.OnPreferenceChangeListener { + + private static String TAG = "AppGroupConfig"; + + private static final int DIALOG_APPS = 0; + + private static final int DELETE_CONFIRM = 1; + + private static final int DELETE_GROUP_CONFIRM = 2; + + public static final String PROFILE_SERVICE = "profile"; + + private ListView mListView; + + private PackageManager mPackageManager; + + private NotificationGroup mNotificationGroup; + + private ProfileManager mProfileManager; + + private NamePreference mNamePreference; + + private static final int MENU_DELETE = Menu.FIRST; + + private static final int MENU_ADD = Menu.FIRST + 1; + + private PackageListAdapter mAppAdapter; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mProfileManager = (ProfileManager) getActivity().getSystemService(PROFILE_SERVICE); + addPreferencesFromResource(R.xml.application_list); + + final Bundle args = getArguments(); + if (args != null) { + mNotificationGroup = (NotificationGroup) args.getParcelable("NotificationGroup"); + mPackageManager = getPackageManager(); + mAppAdapter = new PackageListAdapter(getActivity()); + + updatePackages(); + + setHasOptionsMenu(true); + } + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + MenuItem delete = menu.add(0, MENU_DELETE, 0, R.string.profile_menu_delete_title) + .setIcon(R.drawable.ic_menu_trash_holo_dark); + delete.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | + MenuItem.SHOW_AS_ACTION_WITH_TEXT); + + MenuItem addApplication = menu.add(0, MENU_ADD, 0, R.string.profiles_add) + .setIcon(R.drawable.ic_menu_add) + .setAlphabeticShortcut('a'); + addApplication.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | + MenuItem.SHOW_AS_ACTION_WITH_TEXT); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return super.onCreateView(inflater, container, savedInstanceState); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case MENU_DELETE: + deleteNotificationGroup(); + return true; + case MENU_ADD: + addNewApp(); + return true; + default: + return false; + } + } + + Preference mAddPreference; + + Preference mDeletePreference; + + private void updatePackages() { + PreferenceScreen prefSet = getPreferenceScreen(); + + // Add the General section + PreferenceGroup generalPrefs = (PreferenceGroup) prefSet.findPreference("general_section"); + if (generalPrefs != null) { + generalPrefs.removeAll(); + + // Name preference + mNamePreference = new NamePreference(getActivity(), mNotificationGroup.getName()); + mNamePreference.setOnPreferenceChangeListener(this); + generalPrefs.addPreference(mNamePreference); + } + + PreferenceGroup applicationsList = (PreferenceGroup) prefSet.findPreference("applications_list"); + if (applicationsList != null) { + applicationsList.removeAll(); + for (String pkg : mNotificationGroup.getPackages()) { + Preference pref = new Preference(getActivity()); + try { + PackageInfo group = mPackageManager.getPackageInfo(pkg, 0); + pref.setKey(group.packageName); + pref.setTitle(group.applicationInfo.loadLabel(mPackageManager)); + Drawable icon = group.applicationInfo.loadIcon(mPackageManager); + pref.setIcon(icon); + pref.setSelectable(true); + pref.setPersistent(false); + applicationsList.addPreference(pref); + } catch (NameNotFoundException e) { + e.printStackTrace(); + } + } + } + } + + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { + menu.add(0, R.string.profile_menu_delete_title, 0, R.string.profile_menu_delete_title); + } + + @Override + public boolean onContextItemSelected(MenuItem item) { + AdapterContextMenuInfo aMenuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); + PackageItem selectedGroup = (PackageItem) mListView.getItemAtPosition(aMenuInfo.position); + switch (item.getItemId()) { + case R.string.profile_menu_delete_title: + deleteAppFromGroup(selectedGroup); + return true; + } + return super.onOptionsItemSelected(item); + } + + private void deleteAppFromGroup(PackageItem selectedGroup) { + if (selectedGroup != null) { + mNotificationGroup.removePackage(selectedGroup.packageName); + updatePackages(); + } + } + + @Override + public void onPause() { + if (mNotificationGroup != null) { + mProfileManager.addNotificationGroup(mNotificationGroup); + } + super.onPause(); + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if (preference == mNamePreference) { + String name = mNamePreference.getName().toString(); + if (!name.equals(mNotificationGroup.getName())) { + if (!mProfileManager.notificationGroupExists(name)) { + mNotificationGroup.setName(name); + } else { + mNamePreference.setName(mNotificationGroup.getName()); + Toast.makeText(getActivity(), R.string.duplicate_appgroup_name, Toast.LENGTH_LONG).show(); + } + } + } + return true; + } + + @Override + public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { + if (preference instanceof Preference) { + String deleteItem = preference.getKey(); + removeApp(deleteItem); + return true; + } + return super.onPreferenceTreeClick(preferenceScreen, preference); + } + + private void addNewApp() { + showDialog(DIALOG_APPS); + // TODO: switch to using the built in app list rather than dialog box? + } + + private void removeApp(String key) { + mPackageToDelete = key.toString(); + showDialog(DELETE_CONFIRM); + } + + private void deleteNotificationGroup() { + showDialog(DELETE_GROUP_CONFIRM); + } + + @Override + public Dialog onCreateDialog(int id) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + final Dialog dialog; + switch (id) { + case DIALOG_APPS: + final ListView list = new ListView(getActivity()); + list.setAdapter(mAppAdapter); + builder.setTitle(R.string.profile_choose_app); + builder.setView(list); + dialog = builder.create(); + list.setOnItemClickListener(new OnItemClickListener() { + @Override + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { + PackageItem info = (PackageItem) parent.getItemAtPosition(position); + mNotificationGroup.addPackage(info.packageName); + updatePackages(); + dialog.cancel(); + } + }); + break; + case DELETE_CONFIRM: + builder.setMessage(R.string.profile_app_delete_confirm); + builder.setTitle(R.string.profile_menu_delete_title); + builder.setIconAttribute(android.R.attr.alertDialogIcon); + builder.setPositiveButton(android.R.string.yes, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + doDelete(); + } + }); + builder.setNegativeButton(android.R.string.no, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }); + dialog = builder.create(); + break; + case DELETE_GROUP_CONFIRM: + builder.setMessage(R.string.profile_delete_appgroup); + builder.setTitle(R.string.profile_menu_delete_title); + builder.setIconAttribute(android.R.attr.alertDialogIcon); + builder.setPositiveButton(android.R.string.yes, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + mProfileManager.removeNotificationGroup(mNotificationGroup); + mNotificationGroup = null; + finish(); + } + }); + builder.setNegativeButton(android.R.string.no, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + } + }); + dialog = builder.create(); + break; + default: + dialog = null; + } + return dialog; + } + + String mPackageToDelete; + + private void doDelete() { + mNotificationGroup.removePackage(mPackageToDelete); + updatePackages(); + } +}
\ No newline at end of file diff --git a/src/com/android/settings/profiles/AppGroupList.java b/src/com/android/settings/profiles/AppGroupList.java new file mode 100644 index 0000000..febea3f --- /dev/null +++ b/src/com/android/settings/profiles/AppGroupList.java @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2012 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.profiles; + +import java.util.UUID; + +import android.annotation.Nullable; +import android.app.AlertDialog; +import android.app.NotificationGroup; +import android.app.ProfileManager; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.PreferenceActivity; +import android.preference.PreferenceScreen; +import android.provider.Settings; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; +import com.android.internal.util.cm.ScreenType; +import com.android.settings.R; +import com.android.settings.SettingsPreferenceFragment; +import com.android.settings.Utils; + +public class AppGroupList extends SettingsPreferenceFragment { + + private static final String TAG = "AppGroupSettings"; + + private ProfileManager mProfileManager; + + private View mFab; + + // constant value that can be used to check return code from sub activity. + private static final int APP_GROUP_CONFIG = 1; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + addPreferencesFromResource(R.xml.appgroup_list); + mProfileManager = (ProfileManager) getActivity().getSystemService(Context.PROFILE_SERVICE); + } + + @Override + public void onResume() { + super.onResume(); + refreshList(); + + // On tablet devices remove the padding + if (ScreenType.isTablet(getActivity())) { + getListView().setPadding(0, 0, 0, 0); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + return inflater.inflate(R.layout.preference_list_with_fab, container, false); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + mFab = view.findViewById(R.id.floating_action_button); + mFab.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + addAppGroup(); + } + }); + } + + public void refreshList() { + PreferenceScreen appgroupList = getPreferenceScreen(); + appgroupList.removeAll(); + + // Add the existing app groups + for (NotificationGroup group : mProfileManager.getNotificationGroups()) { + PreferenceScreen pref = new PreferenceScreen(getActivity(), null); + pref.setKey(group.getUuid().toString()); + pref.setTitle(group.getName()); + pref.setPersistent(false); + appgroupList.addPreference(pref); + } + } + + @Override + public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { + if (preference instanceof PreferenceScreen) { + NotificationGroup group = mProfileManager.getNotificationGroup( + UUID.fromString(preference.getKey())); + editGroup(group); + } + return super.onPreferenceTreeClick(preferenceScreen, preference); + } + + private void addAppGroup() { + LayoutInflater inflater = getActivity().getLayoutInflater(); + View content = inflater.inflate(R.layout.profile_name_dialog, null); + final TextView prompt = (TextView) content.findViewById(R.id.prompt); + final EditText entry = (EditText) content.findViewById(R.id.name); + + prompt.setText(R.string.profile_appgroup_name_prompt); + + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(R.string.profile_new_appgroup); + builder.setView(content); + + builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String name = entry.getText().toString(); + if (!mProfileManager.notificationGroupExists(name)) { + NotificationGroup newGroup = new NotificationGroup(name); + mProfileManager.addNotificationGroup(newGroup); + + refreshList(); + } else { + Toast.makeText(getActivity(), + R.string.duplicate_appgroup_name, Toast.LENGTH_LONG).show(); + } + } + }); + builder.setNegativeButton(android.R.string.cancel, null); + + AlertDialog dialog = builder.create(); + dialog.show(); + } + + private void editGroup(NotificationGroup group) { + Bundle args = new Bundle(); + args.putParcelable("NotificationGroup", group); + + startFragment(this, AppGroupConfig.class.getName(), R.string.profile_appgroup_manage, + APP_GROUP_CONFIG, args); + } +}
\ No newline at end of file diff --git a/src/com/android/settings/profiles/NamePreference.java b/src/com/android/settings/profiles/NamePreference.java new file mode 100644 index 0000000..0a89974 --- /dev/null +++ b/src/com/android/settings/profiles/NamePreference.java @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2012 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.profiles; + +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; +import android.preference.Preference; +import android.view.View; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.android.settings.R; + +public class NamePreference extends Preference implements + View.OnClickListener, Preference.OnPreferenceChangeListener { + private static final String TAG = NamePreference.class.getSimpleName(); + + private TextView mNameView; + + private String mName; + + /** + * @param context + * @param title + */ + public NamePreference(Context context, String name) { + super(context); + mName = name.toString(); + init(); + } + + /** + * @param context + */ + public NamePreference(Context context) { + super(context); + init(); + } + + @Override + public void onBindView(View view) { + super.onBindView(view); + + View namePref = view.findViewById(R.id.name_pref); + if ((namePref != null) && namePref instanceof LinearLayout) { + namePref.setOnClickListener(this); + } + + mNameView = (TextView) view.findViewById(R.id.title); + + updatePreferenceViews(); + } + + private void init() { + setLayoutResource(R.layout.preference_name); + } + + public void setName(String name) { + mName = (name.toString()); + updatePreferenceViews(); + } + + public String getName() { + return(mName.toString()); + } + + private void updatePreferenceViews() { + if (mNameView != null) { + mNameView.setText(mName.toString()); + } + } + + @Override + public void onClick(android.view.View v) { + if (v != null) { + Context context = getContext(); + if (context != null) { + final EditText entry = new EditText(context); + entry.setSingleLine(); + entry.setText(mName.toString()); + + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(R.string.rename_dialog_title); + builder.setMessage(R.string.rename_dialog_message); + builder.setView(entry, 34, 16, 34, 16); + builder.setPositiveButton(android.R.string.ok, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String value = entry.getText().toString(); + mName = value.toString(); + mNameView.setText(value.toString()); + callChangeListener(this); + } + }); + builder.setNegativeButton(android.R.string.cancel, null); + AlertDialog dialog = builder.create(); + dialog.show(); + ((TextView)dialog.findViewById(android.R.id.message)).setTextAppearance(context, + android.R.style.TextAppearance_DeviceDefault_Small); + } + } + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + callChangeListener(preference); + return false; + } +}
\ No newline at end of file diff --git a/src/com/android/settings/profiles/ProfileGroupConfig.java b/src/com/android/settings/profiles/ProfileGroupConfig.java new file mode 100644 index 0000000..df55102 --- /dev/null +++ b/src/com/android/settings/profiles/ProfileGroupConfig.java @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2012 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.profiles; + +import java.util.UUID; + +import android.app.Profile; +import android.app.ProfileGroup; +import android.app.ProfileGroup.Mode; +import android.app.ProfileManager; +import android.net.Uri; +import android.os.Bundle; +import android.preference.ListPreference; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceChangeListener; + +import com.android.settings.R; +import com.android.settings.SettingsPreferenceFragment; + +public class ProfileGroupConfig extends SettingsPreferenceFragment implements + OnPreferenceChangeListener { + + public static final String PROFILE_SERVICE = "profile"; + + private static final CharSequence KEY_SOUNDMODE = "sound_mode"; + + private static final CharSequence KEY_VIBRATEMODE = "vibrate_mode"; + + private static final CharSequence KEY_LIGHTSMODE = "lights_mode"; + + private static final CharSequence KEY_RINGERMODE = "ringer_mode"; + + private static final CharSequence KEY_SOUNDTONE = "soundtone"; + + private static final CharSequence KEY_RINGTONE = "ringtone"; + + Profile mProfile; + + ProfileGroup mProfileGroup; + + private ListPreference mSoundMode; + + private ListPreference mRingerMode; + + private ListPreference mVibrateMode; + + private ListPreference mLightsMode; + + private ProfileRingtonePreference mRingTone; + + private ProfileRingtonePreference mSoundTone; + + private ProfileManager mProfileManager; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + addPreferencesFromResource(R.xml.profile_settings); + + final Bundle args = getArguments(); + if (args != null) { + mProfile = (Profile) args.getParcelable("Profile"); + UUID uuid = UUID.fromString(args.getString("ProfileGroup")); + + mProfileManager = (ProfileManager) getSystemService(PROFILE_SERVICE); + mProfileGroup = mProfile.getProfileGroup(uuid); + + mRingerMode = (ListPreference) findPreference(KEY_RINGERMODE); + mSoundMode = (ListPreference) findPreference(KEY_SOUNDMODE); + mVibrateMode = (ListPreference) findPreference(KEY_VIBRATEMODE); + mLightsMode = (ListPreference) findPreference(KEY_LIGHTSMODE); + mRingTone = (ProfileRingtonePreference) findPreference(KEY_RINGTONE); + mSoundTone = (ProfileRingtonePreference) findPreference(KEY_SOUNDTONE); + + mRingTone.setShowSilent(false); + mSoundTone.setShowSilent(false); + + mSoundMode.setOnPreferenceChangeListener(this); + mRingerMode.setOnPreferenceChangeListener(this); + mVibrateMode.setOnPreferenceChangeListener(this); + mLightsMode.setOnPreferenceChangeListener(this); + mSoundTone.setOnPreferenceChangeListener(this); + mRingTone.setOnPreferenceChangeListener(this); + + updateState(); + } + } + + private void updateState() { + + mVibrateMode.setValue(mProfileGroup.getVibrateMode().name()); + mSoundMode.setValue(mProfileGroup.getSoundMode().name()); + mRingerMode.setValue(mProfileGroup.getRingerMode().name()); + mLightsMode.setValue(mProfileGroup.getLightsMode().name()); + + mVibrateMode.setSummary(mVibrateMode.getEntry()); + mSoundMode.setSummary(mSoundMode.getEntry()); + mRingerMode.setSummary(mRingerMode.getEntry()); + mLightsMode.setSummary(mLightsMode.getEntry()); + + if (mProfileGroup.getSoundOverride() != null) { + mSoundTone.setRingtone(mProfileGroup.getSoundOverride()); + } + + if (mProfileGroup.getRingerOverride() != null) { + mRingTone.setRingtone(mProfileGroup.getRingerOverride()); + } + + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if (preference == mVibrateMode) { + mProfileGroup.setVibrateMode(Mode.valueOf((String) newValue)); + } else if (preference == mSoundMode) { + mProfileGroup.setSoundMode(Mode.valueOf((String) newValue)); + } else if (preference == mRingerMode) { + mProfileGroup.setRingerMode(Mode.valueOf((String) newValue)); + } else if (preference == mLightsMode) { + mProfileGroup.setLightsMode(Mode.valueOf((String) newValue)); + } else if (preference == mRingTone) { + Uri uri = Uri.parse((String) newValue); + mProfileGroup.setRingerOverride(uri); + } else if (preference == mSoundTone) { + Uri uri = Uri.parse((String) newValue); + mProfileGroup.setSoundOverride(uri); + } + + mProfileManager.updateProfile(mProfile); + + updateState(); + return true; + } +}
\ No newline at end of file diff --git a/src/com/android/settings/profiles/ProfileRingtonePreference.java b/src/com/android/settings/profiles/ProfileRingtonePreference.java new file mode 100644 index 0000000..bf01e28 --- /dev/null +++ b/src/com/android/settings/profiles/ProfileRingtonePreference.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2012 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.profiles; + +import android.content.Context; +import android.content.Intent; +import android.media.RingtoneManager; +import android.net.Uri; +import android.preference.RingtonePreference; +import android.util.AttributeSet; + +public class ProfileRingtonePreference extends RingtonePreference { + private static final String TAG = "ProfileRingtonePreference"; + + public ProfileRingtonePreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + @Override + protected void onPrepareRingtonePickerIntent(Intent ringtonePickerIntent) { + super.onPrepareRingtonePickerIntent(ringtonePickerIntent); + + /* + * Since this preference is for choosing the default ringtone, it + * doesn't make sense to show a 'Default' item. + */ + ringtonePickerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false); + } + + private Uri mRingtone; + + void setRingtone(Uri uri) { + mRingtone = uri; + } + + @Override + protected Uri onRestoreRingtone() { + if (mRingtone == null) { + return super.onRestoreRingtone(); + } else { + return mRingtone; + } + } +}
\ No newline at end of file diff --git a/src/com/android/settings/profiles/ProfilesList.java b/src/com/android/settings/profiles/ProfilesList.java index 29c7500..9b07b7d 100644 --- a/src/com/android/settings/profiles/ProfilesList.java +++ b/src/com/android/settings/profiles/ProfilesList.java @@ -25,6 +25,9 @@ import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceScreen; import android.text.TextUtils; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import com.android.settings.R; import com.android.settings.SettingsPreferenceFragment; diff --git a/src/com/android/settings/profiles/ProfilesSettings.java b/src/com/android/settings/profiles/ProfilesSettings.java index 92a9610..e97ce15 100644 --- a/src/com/android/settings/profiles/ProfilesSettings.java +++ b/src/com/android/settings/profiles/ProfilesSettings.java @@ -57,6 +57,7 @@ public class ProfilesSettings extends SettingsPreferenceFragment { public static final String EXTRA_NEW_PROFILE = "new_profile_mode"; private static final int MENU_RESET = Menu.FIRST; + private static final int MENU_APP_GROUPS = Menu.FIRST + 1; private final IntentFilter mFilter; private final BroadcastReceiver mReceiver; @@ -169,6 +170,8 @@ public class ProfilesSettings extends SettingsPreferenceFragment { .setAlphabeticShortcut('r') .setEnabled(mEnabled) .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); + menu.add(0, MENU_APP_GROUPS, 0, R.string.profile_appgroups_title) + .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); } @Override @@ -177,6 +180,10 @@ public class ProfilesSettings extends SettingsPreferenceFragment { case MENU_RESET: resetAll(); return true; + case MENU_APP_GROUPS: + startFragment(this, AppGroupList.class.getName(), + R.string.profile_appgroups_title, 0, null); + return true; } return super.onOptionsItemSelected(item); } diff --git a/src/com/android/settings/profiles/SetupActionsFragment.java b/src/com/android/settings/profiles/SetupActionsFragment.java index 34ce056..c7fe6c2 100644 --- a/src/com/android/settings/profiles/SetupActionsFragment.java +++ b/src/com/android/settings/profiles/SetupActionsFragment.java @@ -22,7 +22,9 @@ import android.app.BrightnessSettings; import android.app.ConnectionSettings; import android.app.Dialog; import android.app.Fragment; +import android.app.NotificationGroup; import android.app.Profile; +import android.app.ProfileGroup; import android.app.ProfileManager; import android.app.RingModeSettings; import android.app.StreamSettings; @@ -64,12 +66,14 @@ import android.widget.ListView; import android.widget.SeekBar; import android.widget.TextView; import com.android.settings.R; +import com.android.settings.SettingsActivity; import com.android.settings.SubSettings; import com.android.settings.cyanogenmod.DeviceUtils; import com.android.settings.SettingsPreferenceFragment; import com.android.settings.profiles.actions.ItemListAdapter; import com.android.settings.profiles.actions.item.AirplaneModeItem; import com.android.settings.profiles.actions.item.BrightnessItem; +import com.android.settings.profiles.actions.item.AppGroupItem; import com.android.settings.profiles.actions.item.ConnectionOverrideItem; import com.android.settings.profiles.actions.item.Header; import com.android.settings.profiles.actions.item.Item; @@ -208,6 +212,38 @@ public class SetupActionsFragment extends SettingsPreferenceFragment mItems.add(new AirplaneModeItem(mProfile.getAirplaneMode())); mItems.add(new LockModeItem(mProfile)); mItems.add(new BrightnessItem(mProfile.getBrightness())); + + // app groups + if (SettingsActivity.showAdvancedPreferences(getActivity())) { + mItems.add(new Header(getString(R.string.profile_app_group_category_title))); + + int groupsAdded = 0; + ProfileGroup[] profileGroups = mProfile.getProfileGroups(); + if (profileGroups != null && profileGroups.length > 1) { // it will always have "other" + for (ProfileGroup profileGroup : profileGroups) { + // only display profile group if there's a matching notification group + // and don't' show the wildcard group + if (mProfileManager.getNotificationGroup(profileGroup.getUuid()) != null + && !mProfile.getDefaultGroup().getUuid().equals( + profileGroup.getUuid())) { + mItems.add(new AppGroupItem(mProfile, profileGroup)); + groupsAdded++; + } + } + if (groupsAdded > 0) { + // add "Other" at the end + mItems.add(new AppGroupItem(mProfile, mProfile.getDefaultGroup())); + } + } + if (groupsAdded > 0) { + // add dummy "add/remove app groups" entry + mItems.add(new AppGroupItem()); + } else { + // remove the header since there are no options + mItems.remove(mItems.get(mItems.size() - 1)); + } + } + mAdapter.notifyDataSetChanged(); } @@ -822,6 +858,37 @@ public class SetupActionsFragment extends SettingsPreferenceFragment alertDialog.show(); } + private void requestActiveAppGroupsDialog() { + final NotificationGroup[] notificationGroups = mProfileManager.getNotificationGroups(); + + CharSequence[] items = new CharSequence[notificationGroups.length]; + boolean[] checked = new boolean[notificationGroups.length]; + + for (int i = 0; i < notificationGroups.length; i++) { + items[i] = notificationGroups[i].getName(); + checked[i] = mProfile.getProfileGroup(notificationGroups[i].getUuid()) != null; + } + DialogInterface.OnMultiChoiceClickListener listener = + new DialogInterface.OnMultiChoiceClickListener() { + @Override + public void onClick(DialogInterface dialog, int which, boolean isChecked) { + if (isChecked) { + mProfile.addProfileGroup(new ProfileGroup(notificationGroups[which].getUuid(), false)); + } else { + mProfile.removeProfileGroup(notificationGroups[which].getUuid()); + } + updateProfile(); + rebuildItemList(); + } + }; + + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) + .setMultiChoiceItems(items, checked, listener) + .setTitle(R.string.profile_appgroups_title) + .setPositiveButton(R.string.ok, null); + builder.show(); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -883,9 +950,24 @@ public class SetupActionsFragment extends SettingsPreferenceFragment } else if (itemAtPosition instanceof TriggerItem) { TriggerItem item = (TriggerItem) itemAtPosition; openTriggersFragment(item.getTriggerType()); + } else if (itemAtPosition instanceof AppGroupItem) { + AppGroupItem item = (AppGroupItem) itemAtPosition; + if (item.getGroupUuid() == null) { + requestActiveAppGroupsDialog(); + } else { + startProfileGroupActivity(item); + } } } + private void startProfileGroupActivity(AppGroupItem item) { + Bundle args = new Bundle(); + args.putString("ProfileGroup", item.getGroupUuid().toString()); + args.putParcelable("Profile", mProfile); + + startFragment(this, ProfileGroupConfig.class.getName(), 0, 0, args); + } + private void openTriggersFragment(int openTo) { Bundle args = new Bundle(); args.putParcelable(ProfilesSettings.EXTRA_PROFILE, mProfile); diff --git a/src/com/android/settings/profiles/actions/ItemListAdapter.java b/src/com/android/settings/profiles/actions/ItemListAdapter.java index 5da7532..e23387d 100644 --- a/src/com/android/settings/profiles/actions/ItemListAdapter.java +++ b/src/com/android/settings/profiles/actions/ItemListAdapter.java @@ -36,6 +36,7 @@ public class ItemListAdapter extends ArrayAdapter<Item> { AIRPLANEMODE_ITEM, LOCKSCREENMODE_ITEM, TRIGGER_ITEM, + APP_GROUP_ITEM, BRIGHTNESS_ITEM } diff --git a/src/com/android/settings/profiles/actions/item/AppGroupItem.java b/src/com/android/settings/profiles/actions/item/AppGroupItem.java new file mode 100644 index 0000000..8befd38 --- /dev/null +++ b/src/com/android/settings/profiles/actions/item/AppGroupItem.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2014 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.profiles.actions.item; + +import android.app.NotificationGroup; +import android.app.Profile; +import android.app.ProfileGroup; +import android.app.ProfileManager; +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import com.android.settings.R; +import com.android.settings.profiles.actions.ItemListAdapter; + +import java.util.UUID; + +public class AppGroupItem implements Item { + Profile mProfile; + ProfileGroup mGroup; + + public AppGroupItem() { + // empty app group will act as a "Add/remove app groups" item + } + + public AppGroupItem(Profile profile, ProfileGroup group) { + mProfile = profile; + if (group == null) { + throw new UnsupportedOperationException("profile group can't be null"); + } + mGroup = group; + } + + @Override + public ItemListAdapter.RowType getRowType() { + return ItemListAdapter.RowType.APP_GROUP_ITEM; + } + + @Override + public boolean isEnabled() { + return true; + } + + public UUID getGroupUuid() { + if (mGroup != null) { + return mGroup.getUuid(); + } + return null; + } + + @Override + public View getView(LayoutInflater inflater, View convertView, ViewGroup parent) { + View view; + if (convertView == null) { + view = inflater.inflate(R.layout.list_two_line_item, parent, false); + } else { + view = convertView; + } + + ProfileManager profileManager = (ProfileManager) parent.getContext() + .getSystemService(Context.PROFILE_SERVICE); + + TextView text = (TextView) view.findViewById(R.id.title); + TextView desc = (TextView) view.findViewById(R.id.summary); + + if (mGroup != null) { + NotificationGroup notifGroup = profileManager.getNotificationGroup(mGroup.getUuid()); + if (notifGroup != null) { + text.setText(notifGroup.getName()); + } else { + text.setText("<unknown>"); + } + desc.setVisibility(View.GONE); + } else { + text.setText(R.string.profile_app_group_item_instructions); + desc.setText(R.string.profile_app_group_item_instructions_summary); + + desc.setVisibility(View.VISIBLE); + } + + return view; + } +} |