Remove search v1

Fixes: 69851037
Test: robotests
Change-Id: I53bc6408116031619053066055cb26cac67b9945
This commit is contained in:
Fan Zhang
2018-01-16 09:55:11 -08:00
parent d6b0490dea
commit 85cd77c9f2
44 changed files with 8 additions and 5697 deletions

View File

@@ -20,7 +20,6 @@ package com.android.settings.core;
* This class keeps track of all feature flags in Settings.
*/
public class FeatureFlags {
public static final String SEARCH_V2 = "settings_search_v2";
public static final String APP_INFO_V2 = "settings_app_info_v2";
public static final String CONNECTED_DEVICE_V2 = "settings_connected_device_v2";
public static final String BATTERY_SETTINGS_V2 = "settings_battery_v2";

View File

@@ -1,141 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import static com.android.settings.search.InstalledAppResultLoader.getWordDifference;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.support.annotation.VisibleForTesting;
import android.support.v4.content.ContextCompat;
import android.util.IconDrawableFactory;
import android.util.Log;
import android.view.accessibility.AccessibilityManager;
import com.android.settings.R;
import com.android.settings.accessibility.AccessibilitySettings;
import com.android.settings.dashboard.SiteMapManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class AccessibilityServiceResultLoader extends
FutureTask<List<? extends SearchResult>> {
private static final String TAG = "A11yResultFutureTask";
public AccessibilityServiceResultLoader(Context context, String query,
SiteMapManager manager) {
super(new AccessibilityServiceResultCallable(context, query, manager));
}
static class AccessibilityServiceResultCallable implements
Callable<List<? extends SearchResult>> {
private static final int NAME_NO_MATCH = -1;
private final Context mContext;
private List<String> mBreadcrumb;
private SiteMapManager mSiteMapManager;
@VisibleForTesting
final String mQuery;
private final AccessibilityManager mAccessibilityManager;
private final PackageManager mPackageManager;
private final int mUserId;
public AccessibilityServiceResultCallable(Context context, String query,
SiteMapManager mapManager) {
mUserId = UserHandle.myUserId();
mContext = context;
mSiteMapManager = mapManager;
mPackageManager = context.getPackageManager();
mAccessibilityManager =
(AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
mQuery = query;
}
@Override
public List<? extends SearchResult> call() throws Exception {
long startTime = System.currentTimeMillis();
final List<SearchResult> results = new ArrayList<>();
final List<AccessibilityServiceInfo> services = mAccessibilityManager
.getInstalledAccessibilityServiceList();
final IconDrawableFactory iconFactory = IconDrawableFactory.newInstance(mContext);
final String screenTitle = mContext.getString(R.string.accessibility_settings);
for (AccessibilityServiceInfo service : services) {
if (service == null) {
continue;
}
final ResolveInfo resolveInfo = service.getResolveInfo();
if (service.getResolveInfo() == null) {
continue;
}
final ServiceInfo serviceInfo = resolveInfo.serviceInfo;
final CharSequence title = resolveInfo.loadLabel(mPackageManager);
final int wordDiff = getWordDifference(title.toString(), mQuery);
if (wordDiff == NAME_NO_MATCH) {
continue;
}
final Drawable icon;
if (resolveInfo.getIconResource() == 0) {
icon = ContextCompat.getDrawable(mContext, R.mipmap.ic_accessibility_generic);
} else {
icon = iconFactory.getBadgedIcon(
resolveInfo.serviceInfo,
resolveInfo.serviceInfo.applicationInfo,
mUserId);
}
final String componentName = new ComponentName(serviceInfo.packageName,
serviceInfo.name).flattenToString();
final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext,
AccessibilitySettings.class.getName(), componentName, screenTitle);
results.add(new SearchResult.Builder()
.setTitle(title)
.addBreadcrumbs(getBreadCrumb())
.setPayload(new ResultPayload(intent))
.setRank(wordDiff)
.setIcon(icon)
.setStableId(Objects.hash(screenTitle, componentName))
.build());
}
Collections.sort(results);
Log.i(TAG, "A11y search loading took:" + (System.currentTimeMillis() - startTime));
return results;
}
private List<String> getBreadCrumb() {
if (mBreadcrumb == null || mBreadcrumb.isEmpty()) {
mBreadcrumb = mSiteMapManager.buildBreadCrumb(
mContext, AccessibilitySettings.class.getName(),
mContext.getString(R.string.accessibility_settings));
}
return mBreadcrumb;
}
}
}

View File

@@ -1,50 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.content.pm.ApplicationInfo;
import android.os.UserHandle;
public class AppSearchResult extends SearchResult {
/**
* Installed app's ApplicationInfo for delayed loading of icons
*/
public final ApplicationInfo info;
public AppSearchResult(Builder builder) {
super(builder);
info = builder.mInfo;
}
public UserHandle getAppUserHandle() {
return new UserHandle(UserHandle.getUserId(info.uid));
}
public static class Builder extends SearchResult.Builder {
protected ApplicationInfo mInfo;
public SearchResult.Builder setAppInfo(ApplicationInfo info) {
mInfo = info;
return this;
}
public AppSearchResult build() {
return new AppSearchResult(this);
}
}
}

View File

@@ -18,35 +18,11 @@
package com.android.settings.search;
import static com.android.settings.search.IndexDatabaseHelper.IndexColumns;
import static com.android.settings.search.IndexDatabaseHelper.Tables.TABLE_PREFS_INDEX;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.VisibleForTesting;
import android.util.Log;
import android.util.Pair;
import com.android.settings.dashboard.SiteMapManager;
import com.android.settings.overlay.FeatureFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* AsyncTask to retrieve Settings, first party app and any intent based results.
*/
public class DatabaseResultLoader extends FutureTask<List<? extends SearchResult>> {
public class DatabaseResultLoader {
private static final String TAG = "DatabaseResultLoader";
@@ -66,18 +42,6 @@ public class DatabaseResultLoader extends FutureTask<List<? extends SearchResult
IndexColumns.PAYLOAD
};
public static final String[] MATCH_COLUMNS_PRIMARY = {
IndexColumns.DATA_TITLE,
IndexColumns.DATA_TITLE_NORMALIZED,
};
public static final String[] MATCH_COLUMNS_SECONDARY = {
IndexColumns.DATA_SUMMARY_ON,
IndexColumns.DATA_SUMMARY_ON_NORMALIZED,
IndexColumns.DATA_SUMMARY_OFF,
IndexColumns.DATA_SUMMARY_OFF_NORMALIZED,
};
/**
* Base ranks defines the best possible rank based on what the query matches.
* If the query matches the prefix of the first word in the title, the best rank it can be
@@ -89,256 +53,4 @@ public class DatabaseResultLoader extends FutureTask<List<? extends SearchResult
*/
public static final int[] BASE_RANKS = {1, 3, 7, 9};
public DatabaseResultLoader(Context context, String query, SiteMapManager manager) {
super(new StaticSearchResultCallable(context, query, manager));
}
static class StaticSearchResultCallable implements
Callable<List<? extends SearchResult>> {
public final String[] MATCH_COLUMNS_TERTIARY = {
IndexColumns.DATA_KEYWORDS,
IndexColumns.DATA_ENTRIES
};
@VisibleForTesting
final String mQueryText;
private final Context mContext;
private final CursorToSearchResultConverter mConverter;
private final SiteMapManager mSiteMapManager;
private final SearchFeatureProvider mFeatureProvider;
public StaticSearchResultCallable(Context context, String queryText,
SiteMapManager mapManager) {
mContext = context;
mSiteMapManager = mapManager;
mQueryText = queryText;
mConverter = new CursorToSearchResultConverter(context);
mFeatureProvider = FeatureFactory.getFactory(context).getSearchFeatureProvider();
}
@Override
public List<? extends SearchResult> call() {
if (mQueryText == null || mQueryText.isEmpty()) {
return new ArrayList<>();
}
// TODO (b/68656233) Consolidate timing metrics
long startTime = System.currentTimeMillis();
// Start a Future to get search result scores.
FutureTask<List<Pair<String, Float>>> rankerTask = mFeatureProvider.getRankerTask(
mContext, mQueryText);
if (rankerTask != null) {
ExecutorService executorService = mFeatureProvider.getExecutorService();
executorService.execute(rankerTask);
}
final Set<SearchResult> resultSet = new HashSet<>();
resultSet.addAll(firstWordQuery(MATCH_COLUMNS_PRIMARY, BASE_RANKS[0]));
resultSet.addAll(secondaryWordQuery(MATCH_COLUMNS_PRIMARY, BASE_RANKS[1]));
resultSet.addAll(anyWordQuery(MATCH_COLUMNS_SECONDARY, BASE_RANKS[2]));
resultSet.addAll(anyWordQuery(MATCH_COLUMNS_TERTIARY, BASE_RANKS[3]));
// Try to retrieve the scores in time. Otherwise use static ranking.
if (rankerTask != null) {
try {
final long timeoutMs = mFeatureProvider.smartSearchRankingTimeoutMs(mContext);
List<Pair<String, Float>> searchRankScores = rankerTask.get(timeoutMs,
TimeUnit.MILLISECONDS);
return getDynamicRankedResults(resultSet, searchRankScores);
} catch (TimeoutException | InterruptedException | ExecutionException e) {
Log.d(TAG, "Error waiting for result scores: " + e);
}
}
List<SearchResult> resultList = new ArrayList<>(resultSet);
Collections.sort(resultList);
Log.i(TAG, "Static search loading took:" + (System.currentTimeMillis() - startTime));
return resultList;
}
// TODO (b/33577327) Retrieve all search results with a single query.
/**
* Creates and executes the query which matches prefixes of the first word of the given
* columns.
*
* @param matchColumns The columns to match on
* @param baseRank The highest rank achievable by these results
* @return A set of the matching results.
*/
private Set<SearchResult> firstWordQuery(String[] matchColumns, int baseRank) {
final String whereClause = buildSingleWordWhereClause(matchColumns);
final String query = mQueryText + "%";
final String[] selection = buildSingleWordSelection(query, matchColumns.length);
return query(whereClause, selection, baseRank);
}
/**
* Creates and executes the query which matches prefixes of the non-first words of the
* given columns.
*
* @param matchColumns The columns to match on
* @param baseRank The highest rank achievable by these results
* @return A set of the matching results.
*/
private Set<SearchResult> secondaryWordQuery(String[] matchColumns, int baseRank) {
final String whereClause = buildSingleWordWhereClause(matchColumns);
final String query = "% " + mQueryText + "%";
final String[] selection = buildSingleWordSelection(query, matchColumns.length);
return query(whereClause, selection, baseRank);
}
/**
* Creates and executes the query which matches prefixes of the any word of the given
* columns.
*
* @param matchColumns The columns to match on
* @param baseRank The highest rank achievable by these results
* @return A set of the matching results.
*/
private Set<SearchResult> anyWordQuery(String[] matchColumns, int baseRank) {
final String whereClause = buildTwoWordWhereClause(matchColumns);
final String[] selection = buildAnyWordSelection(matchColumns.length * 2);
return query(whereClause, selection, baseRank);
}
/**
* Generic method used by all of the query methods above to execute a query.
*
* @param whereClause Where clause for the SQL query which uses bindings.
* @param selection List of the transformed query to match each bind in the whereClause
* @param baseRank The highest rank achievable by these results.
* @return A set of the matching results.
*/
private Set<SearchResult> query(String whereClause, String[] selection, int baseRank) {
final SQLiteDatabase database =
IndexDatabaseHelper.getInstance(mContext).getReadableDatabase();
try (Cursor resultCursor = database.query(TABLE_PREFS_INDEX, SELECT_COLUMNS,
whereClause,
selection, null, null, null)) {
return mConverter.convertCursor(mSiteMapManager, resultCursor, baseRank);
}
}
/**
* Builds the SQLite WHERE clause that matches all matchColumns for a single query.
*
* @param matchColumns List of columns that will be used for matching.
* @return The constructed WHERE clause.
*/
private static String buildSingleWordWhereClause(String[] matchColumns) {
StringBuilder sb = new StringBuilder(" (");
final int count = matchColumns.length;
for (int n = 0; n < count; n++) {
sb.append(matchColumns[n]);
sb.append(" like ? ");
if (n < count - 1) {
sb.append(" OR ");
}
}
sb.append(") AND enabled = 1");
return sb.toString();
}
/**
* Builds the SQLite WHERE clause that matches all matchColumns to two different queries.
*
* @param matchColumns List of columns that will be used for matching.
* @return The constructed WHERE clause.
*/
private static String buildTwoWordWhereClause(String[] matchColumns) {
StringBuilder sb = new StringBuilder(" (");
final int count = matchColumns.length;
for (int n = 0; n < count; n++) {
sb.append(matchColumns[n]);
sb.append(" like ? OR ");
sb.append(matchColumns[n]);
sb.append(" like ?");
if (n < count - 1) {
sb.append(" OR ");
}
}
sb.append(") AND enabled = 1");
return sb.toString();
}
/**
* Fills out the selection array to match the query as the prefix of a single word.
*
* @param size is the number of columns to be matched.
*/
private String[] buildSingleWordSelection(String query, int size) {
String[] selection = new String[size];
for (int i = 0; i < size; i++) {
selection[i] = query;
}
return selection;
}
/**
* Fills out the selection array to match the query as the prefix of a word.
*
* @param size is twice the number of columns to be matched. The first match is for the
* prefix
* of the first word in the column. The second match is for any subsequent word
* prefix match.
*/
private String[] buildAnyWordSelection(int size) {
String[] selection = new String[size];
final String query = mQueryText + "%";
final String subStringQuery = "% " + mQueryText + "%";
for (int i = 0; i < (size - 1); i += 2) {
selection[i] = query;
selection[i + 1] = subStringQuery;
}
return selection;
}
private List<SearchResult> getDynamicRankedResults(Set<SearchResult> unsortedSet,
List<Pair<String, Float>> searchRankScores) {
TreeSet<SearchResult> dbResultsSortedByScores = new TreeSet<>(
(o1, o2) -> {
float score1 = getRankingScoreByStableId(searchRankScores, o1.stableId);
float score2 = getRankingScoreByStableId(searchRankScores, o2.stableId);
if (score1 > score2) {
return -1;
} else if (score1 == score2) {
return 0;
} else {
return 1;
}
});
dbResultsSortedByScores.addAll(unsortedSet);
return new ArrayList<>(dbResultsSortedByScores);
}
/**
* Looks up ranking score for stableId
*
* @param stableId String of stableId
* @return the ranking score corresponding to the given stableId. If there is no score
* available for this stableId, -Float.MAX_VALUE is returned.
*/
@VisibleForTesting
Float getRankingScoreByStableId(List<Pair<String, Float>> searchRankScores, int stableId) {
for (Pair<String, Float> rankingScore : searchRankScores) {
if (Integer.toString(stableId).compareTo(rankingScore.first) == 0) {
return rankingScore.second;
}
}
// If stableId not found in the list, we assign the minimum score so it will appear at
// the end of the list.
Log.w(TAG, "stableId " + stableId + " was not in the ranking scores.");
return -Float.MAX_VALUE;
}
}
}

View File

@@ -1,65 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.content.Context;
import android.util.Pair;
import android.view.View;
import android.widget.Switch;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
/**
* ViewHolder for Settings represented as SwitchPreferences.
*/
public class InlineSwitchViewHolder extends SearchViewHolder {
public final Switch switchView;
private final Context mContext;
public InlineSwitchViewHolder(View view, Context context) {
super(view);
mContext = context;
switchView = view.findViewById(R.id.switchView);
}
@Override
public int getClickActionMetricName() {
return MetricsEvent.ACTION_CLICK_SETTINGS_SEARCH_INLINE_RESULT;
}
@Override
public void onBind(SearchFragment fragment, final SearchResult result) {
super.onBind(fragment, result);
if (mContext == null) {
return;
}
final InlineSwitchPayload payload = (InlineSwitchPayload) result.payload;
switchView.setChecked(payload.getValue(mContext) == InlineSwitchPayload.TRUE);
switchView.setOnCheckedChangeListener((buttonView, isChecked) -> {
final Pair<Integer, Object> value = Pair.create(
MetricsEvent.FIELD_SETTINGS_SEARCH_INLINE_RESULT_VALUE, isChecked
? 1L : 0L);
fragment.onSearchResultClicked(this, result, value);
int newValue = isChecked ? InlineSwitchPayload.TRUE : InlineSwitchPayload.FALSE;
payload.setValue(mContext, newValue);
});
}
}

View File

@@ -1,211 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import static android.content.Context.INPUT_METHOD_SERVICE;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.hardware.input.InputManager;
import android.hardware.input.KeyboardLayout;
import android.support.annotation.VisibleForTesting;
import android.util.Log;
import android.view.InputDevice;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import com.android.settings.R;
import com.android.settings.dashboard.SiteMapManager;
import com.android.settings.inputmethod.AvailableVirtualKeyboardFragment;
import com.android.settings.inputmethod.PhysicalKeyboardFragment;
import com.android.settingslib.inputmethod.InputMethodAndSubtypeUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
/**
* Search result for input devices (physical/virtual keyboard, game controllers, etc)
*/
public class InputDeviceResultLoader extends FutureTask<List<? extends SearchResult>> {
private static final String TAG = "InputResultFutureTask";
@VisibleForTesting
static final String PHYSICAL_KEYBOARD_FRAGMENT = PhysicalKeyboardFragment.class.getName();
@VisibleForTesting
static final String VIRTUAL_KEYBOARD_FRAGMENT =
AvailableVirtualKeyboardFragment.class.getName();
public InputDeviceResultLoader(Context context, String query, SiteMapManager manager) {
super(new InputDeviceResultCallable(context, query, manager));
}
static class InputDeviceResultCallable implements
Callable<List<? extends SearchResult>> {
private static final int NAME_NO_MATCH = -1;
private final Context mContext;
private final SiteMapManager mSiteMapManager;
private final InputManager mInputManager;
private final InputMethodManager mImm;
private final PackageManager mPackageManager;
@VisibleForTesting
final String mQuery;
private List<String> mPhysicalKeyboardBreadcrumb;
private List<String> mVirtualKeyboardBreadcrumb;
public InputDeviceResultCallable(Context context, String query, SiteMapManager mapManager) {
mContext = context;
mQuery = query;
mSiteMapManager = mapManager;
mInputManager = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
mImm = (InputMethodManager) context.getSystemService(INPUT_METHOD_SERVICE);
mPackageManager = context.getPackageManager();
}
@Override
public List<? extends SearchResult> call() {
long startTime = System.currentTimeMillis();
final List<SearchResult> results = new ArrayList<>();
results.addAll(buildPhysicalKeyboardSearchResults());
results.addAll(buildVirtualKeyboardSearchResults());
Collections.sort(results);
Log.i(TAG, "Input search loading took:" + (System.currentTimeMillis() - startTime));
return results;
}
private Set<SearchResult> buildPhysicalKeyboardSearchResults() {
final Set<SearchResult> results = new HashSet<>();
final String screenTitle = mContext.getString(R.string.physical_keyboard_title);
for (final InputDevice device : getPhysicalFullKeyboards()) {
final String deviceName = device.getName();
final int wordDiff = InstalledAppResultLoader.getWordDifference(deviceName,
mQuery);
if (wordDiff == NAME_NO_MATCH) {
continue;
}
final String keyboardLayoutDescriptor = mInputManager
.getCurrentKeyboardLayoutForInputDevice(device.getIdentifier());
final KeyboardLayout keyboardLayout = (keyboardLayoutDescriptor != null)
? mInputManager.getKeyboardLayout(keyboardLayoutDescriptor) : null;
final String summary = (keyboardLayout != null)
? keyboardLayout.toString()
: mContext.getString(R.string.keyboard_layout_default_label);
final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext,
PHYSICAL_KEYBOARD_FRAGMENT, deviceName, screenTitle);
results.add(new SearchResult.Builder()
.setTitle(deviceName)
.setPayload(new ResultPayload(intent))
.setStableId(Objects.hash(PHYSICAL_KEYBOARD_FRAGMENT, deviceName))
.setSummary(summary)
.setRank(wordDiff)
.addBreadcrumbs(getPhysicalKeyboardBreadCrumb())
.build());
}
return results;
}
private Set<SearchResult> buildVirtualKeyboardSearchResults() {
final Set<SearchResult> results = new HashSet<>();
final String screenTitle = mContext.getString(R.string.add_virtual_keyboard);
final List<InputMethodInfo> inputMethods = mImm.getInputMethodList();
for (InputMethodInfo info : inputMethods) {
final String title = info.loadLabel(mPackageManager).toString();
final String summary = InputMethodAndSubtypeUtil
.getSubtypeLocaleNameListAsSentence(getAllSubtypesOf(info), mContext, info);
int wordDiff = InstalledAppResultLoader.getWordDifference(title, mQuery);
if (wordDiff == NAME_NO_MATCH) {
wordDiff = InstalledAppResultLoader.getWordDifference(summary, mQuery);
}
if (wordDiff == NAME_NO_MATCH) {
continue;
}
final ServiceInfo serviceInfo = info.getServiceInfo();
final String key = new ComponentName(serviceInfo.packageName, serviceInfo.name)
.flattenToString();
final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext,
VIRTUAL_KEYBOARD_FRAGMENT, key, screenTitle);
results.add(new SearchResult.Builder()
.setTitle(title)
.setSummary(summary)
.setRank(wordDiff)
.setStableId(Objects.hash(VIRTUAL_KEYBOARD_FRAGMENT, key))
.addBreadcrumbs(getVirtualKeyboardBreadCrumb())
.setPayload(new ResultPayload(intent))
.build());
}
return results;
}
private List<String> getPhysicalKeyboardBreadCrumb() {
if (mPhysicalKeyboardBreadcrumb == null || mPhysicalKeyboardBreadcrumb.isEmpty()) {
mPhysicalKeyboardBreadcrumb = mSiteMapManager.buildBreadCrumb(
mContext, PHYSICAL_KEYBOARD_FRAGMENT,
mContext.getString(R.string.physical_keyboard_title));
}
return mPhysicalKeyboardBreadcrumb;
}
private List<String> getVirtualKeyboardBreadCrumb() {
if (mVirtualKeyboardBreadcrumb == null || mVirtualKeyboardBreadcrumb.isEmpty()) {
final Context context = mContext;
mVirtualKeyboardBreadcrumb = mSiteMapManager.buildBreadCrumb(
context, VIRTUAL_KEYBOARD_FRAGMENT,
context.getString(R.string.add_virtual_keyboard));
}
return mVirtualKeyboardBreadcrumb;
}
private List<InputDevice> getPhysicalFullKeyboards() {
final List<InputDevice> keyboards = new ArrayList<>();
final int[] deviceIds = InputDevice.getDeviceIds();
if (deviceIds != null) {
for (int deviceId : deviceIds) {
final InputDevice device = InputDevice.getDevice(deviceId);
if (device != null && !device.isVirtual() && device.isFullKeyboard()) {
keyboards.add(device);
}
}
}
return keyboards;
}
private static List<InputMethodSubtype> getAllSubtypesOf(final InputMethodInfo imi) {
final int subtypeCount = imi.getSubtypeCount();
final List<InputMethodSubtype> allSubtypes = new ArrayList<>(subtypeCount);
for (int index = 0; index < subtypeCount; index++) {
allSubtypes.add(imi.getSubtypeAt(index));
}
return allSubtypes;
}
}
}

