diff --git a/res/values/strings.xml b/res/values/strings.xml index cd9da3b97e2..bb1a20eb0c4 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -6372,7 +6372,10 @@ Loading apps... - Channels + Categories + + + Other This app has not posted any notifications diff --git a/res/xml/app_notification_settings.xml b/res/xml/app_notification_settings.xml index c68e4565fe3..ef8a45cb273 100644 --- a/res/xml/app_notification_settings.xml +++ b/res/xml/app_notification_settings.xml @@ -34,8 +34,4 @@ settings:useAdditionalSummary="true" settings:restrictedSwitchSummary="@string/enabled_by_admin" /> - - diff --git a/src/com/android/settings/notification/AppNotificationSettings.java b/src/com/android/settings/notification/AppNotificationSettings.java index ef62fbb1d30..d6b60fdbd56 100644 --- a/src/com/android/settings/notification/AppNotificationSettings.java +++ b/src/com/android/settings/notification/AppNotificationSettings.java @@ -16,6 +16,9 @@ package com.android.settings.notification; +import static android.app.NotificationManager.IMPORTANCE_LOW; +import static android.app.NotificationManager.IMPORTANCE_NONE; + import android.app.Activity; import android.app.Notification; import android.app.NotificationChannel; @@ -24,6 +27,7 @@ import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.ResolveInfo; +import android.os.AsyncTask; import android.os.Bundle; import android.provider.Settings; import android.support.v7.preference.Preference; @@ -41,10 +45,12 @@ import com.android.settings.applications.AppInfoBase; import com.android.settings.dashboard.DashboardFeatureProvider; import com.android.settings.notification.NotificationBackend.AppRow; import com.android.settings.overlay.FeatureFactory; +import com.android.settings.widget.MasterSwitchPreference; import com.android.settingslib.RestrictedPreference; import com.android.settingslib.RestrictedSwitchPreference; import java.text.Collator; +import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -63,8 +69,8 @@ public class AppNotificationSettings extends NotificationSettingsBase { private static final String KEY_BLOCK = "block"; private DashboardFeatureProvider mDashboardFeatureProvider; - private PreferenceCategory mChannels; private List mChannelGroupList; + private List mChannelGroups = new ArrayList(); @Override public void onActivityCreated(Bundle savedInstanceState) { @@ -98,7 +104,6 @@ public class AppNotificationSettings extends NotificationSettingsBase { mBlock = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BLOCK); mBadge = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BADGE); - mChannels = (PreferenceCategory) findPreference(KEY_CHANNELS); if (mPkgInfo != null) { setupBlock(); @@ -107,60 +112,19 @@ public class AppNotificationSettings extends NotificationSettingsBase { ArrayMap rows = new ArrayMap(); rows.put(mAppRow.pkg, mAppRow); collectConfigActivities(rows); - // TODO: load channels in asynctask? - mChannelGroupList = mBackend.getChannelGroups(mPkg, mUid).getList(); - Collections.sort(mChannelGroupList, mChannelGroupComparator); - - if (mChannelGroupList.isEmpty()) { - Preference empty = new Preference(getPrefContext()); - empty.setTitle(R.string.no_channels); - empty.setEnabled(false); - mChannels.addPreference(empty); - } else { - for (NotificationChannelGroup group : mChannelGroupList) { - PreferenceCategory groupCategory = null; - if (group.getId() != null && group.getName() != null) { - groupCategory = new PreferenceCategory(getPrefContext()); - groupCategory.setTitle(group.getName()); - groupCategory.setKey(group.getId()); - groupCategory.setOrderingAsAdded(true); - getPreferenceScreen().addPreference(groupCategory); - } - final List channels = group.getChannels(); - Collections.sort(channels, mChannelComparator); - int N = channels.size(); - for (int i = 0; i < N; i++) { - final NotificationChannel channel = channels.get(i); - RestrictedPreference channelPref = new RestrictedPreference( - getPrefContext()); - channelPref.setDisabledByAdmin(mSuspendedAppsAdmin); - channelPref.setKey(channel.getId()); - channelPref.setTitle(channel.getName()); - - if (channel.isDeleted()) { - channelPref.setTitle( - getString(R.string.deleted_channel_name, channel.getName())); - channelPref.setEnabled(false); - } else { - Bundle channelArgs = new Bundle(); - channelArgs.putInt(AppInfoBase.ARG_PACKAGE_UID, mUid); - channelArgs.putBoolean(AppHeader.EXTRA_HIDE_INFO_BUTTON, true); - channelArgs.putString(AppInfoBase.ARG_PACKAGE_NAME, mPkg); - channelArgs.putString(Settings.EXTRA_CHANNEL_ID, channel.getId()); - Intent channelIntent = Utils.onBuildStartFragmentIntent(getActivity(), - ChannelNotificationSettings.class.getName(), - channelArgs, null, 0, null, false); - channelPref.setIntent(channelIntent); - } - if (groupCategory != null) { - groupCategory.addPreference(channelPref); - } else { - mChannels.addPreference(channelPref); - } - } + new AsyncTask() { + @Override + protected Void doInBackground(Void... unused) { + mChannelGroupList = mBackend.getChannelGroups(mPkg, mUid).getList(); + Collections.sort(mChannelGroupList, mChannelGroupComparator); + return null; } - } - updateDependents(mAppRow.banned); + + @Override + protected void onPostExecute(Void unused) { + populateChannelList(); + } + }.execute(); } if (mDashboardFeatureProvider.isEnabled()) { final Preference pref = FeatureFactory.getFactory(activity) @@ -188,6 +152,83 @@ public class AppNotificationSettings extends NotificationSettingsBase { } } + private void populateChannelList() { + if (mChannelGroupList.isEmpty()) { + PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext()); + groupCategory.setTitle(R.string.notification_channels); + getPreferenceScreen().addPreference(groupCategory); + mChannelGroups.add(groupCategory); + + Preference empty = new Preference(getPrefContext()); + empty.setTitle(R.string.no_channels); + empty.setEnabled(false); + groupCategory.addPreference(empty); + } else { + for (NotificationChannelGroup group : mChannelGroupList) { + PreferenceCategory groupCategory = new PreferenceCategory(getPrefContext()); + if (group.getName() == null) { + groupCategory.setTitle(mChannelGroupList.size() > 1 + ? R.string.notification_channels_other + : R.string.notification_channels); + } else { + groupCategory.setTitle(group.getName()); + } + groupCategory.setKey(group.getId()); + groupCategory.setOrderingAsAdded(true); + getPreferenceScreen().addPreference(groupCategory); + mChannelGroups.add(groupCategory); + + final List channels = group.getChannels(); + Collections.sort(channels, mChannelComparator); + int N = channels.size(); + for (int i = 0; i < N; i++) { + final NotificationChannel channel = channels.get(i); + MasterSwitchPreference channelPref = new MasterSwitchPreference( + getPrefContext()); + channelPref.setDisabledByAdmin(mSuspendedAppsAdmin); + channelPref.setKey(channel.getId()); + channelPref.setTitle(channel.getName()); + channelPref.setChecked(channel.getImportance() != IMPORTANCE_NONE); + + if (channel.isDeleted()) { + channelPref.setTitle( + getString(R.string.deleted_channel_name, channel.getName())); + channelPref.setEnabled(false); + } else { + channelPref.setSummary(getImportanceSummary(channel.getImportance())); + Bundle channelArgs = new Bundle(); + channelArgs.putInt(AppInfoBase.ARG_PACKAGE_UID, mUid); + channelArgs.putBoolean(AppHeader.EXTRA_HIDE_INFO_BUTTON, true); + channelArgs.putString(AppInfoBase.ARG_PACKAGE_NAME, mPkg); + channelArgs.putString(Settings.EXTRA_CHANNEL_ID, channel.getId()); + Intent channelIntent = Utils.onBuildStartFragmentIntent(getActivity(), + ChannelNotificationSettings.class.getName(), + channelArgs, null, 0, null, false); + channelPref.setIntent(channelIntent); + + channelPref.setOnPreferenceChangeListener( + new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, + Object o) { + boolean value = (Boolean) o; + int importance = value ? IMPORTANCE_LOW : IMPORTANCE_NONE; + channel.setImportance(importance); + channel.lockFields( + NotificationChannel.USER_LOCKED_IMPORTANCE); + mBackend.updateChannel(mPkg, mUid, channel); + + return true; + } + }); + } + groupCategory.addPreference(channelPref); + } + } + } + updateDependents(mAppRow.banned); + } + private void setupBadge() { mBadge.setDisabledByAdmin(mSuspendedAppsAdmin); mBadge.setChecked(mAppRow.showBadge); @@ -223,7 +264,9 @@ public class AppNotificationSettings extends NotificationSettingsBase { } private void updateDependents(boolean banned) { - setVisible(mChannels, !banned); + for (PreferenceCategory category : mChannelGroups) { + setVisible(category, !banned); + } setVisible(mBadge, !banned); } @@ -289,7 +332,7 @@ public class AppNotificationSettings extends NotificationSettingsBase { @Override public int compare(NotificationChannelGroup left, NotificationChannelGroup right) { - // Non-groups channels (in placeholder group with a null id) come first + // Non-grouped channels (in placeholder group with a null id) come last if (left.getId() == null && right.getId() != null) { return 1; } else if (right.getId() == null && left.getId() != null) { diff --git a/src/com/android/settings/notification/ChannelNotificationSettings.java b/src/com/android/settings/notification/ChannelNotificationSettings.java index f35794e5aa9..b6923dcca61 100644 --- a/src/com/android/settings/notification/ChannelNotificationSettings.java +++ b/src/com/android/settings/notification/ChannelNotificationSettings.java @@ -230,12 +230,12 @@ public class ChannelNotificationSettings extends NotificationSettingsBase { List values = new ArrayList<>();; for (int i = 0; i < numImportances; i++) { int importance = i + 1; - summaries.add(getSummary(importance)); + summaries.add(getImportanceSummary(importance)); values.add(String.valueOf(importance)); } if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(mChannel.getId())) { // Add option to reset to letting the app decide - summaries.add(getSummary(NotificationManager.IMPORTANCE_UNSPECIFIED)); + summaries.add(getImportanceSummary(NotificationManager.IMPORTANCE_UNSPECIFIED)); values.add(String.valueOf(NotificationManager.IMPORTANCE_UNSPECIFIED)); } mImportance.setEntryValues(values.toArray(new String[0])); @@ -256,25 +256,6 @@ public class ChannelNotificationSettings extends NotificationSettingsBase { }); } - private String getSummary(int importance) { - switch (importance) { - case NotificationManager.IMPORTANCE_UNSPECIFIED: - return getContext().getString(R.string.notification_importance_unspecified); - case NotificationManager.IMPORTANCE_NONE: - return getContext().getString(R.string.notification_importance_blocked); - case NotificationManager.IMPORTANCE_MIN: - return getContext().getString(R.string.notification_importance_min); - case NotificationManager.IMPORTANCE_LOW: - return getContext().getString(R.string.notification_importance_low); - case NotificationManager.IMPORTANCE_DEFAULT: - return getContext().getString(R.string.notification_importance_default); - case NotificationManager.IMPORTANCE_HIGH: - case NotificationManager.IMPORTANCE_MAX: - default: - return getContext().getString(R.string.notification_importance_high); - } - } - protected void setupPriorityPref(boolean priority) { mPriority.setDisabledByAdmin(mSuspendedAppsAdmin); mPriority.setChecked(priority); diff --git a/src/com/android/settings/notification/NotificationSettingsBase.java b/src/com/android/settings/notification/NotificationSettingsBase.java index f6c65915e8b..b5c52a1aeba 100644 --- a/src/com/android/settings/notification/NotificationSettingsBase.java +++ b/src/com/android/settings/notification/NotificationSettingsBase.java @@ -176,15 +176,22 @@ abstract public class NotificationSettingsBase extends SettingsPreferenceFragmen return null; } - private PackageInfo findPackageInfo(String pkg) { - if (pkg == null) { - return null; + protected String getImportanceSummary(int importance) { + switch (importance) { + case NotificationManager.IMPORTANCE_UNSPECIFIED: + return getContext().getString(R.string.notification_importance_unspecified); + case NotificationManager.IMPORTANCE_NONE: + return getContext().getString(R.string.notification_importance_blocked); + case NotificationManager.IMPORTANCE_MIN: + return getContext().getString(R.string.notification_importance_min); + case NotificationManager.IMPORTANCE_LOW: + return getContext().getString(R.string.notification_importance_low); + case NotificationManager.IMPORTANCE_DEFAULT: + return getContext().getString(R.string.notification_importance_default); + case NotificationManager.IMPORTANCE_HIGH: + case NotificationManager.IMPORTANCE_MAX: + default: + return getContext().getString(R.string.notification_importance_high); } - try { - return mPm.getPackageInfo(pkg, PackageManager.GET_SIGNATURES); - } catch (NameNotFoundException e) { - Log.w(TAG, "Failed to load package " + pkg, e); - } - return null; } }