Add flag to allow NAS to peform ranking

This will be used by implementations of NotificationAssistantService to
rerank notifications within the same importance bucket.

Bug: 146443378
Test: atest
Change-Id: I95e45081ae90f1e60de86b1b850ad18f33eb803b
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 860b96e..daba0be 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -7891,6 +7891,12 @@
     <!-- Configure Notifications: setting summary [CHAR LIMIT=200] -->
     <string name="asst_capability_prioritizer_summary">Automatically set lower priority notifications to Gentle</string>
 
+    <!-- Configure Notifications: setting title [CHAR LIMIT=80 BACKUP_MESSAGE_ID=6691908606916292167] -->
+    <string name="asst_capability_ranking_title">Adaptive notification ranking</string>
+
+    <!-- Configure Notifications: setting summary [CHAR LIMIT=200] -->
+    <string name="asst_capability_ranking_summary">Automatically rank notifications by relevance</string>
+
     <!-- Configure Notifications: setting title [CHAR LIMIT=80] -->
     <string name="asst_capabilities_actions_replies_title">Suggested actions and replies</string>
 
diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml
index 2607c09..3ddf1db 100644
--- a/res/xml/development_settings.xml
+++ b/res/xml/development_settings.xml
@@ -536,6 +536,11 @@
             android:title="@string/asst_capability_prioritizer_title"
             settings:controller="com.android.settings.notification.AssistantCapabilityPreferenceController" />
 
+        <SwitchPreference
+            android:key="asst_capability_ranking"
+            android:title="@string/asst_capability_ranking_title"
+            settings:controller="com.android.settings.notification.AssistantCapabilityPreferenceController" />
+
         <Preference
             android:key="inactive_apps"
             android:title="@string/inactive_apps_title"