View File

@@ -1,260 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.UserInfo;
import android.net.Uri;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
import android.util.Log;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.applications.manageapplications.ManageApplications;
import com.android.settings.dashboard.SiteMapManager;
import com.android.settingslib.wrapper.PackageManagerWrapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
/**
* Search loader for installed apps.
*/
public class InstalledAppResultLoader extends FutureTask<List<? extends SearchResult>> {
private static final String TAG = "InstalledAppFutureTask";
private static final int NAME_NO_MATCH = -1;
private static final Intent LAUNCHER_PROBE = new Intent(Intent.ACTION_MAIN)
.addCategory(Intent.CATEGORY_LAUNCHER);
public InstalledAppResultLoader(Context context, PackageManagerWrapper wrapper,
String query, SiteMapManager manager) {
super(new InstalledAppResultCallable(context, wrapper, query, manager));
}
/**
* Returns "difference" between appName and query string. appName must contain all
* characters from query as a prefix to a word, in the same order.
* If not, returns NAME_NO_MATCH.
* If they do match, returns an int value representing how different they are,
* and larger values means they are less similar.
* <p/>
* Example:
* appName: Abcde, query: Abcde, Returns 0
* appName: Abcde, query: abc, Returns 2
* appName: Abcde, query: ab, Returns 3
* appName: Abcde, query: bc, Returns NAME_NO_MATCH
* appName: Abcde, query: xyz, Returns NAME_NO_MATCH
* appName: Abc de, query: de, Returns 4
* TODO: Move this to a common util class.
*/
static int getWordDifference(String appName, String query) {
if (TextUtils.isEmpty(appName) || TextUtils.isEmpty(query)) {
return NAME_NO_MATCH;
}
final char[] queryTokens = query.toLowerCase().toCharArray();
final char[] appTokens = appName.toLowerCase().toCharArray();
final int appLength = appTokens.length;
if (queryTokens.length > appLength) {
return NAME_NO_MATCH;
}
int i = 0;
int j;
while (i < appLength) {
j = 0;
// Currently matching a prefix
while ((i + j < appLength) && (queryTokens[j] == appTokens[i + j])) {
// Matched the entire query
if (++j >= queryTokens.length) {
// Use the diff in length as a proxy of how close the 2 words match.
// Value range from 0 to infinity.
return appLength - queryTokens.length;
}
}
i += j;
// Remaining string is longer that the query or we have search the whole app name.
if (queryTokens.length > appLength - i) {
return NAME_NO_MATCH;
}
// This is the first index where app name and query name are different
// Find the next space in the app name or the end of the app name.
while ((i < appLength) && (!Character.isWhitespace(appTokens[i++]))) ;
// Find the start of the next word
while ((i < appLength) && !(Character.isLetter(appTokens[i])
|| Character.isDigit(appTokens[i]))) {
// Increment in body because we cannot guarantee which condition was true
i++;
}
}
return NAME_NO_MATCH;
}
static class InstalledAppResultCallable implements
Callable<List<? extends SearchResult>> {
private final Context mContext;
private List<String> mBreadcrumb;
private SiteMapManager mSiteMapManager;
@VisibleForTesting
final String mQuery;
private final UserManager mUserManager;
private final PackageManagerWrapper mPackageManager;
private final List<ResolveInfo> mHomeActivities = new ArrayList<>();
public InstalledAppResultCallable(Context context, PackageManagerWrapper pmWrapper,
String query, SiteMapManager mapManager) {
mContext = context;
mSiteMapManager = mapManager;
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
mPackageManager = pmWrapper;
mQuery = query;
}
@Override
public List<? extends SearchResult> call() throws Exception {
long startTime = System.currentTimeMillis();
final List<AppSearchResult> results = new ArrayList<>();
final PackageManager pm = mPackageManager.getPackageManager();
mHomeActivities.clear();
mPackageManager.getHomeActivities(mHomeActivities);
for (UserInfo user : getUsersToCount()) {
final List<ApplicationInfo> apps =
mPackageManager.getInstalledApplicationsAsUser(
PackageManager.MATCH_DISABLED_COMPONENTS
| PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
| (user.isAdmin() ? PackageManager.MATCH_ANY_USER : 0),
user.id);
for (ApplicationInfo info : apps) {
if (!shouldIncludeAsCandidate(info, user)) {
continue;
}
final CharSequence label = info.loadLabel(pm);
final int wordDiff = getWordDifference(label.toString(), mQuery);
if (wordDiff == NAME_NO_MATCH) {
continue;
}
final Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
.setData(Uri.fromParts("package", info.packageName, null))
.putExtra(SettingsActivity.EXTRA_SOURCE_METRICS_CATEGORY,
MetricsProto.MetricsEvent.DASHBOARD_SEARCH_RESULTS);
final AppSearchResult.Builder builder = new AppSearchResult.Builder();
builder.setAppInfo(info)
.setStableId(Objects.hash(info.packageName, user.id))
.setTitle(info.loadLabel(pm))
.setRank(getRank(wordDiff))
.addBreadcrumbs(getBreadCrumb())
.setPayload(new ResultPayload(intent));
results.add(builder.build());
}
}
Collections.sort(results);
Log.i(TAG, "App search loading took:" + (System.currentTimeMillis() - startTime));
return results;
}
/**
* Returns true if the candidate should be included in candidate list
* <p/>
* This method matches logic in {@code ApplicationState#FILTER_DOWNLOADED_AND_LAUNCHER}.
*/
private boolean shouldIncludeAsCandidate(ApplicationInfo info, UserInfo user) {
// Not system app
if ((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0
|| (info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
return true;
}
// Shows up in launcher
final Intent launchIntent = new Intent(LAUNCHER_PROBE)
.setPackage(info.packageName);
final List<ResolveInfo> intents = mPackageManager.queryIntentActivitiesAsUser(
launchIntent,
PackageManager.MATCH_DISABLED_COMPONENTS
| PackageManager.MATCH_DIRECT_BOOT_AWARE
| PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
user.id);
if (intents != null && intents.size() != 0) {
return true;
}
// Is launcher app itself
return isPackageInList(mHomeActivities, info.packageName);
}
private List<UserInfo> getUsersToCount() {
return mUserManager.getProfiles(UserHandle.myUserId());
}
private boolean isPackageInList(List<ResolveInfo> resolveInfos, String pkg) {
for (ResolveInfo info : resolveInfos) {
if (TextUtils.equals(info.activityInfo.packageName, pkg)) {
return true;
}
}
return false;
}
private List<String> getBreadCrumb() {
if (mBreadcrumb == null || mBreadcrumb.isEmpty()) {
mBreadcrumb = mSiteMapManager.buildBreadCrumb(
mContext, ManageApplications.class.getName(),
mContext.getString(R.string.applications_settings));
}
return mBreadcrumb;
}
/**
* A temporary ranking scheme for installed apps.
*
* @param wordDiff difference between query length and app name length.
* @return the ranking.
*/
private int getRank(int wordDiff) {
if (wordDiff < 6) {
return 2;
}
return 3;
}
}
}

View File

@@ -1,74 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.UserHandle;
import android.support.annotation.VisibleForTesting;
import android.util.Log;
import android.view.View;
import com.android.internal.logging.nano.MetricsProto;
import java.util.List;
/**
* ViewHolder for intent based search results.
* The DatabaseResultLoader is the primary use case for this ViewHolder.
*/
public class IntentSearchViewHolder extends SearchViewHolder {
private static final String TAG = "IntentSearchViewHolder";
@VisibleForTesting
static final int REQUEST_CODE_NO_OP = 0;
public IntentSearchViewHolder(View view) {
super(view);
}
@Override
public int getClickActionMetricName() {
return MetricsProto.MetricsEvent.ACTION_CLICK_SETTINGS_SEARCH_RESULT;
}
@Override
public void onBind(final SearchFragment fragment, final SearchResult result) {
super.onBind(fragment, result);
itemView.setOnClickListener(v -> {
fragment.onSearchResultClicked(this, result);
final Intent intent = result.payload.getIntent();
// Use app user id to support work profile use case.
if (result instanceof AppSearchResult) {
AppSearchResult appResult = (AppSearchResult) result;
UserHandle userHandle = appResult.getAppUserHandle();
fragment.getActivity().startActivityAsUser(intent, userHandle);
} else {
final PackageManager pm = fragment.getActivity().getPackageManager();
final List<ResolveInfo> info = pm.queryIntentActivities(intent, 0 /* flags */);
if (info != null && !info.isEmpty()) {
fragment.startActivityForResult(intent, REQUEST_CODE_NO_OP);
} else {
Log.e(TAG, "Cannot launch search result, title: "
+ result.title + ", " + intent);
}
}
});
}
}

View File

@@ -1,127 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.app.LoaderManager;
import android.content.Context;
import android.content.Loader;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.android.settings.R;
import com.android.settings.overlay.FeatureFactory;
import java.util.List;
public class SavedQueryController implements LoaderManager.LoaderCallbacks,
MenuItem.OnMenuItemClickListener {
// TODO: make a generic background task manager to handle one-off tasks like this one.
private static final String ARG_QUERY = "remove_query";
private static final String TAG = "SearchSavedQueryCtrl";
private static final int MENU_SEARCH_HISTORY = 1000;
private final Context mContext;
private final LoaderManager mLoaderManager;
private final SearchFeatureProvider mSearchFeatureProvider;
private final SearchResultsAdapter mResultAdapter;
public SavedQueryController(Context context, LoaderManager loaderManager,
SearchResultsAdapter resultsAdapter) {
mContext = context;
mLoaderManager = loaderManager;
mResultAdapter = resultsAdapter;
mSearchFeatureProvider = FeatureFactory.getFactory(context)
.getSearchFeatureProvider();
}
@Override
public Loader onCreateLoader(int id, Bundle args) {
switch (id) {
case SearchFragment.SearchLoaderId.SAVE_QUERY_TASK:
return new SavedQueryRecorder(mContext, args.getString(ARG_QUERY));
case SearchFragment.SearchLoaderId.REMOVE_QUERY_TASK:
return new SavedQueryRemover(mContext);
case SearchFragment.SearchLoaderId.SAVED_QUERIES:
return mSearchFeatureProvider.getSavedQueryLoader(mContext);
}
return null;
}
@Override
public void onLoadFinished(Loader loader, Object data) {
switch (loader.getId()) {
case SearchFragment.SearchLoaderId.REMOVE_QUERY_TASK:
mLoaderManager.restartLoader(SearchFragment.SearchLoaderId.SAVED_QUERIES,
null /* args */, this /* callback */);
break;
case SearchFragment.SearchLoaderId.SAVED_QUERIES:
if (SettingsSearchIndexablesProvider.DEBUG) {
Log.d(TAG, "Saved queries loaded");
}
mResultAdapter.displaySavedQuery((List<SearchResult>) data);
break;
}
}
@Override
public void onLoaderReset(Loader loader) {
}
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() != MENU_SEARCH_HISTORY) {
return false;
}
removeQueries();
return true;
}
public void buildMenuItem(Menu menu) {
final MenuItem item =
menu.add(Menu.NONE, MENU_SEARCH_HISTORY, Menu.NONE, R.string.search_clear_history);
item.setOnMenuItemClickListener(this);
}
public void saveQuery(String query) {
final Bundle args = new Bundle();
args.putString(ARG_QUERY, query);
mLoaderManager.restartLoader(SearchFragment.SearchLoaderId.SAVE_QUERY_TASK, args,
this /* callback */);
}
/**
* Remove all saved queries from DB
*/
public void removeQueries() {
final Bundle args = new Bundle();
mLoaderManager.restartLoader(SearchFragment.SearchLoaderId.REMOVE_QUERY_TASK, args,
this /* callback */);
}
public void loadSavedQueries() {
if (SettingsSearchIndexablesProvider.DEBUG) {
Log.d(TAG, "loading saved queries");
}
mLoaderManager.restartLoader(SearchFragment.SearchLoaderId.SAVED_QUERIES, null /* args */,
this /* callback */);
}
}

