Show channel groups in settings.

Test: manual
Change-Id: Icfaed2d2d898245fcfa50b25d7ad456e4db07bae
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 2879002..22d8031 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -6370,6 +6370,9 @@
     <!-- [CHAR LIMIT=NONE] App notification settings: channels title -->
     <string name="notification_channels">Channels</string>
 
+    <!-- [CHAR LIMIT=NONE] App notification settings: no channels -->
+    <string name="no_channels">This app has not posted any notifications</string>
+
     <!-- [CHAR LIMIT=60] App notification settings: Text to display for deleted channels -->
     <string name="deleted_channel_name"><xliff:g id="channel_name" example="Promotions">%1$s</xliff:g> (deleted)</string>
 
diff --git a/res/xml/app_notification_settings.xml b/res/xml/app_notification_settings.xml
index 7d51fa8..c68e456 100644
--- a/res/xml/app_notification_settings.xml
+++ b/res/xml/app_notification_settings.xml
@@ -23,7 +23,6 @@
         android:key="block"
         android:title="@string/app_notification_block_title"
         android:summary="@string/app_notification_block_summary"
-        android:order="1"
         settings:useAdditionalSummary="true"
         settings:restrictedSwitchSummary="@string/enabled_by_admin" />
 
@@ -32,13 +31,11 @@
         android:key="badge"
         android:title="@string/notification_badge_title"
         android:summary="@string/notification_badge_summary"
-        android:order="2"
         settings:useAdditionalSummary="true"
         settings:restrictedSwitchSummary="@string/enabled_by_admin" />
 
     <PreferenceCategory
             android:key="channels"
-            android:title="@string/notification_channels"
-            android:order="3" />
+            android:title="@string/notification_channels" />
 
 </PreferenceScreen>
diff --git a/src/com/android/settings/notification/AppNotificationSettings.java b/src/com/android/settings/notification/AppNotificationSettings.java
index 45762c7..ef62fbb 100644
--- a/src/com/android/settings/notification/AppNotificationSettings.java
+++ b/src/com/android/settings/notification/AppNotificationSettings.java
@@ -19,7 +19,7 @@
 import android.app.Activity;
 import android.app.Notification;
 import android.app.NotificationChannel;
-import android.content.Context;
+import android.app.NotificationChannelGroup;
 import android.content.Intent;
 import android.content.pm.ActivityInfo;
 import android.content.pm.ApplicationInfo;
@@ -38,13 +38,11 @@
 import com.android.settings.Utils;
 import com.android.settings.applications.AppHeaderController;
 import com.android.settings.applications.AppInfoBase;
-import com.android.settings.core.PreferenceController;
 import com.android.settings.dashboard.DashboardFeatureProvider;
 import com.android.settings.notification.NotificationBackend.AppRow;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settingslib.RestrictedPreference;
 import com.android.settingslib.RestrictedSwitchPreference;
-import com.android.settingslib.drawer.CategoryKey;
 
 import java.text.Collator;
 import java.util.Collections;
@@ -66,7 +64,7 @@
 
     private DashboardFeatureProvider mDashboardFeatureProvider;
     private PreferenceCategory mChannels;
-    private List<NotificationChannel> mChannelList;
+    private List<NotificationChannelGroup> mChannelGroupList;
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
@@ -96,6 +94,7 @@
                 FeatureFactory.getFactory(activity).getDashboardFeatureProvider(activity);
 
         addPreferencesFromResource(R.xml.app_notification_settings);
+        getPreferenceScreen().setOrderingAsAdded(true);
 
         mBlock = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BLOCK);
         mBadge = (RestrictedSwitchPreference) getPreferenceScreen().findPreference(KEY_BADGE);
@@ -108,36 +107,57 @@
             ArrayMap<String, AppRow> rows = new ArrayMap<String, AppRow>();
             rows.put(mAppRow.pkg, mAppRow);
             collectConfigActivities(rows);
-            mChannelList = mBackend.getChannels(mPkg, mUid).getList();
-            Collections.sort(mChannelList, mChannelComparator);
+            // TODO: load channels in asynctask?
+            mChannelGroupList = mBackend.getChannelGroups(mPkg, mUid).getList();
+            Collections.sort(mChannelGroupList, mChannelGroupComparator);
 
-            if (mChannelList.isEmpty()) {
-                setVisible(mChannels, false);
+            if (mChannelGroupList.isEmpty()) {
+                Preference empty = new Preference(getPrefContext());
+                empty.setTitle(R.string.no_channels);
+                empty.setEnabled(false);
+                mChannels.addPreference(empty);
             } else {
-                int N = mChannelList.size();
-                for (int i = 0; i < N; i++) {
-                    final NotificationChannel channel = mChannelList.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);
+                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);
                     }
-                    mChannels.addPreference(channelPref);
+                    final List<NotificationChannel> 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);
+                        }
+                    }
                 }
             }
             updateDependents(mAppRow.banned);
@@ -203,7 +223,7 @@
     }
 
     private void updateDependents(boolean banned) {
-        setVisible(mChannels, !(mChannelList.isEmpty() || banned));
+        setVisible(mChannels, !banned);
         setVisible(mBadge, !banned);
     }
 
@@ -262,4 +282,25 @@
             return left.getId().compareTo(right.getId());
         }
     };
+
+    private Comparator<NotificationChannelGroup> mChannelGroupComparator =
+            new Comparator<NotificationChannelGroup>() {
+                private final Collator sCollator = Collator.getInstance();
+
+                @Override
+                public int compare(NotificationChannelGroup left, NotificationChannelGroup right) {
+                    // Non-groups channels (in placeholder group with a null id) come first
+                    if (left.getId() == null && right.getId() != null) {
+                        return 1;
+                    } else if (right.getId() == null && left.getId() != null) {
+                        return -1;
+                    }
+                    // sort rest of the groups by name
+                    if (!Objects.equals(left.getName(), right.getName())) {
+                        return sCollator.compare(left.getName().toString(),
+                                right.getName().toString());
+                    }
+                    return left.getId().compareTo(right.getId());
+                }
+            };
 }
diff --git a/src/com/android/settings/notification/NotificationBackend.java b/src/com/android/settings/notification/NotificationBackend.java
index 124579f..2a2185d 100644
--- a/src/com/android/settings/notification/NotificationBackend.java
+++ b/src/com/android/settings/notification/NotificationBackend.java
@@ -17,6 +17,7 @@
 
 import android.app.INotificationManager;
 import android.app.NotificationChannel;
+import android.app.NotificationChannelGroup;
 import android.app.NotificationManager;
 import android.content.Context;
 import android.content.Intent;
@@ -121,9 +122,9 @@
         }
     }
 
-    public ParceledListSlice<NotificationChannel> getChannels(String pkg, int uid) {
+    public ParceledListSlice<NotificationChannelGroup> getChannelGroups(String pkg, int uid) {
         try {
-            return sINM.getNotificationChannelsForPackage(pkg, uid, true);
+            return sINM.getNotificationChannelGroupsForPackage(pkg, uid, true);
         } catch (Exception e) {
             Log.w(TAG, "Error calling NoMan", e);
             return ParceledListSlice.emptyList();