Create settings screen for Notification Assistant

Test: this atest
Test: manual: change assistant and "adb shell dumpsys notification"
Test: manual: verify persistance through reboot (including none)

Fixes:120852765
Change-Id: Ie4516c3339246d66d7b6719ac5dd1d65c4d03b57
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index ae4ae2a..7565dd8 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -110,6 +110,7 @@
     public static class ZenModeEventRuleSettingsActivity extends SettingsActivity { /* empty */ }
     public static class SoundSettingsActivity extends SettingsActivity { /* empty */ }
     public static class ConfigureNotificationSettingsActivity extends SettingsActivity { /* empty */ }
+    public static class NotificationAssistantSettingsActivity extends SettingsActivity{ /* empty */ }
     public static class NotificationAppListActivity extends SettingsActivity { /* empty */ }
     public static class AppNotificationSettingsActivity extends SettingsActivity { /* empty */ }
     public static class ChannelNotificationSettingsActivity extends SettingsActivity { /* empty */ }
diff --git a/src/com/android/settings/core/gateway/SettingsGateway.java b/src/com/android/settings/core/gateway/SettingsGateway.java
index fb3d0c5..60655fe 100644
--- a/src/com/android/settings/core/gateway/SettingsGateway.java
+++ b/src/com/android/settings/core/gateway/SettingsGateway.java
@@ -101,6 +101,7 @@
 import com.android.settings.notification.ChannelNotificationSettings;
 import com.android.settings.notification.ConfigureNotificationSettings;
 import com.android.settings.notification.NotificationAccessSettings;
+import com.android.settings.notification.NotificationAssistantPicker;
 import com.android.settings.notification.NotificationStation;
 import com.android.settings.notification.SoundSettings;
 import com.android.settings.notification.ZenAccessSettings;
@@ -218,6 +219,7 @@
             AppInfoDashboardFragment.class.getName(),
             BatterySaverSettings.class.getName(),
             AppNotificationSettings.class.getName(),
+            NotificationAssistantPicker.class.getName(),
             ChannelNotificationSettings.class.getName(),
             ChannelGroupNotificationSettings.class.getName(),
             ApnSettings.class.getName(),
diff --git a/src/com/android/settings/notification/ConfigureNotificationSettings.java b/src/com/android/settings/notification/ConfigureNotificationSettings.java
index 1b860e3..73f6e06 100644
--- a/src/com/android/settings/notification/ConfigureNotificationSettings.java
+++ b/src/com/android/settings/notification/ConfigureNotificationSettings.java
@@ -58,6 +58,8 @@
     static final String KEY_LOCKSCREEN_WORK_PROFILE = "lock_screen_notifications_profile";
     @VisibleForTesting
     static final String KEY_SWIPE_DOWN = "gesture_swipe_down_fingerprint_notifications";
+    @VisibleForTesting
+    static final String KEY_NOTIFICATION_ASSISTANT = "notification_assistant";
 
     private static final String KEY_NOTI_DEFAULT_RINGTONE = "notification_default_ringtone";
 