View File

@@ -1,80 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.VisibleForTesting;
import com.android.settings.search.IndexDatabaseHelper.SavedQueriesColumns;
import com.android.settingslib.utils.AsyncLoader;
import java.util.ArrayList;
import java.util.List;
/**
* Loader for recently searched queries.
*/
public class SavedQueryLoader extends AsyncLoader<List<? extends SearchResult>> {
// Max number of proposed suggestions
@VisibleForTesting
static final int MAX_PROPOSED_SUGGESTIONS = 5;
private final SQLiteDatabase mDatabase;
public SavedQueryLoader(Context context) {
super(context);
mDatabase = IndexDatabaseHelper.getInstance(context).getReadableDatabase();
}
@Override
protected void onDiscardResult(List<? extends SearchResult> result) {
}
@Override
public List<? extends SearchResult> loadInBackground() {
try (final Cursor cursor = mDatabase.query(
IndexDatabaseHelper.Tables.TABLE_SAVED_QUERIES /* table */,
new String[]{SavedQueriesColumns.QUERY} /* columns */,
null /* selection */,
null /* selectionArgs */,
null /* groupBy */,
null /* having */,
"rowId DESC" /* orderBy */,
String.valueOf(MAX_PROPOSED_SUGGESTIONS) /* limit */)) {
return convertCursorToResult(cursor);
}
}
private List<SearchResult> convertCursorToResult(Cursor cursor) {
final List<SearchResult> results = new ArrayList<>();
while (cursor.moveToNext()) {
final SavedQueryPayload payload = new SavedQueryPayload(
cursor.getString(cursor.getColumnIndex(SavedQueriesColumns.QUERY)));
results.add(new SearchResult.Builder()
.setStableId(payload.hashCode())
.setTitle(payload.query)
.setPayload(payload)
.build());
}
return results;
}
}

