基于Android 11的原理:
新增:
通过@SearchIndexable 和 SEARCH_INDEX_DATA_PROVIDER 插入search_index.db中
@SearchIndexable
public class DeviceAdminSettings extends DashboardFragment {
static final String TAG = "DeviceAdminSettings";
public int getMetricsCategory() {
return SettingsEnums.DEVICE_ADMIN_SETTINGS;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.device_admin_settings;
}
@Override
protected String getLogTag() {
return TAG;
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.device_admin_settings);
}
SlicesIndexer 数据存储核心类
/**
* Manages the conversion of {@link DashboardFragment} and {@link BasePreferenceController} to
* indexable data {@link SliceData} to be stored for Slices.
*/
class SlicesIndexer implements Runnable {
private static final String TAG = "SlicesIndexer";
private Context mContext;
private SlicesDatabaseHelper mHelper;
public SlicesIndexer(Context context) {
mContext = context;
mHelper = SlicesDatabaseHelper.getInstance(mContext);
}
/**
* Asynchronously index slice data from {@link #indexSliceData()}.
*/
@Override
public void run() {
indexSliceData();
}
/**
* Synchronously takes data obtained from {@link SliceDataConverter} and indexes it into a
* SQLite database
*/
protected void indexSliceData() {
if (mHelper.isSliceDataIndexed()) {
Log.d(TAG, "Slices already indexed - returning.");
return;
}
final SQLiteDatabase database = mHelper.getWritableDatabase();
long startTime = System.currentTimeMillis();
database.beginTransaction();
try {
mHelper.reconstruct(database);
List<SliceData> indexData = getSliceData();
insertSliceData(database, indexData);
mHelper.setIndexedState();
// TODO (b/71503044) Log indexing time.
Log.d(TAG,
"Indexing slices database took: " + (System.currentTimeMillis() - startTime));
database.setTransactionSuccessful();
} finally {
database.endTransaction();
}
}
@VisibleForTesting
List<SliceData> getSliceData() {
return FeatureFactory.getFactory(mContext)
.getSlicesFeatureProvider()
.getSliceDataConverter(mContext)
.getSliceData();
}
@VisibleForTesting
void insertSliceData(SQLiteDatabase database, List<SliceData> indexData) {
ContentValues values;
for (SliceData dataRow : indexData) {
values = new ContentValues();
values.put(IndexColumns.KEY, dataRow.getKey());
values.put(IndexColumns.SLICE_URI, dataRow.getUri().toSafeString());
values.put(IndexColumns.TITLE, dataRow.getTitle());
values.put(IndexColumns.SUMMARY, dataRow.getSummary());
final CharSequence screenTitle = dataRow.getScreenTitle();
if (screenTitle != null) {
values.put(IndexColumns.SCREENTITLE, screenTitle.toString());
}
values.put(IndexColumns.KEYWORDS, dataRow.getKeywords());
values.put(IndexColumns.ICON_RESOURCE, dataRow.getIconResource());
values.put(IndexColumns.FRAGMENT, dataRow.getFragmentClassName());
values.put(IndexColumns.CONTROLLER, dataRow.getPreferenceController());
values.put(IndexColumns.SLICE_TYPE, dataRow.getSliceType());
values.put(IndexColumns.UNAVAILABLE_SLICE_SUBTITLE,
dataRow.getUnavailableSliceSubtitle());
values.put(IndexColumns.PUBLIC_SLICE, dataRow.isPublicSlice());
database.replaceOrThrow(Tables.TABLE_SLICES_INDEX, null /* nullColumnHack */,
values);
}
}
}
屏蔽:
使用settings:searchable="false" 添加到过滤关键词列表
<com.android.settingslib.widget.FooterPreference
android:key="auto_brightness_footer"
android:title="@string/auto_brightness_description"
android:selectable="false"
settings:searchable="false" />
可通过SettingsSearchIndexablesProvider查看所有被过滤的key
/**
* Gets a combined list non-indexable keys that come from providers inside of settings.
* The non-indexable keys are used in Settings search at both index and update time to verify
* the validity of results in the database.
*/
@Override
public Cursor queryNonIndexableKeys(String[] projection) {
final MatrixCursor cursor = new MatrixCursor(NON_INDEXABLES_KEYS_COLUMNS);
final List<String> nonIndexableKeys = getNonIndexableKeysFromProvider(getContext());
for (String nik : nonIndexableKeys) {
final Object[] ref = new Object[NON_INDEXABLES_KEYS_COLUMNS.length];
ref[COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE] = nik;
cursor.addRow(ref);
//这里增加log打印
}
return cursor;
}
注意事项:
- 原有的功能类不能注释@SearchIndexable 和 SEARCH_INDEX_DATA_PROVIDER,否则无法添加到过滤关键词列表,可以通过settings:isPreferenceVisible="false"隐藏;
2.PreferenceScreen 和 Preference都可以使用settings:searchable="false",否则可能出现不能彻底屏蔽;
3.如果升级固件是替换settings app,不建议直接注释或删除对应代码,因为本地数据库已存储了key,无法打到屏蔽的效果,如果确实要去除,自行删除本地数据库key才行。