diff --git a/src/com/android/settings/notification/NotificationAssistantPicker.java b/src/com/android/settings/notification/NotificationAssistantPicker.java
new file mode 100644
index 0000000..7720e6f
--- /dev/null
+++ b/src/com/android/settings/notification/NotificationAssistantPicker.java
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2019 The Android Open Source 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.notification;
+
+import android.app.settings.SettingsEnums;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.PackageItemInfo;
+import android.content.pm.ServiceInfo;
+import android.graphics.drawable.Drawable;
+import android.provider.SearchIndexableResource;
+import android.provider.Settings;
+import android.service.notification.NotificationAssistantService;
+import android.text.TextUtils;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.settings.R;
+import com.android.settings.applications.defaultapps.DefaultAppPickerFragment;
+import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settings.search.Indexable;
+import com.android.settingslib.applications.DefaultAppInfo;
+import com.android.settingslib.applications.ServiceListing;
+import com.android.settingslib.widget.CandidateInfo;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class NotificationAssistantPicker extends DefaultAppPickerFragment implements
+        ServiceListing.Callback {
+
+    private static final String TAG = "NotiAssistantPicker";
+
+    @VisibleForTesting
+    protected NotificationBackend mNotificationBackend;
+    private List<CandidateInfo> mCandidateInfos = new ArrayList<>();
+    @VisibleForTesting
+    protected Context mContext;
+    private ServiceListing mServiceListing;
+
+    @Override
+    public void onAttach(Context context) {
+        super.onAttach(context);
+        mContext = context;
+        mNotificationBackend = new NotificationBackend();
+        mServiceListing = new ServiceListing.Builder(context)
+                .setTag(TAG)
+                .setSetting(Settings.Secure.ENABLED_NOTIFICATION_ASSISTANT)
+                .setIntentAction(NotificationAssistantService.SERVICE_INTERFACE)
+                .setPermission(android.Manifest.permission.BIND_NOTIFICATION_ASSISTANT_SERVICE)
+                .setNoun("notification assistant")
+                .build();
+        mServiceListing.addCallback(this);
+        mServiceListing.reload();
+    }
+
+    @Override
+    public void onDetach() {
+        super.onDetach();
+        mServiceListing.removeCallback(this);
+    }
+
+    @Override
+    protected int getPreferenceScreenResId() {
+        return R.xml.notification_assistant_settings;
+    }
+
+    @Override
+    protected List<? extends CandidateInfo> getCandidates() {
+        return mCandidateInfos;
+    }
+
+    @Override
+    protected String getDefaultKey() {
+        ComponentName cn = mNotificationBackend.getAllowedNotificationAssistant();
+        return (cn != null) ? cn.flattenToString() : "";
+    }
+
+    @Override
+    protected boolean setDefaultKey(String key) {
+        return mNotificationBackend.setNotificationAssistantGranted(
+                ComponentName.unflattenFromString(key));
+    }
+
+    @Override
+    public int getMetricsCategory() {
+        return SettingsEnums.DEFAULT_NOTIFICATION_ASSISTANT;
+    }
+
+    @Override
+    protected CharSequence getConfirmationMessage(CandidateInfo info) {
+        if (TextUtils.isEmpty(info.getKey())) {
+            return null;
+        }
+        return mContext.getString(R.string.notification_assistant_security_warning_summary,
+                info.loadLabel());
+    }
+
+    @Override
+    public void onServicesReloaded(List<ServiceInfo> services) {
+        List<CandidateInfo> list = new ArrayList<>();
+        services.sort(new PackageItemInfo.DisplayNameComparator(mPm));
+        for (ServiceInfo service : services) {
+            final ComponentName cn = new ComponentName(service.packageName, service.name);
+            list.add(new DefaultAppInfo(mContext, mPm, mUserId, cn));
+        }
+        list.add(new CandidateNone(mContext));
+        mCandidateInfos = list;
+    }
+
+    public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+            new BaseSearchIndexProvider() {
+                @Override
+                public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
+                        boolean enabled) {
+                    final List<SearchIndexableResource> result = new ArrayList<>();
+
+                    final SearchIndexableResource sir = new SearchIndexableResource(context);
+                    sir.xmlResId = R.xml.notification_assistant_settings;
+                    result.add(sir);
+                    return result;
+                }
+            };
+
+    public static class CandidateNone extends CandidateInfo {
+
+        public Context mContext;
+
+        public CandidateNone(Context context) {
+            super(true);
+            mContext = context;
+        }
+
+        @Override
+        public CharSequence loadLabel() {
+            return mContext.getString(R.string.no_notification_assistant);
+        }
+
+        @Override
+        public Drawable loadIcon() {
+            return null;
+        }
+
+        @Override
+        public String getKey() {
+            return "";
+        }
+    }
+}
diff --git a/src/com/android/settings/notification/NotificationAssistantPreferenceController.java b/src/com/android/settings/notification/NotificationAssistantPreferenceController.java
new file mode 100644
index 0000000..5c591b8
--- /dev/null
+++ b/src/com/android/settings/notification/NotificationAssistantPreferenceController.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2019 The Android Open Source 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.notification;
+
+import android.content.Context;
+
+import com.android.settings.core.BasePreferenceController;
+
+public class NotificationAssistantPreferenceController extends BasePreferenceController {
+
+    public NotificationAssistantPreferenceController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return BasePreferenceController.AVAILABLE;
+    }
+}
diff --git a/src/com/android/settings/notification/NotificationBackend.java b/src/com/android/settings/notification/NotificationBackend.java
index dbba616..ba07438 100644
--- a/src/com/android/settings/notification/NotificationBackend.java
+++ b/src/com/android/settings/notification/NotificationBackend.java
@@ -23,6 +23,7 @@
 import android.app.NotificationChannelGroup;
 import android.app.usage.IUsageStatsManager;
 import android.app.usage.UsageEvents;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
@@ -410,6 +411,29 @@
         }
     }
 
+    public ComponentName getAllowedNotificationAssistant() {
+        try {
+            return sINM.getAllowedNotificationAssistant();
+        } catch (Exception e) {
+            Log.w(TAG, "Error calling NoMan", e);
+            return null;
+        }
+    }
+
+    public boolean setNotificationAssistantGranted(ComponentName cn) {
+        try {
+            sINM.setNotificationAssistantAccessGranted(cn, true);
+            if (cn == null) {
+                return sINM.getAllowedNotificationAssistant() == null;
+            } else {
+                return cn.equals(sINM.getAllowedNotificationAssistant());
+            }
+        } catch (Exception e) {
+            Log.w(TAG, "Error calling NoMan", e);
+            return false;
+        }
+    }
+
     /**
      * NotificationsSentState contains how often an app sends notifications and how recently it sent
      * one.