View File

@@ -1,67 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.os.Parcel;
import android.support.annotation.VisibleForTesting;
/**
* {@link ResultPayload} for saved query.
*/
public class SavedQueryPayload extends ResultPayload {
public final String query;
public SavedQueryPayload(String query) {
super(null /* Intent */);
this.query = query;
}
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
SavedQueryPayload(Parcel in) {
super(null /* Intent */);
query = in.readString();
}
@Override
public int getType() {
return PayloadType.SAVED_QUERY;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(query);
}
public static final Creator<SavedQueryPayload> CREATOR = new Creator<SavedQueryPayload>() {
@Override
public SavedQueryPayload createFromParcel(Parcel in) {
return new SavedQueryPayload(in);
}
@Override
public SavedQueryPayload[] newArray(int size) {
return new SavedQueryPayload[size];
}
};
}

View File

@@ -1,98 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
import com.android.settings.search.IndexDatabaseHelper;
import com.android.settingslib.utils.AsyncLoader;
import static com.android.settings.search.IndexDatabaseHelper.Tables.TABLE_SAVED_QUERIES;
/**
* A background task to update saved queries.
*/
public class SavedQueryRecorder extends AsyncLoader<Void> {
private static final String LOG_TAG = "SavedQueryRecorder";
// Max number of saved search queries (who will be used for proposing suggestions)
private static long MAX_SAVED_SEARCH_QUERY = 64;
private final String mQuery;
public SavedQueryRecorder(Context context, String query) {
super(context);
mQuery = query;
}
@Override
protected void onDiscardResult(Void result) {
}
@Override
public Void loadInBackground() {
final long now = System.currentTimeMillis();
final ContentValues values = new ContentValues();
values.put(IndexDatabaseHelper.SavedQueriesColumns.QUERY, mQuery);
values.put(IndexDatabaseHelper.SavedQueriesColumns.TIME_STAMP, now);
final SQLiteDatabase database = getWritableDatabase();
if (database == null) {
return null;
}
long lastInsertedRowId;
try {
// First, delete all saved queries that are the same
database.delete(TABLE_SAVED_QUERIES,
IndexDatabaseHelper.SavedQueriesColumns.QUERY + " = ?",
new String[]{mQuery});
// Second, insert the saved query
lastInsertedRowId = database.insertOrThrow(TABLE_SAVED_QUERIES, null, values);
// Last, remove "old" saved queries
final long delta = lastInsertedRowId - MAX_SAVED_SEARCH_QUERY;
if (delta > 0) {
int count = database.delete(TABLE_SAVED_QUERIES,
"rowId <= ?",
new String[]{Long.toString(delta)});
Log.d(LOG_TAG, "Deleted '" + count + "' saved Search query(ies)");
}
} catch (Exception e) {
Log.d(LOG_TAG, "Cannot update saved Search queries", e);
}
return null;
}
private SQLiteDatabase getWritableDatabase() {
try {
return IndexDatabaseHelper.getInstance(getContext()).getWritableDatabase();
} catch (SQLiteException e) {
Log.e(LOG_TAG, "Cannot open writable database", e);
return null;
}
}
}

