diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java index 99c46bf0696..acb99a6739e 100644 --- a/src/com/android/settings/bluetooth/BluetoothSettings.java +++ b/src/com/android/settings/bluetooth/BluetoothSettings.java @@ -99,6 +99,8 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem private final IntentFilter mIntentFilter; + // For Search + private static final String DATA_KEY_REFERENCE = "main_toggle_bluetooth"; // accessed from inner class (not private to avoid thunks) FooterPreference mMyDevicePreference; @@ -567,6 +569,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem SearchIndexableRaw data = new SearchIndexableRaw(context); data.title = res.getString(R.string.bluetooth_settings); data.screenTitle = res.getString(R.string.bluetooth_settings); + data.key = DATA_KEY_REFERENCE; result.add(data); // Add cached paired BT devices diff --git a/src/com/android/settings/search2/CursorToSearchResultConverter.java b/src/com/android/settings/search2/CursorToSearchResultConverter.java index 880a65b81de..71d999e0612 100644 --- a/src/com/android/settings/search2/CursorToSearchResultConverter.java +++ b/src/com/android/settings/search2/CursorToSearchResultConverter.java @@ -35,6 +35,7 @@ import com.android.settings.Utils; import com.android.settings.dashboard.SiteMapManager; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -77,6 +78,22 @@ class CursorToSearchResultConverter { private final Set mKeys; + private final int LONG_TITLE_LENGTH = 20; + + private static final String[] whiteList = { + "main_toggle_wifi", + "main_toggle_bluetooth", + "toggle_airplane", + "tether_settings", + "battery_saver", + "toggle_nfc", + "restrict_background", + "data_usage_enable", + "button_roaming_key", + }; + private static final Set prioritySettings = new HashSet(Arrays.asList(whiteList)); + + public CursorToSearchResultConverter(Context context, String queryText) { mContext = context; mKeys = new HashSet<>(); @@ -135,7 +152,7 @@ class CursorToSearchResultConverter { } final List breadcrumbs = getBreadcrumbs(sitemapManager, cursor); - final int rank = getRank(breadcrumbs, baseRank); + final int rank = getRank(title, breadcrumbs, baseRank, key); final SearchResult.Builder builder = new SearchResult.Builder(); builder.addTitle(title) @@ -225,18 +242,32 @@ class CursorToSearchResultConverter { } /** Uses the breadcrumbs to determine the offset to the base rank. - * There are two checks - * A) If the query matches the highest level menu title - * B) If the query matches a subsequent menu title + * There are three checks + * A) If the result is prioritized and the highest base level + * B) If the query matches the highest level menu title + * C) If the query matches a subsequent menu title + * D) Is the title longer than 20 * - * If the query matches A and B, the offset is 0. - * If the query matches A only, the offset is 1. - * If the query matches neither A nor B, the offset is 2. + * If the query matches A, set it to TOP_RANK + * If the query matches B and C, the offset is 0. + * If the query matches C only, the offset is 1. + * If the query matches neither B nor C, the offset is 2. + * If the query matches D, the offset is 2 + + * @param title of the result. * @param crumbs from the Information Architecture * @param baseRank of the result. Lower if it's a better result. * @return */ - private int getRank(List crumbs, int baseRank) { + private int getRank(String title, List crumbs, int baseRank, String key) { + // The result can only be prioritized if it is a top ranked result. + if (prioritySettings.contains(key) && baseRank < DatabaseResultLoader.BASE_RANKS[1]) { + return SearchResult.TOP_RANK; + } + if (title.length() > LONG_TITLE_LENGTH) { + return baseRank + 2; + } return baseRank; } + } diff --git a/src/com/android/settings/search2/DatabaseResultLoader.java b/src/com/android/settings/search2/DatabaseResultLoader.java index ad1e6a9bd30..5892ebc1ede 100644 --- a/src/com/android/settings/search2/DatabaseResultLoader.java +++ b/src/com/android/settings/search2/DatabaseResultLoader.java @@ -97,7 +97,7 @@ public class DatabaseResultLoader extends AsyncLoader { /** - * Defines the max rank for a search result to be considered as ranked. Results with ranks + * Defines the lowest rank for a search result to be considered as ranked. Results with ranks * higher than this have no guarantee for sorting order. */ - public static final int MAX_RANK = 10; + public static final int BOTTOM_RANK = 10; + + /** + * Defines the highest rank for a search result. Used for special search results only. + */ + public static final int TOP_RANK = 0; /** * The title of the result and main text displayed. diff --git a/src/com/android/settings/search2/SearchResultsAdapter.java b/src/com/android/settings/search2/SearchResultsAdapter.java index afe432a996d..5151b644f49 100644 --- a/src/com/android/settings/search2/SearchResultsAdapter.java +++ b/src/com/android/settings/search2/SearchResultsAdapter.java @@ -32,7 +32,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import static com.android.settings.search2.SearchResult.MAX_RANK; +import static com.android.settings.search2.SearchResult.TOP_RANK; +import static com.android.settings.search2.SearchResult.BOTTOM_RANK; public class SearchResultsAdapter extends Adapter { @@ -117,9 +118,9 @@ public class SearchResultsAdapter extends Adapter { int dbIndex = 0; int appIndex = 0; - int rank = 1; + int rank = TOP_RANK; - while (rank <= MAX_RANK) { + while (rank <= BOTTOM_RANK) { while ((dbIndex < dbSize) && (databaseResults.get(dbIndex).rank == rank)) { results.add(databaseResults.get(dbIndex++)); } diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java index d652efa4a6b..9405e8c81fd 100644 --- a/src/com/android/settings/wifi/WifiSettings.java +++ b/src/com/android/settings/wifi/WifiSettings.java @@ -168,6 +168,9 @@ public class WifiSettings extends RestrictedSettingsFragment private MenuItem mScanMenuItem; + // For Search + private static final String DATA_KEY_REFERENCE = "main_toggle_wifi"; + /* End of "used in Wifi Setup context" */ public WifiSettings() { @@ -1028,6 +1031,7 @@ public class WifiSettings extends RestrictedSettingsFragment data.title = res.getString(R.string.wifi_settings); data.screenTitle = res.getString(R.string.wifi_settings); data.keywords = res.getString(R.string.keywords_wifi); + data.key = DATA_KEY_REFERENCE; result.add(data); // Add saved Wi-Fi access points diff --git a/tests/robotests/src/com/android/settings/search2/CursorToSearchResultConverterTest.java b/tests/robotests/src/com/android/settings/search2/CursorToSearchResultConverterTest.java index 6f326ab52dc..ab55605b106 100644 --- a/tests/robotests/src/com/android/settings/search2/CursorToSearchResultConverterTest.java +++ b/tests/robotests/src/com/android/settings/search2/CursorToSearchResultConverterTest.java @@ -24,6 +24,7 @@ import android.database.MatrixCursor; import android.graphics.drawable.Drawable; import android.util.ArrayMap; +import com.android.settings.DisplaySettings; import com.android.settings.R; import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.SubSettings; @@ -32,6 +33,7 @@ import com.android.settings.dashboard.SiteMapManager; import com.android.settings.gestures.SwipeToNotificationSettings; import com.android.settings.search2.ResultPayload.PayloadType; +import com.android.settings.wifi.WifiSettings; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -146,6 +148,29 @@ public class CursorToSearchResultConverterTest { } } + @Test + public void testLongTitle_PenalizedInRank() { + MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS); + final String BLANK = ""; + cursor.addRow(new Object[]{ + ID, // Doc ID + "Longer than 20 characters", // Title + SUMMARY, // Summary on + SUMMARY, // summary off + DisplaySettings.class.getName(), + BLANK, // screen title + null, // icon + BLANK, // action + null, // target package + BLANK, // target class + BLANK, // Key + 0, // Payload Type + null // Payload + }); + List results = mConverter.convertCursor(mSiteMapManager, cursor, BASE_RANK); + assertThat(results.get(0).rank).isEqualTo(BASE_RANK + 2); + } + @Test public void testParseCursor_MatchesIntentForSubSettings() { MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS); @@ -246,6 +271,245 @@ public class CursorToSearchResultConverterTest { assertThat(newPayload.valueMap.get(0)).isFalse(); } + // The following tests are temporary, and should be removed when we replace the Search + // White-list solution for elevating ranking. + + @Test + public void testWifiKey_PrioritizedResult() { + MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS); + final String BLANK = ""; + final String key = "main_toggle_wifi"; + + cursor.addRow(new Object[]{ + ID, // Doc ID + TITLES[0], // Title + SUMMARY, // Summary on + SUMMARY, // summary off + WifiSettings.class.getName(), // ClassName + BLANK, // screen title + null, // icon + BLANK, // action + null, // target package + BLANK, // target class + key, // Key + 0, // Payload Type + null // Payload + }); + List results = mConverter.convertCursor(mSiteMapManager, cursor, BASE_RANK); + + assertThat(results.get(0).rank).isEqualTo(SearchResult.TOP_RANK); + } + + @Test + public void testBluetoothKey_PrioritizedResult() { + MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS); + final String BLANK = ""; + final String key = "main_toggle_bluetooth"; + + cursor.addRow(new Object[]{ + ID, // Doc ID + TITLES[0], // Title + SUMMARY, // Summary on + SUMMARY, // summary off + WifiSettings.class.getName(), // ClassName + BLANK, // screen title + null, // icon + BLANK, // action + null, // target package + BLANK, // target class + key, // Key + 0, // Payload Type + null // Payload + }); + List results = mConverter.convertCursor(mSiteMapManager, cursor, BASE_RANK); + + assertThat(results.get(0).rank).isEqualTo(SearchResult.TOP_RANK); + } + + @Test + public void testAirplaneKey_PrioritizedResult() { + MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS); + final String BLANK = ""; + final String key = "toggle_airplane"; + + cursor.addRow(new Object[]{ + ID, // Doc ID + TITLES[0], // Title + SUMMARY, // Summary on + SUMMARY, // summary off + WifiSettings.class.getName(), // ClassName + BLANK, // screen title + null, // icon + BLANK, // action + null, // target package + BLANK, // target class + key, // Key + 0, // Payload Type + null // Payload + }); + List results = mConverter.convertCursor(mSiteMapManager, cursor, BASE_RANK); + + assertThat(results.get(0).rank).isEqualTo(SearchResult.TOP_RANK); + } + + @Test + public void testHotspotKey_PrioritizedResult() { + MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS); + final String BLANK = ""; + final String key = "tether_settings"; + + cursor.addRow(new Object[]{ + ID, // Doc ID + TITLES[0], // Title + SUMMARY, // Summary on + SUMMARY, // summary off + WifiSettings.class.getName(), // ClassName + BLANK, // screen title + null, // icon + BLANK, // action + null, // target package + BLANK, // target class + key, // Key + 0, // Payload Type + null // Payload + }); + List results = mConverter.convertCursor(mSiteMapManager, cursor, BASE_RANK); + + assertThat(results.get(0).rank).isEqualTo(SearchResult.TOP_RANK); + } + + @Test + public void testBatterySaverKey_PrioritizedResult() { + MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS); + final String BLANK = ""; + final String key = "battery_saver"; + + cursor.addRow(new Object[]{ + ID, // Doc ID + TITLES[0], // Title + SUMMARY, // Summary on + SUMMARY, // summary off + WifiSettings.class.getName(), // ClassName + BLANK, // screen title + null, // icon + BLANK, // action + null, // target package + BLANK, // target class + key, // Key + 0, // Payload Type + null // Payload + }); + List results = mConverter.convertCursor(mSiteMapManager, cursor, BASE_RANK); + + assertThat(results.get(0).rank).isEqualTo(SearchResult.TOP_RANK); + } + + @Test + public void testNFCKey_PrioritizedResult() { + MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS); + final String BLANK = ""; + final String key = "toggle_nfc"; + + cursor.addRow(new Object[]{ + ID, // Doc ID + TITLES[0], // Title + SUMMARY, // Summary on + SUMMARY, // summary off + WifiSettings.class.getName(), // ClassName + BLANK, // screen title + null, // icon + BLANK, // action + null, // target package + BLANK, // target class + key, // Key + 0, // Payload Type + null // Payload + }); + List results = mConverter.convertCursor(mSiteMapManager, cursor, BASE_RANK); + + assertThat(results.get(0).rank).isEqualTo(SearchResult.TOP_RANK); + } + + @Test + public void testDataSaverKey_PrioritizedResult() { + MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS); + final String BLANK = ""; + final String key = "restrict_background"; + + cursor.addRow(new Object[]{ + ID, // Doc ID + TITLES[0], // Title + SUMMARY, // Summary on + SUMMARY, // summary off + WifiSettings.class.getName(), // ClassName + BLANK, // screen title + null, // icon + BLANK, // action + null, // target package + BLANK, // target class + key, // Key + 0, // Payload Type + null // Payload + }); + List results = mConverter.convertCursor(mSiteMapManager, cursor, BASE_RANK); + + assertThat(results.get(0).rank).isEqualTo(SearchResult.TOP_RANK); + } + + @Test + public void testDataUsageKey_PrioritizedResult() { + MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS); + final String BLANK = ""; + final String key = "data_usage_enable"; + + cursor.addRow(new Object[]{ + ID, // Doc ID + TITLES[0], // Title + SUMMARY, // Summary on + SUMMARY, // summary off + WifiSettings.class.getName(), // ClassName + BLANK, // screen title + null, // icon + BLANK, // action + null, // target package + BLANK, // target class + key, // Key + 0, // Payload Type + null // Payload + }); + List results = mConverter.convertCursor(mSiteMapManager, cursor, BASE_RANK); + + assertThat(results.get(0).rank).isEqualTo(SearchResult.TOP_RANK); + } + + @Test + public void testRoamingKey_PrioritizedResult() { + MatrixCursor cursor = new MatrixCursor(DatabaseResultLoader.SELECT_COLUMNS); + final String BLANK = ""; + final String key = "button_roaming_key"; + + cursor.addRow(new Object[]{ + ID, // Doc ID + TITLES[0], // Title + SUMMARY, // Summary on + SUMMARY, // summary off + WifiSettings.class.getName(), // ClassName + BLANK, // screen title + null, // icon + BLANK, // action + null, // target package + BLANK, // target class + key, // Key + 0, // Payload Type + null // Payload + }); + List results = mConverter.convertCursor(mSiteMapManager, cursor, BASE_RANK); + + assertThat(results.get(0).rank).isEqualTo(SearchResult.TOP_RANK); + } + + // End of temporary tests + private MatrixCursor getDummyCursor() { return getDummyCursor(true /* hasIcon */); }