Add public intent filters to indexing

Adds a public intent action to the index so that intents
can be created for callers outside of settings.

Change-Id: I9f87263f213b6de40542e8735c931ee1f0d82094
Fixes: 63136008
Test: make RunSettingsRoboTests
diff --git a/src/com/android/settings/search/DatabaseIndexingManager.java b/src/com/android/settings/search/DatabaseIndexingManager.java
index 1fbe055..bfa2794 100644
--- a/src/com/android/settings/search/DatabaseIndexingManager.java
+++ b/src/com/android/settings/search/DatabaseIndexingManager.java
@@ -17,6 +17,8 @@
 
 package com.android.settings.search;
 
+import com.android.settings.R;
+
 import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.ContentValues;
@@ -851,7 +853,8 @@
             List<String> nonIndexableKeys) {
 
         final String className = sir.className;
-        final int rank = sir.rank;
+        final String intentAction = sir.intentAction;
+        final String intentTargetPackage = sir.intentTargetPackage;
 
         if (provider == null) {
             Log.w(LOG_TAG, "Cannot find provider: " + className);
@@ -879,7 +882,6 @@
                         .setClassName(className)
                         .setScreenTitle(raw.screenTitle)
                         .setIconResId(raw.iconResId)
-                        .setRank(rank)
                         .setIntentAction(raw.intentAction)
                         .setIntentTargetPackage(raw.intentTargetPackage)
                         .setIntentTargetClass(raw.intentTargetClass)
@@ -904,7 +906,15 @@
                     continue;
                 }
 
-                item.className = (TextUtils.isEmpty(item.className)) ? className : item.className;
+                item.className = TextUtils.isEmpty(item.className)
+                        ? className
+                        : item.className;
+                item.intentAction = TextUtils.isEmpty(item.intentAction)
+                        ? intentAction
+                        : item.intentAction;
+                item.intentTargetPackage = TextUtils.isEmpty(item.intentTargetPackage)
+                        ? intentTargetPackage
+                        : item.intentTargetPackage;
 
                 indexFromResource(database, localeStr, item, nonIndexableKeys);
             }