View File

@@ -1,64 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import static com.android.settings.search.IndexDatabaseHelper.Tables.TABLE_SAVED_QUERIES;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
import com.android.settingslib.utils.AsyncLoader;
public class SavedQueryRemover extends AsyncLoader<Void> {
private static final String LOG_TAG = "SavedQueryRemover";
public SavedQueryRemover(Context context) {
super(context);
}
@Override
public Void loadInBackground() {
final SQLiteDatabase database = getWritableDatabase();
try {
// First, delete all saved queries that are the same
database.delete(TABLE_SAVED_QUERIES,
null /* where */,
null /* whereArgs */);
} catch (Exception e) {
Log.d(LOG_TAG, "Cannot update saved Search queries", e);
}
return null;
}
@Override
protected void onDiscardResult(Void result) {
}
private SQLiteDatabase getWritableDatabase() {
try {
return IndexDatabaseHelper.getInstance(getContext()).getWritableDatabase();
} catch (SQLiteException e) {
Log.e(LOG_TAG, "Cannot open writable database", e);
return null;
}
}
}

View File

@@ -1,44 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.view.View;
import android.widget.TextView;
import com.android.internal.logging.nano.MetricsProto;
public class SavedQueryViewHolder extends SearchViewHolder {
public final TextView titleView;
public SavedQueryViewHolder(View view) {
super(view);
titleView = view.findViewById(android.R.id.title);
}
@Override
public int getClickActionMetricName() {
return MetricsProto.MetricsEvent.ACTION_CLICK_SETTINGS_SEARCH_SAVED_QUERY;
}
@Override
public void onBind(SearchFragment fragment, SearchResult result) {
itemView.setOnClickListener(v -> fragment.onSavedQueryClicked(result.title));
titleView.setText(result.title);
}
}

View File

@@ -1,45 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.WindowManager;
import com.android.settings.R;
public class SearchActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_main);
// Keeps layouts in-place when keyboard opens.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(R.id.main_content);
if (fragment == null) {
fragmentManager.beginTransaction()
.add(R.id.main_content, new SearchFragment())
.commit();
}
}
}

View File

