Add indexing for SwitchPreferences

- now support the SwitchPreferences and save the "switchOn" and "switchOff"
attributes
- update Index database schema (and increment its version)
- fix an issue with some previous schema not rebuilt

Change-Id: I9cd48c666525f19474ef9bd5746d61b589058063
This commit is contained in:
Fabrice Di Meglio
2014-04-02 19:37:39 -07:00
parent a41707200b
commit dd41dfc483
3 changed files with 105 additions and 31 deletions

View File

@@ -61,14 +61,16 @@ public class Index {
public static final int COLUMN_INDEX_SUMMARY_ON = 2; public static final int COLUMN_INDEX_SUMMARY_ON = 2;
public static final int COLUMN_INDEX_SUMMARY_OFF = 3; public static final int COLUMN_INDEX_SUMMARY_OFF = 3;
public static final int COLUMN_INDEX_ENTRIES = 4; public static final int COLUMN_INDEX_ENTRIES = 4;
public static final int COLUMN_INDEX_KEYWORDS = 5; public static final int COLUMN_INDEX_SWITCH_ON = 5;
public static final int COLUMN_INDEX_CLASS_NAME = 6; public static final int COLUMN_INDEX_SWITCH_OFF = 6;
public static final int COLUMN_INDEX_SCREEN_TITLE = 7; public static final int COLUMN_INDEX_KEYWORDS = 7;
public static final int COLUMN_INDEX_ICON = 8; public static final int COLUMN_INDEX_CLASS_NAME = 8;
public static final int COLUMN_INDEX_INTENT_ACTION = 9; public static final int COLUMN_INDEX_SCREEN_TITLE = 9;
public static final int COLUMN_INDEX_INTENT_ACTION_TARGET_PACKAGE = 10; public static final int COLUMN_INDEX_ICON = 10;
public static final int COLUMN_INDEX_INTENT_ACTION_TARGET_CLASS = 11; public static final int COLUMN_INDEX_INTENT_ACTION = 11;
public static final int COLUMN_INDEX_ENABLED = 12; public static final int COLUMN_INDEX_INTENT_ACTION_TARGET_PACKAGE = 12;
public static final int COLUMN_INDEX_INTENT_ACTION_TARGET_CLASS = 13;
public static final int COLUMN_INDEX_ENABLED = 14;
// If you change the order of columns here, you SHOULD change the COLUMN_INDEX_XXX values // If you change the order of columns here, you SHOULD change the COLUMN_INDEX_XXX values
private static final String[] SELECT_COLUMNS = new String[] { private static final String[] SELECT_COLUMNS = new String[] {
@@ -77,13 +79,15 @@ public class Index {
IndexColumns.DATA_SUMMARY_ON, // 2 IndexColumns.DATA_SUMMARY_ON, // 2
IndexColumns.DATA_SUMMARY_OFF, // 3 IndexColumns.DATA_SUMMARY_OFF, // 3
IndexColumns.DATA_ENTRIES, // 4 IndexColumns.DATA_ENTRIES, // 4
IndexColumns.DATA_KEYWORDS, // 5 IndexColumns.DATA_SWITCH_ON, // 5
IndexColumns.CLASS_NAME, // 6 IndexColumns.DATA_SWITCH_OFF, // 6
IndexColumns.SCREEN_TITLE, // 7 IndexColumns.DATA_KEYWORDS, // 7
IndexColumns.ICON, // 8 IndexColumns.CLASS_NAME, // 8
IndexColumns.INTENT_ACTION, // 9 IndexColumns.SCREEN_TITLE, // 9
IndexColumns.INTENT_TARGET_PACKAGE, // 10 IndexColumns.ICON, // 10
IndexColumns.INTENT_TARGET_CLASS // 11 IndexColumns.INTENT_ACTION, // 11
IndexColumns.INTENT_TARGET_PACKAGE, // 12
IndexColumns.INTENT_TARGET_CLASS // 13
}; };
private static final String[] MATCH_COLUMNS = { private static final String[] MATCH_COLUMNS = {
@@ -93,6 +97,10 @@ public class Index {
IndexColumns.DATA_SUMMARY_ON_NORMALIZED, IndexColumns.DATA_SUMMARY_ON_NORMALIZED,
IndexColumns.DATA_SUMMARY_OFF, IndexColumns.DATA_SUMMARY_OFF,
IndexColumns.DATA_SUMMARY_OFF_NORMALIZED, IndexColumns.DATA_SUMMARY_OFF_NORMALIZED,
IndexColumns.DATA_SWITCH_ON,
IndexColumns.DATA_SWITCH_ON_NORMALIZED,
IndexColumns.DATA_SWITCH_OFF,
IndexColumns.DATA_SWITCH_OFF_NORMALIZED,
IndexColumns.DATA_ENTRIES, IndexColumns.DATA_ENTRIES,
IndexColumns.DATA_KEYWORDS IndexColumns.DATA_KEYWORDS
}; };
@@ -107,6 +115,7 @@ public class Index {
private static final String NODE_NAME_PREFERENCE_SCREEN = "PreferenceScreen"; private static final String NODE_NAME_PREFERENCE_SCREEN = "PreferenceScreen";
private static final String NODE_NAME_CHECK_BOX_PREFERENCE = "CheckBoxPreference"; private static final String NODE_NAME_CHECK_BOX_PREFERENCE = "CheckBoxPreference";
private static final String NODE_NAME_LIST_PREFERENCE = "ListPreference"; private static final String NODE_NAME_LIST_PREFERENCE = "ListPreference";
private static final String NODE_NAME_SWITCH_PREFERENCE = "SwitchPreference";
private static Index sInstance; private static Index sInstance;
private final AtomicBoolean mIsAvailable = new AtomicBoolean(false); private final AtomicBoolean mIsAvailable = new AtomicBoolean(false);
@@ -353,16 +362,18 @@ public class Index {
final String summaryOn = cursor.getString(2); final String summaryOn = cursor.getString(2);
final String summaryOff = cursor.getString(3); final String summaryOff = cursor.getString(3);
final String entries = cursor.getString(4); final String entries = cursor.getString(4);
final String keywords = cursor.getString(5); final String switchOn = cursor.getString(5);
final String switchOff = cursor.getString(6);
final String keywords = cursor.getString(7);
final String screenTitle = cursor.getString(6); final String screenTitle = cursor.getString(8);
final String className = cursor.getString(7); final String className = cursor.getString(9);
final int iconResId = cursor.getInt(8); final int iconResId = cursor.getInt(10);
final String action = cursor.getString(9); final String action = cursor.getString(11);
final String targetPackage = cursor.getString(10); final String targetPackage = cursor.getString(12);
final String targetClass = cursor.getString(11); final String targetClass = cursor.getString(13);
SearchIndexableRaw data = new SearchIndexableRaw(packageContext); SearchIndexableRaw data = new SearchIndexableRaw(packageContext);
data.rank = rank; data.rank = rank;
@@ -370,6 +381,8 @@ public class Index {
data.summaryOn = summaryOn; data.summaryOn = summaryOn;
data.summaryOff = summaryOff; data.summaryOff = summaryOff;
data.entries = entries; data.entries = entries;
data.switchOn = switchOn;
data.switchOff = switchOff;
data.keywords = keywords; data.keywords = keywords;
data.screenTitle = screenTitle; data.screenTitle = screenTitle;
data.className = className; data.className = className;
@@ -494,7 +507,7 @@ public class Index {
// Insert rows for the main PreferenceScreen node. Rewrite the data for removing // Insert rows for the main PreferenceScreen node. Rewrite the data for removing
// hyphens. // hyphens.
updateOneRowWithFilteredData(database, localeStr, title, summary, null, null, updateOneRowWithFilteredData(database, localeStr, title, summary, null, null,
fragmentName, screenTitle, iconResId, rank, keywords, null, null, fragmentName, screenTitle, iconResId, rank, keywords,
intentAction, intentTargetPackage, intentTargetClass, true); intentAction, intentTargetPackage, intentTargetClass, true);
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
@@ -512,20 +525,26 @@ public class Index {
summary = getDataSummary(context, attrs); summary = getDataSummary(context, attrs);
String entries = null; String entries = null;
String switchOn = null;
String switchOff = null;
if (nodeName.endsWith(NODE_NAME_LIST_PREFERENCE)) { if (nodeName.endsWith(NODE_NAME_LIST_PREFERENCE)) {
entries = getDataEntries(context, attrs); entries = getDataEntries(context, attrs);
} else if (nodeName.endsWith(NODE_NAME_SWITCH_PREFERENCE)) {
switchOn = getDataSwitchOn(context, attrs);
switchOff = getDataSwitchOff(context, attrs);
} }
// Insert rows for the child nodes of PreferenceScreen // Insert rows for the child nodes of PreferenceScreen
updateOneRowWithFilteredData(database, localeStr, title, summary, null, entries, updateOneRowWithFilteredData(database, localeStr, title, summary, null, entries,
fragmentName, screenTitle, iconResId, rank, keywords, switchOn, switchOff, fragmentName, screenTitle, iconResId, rank, keywords,
intentAction, intentTargetPackage, intentTargetClass, true); intentAction, intentTargetPackage, intentTargetClass, true);
} else if (nodeName.equals(NODE_NAME_CHECK_BOX_PREFERENCE)) { } else if (nodeName.equals(NODE_NAME_CHECK_BOX_PREFERENCE)) {
final String summaryOn = getDataSummaryOn(context, attrs); final String summaryOn = getDataSummaryOn(context, attrs);
final String summaryOff = getDataSummaryOff(context, attrs); final String summaryOff = getDataSummaryOff(context, attrs);
updateOneRowWithFilteredData(database, localeStr, title, summaryOn, summaryOff, updateOneRowWithFilteredData(database, localeStr, title, summaryOn, summaryOff,
null, fragmentName, screenTitle, iconResId, rank, keywords, null, null, null, fragmentName, screenTitle, iconResId, rank, keywords,
intentAction, intentTargetPackage, intentTargetClass, true); intentAction, intentTargetPackage, intentTargetClass, true);
} }
} }
@@ -551,6 +570,8 @@ public class Index {
raw.summaryOn, raw.summaryOn,
raw.summaryOff, raw.summaryOff,
raw.entries, raw.entries,
raw.switchOn,
raw.switchOff,
raw.className, raw.className,
raw.screenTitle, raw.screenTitle,
raw.iconResId, raw.iconResId,
@@ -588,6 +609,8 @@ public class Index {
raw.summaryOn, raw.summaryOn,
raw.summaryOff, raw.summaryOff,
raw.entries, raw.entries,
raw.switchOn,
raw.switchOff,
sir.className, sir.className,
raw.screenTitle, raw.screenTitle,
sir.iconResId, sir.iconResId,
@@ -630,7 +653,8 @@ public class Index {
} }
private void updateOneRowWithFilteredData(SQLiteDatabase database, String locale, private void updateOneRowWithFilteredData(SQLiteDatabase database, String locale,
String title, String summaryOn, String summaryOff, String entries, String className, String title, String summaryOn, String summaryOff, String entries,
String switchOn, String switchOff, String className,
String screenTitle, int iconResId, int rank, String keywords, String screenTitle, int iconResId, int rank, String keywords,
String intentAction, String intentTargetPackage, String intentTargetClass, String intentAction, String intentTargetPackage, String intentTargetClass,
boolean enabled) { boolean enabled) {
@@ -657,21 +681,41 @@ public class Index {
updatedSummaryOff = EMPTY; updatedSummaryOff = EMPTY;
} }
String updatedSwitchOn;
if (switchOn != null) {
updatedSwitchOn = switchOn.replaceAll(NON_BREAKING_HYPHEN, HYPHEN);
} else {
updatedSwitchOn = EMPTY;
}
String updatedSwitchOff;
if (switchOff != null) {
updatedSwitchOff = switchOff.replaceAll(NON_BREAKING_HYPHEN, HYPHEN);
} else {
updatedSwitchOff = EMPTY;
}
String normalizedTitle = updatedTitle.replaceAll(HYPHEN, EMPTY); String normalizedTitle = updatedTitle.replaceAll(HYPHEN, EMPTY);
String normalizedSummaryOn = updatedSummaryOn.replaceAll(HYPHEN, EMPTY); String normalizedSummaryOn = updatedSummaryOn.replaceAll(HYPHEN, EMPTY);
String normalizedSummaryOff = updatedSummaryOff.replaceAll(HYPHEN, EMPTY); String normalizedSummaryOff = updatedSummaryOff.replaceAll(HYPHEN, EMPTY);
String normalizedSwitchOn = updatedSwitchOn.replaceAll(HYPHEN, EMPTY);
String normalizedSwitchOff = updatedSwitchOff.replaceAll(HYPHEN, EMPTY);
updateOneRow(database, locale, updateOneRow(database, locale,
updatedTitle, normalizedTitle, updatedSummaryOn, normalizedSummaryOn, updatedTitle, normalizedTitle, updatedSummaryOn, normalizedSummaryOn,
updatedSummaryOff, normalizedSummaryOff, entries, className, screenTitle, iconResId, updatedSummaryOff, normalizedSummaryOff, entries,
updatedSwitchOn, normalizedSwitchOn, updatedSwitchOff, normalizedSwitchOff,
className, screenTitle, iconResId,
rank, keywords, intentAction, intentTargetPackage, intentTargetClass, enabled); rank, keywords, intentAction, intentTargetPackage, intentTargetClass, enabled);
} }
private void updateOneRow(SQLiteDatabase database, String locale, private void updateOneRow(SQLiteDatabase database, String locale,
String updatedTitle, String normalizedTitle, String updatedTitle, String normalizedTitle,
String updatedSummaryOn, String normalizedSummaryOn, String updatedSummaryOn, String normalizedSummaryOn,
String updatedSummaryOff, String normalizedSummaryOff, String entries, String className, String updatedSummaryOff, String normalizedSummaryOff, String entries,
String screenTitle, int iconResId, int rank, String keywords, String updatedSwitchOn, String normalizedSwitchOn,
String updatedSwitchOff, String normalizedSwitchOff,
String className, String screenTitle, int iconResId, int rank, String keywords,
String intentAction, String intentTargetPackage, String intentTargetClass, String intentAction, String intentTargetPackage, String intentTargetClass,
boolean enabled) { boolean enabled) {
@@ -690,6 +734,10 @@ public class Index {
values.put(IndexColumns.DATA_SUMMARY_OFF, updatedSummaryOff); values.put(IndexColumns.DATA_SUMMARY_OFF, updatedSummaryOff);
values.put(IndexColumns.DATA_SUMMARY_OFF_NORMALIZED, normalizedSummaryOff); values.put(IndexColumns.DATA_SUMMARY_OFF_NORMALIZED, normalizedSummaryOff);
values.put(IndexColumns.DATA_ENTRIES, entries); values.put(IndexColumns.DATA_ENTRIES, entries);
values.put(IndexColumns.DATA_SWITCH_ON, updatedSwitchOn);
values.put(IndexColumns.DATA_SWITCH_ON_NORMALIZED, normalizedSwitchOn);
values.put(IndexColumns.DATA_SWITCH_OFF, updatedSwitchOff);
values.put(IndexColumns.DATA_SWITCH_OFF_NORMALIZED, normalizedSwitchOff);
values.put(IndexColumns.DATA_KEYWORDS, keywords); values.put(IndexColumns.DATA_KEYWORDS, keywords);
values.put(IndexColumns.CLASS_NAME, className); values.put(IndexColumns.CLASS_NAME, className);
values.put(IndexColumns.SCREEN_TITLE, screenTitle); values.put(IndexColumns.SCREEN_TITLE, screenTitle);
@@ -732,6 +780,18 @@ public class Index {
com.android.internal.R.styleable.ListPreference_entries); com.android.internal.R.styleable.ListPreference_entries);
} }
private String getDataSwitchOn(Context context, AttributeSet attrs) {
return getData(context, attrs,
com.android.internal.R.styleable.SwitchPreference,
com.android.internal.R.styleable.SwitchPreference_switchTextOn);
}
private String getDataSwitchOff(Context context, AttributeSet attrs) {
return getData(context, attrs,
com.android.internal.R.styleable.SwitchPreference,
com.android.internal.R.styleable.SwitchPreference_switchTextOff);
}
private String getDataKeywords(Context context, AttributeSet attrs) { private String getDataKeywords(Context context, AttributeSet attrs) {
return getData(context, attrs, R.styleable.Preference, R.styleable.Preference_keywords); return getData(context, attrs, R.styleable.Preference, R.styleable.Preference_keywords);
} }

View File

@@ -28,7 +28,7 @@ public class IndexDatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "IndexDatabaseHelper"; private static final String TAG = "IndexDatabaseHelper";
private static final String DATABASE_NAME = "search_index.db"; private static final String DATABASE_NAME = "search_index.db";
private static final int DATABASE_VERSION = 105; private static final int DATABASE_VERSION = 106;
public interface Tables { public interface Tables {
public static final String TABLE_PREFS_INDEX = "prefs_index"; public static final String TABLE_PREFS_INDEX = "prefs_index";
@@ -46,6 +46,10 @@ public class IndexDatabaseHelper extends SQLiteOpenHelper {
public static final String DATA_SUMMARY_OFF = "data_summary_off"; public static final String DATA_SUMMARY_OFF = "data_summary_off";
public static final String DATA_SUMMARY_OFF_NORMALIZED = "data_summary_off_normalized"; public static final String DATA_SUMMARY_OFF_NORMALIZED = "data_summary_off_normalized";
public static final String DATA_ENTRIES = "data_entries"; public static final String DATA_ENTRIES = "data_entries";
public static final String DATA_SWITCH_ON = "data_switch_on";
public static final String DATA_SWITCH_ON_NORMALIZED = "data_switch_on_normalized";
public static final String DATA_SWITCH_OFF = "data_switch_off";
public static final String DATA_SWITCH_OFF_NORMALIZED = "data_switch_off_normalized";
public static final String DATA_KEYWORDS = "data_keywords"; public static final String DATA_KEYWORDS = "data_keywords";
public static final String CLASS_NAME = "class_name"; public static final String CLASS_NAME = "class_name";
public static final String SCREEN_TITLE = "screen_title"; public static final String SCREEN_TITLE = "screen_title";
@@ -81,6 +85,14 @@ public class IndexDatabaseHelper extends SQLiteOpenHelper {
", " + ", " +
IndexColumns.DATA_ENTRIES + IndexColumns.DATA_ENTRIES +
", " + ", " +
IndexColumns.DATA_SWITCH_ON +
", " +
IndexColumns.DATA_SWITCH_ON_NORMALIZED +
", " +
IndexColumns.DATA_SWITCH_OFF +
", " +
IndexColumns.DATA_SWITCH_OFF_NORMALIZED +
", " +
IndexColumns.DATA_KEYWORDS + IndexColumns.DATA_KEYWORDS +
", " + ", " +
IndexColumns.SCREEN_TITLE + IndexColumns.SCREEN_TITLE +
@@ -153,7 +165,7 @@ public class IndexDatabaseHelper extends SQLiteOpenHelper {
@Override @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 100 || oldVersion == 101 || oldVersion == 102 || oldVersion == 103) { if (oldVersion < DATABASE_VERSION) {
Log.w(TAG, "Detected schema version '" + oldVersion + "'. " + Log.w(TAG, "Detected schema version '" + oldVersion + "'. " +
"Index needs to be rebuilt for schema version '" + newVersion + "'."); "Index needs to be rebuilt for schema version '" + newVersion + "'.");
// We need to drop the tables and recreate them // We need to drop the tables and recreate them

View File

@@ -32,6 +32,8 @@ public class SearchIndexableRaw extends SearchIndexableData {
public String summaryOn; public String summaryOn;
public String summaryOff; public String summaryOff;
public String entries; public String entries;
public String switchOn;
public String switchOff;
public String keywords; public String keywords;
public String screenTitle; public String screenTitle;