Merge ""Fix" search" into nyc-dev

This commit is contained in:
Jason Monk
2016-04-15 16:06:47 +00:00
committed by Android (Google) Code Review
2 changed files with 60 additions and 55 deletions

View File

@@ -564,12 +564,7 @@ public class SettingsActivity extends SettingsDrawerActivity
// Run the Index update only if we have some space // Run the Index update only if we have some space
if (!Utils.isLowStorage(this)) { if (!Utils.isLowStorage(this)) {
long indexStartTime = System.currentTimeMillis(); long indexStartTime = System.currentTimeMillis();
AsyncTask.execute(new Runnable() {
@Override
public void run() {
Index.getInstance(getApplicationContext()).update(); Index.getInstance(getApplicationContext()).update();
}
});
if (DEBUG_TIMING) Log.d(LOG_TAG, "Index.update() took " if (DEBUG_TIMING) Log.d(LOG_TAG, "Index.update() took "
+ (System.currentTimeMillis() - indexStartTime) + " ms"); + (System.currentTimeMillis() - indexStartTime) + " ms");
} else { } else {

View File

@@ -140,6 +140,7 @@ public class Index {
IndexColumns.DATA_SUMMARY_OFF_NORMALIZED, IndexColumns.DATA_SUMMARY_OFF_NORMALIZED,
IndexColumns.DATA_ENTRIES IndexColumns.DATA_ENTRIES
}; };
private static final String INDEX = "index";
// Max number of saved search queries (who will be used for proposing suggestions) // Max number of saved search queries (who will be used for proposing suggestions)
private static long MAX_SAVED_SEARCH_QUERY = 64; private static long MAX_SAVED_SEARCH_QUERY = 64;
@@ -177,6 +178,7 @@ public class Index {
public Map<String, List<String>> nonIndexableKeys; public Map<String, List<String>> nonIndexableKeys;
public boolean forceUpdate = false; public boolean forceUpdate = false;
public boolean fullIndex = true;
public UpdateData() { public UpdateData() {
dataToUpdate = new ArrayList<SearchIndexableData>(); dataToUpdate = new ArrayList<SearchIndexableData>();
@@ -189,6 +191,7 @@ public class Index {
dataToDelete = new ArrayList<SearchIndexableData>(other.dataToDelete); dataToDelete = new ArrayList<SearchIndexableData>(other.dataToDelete);
nonIndexableKeys = new HashMap<String, List<String>>(other.nonIndexableKeys); nonIndexableKeys = new HashMap<String, List<String>>(other.nonIndexableKeys);
forceUpdate = other.forceUpdate; forceUpdate = other.forceUpdate;
fullIndex = other.fullIndex;
} }
public UpdateData copy() { public UpdateData copy() {
@@ -200,6 +203,7 @@ public class Index {
dataToDelete.clear(); dataToDelete.clear();
nonIndexableKeys.clear(); nonIndexableKeys.clear();
forceUpdate = false; forceUpdate = false;
fullIndex = false;
} }
} }
@@ -213,9 +217,7 @@ public class Index {
*/ */
public static Index getInstance(Context context) { public static Index getInstance(Context context) {
if (sInstance == null) { if (sInstance == null) {
sInstance = new Index(context, BASE_AUTHORITY); sInstance = new Index(context.getApplicationContext(), BASE_AUTHORITY);
} else {
sInstance.setContext(context);
} }
return sInstance; return sInstance;
} }
@@ -301,6 +303,9 @@ public class Index {
} }
public void update() { public void update() {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
final Intent intent = new Intent(SearchIndexablesContract.PROVIDER_INTERFACE); final Intent intent = new Intent(SearchIndexablesContract.PROVIDER_INTERFACE);
List<ResolveInfo> list = List<ResolveInfo> list =
mContext.getPackageManager().queryIntentContentProviders(intent, 0); mContext.getPackageManager().queryIntentContentProviders(intent, 0);
@@ -318,8 +323,11 @@ public class Index {
addNonIndexablesKeysFromRemoteProvider(packageName, authority); addNonIndexablesKeysFromRemoteProvider(packageName, authority);
} }
mDataToProcess.fullIndex = true;
updateInternal(); updateInternal();
} }
});
}
private boolean addIndexablesFromRemoteProvider(String packageName, String authority) { private boolean addIndexablesFromRemoteProvider(String packageName, String authority) {
try { try {
@@ -472,7 +480,7 @@ public class Index {
* @param includeInSearchResults true means that you want the bit "enabled" set so that the * @param includeInSearchResults true means that you want the bit "enabled" set so that the
* data will be seen included into the search results * data will be seen included into the search results
*/ */
public void updateFromClassNameResource(String className, boolean rebuild, public void updateFromClassNameResource(String className, final boolean rebuild,
boolean includeInSearchResults) { boolean includeInSearchResults) {
if (className == null) { if (className == null) {
throw new IllegalArgumentException("class name cannot be null!"); throw new IllegalArgumentException("class name cannot be null!");
@@ -484,6 +492,9 @@ public class Index {
} }
res.context = mContext; res.context = mContext;
res.enabled = includeInSearchResults; res.enabled = includeInSearchResults;
AsyncTask.execute(new Runnable() {
@Override
public void run() {
if (rebuild) { if (rebuild) {
deleteIndexableData(res); deleteIndexableData(res);
} }
@@ -492,12 +503,19 @@ public class Index {
updateInternal(); updateInternal();
res.enabled = false; res.enabled = false;
} }
});
}
public void updateFromSearchIndexableData(SearchIndexableData data) { public void updateFromSearchIndexableData(SearchIndexableData data) {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
addIndexableData(data); addIndexableData(data);
mDataToProcess.forceUpdate = true; mDataToProcess.forceUpdate = true;
updateInternal(); updateInternal();
} }
});
}
private SQLiteDatabase getReadableDatabase() { private SQLiteDatabase getReadableDatabase() {
return IndexDatabaseHelper.getInstance(mContext).getReadableDatabase(); return IndexDatabaseHelper.getInstance(mContext).getReadableDatabase();
@@ -1182,6 +1200,7 @@ public class Index {
final Map<String, List<String>> nonIndexableKeys = params[0].nonIndexableKeys; final Map<String, List<String>> nonIndexableKeys = params[0].nonIndexableKeys;
final boolean forceUpdate = params[0].forceUpdate; final boolean forceUpdate = params[0].forceUpdate;
final boolean fullIndex = params[0].fullIndex;
final SQLiteDatabase database = getWritableDatabase(); final SQLiteDatabase database = getWritableDatabase();
if (database == null) { if (database == null) {
@@ -1203,6 +1222,9 @@ public class Index {
} finally { } finally {
database.endTransaction(); database.endTransaction();
} }
if (fullIndex) {
setLocaleIndexed(localeStr);
}
return null; return null;
} }
@@ -1225,8 +1247,8 @@ public class Index {
try { try {
indexOneSearchIndexableData(database, localeStr, data, nonIndexableKeys); indexOneSearchIndexableData(database, localeStr, data, nonIndexableKeys);
} catch (Exception e) { } catch (Exception e) {
Log.e(LOG_TAG, Log.e(LOG_TAG, "Cannot index: " + (data != null ? data.className : data)
"Cannot index: " + data.className + " for locale: " + localeStr, e); + " for locale: " + localeStr, e);
} }
} }
@@ -1273,24 +1295,12 @@ public class Index {
return database.delete(Tables.TABLE_PREFS_INDEX, whereClause, whereArgs); return database.delete(Tables.TABLE_PREFS_INDEX, whereClause, whereArgs);
} }
private void setLocaleIndexed(String locale) {
mContext.getSharedPreferences(INDEX, 0).edit().putBoolean(locale, true).commit();
}
private boolean isLocaleAlreadyIndexed(SQLiteDatabase database, String locale) { private boolean isLocaleAlreadyIndexed(SQLiteDatabase database, String locale) {
Cursor cursor = null; return mContext.getSharedPreferences(INDEX, 0).getBoolean(locale, false);
boolean result = false;
final StringBuilder sb = new StringBuilder(IndexColumns.LOCALE);
sb.append(" = ");
DatabaseUtils.appendEscapedSQLString(sb, locale);
try {
// We care only for 1 row
cursor = database.query(Tables.TABLE_PREFS_INDEX, null,
sb.toString(), null, null, null, null, "1");
final int count = cursor.getCount();
result = (count >= 1);
} finally {
if (cursor != null) {
cursor.close();
}
}
return result;
} }
} }