Index Data to build Slices in Settings
The indexing is done by taking the indexable fragments from search, grabbing their XML via SearchIndexableResources, and then looking for controllers defined in preferences. For each controller found, we take the combination of the fragment providing the XML and the Preference info to create an indexable row. Buiding a Slice will be handled in a subsquent CL, but a prototype can be found here: ag/3324435 Test: robotests Bug: 67996923 Change-Id: I48668618079bcc3da55ab77b7323ee8e467073af
This commit is contained in:
102
src/com/android/settings/slices/SlicesIndexer.java
Normal file
102
src/com/android/settings/slices/SlicesIndexer.java
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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.slices;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.slices.SlicesDatabaseHelper.IndexColumns;
|
||||
import com.android.settings.slices.SlicesDatabaseHelper.Tables;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 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 = "SlicesIndexingManager";
|
||||
|
||||
private Context mContext;
|
||||
|
||||
private SlicesDatabaseHelper mHelper;
|
||||
|
||||
public SlicesIndexer(Context context) {
|
||||
mContext = context;
|
||||
mHelper = SlicesDatabaseHelper.getInstance(mContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously takes data obtained from {@link SliceDataConverter} and indexes it into a
|
||||
* SQLite database.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
if (mHelper.isSliceDataIndexed()) {
|
||||
return;
|
||||
}
|
||||
|
||||
SQLiteDatabase database = mHelper.getWritableDatabase();
|
||||
|
||||
try {
|
||||
database.beginTransaction();
|
||||
|
||||
mHelper.reconstruct(mHelper.getWritableDatabase());
|
||||
List<SliceData> indexData = getSliceData();
|
||||
insertSliceData(database, indexData);
|
||||
|
||||
mHelper.setIndexedState();
|
||||
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.TITLE, dataRow.getTitle());
|
||||
values.put(IndexColumns.SUMMARY, dataRow.getSummary());
|
||||
values.put(IndexColumns.SCREENTITLE, dataRow.getScreenTitle());
|
||||
values.put(IndexColumns.ICON_RESOURCE, dataRow.getIconResource());
|
||||
values.put(IndexColumns.FRAGMENT, dataRow.getFragmentClassName());
|
||||
values.put(IndexColumns.CONTROLLER, dataRow.getPreferenceController());
|
||||
|
||||
database.replaceOrThrow(Tables.TABLE_SLICES_INDEX, null /* nullColumnHack */,
|
||||
values);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user