diff --git a/src/com/android/settings/notification/AssistantCapabilityPreferenceController.java b/src/com/android/settings/notification/AssistantCapabilityPreferenceController.java
index 812bc65..3c6f17f 100644
--- a/src/com/android/settings/notification/AssistantCapabilityPreferenceController.java
+++ b/src/com/android/settings/notification/AssistantCapabilityPreferenceController.java
@@ -28,6 +28,7 @@
 public class AssistantCapabilityPreferenceController extends TogglePreferenceController {
 
     static final String PRIORITIZER_KEY = "asst_capability_prioritizer";
+    static final String RANKING_KEY = "asst_capability_ranking";
     static final String SMART_KEY = "asst_capabilities_actions_replies";
     private NotificationBackend mBackend;
 
@@ -45,7 +46,9 @@
     public boolean isChecked() {
         List<String> capabilities = mBackend.getAssistantAdjustments(mContext.getPackageName());
         if (PRIORITIZER_KEY.equals(getPreferenceKey())) {
-           return capabilities.contains(Adjustment.KEY_IMPORTANCE);
+            return capabilities.contains(Adjustment.KEY_IMPORTANCE);
+        } else if (RANKING_KEY.equals(getPreferenceKey())) {
+            return capabilities.contains(Adjustment.KEY_RANKING_SCORE);
         } else if (SMART_KEY.equals(getPreferenceKey())) {
             return capabilities.contains(Adjustment.KEY_CONTEXTUAL_ACTIONS)
                     && capabilities.contains(Adjustment.KEY_TEXT_REPLIES);
@@ -57,6 +60,8 @@
     public boolean setChecked(boolean isChecked) {
         if (PRIORITIZER_KEY.equals(getPreferenceKey())) {
             mBackend.allowAssistantAdjustment(Adjustment.KEY_IMPORTANCE, isChecked);
+        } else if (RANKING_KEY.equals(getPreferenceKey())) {
+            mBackend.allowAssistantAdjustment(Adjustment.KEY_RANKING_SCORE, isChecked);
         } else if (SMART_KEY.equals(getPreferenceKey())) {
             mBackend.allowAssistantAdjustment(Adjustment.KEY_CONTEXTUAL_ACTIONS, isChecked);
             mBackend.allowAssistantAdjustment(Adjustment.KEY_TEXT_REPLIES, isChecked);
diff --git a/tests/robotests/src/com/android/settings/notification/AssistantCapabilityPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/AssistantCapabilityPreferenceControllerTest.java
index 397b72e..5244c7e 100644
--- a/tests/robotests/src/com/android/settings/notification/AssistantCapabilityPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/AssistantCapabilityPreferenceControllerTest.java
@@ -19,6 +19,7 @@
 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
 import static com.android.settings.core.BasePreferenceController.DISABLED_DEPENDENT_SETTING;
 import static com.android.settings.notification.AssistantCapabilityPreferenceController.PRIORITIZER_KEY;
+import static com.android.settings.notification.AssistantCapabilityPreferenceController.RANKING_KEY;
 import static com.android.settings.notification.AssistantCapabilityPreferenceController.SMART_KEY;
 
 import static com.google.common.truth.Truth.assertThat;
@@ -56,8 +57,10 @@
 
     private Context mContext;
     private AssistantCapabilityPreferenceController mPrioritizerController;
+    private AssistantCapabilityPreferenceController mRankingController;
     private AssistantCapabilityPreferenceController mChipController;
     private Preference mPrioritizerPreference;
+    private Preference mRankingPreference;
     private Preference mChipPreference;
 
     @Before
@@ -71,6 +74,13 @@
         mPrioritizerPreference.setKey(mPrioritizerController.getPreferenceKey());
         when(mScreen.findPreference(
                 mPrioritizerController.getPreferenceKey())).thenReturn(mPrioritizerPreference);
+        mRankingController = new AssistantCapabilityPreferenceController(
+                mContext, RANKING_KEY);
+        mRankingController.setBackend(mBackend);
+        mRankingPreference = new Preference(mContext);
+        mRankingPreference.setKey(mRankingController.getPreferenceKey());
+        when(mScreen.findPreference(
+                mRankingController.getPreferenceKey())).thenReturn(mRankingPreference);
         mChipController = new AssistantCapabilityPreferenceController(mContext, SMART_KEY);
         mChipController.setBackend(mBackend);
         mChipPreference = new Preference(mContext);
@@ -111,6 +121,27 @@
         capabilities.add(Adjustment.KEY_IMPORTANCE);
         when(mBackend.getAssistantAdjustments(anyString())).thenReturn(capabilities);
         assertThat(mPrioritizerController.isChecked()).isTrue();
+
+        capabilities = new ArrayList<>();
+        capabilities.add(Adjustment.KEY_RANKING_SCORE);
+        when(mBackend.getAssistantAdjustments(anyString())).thenReturn(capabilities);
+        assertThat(mPrioritizerController.isChecked()).isFalse();
+    }
+
+    @Test
+    public void isChecked_rankingSettingIsOff_false() {
+        List<String> capabilities = new ArrayList<>();
+        capabilities.add(Adjustment.KEY_IMPORTANCE);
+        when(mBackend.getAssistantAdjustments(anyString())).thenReturn(capabilities);
+        assertThat(mRankingController.isChecked()).isFalse();
+    }
+
+    @Test
+    public void isChecked_rankingSettingIsOn_true() {
+        List<String> capabilities = new ArrayList<>();
+        capabilities.add(Adjustment.KEY_RANKING_SCORE);
+        when(mBackend.getAssistantAdjustments(anyString())).thenReturn(capabilities);
+        assertThat(mRankingController.isChecked()).isTrue();
     }
 
     @Test
@@ -121,6 +152,11 @@
         assertThat(mChipController.isChecked()).isFalse();
 
         capabilities = new ArrayList<>();
+        capabilities.add(Adjustment.KEY_RANKING_SCORE);
+        when(mBackend.getAssistantAdjustments(anyString())).thenReturn(capabilities);
+        assertThat(mChipController.isChecked()).isFalse();
+
+        capabilities = new ArrayList<>();
         capabilities.add(Adjustment.KEY_CONTEXTUAL_ACTIONS);
         when(mBackend.getAssistantAdjustments(anyString())).thenReturn(capabilities);
         assertThat(mChipController.isChecked()).isFalse();
@@ -153,6 +189,18 @@
     }
 
     @Test
+    public void onPreferenceChange_rankingOn() {
+        mRankingController.onPreferenceChange(mRankingPreference, true);
+        verify(mBackend).allowAssistantAdjustment(Adjustment.KEY_RANKING_SCORE, true);
+    }
+
+    @Test
+    public void onPreferenceChange_rankingOff() {
+        mRankingController.onPreferenceChange(mRankingPreference, false);
+        verify(mBackend).allowAssistantAdjustment(Adjustment.KEY_RANKING_SCORE, false);
+    }
+
+    @Test
     public void onPreferenceChange_chipsOn() {
         mChipController.onPreferenceChange(mChipPreference, true);
         verify(mBackend).allowAssistantAdjustment(Adjustment.KEY_CONTEXTUAL_ACTIONS, true);