diff --git a/res/values/cherish_strings.xml b/res/values/cherish_strings.xml index 06bd666..908657d 100644 --- a/res/values/cherish_strings.xml +++ b/res/values/cherish_strings.xml @@ -508,7 +508,9 @@ Pop-up notifications are disabled Add app Disable heads up notifications in these applications - Applist + Disable heads up from these applications + Stoplist + Blacklist Choose app Delete Remove selected item? diff --git a/res/xml/heads_up_settings.xml b/res/xml/heads_up_settings.xml index 9c6b9ff..ed1c77d 100644 --- a/res/xml/heads_up_settings.xml +++ b/res/xml/heads_up_settings.xml @@ -1,47 +1,48 @@ - - - - - + - - - + - - - + + + + + + + + + \ No newline at end of file diff --git a/src/com/cherish/settings/fragments/HeadsUpSettings.java b/src/com/cherish/settings/fragments/HeadsUpSettings.java index cf559e4..3ca3db0 100644 --- a/src/com/cherish/settings/fragments/HeadsUpSettings.java +++ b/src/com/cherish/settings/fragments/HeadsUpSettings.java @@ -51,14 +51,19 @@ public class HeadsUpSettings extends SettingsPreferenceFragment implements Preference.OnPreferenceClickListener { private static final int DIALOG_STOPLIST_APPS = 0; + private static final int DIALOG_BLACKLIST_APPS = 1; private PackageListAdapter mPackageAdapter; private PackageManager mPackageManager; private PreferenceGroup mStoplistPrefList; + private PreferenceGroup mBlacklistPrefList; private Preference mAddStoplistPref; + private Preference mAddBlacklistPref; private String mStoplistPackageList; + private String mBlacklistPackageList; private Map mStoplistPackages; + private Map mBlacklistPackages; @Override public void onCreate(Bundle savedInstanceState) { @@ -71,11 +76,17 @@ public class HeadsUpSettings extends SettingsPreferenceFragment mStoplistPrefList = (PreferenceGroup) findPreference("stoplist_applications"); mStoplistPrefList.setOrderingAsAdded(false); + mBlacklistPrefList = (PreferenceGroup) findPreference("blacklist_applications"); + mBlacklistPrefList.setOrderingAsAdded(false); + mStoplistPackages = new HashMap(); + mBlacklistPackages = new HashMap(); mAddStoplistPref = findPreference("add_stoplist_packages"); + mAddBlacklistPref = findPreference("add_blacklist_packages"); mAddStoplistPref.setOnPreferenceClickListener(this); + mAddBlacklistPref.setOnPreferenceClickListener(this); } @Override @@ -91,7 +102,7 @@ public class HeadsUpSettings extends SettingsPreferenceFragment @Override public int getDialogMetricsCategory(int dialogId) { - if (dialogId == DIALOG_STOPLIST_APPS) { + if (dialogId == DIALOG_STOPLIST_APPS || dialogId == DIALOG_BLACKLIST_APPS ) { return MetricsProto.MetricsEvent.CHERISH_SETTINGS; } return 0; @@ -104,27 +115,35 @@ public class HeadsUpSettings extends SettingsPreferenceFragment public Dialog onCreateDialog(int id) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final Dialog dialog; + final ListView list = new ListView(getActivity()); + list.setAdapter(mPackageAdapter); + + builder.setTitle(R.string.profile_choose_app); + builder.setView(list); + dialog = builder.create(); + switch (id) { case DIALOG_STOPLIST_APPS: - final ListView list = new ListView(getActivity()); - list.setAdapter(mPackageAdapter); - - 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) { // Add empty application definition, the user will be able to edit it later PackageItem info = (PackageItem) parent.getItemAtPosition(position); - addCustomApplicationPref(info.packageName); + addCustomApplicationPref(info.packageName, mStoplistPackages); dialog.cancel(); } }); break; - default: - dialog = null; + case DIALOG_BLACKLIST_APPS: + list.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, + View view, int position, long id) { + PackageItem info = (PackageItem) parent.getItemAtPosition(position); + addCustomApplicationPref(info.packageName, mBlacklistPackages); + dialog.cancel(); + } + }); } return dialog; } @@ -169,8 +188,9 @@ public class HeadsUpSettings extends SettingsPreferenceFragment } // Add the Application Preferences - if (mStoplistPrefList != null) { + if (mStoplistPrefList != null && mBlacklistPrefList != null) { mStoplistPrefList.removeAll(); + mBlacklistPrefList.removeAll(); for (Package pkg : mStoplistPackages.values()) { try { @@ -180,27 +200,44 @@ public class HeadsUpSettings extends SettingsPreferenceFragment // Do nothing } } + + for (Package pkg : mBlacklistPackages.values()) { + try { + Preference pref = createPreferenceFromInfo(pkg); + mBlacklistPrefList.addPreference(pref); + } catch (PackageManager.NameNotFoundException e) { + // Do nothing + } + } } // Keep these at the top mAddStoplistPref.setOrder(0); + mAddBlacklistPref.setOrder(0); // Add 'add' options mStoplistPrefList.addPreference(mAddStoplistPref); + mBlacklistPrefList.addPreference(mAddBlacklistPref); } @Override public boolean onPreferenceClick(Preference preference) { if (preference == mAddStoplistPref) { showDialog(DIALOG_STOPLIST_APPS); + } else if (preference == mAddBlacklistPref) { + showDialog(DIALOG_BLACKLIST_APPS); } else { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) .setTitle(R.string.dialog_delete_title) .setMessage(R.string.dialog_delete_message) .setIconAttribute(android.R.attr.alertDialogIcon) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - removeCustomApplicationPref(preference.getKey()); + if (preference == mBlacklistPrefList.findPreference(preference.getKey())) { + removeApplicationPref(preference.getKey(), mBlacklistPackages); + } else if (preference == mStoplistPrefList.findPreference(preference.getKey())) { + removeApplicationPref(preference.getKey(), mStoplistPackages); + } } }) .setNegativeButton(android.R.string.cancel, null); @@ -210,12 +247,12 @@ public class HeadsUpSettings extends SettingsPreferenceFragment return true; } - private void addCustomApplicationPref(String packageName) { - Package pkg = mStoplistPackages.get(packageName); + private void addCustomApplicationPref(String packageName, Map map) { + Package pkg = map.get(packageName); if (pkg == null) { pkg = new Package(packageName); - mStoplistPackages.put(packageName, pkg); - savePackageList(false); + map.put(packageName, pkg); + savePackageList(false, map); refreshCustomApplicationPrefs(); } } @@ -235,51 +272,73 @@ public class HeadsUpSettings extends SettingsPreferenceFragment return pref; } - private void removeCustomApplicationPref(String packageName) { - if (mStoplistPackages.remove(packageName) != null) { - savePackageList(false); + private void removeApplicationPref(String packageName, Map map) { + if (map.remove(packageName) != null) { + savePackageList(false, map); refreshCustomApplicationPrefs(); } } private boolean parsePackageList() { + boolean parsed = false; final String stoplistString = Settings.System.getString(getContentResolver(), Settings.System.HEADS_UP_STOPLIST_VALUES); + final String blacklistString = Settings.System.getString(getContentResolver(), + Settings.System.HEADS_UP_BLACKLIST_VALUES); - if (TextUtils.equals(mStoplistPackageList, stoplistString)) { - return false; - } + if (!TextUtils.equals(mStoplistPackageList, stoplistString)) { mStoplistPackageList = stoplistString; mStoplistPackages.clear(); - - if (stoplistString != null) { - final String[] array = TextUtils.split(stoplistString, "\\|"); - for (String item : array) { - if (TextUtils.isEmpty(item)) { - continue; - } - Package pkg = Package.fromString(item); - if (pkg != null) { - mStoplistPackages.put(pkg.name, pkg); - } - } + parseAndAddToMap(stoplistString, mStoplistPackages); + parsed = true; } - return true; + if (!TextUtils.equals(mBlacklistPackageList, blacklistString)) { + mBlacklistPackageList = blacklistString; + mBlacklistPackages.clear(); + parseAndAddToMap(blacklistString, mBlacklistPackages); + parsed = true; + } + + return parsed; } - private void savePackageList(boolean preferencesUpdated) { + private void parseAndAddToMap(String baseString, Map map) { + if (baseString == null) { + return; + } + + final String[] array = TextUtils.split(baseString, "\\|"); + for (String item : array) { + if (TextUtils.isEmpty(item)) { + continue; + } + Package pkg = Package.fromString(item); + map.put(pkg.name, pkg); + } + } + + + private void savePackageList(boolean preferencesUpdated, Map map) { + String setting = map == mStoplistPackages + ? Settings.System.HEADS_UP_STOPLIST_VALUES + : Settings.System.HEADS_UP_BLACKLIST_VALUES; + List settings = new ArrayList(); - for (Package app : mStoplistPackages.values()) { + for (Package app : map.values()) { settings.add(app.toString()); } final String value = TextUtils.join("|", settings); if (preferencesUpdated) { + if (TextUtils.equals(setting, Settings.System.HEADS_UP_STOPLIST_VALUES)) { mStoplistPackageList = value; + } else { + mBlacklistPackageList = value; } + } Settings.System.putString(getContentResolver(), - Settings.System.HEADS_UP_STOPLIST_VALUES, value); + setting, value); } }