@@ -1238,7 +1248,11 @@
             private Intent buildIntent(Context context) {
                 final Intent intent;
 
-                if (TextUtils.isEmpty(mIntentAction)) {
+                boolean isEmptyIntentAction = TextUtils.isEmpty(mIntentAction);
+                // No intent action is set, or the intent action is for a subsetting.
+                if (isEmptyIntentAction
+                        || (!isEmptyIntentAction && TextUtils.equals(mIntentTargetPackage,
+                        SearchIndexableResources.SUBSETTING_TARGET_PACKAGE))) {
                     // Action is null, we will launch it as a sub-setting
                     intent = DatabaseIndexingUtils.buildSubsettingIntent(context, mClassName, mKey,
                             mScreenTitle);
diff --git a/src/com/android/settings/search/SearchIndexableResources.java b/src/com/android/settings/search/SearchIndexableResources.java
index 431729f..b736d9b 100644
--- a/src/com/android/settings/search/SearchIndexableResources.java
+++ b/src/com/android/settings/search/SearchIndexableResources.java
@@ -21,6 +21,7 @@
 import android.support.annotation.VisibleForTesting;
 import android.support.annotation.XmlRes;
 
+import android.text.TextUtils;
 import com.android.settings.DateTimeSettings;
 import com.android.settings.DeviceInfoSettings;
 import com.android.settings.DisplaySettings;
@@ -93,6 +94,12 @@
 
 public final class SearchIndexableResources {
 
+    /**
+     * Identifies subsettings which have an {@link SearchIndexableResource#intentAction} but
+     * whose intents should still be treated as subsettings inside of Settings.
+     */
+    public static final String SUBSETTING_TARGET_PACKAGE = "subsetting_target_package";
+
     @XmlRes
     public static final int NO_DATA_RES_ID = 0;
 
@@ -102,8 +109,22 @@
     @VisibleForTesting
     static void addIndex(Class<?> indexClass, @XmlRes int xmlResId,
             @DrawableRes int iconResId) {
+        addIndex(indexClass, xmlResId, iconResId, null /* targetAction */);
+    }
+
+    @VisibleForTesting
+    static void addIndex(Class<?> indexClass, @XmlRes int xmlResId,
+            @DrawableRes int iconResId, String targetAction) {
         String className = indexClass.getName();
-        sResMap.put(className, new SearchIndexableResource(0, xmlResId, className, iconResId));
+        SearchIndexableResource resource =
+                new SearchIndexableResource(0, xmlResId, className, iconResId);
+
+        if (!TextUtils.isEmpty(targetAction)) {
+            resource.intentAction = targetAction;
+            resource.intentTargetPackage = SUBSETTING_TARGET_PACKAGE;
+        }
+
+        sResMap.put(className, resource);
     }
 
     static {
@@ -118,14 +139,16 @@
         addIndex(DataUsageSummary.class, NO_DATA_RES_ID, R.drawable.ic_settings_data_usage);
         addIndex(DataUsageMeteredSettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_data_usage);
         addIndex(ScreenZoomSettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_display);
-        addIndex(DisplaySettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_display);
+        addIndex(DisplaySettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_display,
+                "android.settings.DISPLAY_SETTINGS");
         addIndex(AmbientDisplaySettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_display);
         addIndex(WallpaperTypeSettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_display);
         addIndex(ConfigureNotificationSettings.class,
                 R.xml.configure_notification_settings, R.drawable.ic_settings_notifications);
         addIndex(AppAndNotificationDashboardFragment.class, NO_DATA_RES_ID,
                 R.drawable.ic_settings_applications);
-        addIndex(SoundSettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_sound);
+        addIndex(SoundSettings.class, NO_DATA_RES_ID, R.drawable.ic_settings_sound,
+                "android.settings.SOUND_SETTINGS");
         addIndex(ZenModeSettings.class,
                 R.xml.zen_mode_settings, R.drawable.ic_settings_notifications);
         addIndex(ZenModePrioritySettings.class,
diff --git a/src/com/android/settings/search/SettingsSearchIndexablesProvider.java b/src/com/android/settings/search/SettingsSearchIndexablesProvider.java
index f7c2a16..a13081a 100644
--- a/src/com/android/settings/search/SettingsSearchIndexablesProvider.java
+++ b/src/com/android/settings/search/SettingsSearchIndexablesProvider.java
@@ -58,8 +58,8 @@
             ref[COLUMN_INDEX_XML_RES_RESID] = val.xmlResId;
             ref[COLUMN_INDEX_XML_RES_CLASS_NAME] = val.className;
             ref[COLUMN_INDEX_XML_RES_ICON_RESID] = val.iconResId;
-            ref[COLUMN_INDEX_XML_RES_INTENT_ACTION] = null; // intent action
-            ref[COLUMN_INDEX_XML_RES_INTENT_TARGET_PACKAGE] = null; // intent target package
+            ref[COLUMN_INDEX_XML_RES_INTENT_ACTION] = val.intentAction;
+            ref[COLUMN_INDEX_XML_RES_INTENT_TARGET_PACKAGE] = val.intentTargetPackage;
             ref[COLUMN_INDEX_XML_RES_INTENT_TARGET_CLASS] = null; // intent target class
             cursor.addRow(ref);
         }
diff --git a/tests/robotests/src/com/android/settings/search/DatabaseIndexingManagerTest.java b/tests/robotests/src/com/android/settings/search/DatabaseIndexingManagerTest.java
index 729541c..ea2a05b 100644
--- a/tests/robotests/src/com/android/settings/search/DatabaseIndexingManagerTest.java
+++ b/tests/robotests/src/com/android/settings/search/DatabaseIndexingManagerTest.java
@@ -572,7 +572,7 @@
         // Locale
         assertThat(cursor.getString(0)).isEqualTo(localeStr);
         // Data Rank
-        assertThat(cursor.getInt(1)).isEqualTo(rank);
+        assertThat(cursor.getInt(1)).isEqualTo(0);
         // Data Title
         assertThat(cursor.getString(2)).isEqualTo("Display size");
         // Normalized Title
@@ -629,7 +629,7 @@
 
     @Test
     public void testResourceProvider_resourceRowMatches() {
-        SearchIndexableResource resource = getFakeResource(0);
+        SearchIndexableResource resource = getFakeResource(0 /* xml */);
         resource.className = "com.android.settings.display.ScreenZoomSettings";
 
         mManager.indexOneSearchIndexableData(mDb, localeStr, resource, new HashMap<>());
@@ -639,7 +639,7 @@
         // Locale
         assertThat(cursor.getString(0)).isEqualTo(localeStr);
         // Data Rank
-        assertThat(cursor.getInt(1)).isEqualTo(rank);
+        assertThat(cursor.getInt(1)).isEqualTo(0);
         // Data Title
         assertThat(cursor.getString(2)).isEqualTo("Display size");
         // Normalized Title
@@ -687,7 +687,7 @@
 
     @Test
     public void testResourceProvider_disabledResource_rowsInserted() {
-        SearchIndexableResource resource = getFakeResource(0);
+        SearchIndexableResource resource = getFakeResource(0 /* xml */);
         resource.className = "com.android.settings.LegalSettings";
 
         mManager.indexOneSearchIndexableData(mDb, localeStr, resource,
@@ -709,6 +709,25 @@
         assertThat(cursor.getCount()).isEqualTo(1);
     }
 
+    @Test
+    public void testResourceProvider_nonSubsettingIntent() {
+        SearchIndexableResource resource = getFakeResource(0 /* xml */);
+        String fakeAction = "fake_action";
+        resource.className = "com.android.settings.LegalSettings";
+        resource.intentAction = fakeAction;
+        resource.intentTargetPackage = SearchIndexableResources.SUBSETTING_TARGET_PACKAGE;
+
+        mManager.indexOneSearchIndexableData(mDb, localeStr, resource, new HashMap<>());
+        Cursor cursor = mDb.rawQuery("SELECT * FROM prefs_index", null);
+        cursor.moveToPosition(0);
+
+        // Intent Action
+        assertThat(cursor.getString(13)).isEqualTo(fakeAction);
+        // Target Package
+        assertThat(cursor.getString(14))
+                .isEqualTo(SearchIndexableResources.SUBSETTING_TARGET_PACKAGE);
+    }
+
     // Test new public indexing flow
 
     @Test