Skip indexing dynamic and injected raw data when page search is disabled

Test: robotest
Fixes: 188059978
Change-Id: I99c86ee12c7696123df26c47cfed8c3ddfbdf353
This commit is contained in:
Jason Chiu
2021-05-14 11:30:04 +08:00
parent 4335268c81
commit 1ee1911104
4 changed files with 164 additions and 44 deletions

View File

@@ -55,20 +55,22 @@ import android.provider.SearchIndexablesContract;
import android.provider.SearchIndexablesProvider;
import android.provider.SettingsSlicesContract;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.slice.SliceViewManager;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.dashboard.CategoryManager;
import com.android.settings.dashboard.DashboardFeatureProvider;
import com.android.settings.dashboard.DashboardFragmentRegistry;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.slices.SettingsSliceProvider;
import com.android.settingslib.drawer.ActivityTile;
import com.android.settingslib.drawer.CategoryKey;
import com.android.settingslib.drawer.DashboardCategory;
import com.android.settingslib.drawer.Tile;
import com.android.settingslib.search.Indexable;
@@ -78,6 +80,7 @@ import com.android.settingslib.search.SearchIndexableRaw;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class SettingsSearchIndexablesProvider extends SearchIndexablesProvider {
@@ -94,6 +97,9 @@ public class SettingsSearchIndexablesProvider extends SearchIndexablesProvider {
private static final Collection<String> INVALID_KEYS;
// Search enabled states for injection (key: category key, value: search enabled)
private Map<String, Boolean> mSearchEnabledByCategoryKeyMap;
static {
INVALID_KEYS = new ArraySet<>();
INVALID_KEYS.add(null);
@@ -102,6 +108,7 @@ public class SettingsSearchIndexablesProvider extends SearchIndexablesProvider {
@Override
public boolean onCreate() {
mSearchEnabledByCategoryKeyMap = new ArrayMap<>();
return true;
}
@@ -166,7 +173,18 @@ public class SettingsSearchIndexablesProvider extends SearchIndexablesProvider {
public Cursor queryDynamicRawData(String[] projection) {
final Context context = getContext();
final List<SearchIndexableRaw> rawList = new ArrayList<>();
rawList.addAll(getDynamicSearchIndexableRawFromProvider(context));
final Collection<SearchIndexableData> bundles = FeatureFactory.getFactory(context)
.getSearchFeatureProvider().getSearchIndexableResources().getProviderValues();
for (SearchIndexableData bundle : bundles) {
rawList.addAll(getDynamicSearchIndexableRawData(context, bundle));
// Refresh the search enabled state for indexing injection raw data
final Indexable.SearchIndexProvider provider = bundle.getSearchIndexProvider();
if (provider instanceof BaseSearchIndexProvider) {
refreshSearchEnabledState(context, (BaseSearchIndexProvider) provider);
}
}
rawList.addAll(getInjectionIndexableRawData(context));
final MatrixCursor cursor = new MatrixCursor(INDEXABLES_RAW_COLUMNS);
@@ -355,39 +373,35 @@ public class SettingsSearchIndexablesProvider extends SearchIndexablesProvider {
return rawList;
}
private List<SearchIndexableRaw> getDynamicSearchIndexableRawFromProvider(Context context) {
final Collection<SearchIndexableData> bundles = FeatureFactory.getFactory(context)
.getSearchFeatureProvider().getSearchIndexableResources().getProviderValues();
final List<SearchIndexableRaw> rawList = new ArrayList<>();
for (SearchIndexableData bundle : bundles) {
final Indexable.SearchIndexProvider provider = bundle.getSearchIndexProvider();
final List<SearchIndexableRaw> providerRaws =
provider.getDynamicRawDataToIndex(context, true /* enabled */);
if (providerRaws == null) {
continue;
}
for (SearchIndexableRaw raw : providerRaws) {
// The classname and intent information comes from the PreIndexData
// This will be more clear when provider conversion is done at PreIndex time.
raw.className = bundle.getTargetClass().getName();
}
rawList.addAll(providerRaws);
private List<SearchIndexableRaw> getDynamicSearchIndexableRawData(Context context,
SearchIndexableData bundle) {
final Indexable.SearchIndexProvider provider = bundle.getSearchIndexProvider();
final List<SearchIndexableRaw> providerRaws =
provider.getDynamicRawDataToIndex(context, true /* enabled */);
if (providerRaws == null) {
return new ArrayList<>();
}
return rawList;
for (SearchIndexableRaw raw : providerRaws) {
// The classname and intent information comes from the PreIndexData
// This will be more clear when provider conversion is done at PreIndex time.
raw.className = bundle.getTargetClass().getName();
}
return providerRaws;
}
private List<SearchIndexableRaw> getInjectionIndexableRawData(Context context) {
@VisibleForTesting
List<SearchIndexableRaw> getInjectionIndexableRawData(Context context) {
final DashboardFeatureProvider dashboardFeatureProvider =
FeatureFactory.getFactory(context).getDashboardFeatureProvider(context);
final List<SearchIndexableRaw> rawList = new ArrayList<>();
final String currentPackageName = context.getPackageName();
for (DashboardCategory category : dashboardFeatureProvider.getAllCategories()) {
if (mSearchEnabledByCategoryKeyMap.containsKey(category.key)
&& !mSearchEnabledByCategoryKeyMap.get(category.key)) {
Log.i(TAG, "Skip indexing category: " + category.key);
continue;
}
for (Tile tile : category.getTiles()) {
if (!isEligibleForIndexing(currentPackageName, tile)) {
continue;
@@ -410,6 +424,30 @@ public class SettingsSearchIndexablesProvider extends SearchIndexablesProvider {
return rawList;
}
@VisibleForTesting
void refreshSearchEnabledState(Context context, BaseSearchIndexProvider provider) {
// Provider's class name is like "com.android.settings.Settings$1"
String className = provider.getClass().getName();
final int delimiter = className.lastIndexOf("$");
if (delimiter > 0) {
// Parse the outer class name of this provider
className = className.substring(0, delimiter);
}
// Lookup the category key by the class name
final String categoryKey = DashboardFragmentRegistry.PARENT_TO_CATEGORY_KEY_MAP
.get(className);
if (categoryKey == null) {
return;
}
final DashboardCategory category = CategoryManager.get(context)
.getTilesByCategory(context, categoryKey);
if (category != null) {
mSearchEnabledByCategoryKeyMap.put(category.key, provider.isPageSearchEnabled(context));
}
}
@VisibleForTesting
boolean isEligibleForIndexing(String packageName, Tile tile) {
if (TextUtils.equals(packageName, tile.getPackageName())
@@ -417,10 +455,6 @@ public class SettingsSearchIndexablesProvider extends SearchIndexablesProvider {
// Skip Settings injected items because they should be indexed in the sub-pages.
return false;
}
if (TextUtils.equals(tile.getCategory(), CategoryKey.CATEGORY_HOMEPAGE)) {
// Skip homepage injected items since we would like to index their target activity.
return false;
}
return true;
}