@@ -21,12 +21,9 @@ import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.FeatureFlagUtils;
import android.util.Pair;
import android.view.View;
import android.widget.Toolbar;
import com.android.settings.core.FeatureFlags;
import com.android.settings.dashboard.SiteMapManager;
import com.android.settings.overlay.FeatureFactory;
@@ -50,58 +47,18 @@ public interface SearchFeatureProvider {
void verifyLaunchSearchResultPageCaller(Context context, @NonNull ComponentName caller)
throws SecurityException, IllegalArgumentException;
/**
* Returns a new loader to get settings search results.
*/
SearchResultLoader getSearchResultLoader(Context context, String query);
/**
* Returns a new loader to search in index database.
*/
DatabaseResultLoader getStaticSearchResultTask(Context context, String query);
/**
* Returns a new loader to search installed apps.
*/
InstalledAppResultLoader getInstalledAppSearchTask(Context context, String query);
/**
* Returns a new loader to search accessibility services.
*/
AccessibilityServiceResultLoader getAccessibilityServiceResultTask(Context context,
String query);
/**
* Returns a new loader to search input devices.
*/
InputDeviceResultLoader getInputDeviceResultTask(Context context, String query);
/**
* Returns a new loader to get all recently saved queries search terms.
*/
SavedQueryLoader getSavedQueryLoader(Context context);
/**
* Returns the manager for indexing Settings data.
*/
DatabaseIndexingManager getIndexingManager(Context context);
/**
* Returns the manager for looking up breadcrumbs.
*/
SiteMapManager getSiteMapManager();
/**
* Updates the Settings indexes and calls {@link IndexingCallback#onIndexingFinished()} on
* {@param callback} when indexing is complete.
*/
void updateIndexAsync(Context context, IndexingCallback callback);
/**
* Synchronously updates the Settings database.
*/
void updateIndex(Context context);
DatabaseIndexingManager getIndexingManager(Context context);
/**
* @returns true when indexing is complete.
*/
@@ -112,55 +69,6 @@ public interface SearchFeatureProvider {
*/
ExecutorService getExecutorService();
/**
* Initializes the feedback button in case it was dismissed.
*/
default void initFeedbackButton() {
}
/**
* Show a button users can click to submit feedback on the quality of the search results.
*/
default void showFeedbackButton(SearchFragment fragment, View view) {
}
/**
* Hide the feedback button shown by
* {@link #showFeedbackButton(SearchFragment fragment, View view) showFeedbackButton}
*/
default void hideFeedbackButton() {
}
/**
* Notify that a search result is clicked.
*
* @param context application context
* @param query input user query
* @param searchResult clicked result
*/
default void searchResultClicked(Context context, String query, SearchResult searchResult) {
}
/**
* @return true to enable search ranking.
*/
default boolean isSmartSearchRankingEnabled(Context context) {
return false;
}
/**
* @return smart ranking timeout in milliseconds.
*/
default long smartSearchRankingTimeoutMs(Context context) {
return 300L;
}
/**
* Prepare for search ranking predictions to avoid latency on the first prediction call.
*/
default void searchRankingWarmup(Context context) {
}
/**
* Return a FutureTask to get a list of scores for search results.
*/
@@ -168,10 +76,6 @@ public interface SearchFeatureProvider {
return null;
}
default boolean isSearchV2Enabled(Context context) {
return FeatureFlagUtils.isEnabled(context, FeatureFlags.SEARCH_V2);
}
/**
* Initializes the search toolbar.
*/
@@ -180,12 +84,8 @@ public interface SearchFeatureProvider {
return;
}
toolbar.setOnClickListener(tb -> {
final Intent intent;
if (isSearchV2Enabled(activity)) {
intent = SEARCH_UI_INTENT;
} else {
intent = new Intent(activity, SearchActivity.class);
}
final Intent intent = SEARCH_UI_INTENT;
FeatureFactory.getFactory(
activity.getApplicationContext()).getSlicesFeatureProvider()
.indexSliceDataAsync(activity.getApplicationContext());

View File

@@ -20,13 +20,11 @@ package com.android.settings.search;
import android.content.ComponentName;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.dashboard.SiteMapManager;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.indexing.IndexData;
import com.android.settingslib.wrapper.PackageManagerWrapper;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
@@ -62,40 +60,6 @@ public class SearchFeatureProviderImpl implements SearchFeatureProvider {
+ "whitelisted package.");
}
@Override
public SearchResultLoader getSearchResultLoader(Context context, String query) {
return new SearchResultLoader(context, cleanQuery(query));
}
@Override
public DatabaseResultLoader getStaticSearchResultTask(Context context, String query) {
return new DatabaseResultLoader(context, cleanQuery(query), getSiteMapManager());
}
@Override
public InstalledAppResultLoader getInstalledAppSearchTask(Context context, String query) {
return new InstalledAppResultLoader(
context, new PackageManagerWrapper(context.getPackageManager()),
cleanQuery(query), getSiteMapManager());
}
@Override
public AccessibilityServiceResultLoader getAccessibilityServiceResultTask(Context context,
String query) {
return new AccessibilityServiceResultLoader(context, cleanQuery(query),
getSiteMapManager());
}
@Override
public InputDeviceResultLoader getInputDeviceResultTask(Context context, String query) {
return new InputDeviceResultLoader(context, cleanQuery(query), getSiteMapManager());
}
@Override
public SavedQueryLoader getSavedQueryLoader(Context context) {
return new SavedQueryLoader(context);
}
@Override
public DatabaseIndexingManager getIndexingManager(Context context) {
if (mDatabaseIndexingManager == null) {
@@ -116,14 +80,6 @@ public class SearchFeatureProviderImpl implements SearchFeatureProvider {
return mSiteMapManager;
}
@Override
public void updateIndexAsync(Context context, IndexingCallback callback) {
if (SettingsSearchIndexablesProvider.DEBUG) {
Log.d(TAG, "updating index async");
}
getIndexingManager(context).indexDatabase(callback);
}
@Override
public void updateIndex(Context context) {
long indexStartTime = System.currentTimeMillis();

View File

@@ -1,433 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.Loader;
import android.os.Bundle;
import android.support.annotation.VisibleForTesting;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.EventLog;
import android.util.Log;
import android.util.Pair;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toolbar;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.EventLogTags;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.Utils;
import com.android.settings.core.InstrumentedFragment;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.ActionBarShadowController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* This fragment manages the lifecycle of indexing and searching.
*
* In onCreate, the indexing process is initiated in DatabaseIndexingManager.
* While the indexing is happening, loaders are blocked from accessing the database, but the user
* is free to start typing their query.
*
* When the indexing is complete, the fragment gets a callback to initialize the loaders and search
* the query if the user has entered text.
*/
public class SearchFragment extends InstrumentedFragment implements SearchView.OnQueryTextListener,
LoaderManager.LoaderCallbacks<List<? extends SearchResult>>, IndexingCallback {
private static final String TAG = "SearchFragment";
// State values
private static final String STATE_QUERY = "state_query";
private static final String STATE_SHOWING_SAVED_QUERY = "state_showing_saved_query";
private static final String STATE_NEVER_ENTERED_QUERY = "state_never_entered_query";
static final class SearchLoaderId {
// Search Query IDs
public static final int SEARCH_RESULT = 1;
// Saved Query IDs
public static final int SAVE_QUERY_TASK = 2;
public static final int REMOVE_QUERY_TASK = 3;
public static final int SAVED_QUERIES = 4;
}
@VisibleForTesting
String mQuery;
private boolean mNeverEnteredQuery = true;
@VisibleForTesting
boolean mShowingSavedQuery;
private MetricsFeatureProvider mMetricsFeatureProvider;
@VisibleForTesting
SavedQueryController mSavedQueryController;
@VisibleForTesting
SearchFeatureProvider mSearchFeatureProvider;
@VisibleForTesting
SearchResultsAdapter mSearchAdapter;
@VisibleForTesting
RecyclerView mResultsRecyclerView;
@VisibleForTesting
SearchView mSearchView;
@VisibleForTesting
LinearLayout mNoResultsView;
@VisibleForTesting
final RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy != 0) {
hideKeyboard();
}
}
};
@Override
public int getMetricsCategory() {
return MetricsEvent.DASHBOARD_SEARCH_RESULTS;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mSearchFeatureProvider = FeatureFactory.getFactory(context).getSearchFeatureProvider();
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
long startTime = System.currentTimeMillis();
setHasOptionsMenu(true);
final LoaderManager loaderManager = getLoaderManager();
mSearchAdapter = new SearchResultsAdapter(this /* fragment */);
mSavedQueryController = new SavedQueryController(
getContext(), loaderManager, mSearchAdapter);
mSearchFeatureProvider.initFeedbackButton();
if (savedInstanceState != null) {
mQuery = savedInstanceState.getString(STATE_QUERY);
mNeverEnteredQuery = savedInstanceState.getBoolean(STATE_NEVER_ENTERED_QUERY);
mShowingSavedQuery = savedInstanceState.getBoolean(STATE_SHOWING_SAVED_QUERY);
} else {
mShowingSavedQuery = true;
}
final Activity activity = getActivity();
// Run the Index update only if we have some space
if (!Utils.isLowStorage(activity)) {
mSearchFeatureProvider.updateIndexAsync(activity, this /* indexingCallback */);
} else {
Log.w(TAG, "Cannot update the Indexer as we are running low on storage space!");
}
if (SettingsSearchIndexablesProvider.DEBUG) {
Log.d(TAG, "onCreate spent " + (System.currentTimeMillis() - startTime) + " ms");
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
mSavedQueryController.buildMenuItem(menu);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.search_panel, container, false);
mResultsRecyclerView = view.findViewById(R.id.list_results);
mResultsRecyclerView.setAdapter(mSearchAdapter);
mResultsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mResultsRecyclerView.addOnScrollListener(mScrollListener);
mNoResultsView = view.findViewById(R.id.no_results_layout);
Toolbar toolbar = view.findViewById(R.id.search_toolbar);
getActivity().setActionBar(toolbar);
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
mSearchView = toolbar.findViewById(R.id.search_view);
mSearchView.setQuery(mQuery, false /* submitQuery */);
mSearchView.setOnQueryTextListener(this);
mSearchView.requestFocus();
// Updating internal views inside SearchView was the easiest way to get this too look right.
// Instead of grabbing the TextView directly, we grab it as a view and do an instanceof
// check. This ensures if we return, say, a LinearLayout in the tests, they won't fail.
View searchText = mSearchView.findViewById(com.android.internal.R.id.search_src_text);
if (searchText instanceof TextView) {
TextView searchTextView = (TextView) searchText;
searchTextView.setTextColor(getContext().getColorStateList(
com.android.internal.R.color.text_color_primary));
searchTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.search_bar_text_size));
}
View editFrame = mSearchView.findViewById(com.android.internal.R.id.search_edit_frame);
if (editFrame != null) {
ViewGroup.MarginLayoutParams params =
(ViewGroup.MarginLayoutParams) editFrame.getLayoutParams();
params.setMarginStart(0);
editFrame.setLayoutParams(params);
}
ActionBarShadowController.attachToRecyclerView(
view.findViewById(R.id.search_bar_container), getLifecycle(), mResultsRecyclerView);
return view;
}
@Override
public void onResume() {
super.onResume();
Context appContext = getContext().getApplicationContext();
if (mSearchFeatureProvider.isSmartSearchRankingEnabled(appContext)) {
mSearchFeatureProvider.searchRankingWarmup(appContext);
}
requery();
}
@Override
public void onStop() {
super.onStop();
final Activity activity = getActivity();
if (activity != null && activity.isFinishing()) {
if (mNeverEnteredQuery) {
mMetricsFeatureProvider.action(activity,
MetricsEvent.ACTION_LEAVE_SEARCH_RESULT_WITHOUT_QUERY);
}
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(STATE_QUERY, mQuery);
outState.putBoolean(STATE_NEVER_ENTERED_QUERY, mNeverEnteredQuery);
outState.putBoolean(STATE_SHOWING_SAVED_QUERY, mShowingSavedQuery);
}
@Override
public boolean onQueryTextChange(String query) {
if (TextUtils.equals(query, mQuery)) {
return true;
}
final boolean isEmptyQuery = TextUtils.isEmpty(query);
// Hide no-results-view when the new query is not a super-string of the previous
if (mQuery != null
&& mNoResultsView.getVisibility() == View.VISIBLE
&& query.length() < mQuery.length()) {
mNoResultsView.setVisibility(View.GONE);
}
mNeverEnteredQuery = false;
mQuery = query;
// If indexing is not finished, register the query text, but don't search.
if (!mSearchFeatureProvider.isIndexingComplete(getActivity())) {
return true;
}
if (isEmptyQuery) {
final LoaderManager loaderManager = getLoaderManager();
loaderManager.destroyLoader(SearchLoaderId.SEARCH_RESULT);
mShowingSavedQuery = true;
mSavedQueryController.loadSavedQueries();
mSearchFeatureProvider.hideFeedbackButton();
} else {
restartLoaders();
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// Save submitted query.
mSavedQueryController.saveQuery(mQuery);
hideKeyboard();
return true;
}
@Override
public Loader<List<? extends SearchResult>> onCreateLoader(int id, Bundle args) {
final Activity activity = getActivity();
switch(id) {
case SearchLoaderId.SEARCH_RESULT:
return mSearchFeatureProvider.getSearchResultLoader(activity, mQuery);
default:
return null;
}
}
@Override
public void onLoadFinished(Loader<List<? extends SearchResult>> loader,
List<? extends SearchResult> data) {
mSearchAdapter.postSearchResults(data);
}
@Override
public void onLoaderReset(Loader<List<? extends SearchResult>> loader) {
}
/**
* Gets called when Indexing is completed.
*/
@Override
public void onIndexingFinished() {
if (getActivity() == null) {
return;
}
if (mShowingSavedQuery) {
mSavedQueryController.loadSavedQueries();
} else {
final LoaderManager loaderManager = getLoaderManager();
loaderManager.initLoader(SearchLoaderId.SEARCH_RESULT, null /* args */,
this /* callback */);
}
requery();
}
public void onSearchResultClicked(SearchViewHolder resultViewHolder, SearchResult result,
Pair<Integer, Object>... logTaggedData) {
logSearchResultClicked(resultViewHolder, result, logTaggedData);
mSearchFeatureProvider.searchResultClicked(getContext(), mQuery, result);
mSavedQueryController.saveQuery(mQuery);
}
public void onSearchResultsDisplayed(int resultCount) {
if (resultCount == 0) {
mNoResultsView.setVisibility(View.VISIBLE);
mMetricsFeatureProvider.visible(getContext(), getMetricsCategory(),
MetricsEvent.SETTINGS_SEARCH_NO_RESULT);
// Log settings_latency for search end-to-end.
EventLog.writeEvent(EventLogTags.SETTINGS_LATENCY, 1, 10);
} else {
mNoResultsView.setVisibility(View.GONE);
mResultsRecyclerView.scrollToPosition(0);
}
mMetricsFeatureProvider.action(
getVisibilityLogger(), MetricsEvent.ACTION_SEARCH_RESULTS, 1);
mSearchFeatureProvider.showFeedbackButton(this, getView());
}
public void onSavedQueryClicked(CharSequence query) {
final String queryString = query.toString();
mMetricsFeatureProvider.action(getContext(),
MetricsEvent.ACTION_CLICK_SETTINGS_SEARCH_SAVED_QUERY);
mSearchView.setQuery(queryString, false /* submit */);
onQueryTextChange(queryString);
}
private void restartLoaders() {
mShowingSavedQuery = false;
final LoaderManager loaderManager = getLoaderManager();
loaderManager.restartLoader(
SearchLoaderId.SEARCH_RESULT, null /* args */, this /* callback */);
}
public String getQuery() {
return mQuery;
}
public List<SearchResult> getSearchResults() {
return mSearchAdapter.getSearchResults();
}
private void requery() {
if (TextUtils.isEmpty(mQuery)) {
return;
}
final String query = mQuery;
mQuery = "";
onQueryTextChange(query);
}
private void hideKeyboard() {
final Activity activity = getActivity();
if (activity != null) {
View view = activity.getCurrentFocus();
InputMethodManager imm = (InputMethodManager)
activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
if (mResultsRecyclerView != null) {
mResultsRecyclerView.requestFocus();
}
}
private void logSearchResultClicked(SearchViewHolder resultViewHolder, SearchResult result,
Pair<Integer, Object>... logTaggedData) {
final Intent intent = result.payload.getIntent();
if (intent == null) {
Log.w(TAG, "Skipped logging click on search result because of null intent, which can " +
"happen on saved query results.");
return;
}
final ComponentName cn = intent.getComponent();
String resultName = intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT);
if (TextUtils.isEmpty(resultName) && cn != null) {
resultName = cn.flattenToString();
}
final List<Pair<Integer, Object>> taggedData = new ArrayList<>();
if (logTaggedData != null) {
taggedData.addAll(Arrays.asList(logTaggedData));
}
taggedData.add(Pair.create(
MetricsEvent.FIELD_SETTINGS_SEARCH_RESULT_COUNT,
mSearchAdapter.getItemCount()));
taggedData.add(Pair.create(
MetricsEvent.FIELD_SETTINGS_SEARCH_RESULT_RANK,
resultViewHolder.getAdapterPosition()));
// TODO (b/67744820) Move metrics to SettingsIntelligence (including ranking state).
taggedData.add(Pair.create(
MetricsEvent.FIELD_SETTINGS_SEARCH_QUERY_LENGTH,
TextUtils.isEmpty(mQuery) ? 0 : mQuery.length()));
mMetricsFeatureProvider.action(getContext(),
resultViewHolder.getClickActionMetricName(),
resultName,
taggedData.toArray(new Pair[0]));
}
}

View File

@@ -1,177 +0,0 @@
package com.android.settings.search;
import android.annotation.NonNull;
import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import com.android.settings.overlay.FeatureFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Collects the sorted list of all setting search results.
*
* TODO (b/64939692) Convert the timing logs to metrics
*/
public class SearchResultAggregator {
private static final String TAG = "SearchResultAggregator";
/**
* Timeout for first task. Allows for longer delay.
*/
private static final long LONG_CHECK_TASK_TIMEOUT_MS = 500;
/**
* Timeout for subsequent tasks to allow for fast returning tasks.
*/
private static final long SHORT_CHECK_TASK_TIMEOUT_MS = 150;
private static SearchResultAggregator sResultAggregator;
// TODO (b/33577327) Merge the other loaders into a single dynamic loader
static final class ResultLoaderId {
static final int STATIC_RESULTS = 1;
static final int INSTALLED_RESULTS = 2;
static final int INPUT_RESULTS = 3;
static final int ACCESSIBILITY_RESULTS = 4;
}
private SearchResultAggregator() {
}
public static SearchResultAggregator getInstance() {
if (sResultAggregator == null) {
sResultAggregator = new SearchResultAggregator();
}
return sResultAggregator;
}
@NonNull
public synchronized List<? extends SearchResult> fetchResults(Context context, String query) {
SearchFeatureProvider mFeatureProvider = FeatureFactory.getFactory(
context).getSearchFeatureProvider();
ExecutorService executorService = mFeatureProvider.getExecutorService();
final DatabaseResultLoader staticResultsTask =
mFeatureProvider.getStaticSearchResultTask(context, query);
final InstalledAppResultLoader installedAppTask =
mFeatureProvider.getInstalledAppSearchTask(context, query);
final InputDeviceResultLoader inputDevicesTask =
mFeatureProvider.getInputDeviceResultTask(context, query);
final AccessibilityServiceResultLoader accessibilityServicesTask =
mFeatureProvider.getAccessibilityServiceResultTask(context,
query);
executorService.execute(staticResultsTask);
executorService.execute(installedAppTask);
executorService.execute(inputDevicesTask);
executorService.execute(accessibilityServicesTask);
SparseArray<List<? extends SearchResult>> resultsArray = new SparseArray<>();
List<? extends SearchResult> EMPTY_LIST = new ArrayList<>();
long allTasksStart = System.currentTimeMillis();
try {
resultsArray.put(ResultLoaderId.INPUT_RESULTS,
inputDevicesTask.get(SHORT_CHECK_TASK_TIMEOUT_MS, TimeUnit.MILLISECONDS));
} catch (TimeoutException | InterruptedException | ExecutionException e) {
Log.d(TAG, "Could not retrieve input devices results in time: " + e);
resultsArray.put(ResultLoaderId.INPUT_RESULTS, EMPTY_LIST);
}
try {
resultsArray.put(ResultLoaderId.ACCESSIBILITY_RESULTS,
accessibilityServicesTask.get(SHORT_CHECK_TASK_TIMEOUT_MS,
TimeUnit.MILLISECONDS));
} catch (TimeoutException | InterruptedException | ExecutionException e) {
Log.d(TAG, "Could not retrieve accessibility results in time: " + e);
resultsArray.put(ResultLoaderId.ACCESSIBILITY_RESULTS, EMPTY_LIST);
}
try {
resultsArray.put(ResultLoaderId.STATIC_RESULTS,
staticResultsTask.get(LONG_CHECK_TASK_TIMEOUT_MS, TimeUnit.MILLISECONDS));
} catch (TimeoutException | InterruptedException | ExecutionException e) {
Log.d(TAG, "Could not retrieve static results: " + e);
resultsArray.put(ResultLoaderId.STATIC_RESULTS, EMPTY_LIST);
}
try {
resultsArray.put(ResultLoaderId.INSTALLED_RESULTS,
installedAppTask.get(SHORT_CHECK_TASK_TIMEOUT_MS, TimeUnit.MILLISECONDS));
} catch (TimeoutException | InterruptedException | ExecutionException e) {
Log.d(TAG, "Could not retrieve installed app results in time: " + e);
resultsArray.put(ResultLoaderId.INSTALLED_RESULTS, EMPTY_LIST);
}
long mergeStartTime = System.currentTimeMillis();
Log.i(TAG, "Total result loader time: " + (mergeStartTime - allTasksStart));
List<? extends SearchResult> mergedResults = mergeSearchResults(resultsArray);
Log.i(TAG, "Total merge time: " + (System.currentTimeMillis() - mergeStartTime));
Log.i(TAG, "Total aggregator time: " + (System.currentTimeMillis() - allTasksStart));
return mergedResults;
}
// TODO (b/68255021) scale the dynamic search results ranks and do a k-way merge
private List<? extends SearchResult> mergeSearchResults(
SparseArray<List<? extends SearchResult>> resultsArray) {
List<? extends SearchResult> staticResults = resultsArray.get(
ResultLoaderId.STATIC_RESULTS);
List<? extends SearchResult> installedAppResults = resultsArray.get(
ResultLoaderId.INSTALLED_RESULTS);
List<? extends SearchResult> accessibilityResults = resultsArray.get(
ResultLoaderId.ACCESSIBILITY_RESULTS);
List<? extends SearchResult> inputDeviceResults = resultsArray.get(
ResultLoaderId.INPUT_RESULTS);
List<SearchResult> searchResults;
int staticSize = staticResults.size();
int appSize = installedAppResults.size();
int a11ySize = accessibilityResults.size();
int inputDeviceSize = inputDeviceResults.size();
int appIndex = 0;
int a11yIndex = 0;
int inputDeviceIndex = 0;
int rank = SearchResult.TOP_RANK;
// TODO: We need a helper method to do k-way merge.
searchResults = new ArrayList<>(staticSize + appSize + a11ySize + inputDeviceSize);
searchResults.addAll(resultsArray.get(ResultLoaderId.STATIC_RESULTS));
while (rank <= SearchResult.BOTTOM_RANK) {
while ((appIndex < appSize) && (installedAppResults.get(appIndex).rank == rank)) {
searchResults.add(installedAppResults.get(appIndex++));
}
while ((a11yIndex < a11ySize) && (accessibilityResults.get(a11yIndex).rank == rank)) {
searchResults.add(accessibilityResults.get(a11yIndex++));
}
while (inputDeviceIndex < inputDeviceSize
&& inputDeviceResults.get(inputDeviceIndex).rank == rank) {
searchResults.add(inputDeviceResults.get(inputDeviceIndex++));
}
rank++;
}
while (appIndex < appSize) {
searchResults.add(installedAppResults.get(appIndex++));
}
while (a11yIndex < a11ySize) {
searchResults.add(accessibilityResults.get(a11yIndex++));
}
while (inputDeviceIndex < inputDeviceSize) {
searchResults.add(inputDeviceResults.get(inputDeviceIndex++));
}
return searchResults;
}
}

View File

@@ -1,57 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.support.v7.util.DiffUtil;
import java.util.List;
/**
* Callback for DiffUtil to elegantly update search data when the query changes.
*/
public class SearchResultDiffCallback extends DiffUtil.Callback {
private List<? extends SearchResult> mOldList;
private List<? extends SearchResult> mNewList;
public SearchResultDiffCallback(List<? extends SearchResult> oldList,
List<? extends SearchResult> newList) {
mOldList = oldList;
mNewList = newList;
}
@Override
public int getOldListSize() {
return mOldList.size();
}
@Override
public int getNewListSize() {
return mNewList.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return mOldList.get(oldItemPosition).equals(mNewList.get(newItemPosition));
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return mOldList.get(oldItemPosition).equals(mNewList.get(newItemPosition));
}
}

View File

@@ -1,30 +0,0 @@
package com.android.settings.search;
import com.android.settingslib.utils.AsyncLoader;
import android.content.Context;
import java.util.List;
/**
* Loads a sorted list of Search results for a given query.
*/
public class SearchResultLoader extends AsyncLoader<List<? extends SearchResult>> {
private final String mQuery;
public SearchResultLoader(Context context, String query) {
super(context);
mQuery = query;
}
@Override
public List<? extends SearchResult> loadInBackground() {
SearchResultAggregator aggregator = SearchResultAggregator.getInstance();
return aggregator.fetchResults(getContext(), mQuery);
}
@Override
protected void onDiscardResult(List<? extends SearchResult> result) {
}
}

View File

@@ -1,116 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.content.Context;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.settings.R;
import java.util.ArrayList;
import java.util.List;
public class SearchResultsAdapter extends RecyclerView.Adapter<SearchViewHolder> {
private final SearchFragment mFragment;
private final List<SearchResult> mSearchResults;
public SearchResultsAdapter(SearchFragment fragment) {
mFragment = fragment;
mSearchResults = new ArrayList<>();
setHasStableIds(true);
}
@Override
public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final Context context = parent.getContext();
final LayoutInflater inflater = LayoutInflater.from(context);
final View view;
switch (viewType) {
case ResultPayload.PayloadType.INTENT:
view = inflater.inflate(R.layout.search_intent_item, parent, false);
return new IntentSearchViewHolder(view);
case ResultPayload.PayloadType.INLINE_SWITCH:
// TODO (b/62807132) replace layout InlineSwitchViewHolder and return an
// InlineSwitchViewHolder.
view = inflater.inflate(R.layout.search_intent_item, parent, false);
return new IntentSearchViewHolder(view);
case ResultPayload.PayloadType.INLINE_LIST:
// TODO (b/62807132) build a inline-list view holder & layout.
view = inflater.inflate(R.layout.search_intent_item, parent, false);
return new IntentSearchViewHolder(view);
case ResultPayload.PayloadType.SAVED_QUERY:
view = inflater.inflate(R.layout.search_saved_query_item, parent, false);
return new SavedQueryViewHolder(view);
default:
return null;
}
}
@Override
public void onBindViewHolder(SearchViewHolder holder, int position) {
holder.onBind(mFragment, mSearchResults.get(position));
}
@Override
public long getItemId(int position) {
return mSearchResults.get(position).stableId;
}
@Override
public int getItemViewType(int position) {
return mSearchResults.get(position).viewType;
}
@Override
public int getItemCount() {
return mSearchResults.size();
}
/**
* Displays recent searched queries.
*/
public void displaySavedQuery(List<? extends SearchResult> data) {
clearResults();
mSearchResults.addAll(data);
notifyDataSetChanged();
}
public void clearResults() {
mSearchResults.clear();
notifyDataSetChanged();
}
public List<SearchResult> getSearchResults() {
return mSearchResults;
}
public void postSearchResults(List<? extends SearchResult> newSearchResults) {
final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(
new SearchResultDiffCallback(mSearchResults, newSearchResults));
mSearchResults.clear();
mSearchResults.addAll(newSearchResults);
diffResult.dispatchUpdatesTo(this);
mFragment.onSearchResultsDisplayed(mSearchResults.size());
}
}

View File

@@ -1,121 +0,0 @@
/*
* Copyright (C) 2017 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.search;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.IconDrawableFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.annotation.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.overlay.FeatureFactory;
/**
* The ViewHolder for the Search RecyclerView.
* There are multiple search result types in the same Recycler view with different UI requirements.
* Some examples include Intent results, Inline results, and Help articles.
*/
public abstract class SearchViewHolder extends RecyclerView.ViewHolder {
private final String DYNAMIC_PLACEHOLDER = "%s";
private final String mPlaceholderSummary;
public final TextView titleView;
public final TextView summaryView;
public final TextView breadcrumbView;
public final ImageView iconView;
protected final MetricsFeatureProvider mMetricsFeatureProvider;
protected final SearchFeatureProvider mSearchFeatureProvider;
private final IconDrawableFactory mIconDrawableFactory;
public SearchViewHolder(View view) {
super(view);
final FeatureFactory featureFactory = FeatureFactory
.getFactory(view.getContext().getApplicationContext());
mMetricsFeatureProvider = featureFactory.getMetricsFeatureProvider();
mSearchFeatureProvider = featureFactory.getSearchFeatureProvider();
titleView = view.findViewById(android.R.id.title);
summaryView = view.findViewById(android.R.id.summary);
iconView = view.findViewById(android.R.id.icon);
breadcrumbView = view.findViewById(R.id.breadcrumb);
mPlaceholderSummary = view.getContext().getString(R.string.summary_placeholder);
mIconDrawableFactory = IconDrawableFactory.newInstance(view.getContext());
}
public abstract int getClickActionMetricName();
public void onBind(SearchFragment fragment, SearchResult result) {
titleView.setText(result.title);
// TODO (b/36101902) remove check for DYNAMIC_PLACEHOLDER
if (TextUtils.isEmpty(result.summary)
|| TextUtils.equals(result.summary, mPlaceholderSummary)
|| TextUtils.equals(result.summary, DYNAMIC_PLACEHOLDER)) {
summaryView.setVisibility(View.GONE);
} else {
summaryView.setText(result.summary);
summaryView.setVisibility(View.VISIBLE);
}
if (result instanceof AppSearchResult) {
AppSearchResult appResult = (AppSearchResult) result;
PackageManager pm = fragment.getActivity().getPackageManager();
UserHandle userHandle = appResult.getAppUserHandle();
Drawable badgedIcon = getBadgedIcon(appResult.info, userHandle.getIdentifier());
iconView.setImageDrawable(badgedIcon);
titleView.setContentDescription(
pm.getUserBadgedLabel(appResult.info.loadLabel(pm), userHandle));
} else {
// Valid even when result.icon is null.
iconView.setImageDrawable(result.icon);
}
bindBreadcrumbView(result);
}
private void bindBreadcrumbView(SearchResult result) {
if (result.breadcrumbs == null || result.breadcrumbs.isEmpty()) {
breadcrumbView.setVisibility(View.GONE);
return;
}
final Context context = breadcrumbView.getContext();
String breadcrumb = result.breadcrumbs.get(0);
final int count = result.breadcrumbs.size();
for (int i = 1; i < count; i++) {
breadcrumb = context.getString(R.string.search_breadcrumb_connector,
breadcrumb, result.breadcrumbs.get(i));
}
breadcrumbView.setText(breadcrumb);
breadcrumbView.setVisibility(View.VISIBLE);
}
@VisibleForTesting
Drawable getBadgedIcon(ApplicationInfo info, int userId) {
return mIconDrawableFactory.getBadgedIcon(info, userId);
}
}

View File

@@ -18,14 +18,12 @@ package com.android.settings.search.actionbar;
import android.annotation.NonNull;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.android.settings.R;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.search.SearchFeatureProvider;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.ObservablePreferenceFragment;
@@ -38,14 +36,7 @@ public class SearchMenuController implements LifecycleObserver, OnCreateOptionsM
private final Fragment mHost;
public static void init(@NonNull ObservablePreferenceFragment host) {
final Context context = host.getContext();
final boolean isSearchV2Enabled = FeatureFactory.getFactory(context)
.getSearchFeatureProvider()
.isSearchV2Enabled(context);
if (isSearchV2Enabled) {
host.getLifecycle().addObserver(new SearchMenuController(host));
}
host.getLifecycle().addObserver(new SearchMenuController(host));
}
private SearchMenuController(@NonNull Fragment host) {

View File

@@ -1,38 +0,0 @@
/*
* Copyright (C) 2017 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.search.ranking;
import android.util.Pair;
import java.util.List;
public interface SearchResultsRankerCallback {
/**
* Called when ranker provides the ranking scores.
* @param searchRankingScores Ordered List of Pairs of String and Float corresponding to
* stableIds and ranking scores. The list must be descendingly
* ordered based on scores.
*/
public void onRankingScoresAvailable(List<Pair<String, Float>> searchRankingScores);
/**
* Called when for any reason ranker fails, which notifies the client to proceed
* without ranking results.
*/
public void onRankingFailed();
}