Add first inline switch result.
Adds gestures as Inline switch results to Settings Search. Change-Id: Ie9b69ac2a54756d5b756401e348cae0a02c907c9 Fixes: 33669037 Test: make RunSettingsRoboTests
This commit is contained in:
@@ -48,14 +48,36 @@ import java.util.Map;
|
||||
public class DatabaseResultLoader extends AsyncLoader<List<SearchResult>> {
|
||||
private static final String LOG = "DatabaseResultLoader";
|
||||
private final String mQueryText;
|
||||
private final Context mContext;
|
||||
|
||||
protected final SQLiteDatabase mDatabase;
|
||||
|
||||
private final CursorToSearchResultConverter mConverter;
|
||||
|
||||
/* These indices are used to match the columns of the this loader's SELECT statement.
|
||||
These are not necessarily the same order or coverage as the schema defined in
|
||||
IndexDatabaseHelper */
|
||||
public static final int COLUMN_INDEX_RANK = 0;
|
||||
public static final int COLUMN_INDEX_TITLE = 1;
|
||||
public static final int COLUMN_INDEX_SUMMARY_ON = 2;
|
||||
public static final int COLUMN_INDEX_SUMMARY_OFF = 3;
|
||||
public static final int COLUMN_INDEX_ENTRIES = 4;
|
||||
public static final int COLUMN_INDEX_KEYWORDS = 5;
|
||||
public static final int COLUMN_INDEX_CLASS_NAME = 6;
|
||||
public static final int COLUMN_INDEX_SCREEN_TITLE = 7;
|
||||
public static final int COLUMN_INDEX_ICON = 8;
|
||||
public static final int COLUMN_INDEX_INTENT_ACTION = 9;
|
||||
public static final int COLUMN_INDEX_INTENT_ACTION_TARGET_PACKAGE = 10;
|
||||
public static final int COLUMN_INDEX_INTENT_ACTION_TARGET_CLASS = 11;
|
||||
public static final int COLUMN_INDEX_ENABLED = 12;
|
||||
public static final int COLUMN_INDEX_KEY = 13;
|
||||
public static final int COLUMN_INDEX_PAYLOAD_TYPE = 14;
|
||||
public static final int COLUMN_INDEX_PAYLOAD = 15;
|
||||
|
||||
public DatabaseResultLoader(Context context, String queryText) {
|
||||
super(context);
|
||||
mDatabase = IndexDatabaseHelper.getInstance(context).getReadableDatabase();
|
||||
mQueryText = queryText;
|
||||
mContext = context;
|
||||
mConverter = new CursorToSearchResultConverter(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -72,7 +94,7 @@ public class DatabaseResultLoader extends AsyncLoader<List<SearchResult>> {
|
||||
String query = getSQLQuery();
|
||||
Cursor result = mDatabase.rawQuery(query, null);
|
||||
|
||||
return parseCursorForSearch(result);
|
||||
return mConverter.convertCursor(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -86,100 +108,12 @@ public class DatabaseResultLoader extends AsyncLoader<List<SearchResult>> {
|
||||
"data_summary_off, data_entries, data_keywords, class_name, screen_title,"
|
||||
+ " icon, " +
|
||||
"intent_action, intent_target_package, intent_target_class, enabled, " +
|
||||
"data_key_reference FROM prefs_index WHERE prefs_index MATCH "
|
||||
"data_key_reference, payload_type, payload FROM prefs_index WHERE prefs_index MATCH "
|
||||
+ "'data_title:%s* " +
|
||||
"OR data_title_normalized:%s* OR data_keywords:%s*' AND locale = 'en_US'",
|
||||
mQueryText, mQueryText, mQueryText);
|
||||
}
|
||||
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
||||
public ArrayList<SearchResult> parseCursorForSearch(Cursor cursorResults) {
|
||||
if (cursorResults == null) {
|
||||
return null;
|
||||
}
|
||||
final Map<String, Context> contextMap = new HashMap<>();
|
||||
final ArrayList<SearchResult> results = new ArrayList<>();
|
||||
|
||||
while (cursorResults.moveToNext()) {
|
||||
SearchResult result = buildSingleSearchResultFromCursor(contextMap, cursorResults);
|
||||
if (result != null) {
|
||||
results.add(result);
|
||||
}
|
||||
}
|
||||
Collections.sort(results);
|
||||
return results;
|
||||
}
|
||||
|
||||
private SearchResult buildSingleSearchResultFromCursor(Map<String, Context> contextMap,
|
||||
Cursor cursor) {
|
||||
final String pkgName = cursor.getString(Index.COLUMN_INDEX_INTENT_ACTION_TARGET_PACKAGE);
|
||||
final String action = cursor.getString(Index.COLUMN_INDEX_INTENT_ACTION);
|
||||
final String title = cursor.getString(Index.COLUMN_INDEX_TITLE);
|
||||
final String summaryOn = cursor.getString(Index.COLUMN_INDEX_SUMMARY_ON);
|
||||
final String className = cursor.getString(Index.COLUMN_INDEX_CLASS_NAME);
|
||||
final int rank = cursor.getInt(Index.COLUMN_INDEX_RANK);
|
||||
final String key = cursor.getString(Index.COLUMN_INDEX_KEY);
|
||||
final String iconResStr = cursor.getString(Index.COLUMN_INDEX_ICON);
|
||||
|
||||
final ResultPayload payload;
|
||||
if (TextUtils.isEmpty(action)) {
|
||||
final String screenTitle = cursor.getString(Index.COLUMN_INDEX_SCREEN_TITLE);
|
||||
// Action is null, we will launch it as a sub-setting
|
||||
final Bundle args = new Bundle();
|
||||
args.putString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY, key);
|
||||
final Intent intent = Utils.onBuildStartFragmentIntent(mContext,
|
||||
className, args, null, 0, screenTitle, false);
|
||||
payload = new IntentPayload(intent);
|
||||
} else {
|
||||
final Intent intent = new Intent(action);
|
||||
final String targetClass = cursor.getString(
|
||||
Index.COLUMN_INDEX_INTENT_ACTION_TARGET_CLASS);
|
||||
if (!TextUtils.isEmpty(pkgName) && !TextUtils.isEmpty(targetClass)) {
|
||||
final ComponentName component = new ComponentName(pkgName, targetClass);
|
||||
intent.setComponent(component);
|
||||
}
|
||||
intent.putExtra(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY, key);
|
||||
payload = new IntentPayload(intent);
|
||||
}
|
||||
SearchResult.Builder builder = new SearchResult.Builder();
|
||||
builder.addTitle(title)
|
||||
.addSummary(summaryOn)
|
||||
.addRank(rank)
|
||||
.addIcon(getIconForPackage(contextMap, pkgName, className, iconResStr))
|
||||
.addPayload(payload);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
private Drawable getIconForPackage(Map<String, Context> contextMap, String pkgName,
|
||||
String className, String iconResStr) {
|
||||
final int iconId = TextUtils.isEmpty(iconResStr)
|
||||
? 0 : Integer.parseInt(iconResStr);
|
||||
Drawable icon;
|
||||
Context packageContext;
|
||||
if (iconId == 0) {
|
||||
icon = null;
|
||||
} else {
|
||||
if (TextUtils.isEmpty(className) && !TextUtils.isEmpty(pkgName)) {
|
||||
packageContext = contextMap.get(pkgName);
|
||||
if (packageContext == null) {
|
||||
try {
|
||||
packageContext = mContext.createPackageContext(pkgName, 0);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
Log.e(LOG, "Cannot create Context for package: " + pkgName);
|
||||
return null;
|
||||
}
|
||||
contextMap.put(pkgName, packageContext);
|
||||
}
|
||||
} else {
|
||||
packageContext = mContext;
|
||||
}
|
||||
try {
|
||||
icon = packageContext.getDrawable(iconId);
|
||||
} catch (Resources.NotFoundException nfe) {
|
||||
icon = null;
|
||||
}
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user