Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 543240cb authored by Jason Chiu's avatar Jason Chiu
Browse files

Store authority with indexables so that indexables could be mapped to non-indexable keys

Test: manual
Bug: 113949972
Change-Id: Ia3863b1c45694127a1651e6c27326a47b9043bdd
parent b9c694ee
Loading
Loading
Loading
Loading
+16 −14
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settings.intelligence.search.indexing;

import static com.android.settings.intelligence.search.query.DatabaseResultTask.SELECT_COLUMNS;
import static com.android.settings.intelligence.search.indexing.IndexDatabaseHelper.IndexColumns.DATA_AUTHORITY;
import static com.android.settings.intelligence.search.indexing.IndexDatabaseHelper.IndexColumns.DATA_PACKAGE;
import static com.android.settings.intelligence.search.indexing.IndexDatabaseHelper.IndexColumns.CLASS_NAME;
import static com.android.settings.intelligence.search.indexing.IndexDatabaseHelper.IndexColumns.DATA_ENTRIES;
@@ -220,6 +221,7 @@ public class DatabaseIndexingManager {
            values.put(DATA_ENTRIES, dataRow.entries);
            values.put(DATA_KEYWORDS, dataRow.spaceDelimitedKeywords);
            values.put(DATA_PACKAGE, dataRow.packageName);
            values.put(DATA_AUTHORITY, dataRow.authority);
            values.put(CLASS_NAME, dataRow.className);
            values.put(SCREEN_TITLE, dataRow.screenTitle);
            values.put(INTENT_ACTION, dataRow.intentAction);
@@ -241,7 +243,7 @@ public class DatabaseIndexingManager {
     * All rows which are disabled but no longer a non-indexable key will become enabled.
     *
     * @param database         The database to validate.
     * @param nonIndexableKeys A map between package name and the set of non-indexable keys for it.
     * @param nonIndexableKeys A map between authority and the set of non-indexable keys for it.
     */
    @VisibleForTesting
    void updateDataInDatabase(SQLiteDatabase database,
@@ -255,17 +257,17 @@ public class DatabaseIndexingManager {
        final ContentValues enabledToDisabledValue = new ContentValues();
        enabledToDisabledValue.put(ENABLED, 0);

        String packageName;
        String authority;
        // TODO Refactor: Move these two loops into one method.
        while (enabledResults.moveToNext()) {
            packageName = enabledResults.getString(enabledResults.getColumnIndexOrThrow(
                    IndexDatabaseHelper.IndexColumns.DATA_PACKAGE));
            authority = enabledResults.getString(enabledResults.getColumnIndexOrThrow(
                    DATA_AUTHORITY));
            final String key = enabledResults.getString(enabledResults.getColumnIndexOrThrow(
                    IndexDatabaseHelper.IndexColumns.DATA_KEY_REF));
            final Set<String> packageKeys = nonIndexableKeys.get(packageName);
                    DATA_KEY_REF));
            final Set<String> authorityKeys = nonIndexableKeys.get(authority);

            // The indexed item is set to Enabled but is now non-indexable
            if (packageKeys != null && packageKeys.contains(key)) {
            if (authorityKeys != null && authorityKeys.contains(key)) {
                final String whereClause = getKeyWhereClause(key);
                database.update(TABLE_PREFS_INDEX, enabledToDisabledValue, whereClause, null);
            }
@@ -279,17 +281,17 @@ public class DatabaseIndexingManager {
        disabledToEnabledValue.put(ENABLED, 1);

        while (disabledResults.moveToNext()) {
            packageName = disabledResults.getString(disabledResults.getColumnIndexOrThrow(
                    IndexDatabaseHelper.IndexColumns.DATA_PACKAGE));
            authority = disabledResults.getString(disabledResults.getColumnIndexOrThrow(
                    DATA_AUTHORITY));

            final String key = disabledResults.getString(disabledResults.getColumnIndexOrThrow(
                    IndexDatabaseHelper.IndexColumns.DATA_KEY_REF));
            final Set<String> packageKeys = nonIndexableKeys.get(packageName);
                    DATA_KEY_REF));
            final Set<String> authorityKeys = nonIndexableKeys.get(authority);

            // The indexed item is set to Disabled but is no longer non-indexable.
            // We do not enable keys when packageKeys is null because it means the keys came
            // from an unrecognized package and therefore should not be surfaced as results.
            if (packageKeys != null && !packageKeys.contains(key)) {
            // We do not enable keys when authorityKeys is null because it means the keys came
            // from an unrecognized authority and therefore should not be surfaced as results.
            if (authorityKeys != null && !authorityKeys.contains(key)) {
                final String whereClause = getKeyWhereClause(key);
                database.update(TABLE_PREFS_INDEX, disabledToEnabledValue, whereClause, null);
            }
+8 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ public class IndexData {
     * @see SearchIndexableData#iconResId
     */
    public final String packageName;
    public final String authority;
    public final String locale;
    public final String updatedTitle;
    public final String normalizedTitle;
@@ -87,6 +88,7 @@ public class IndexData {
        spaceDelimitedKeywords = normalizeKeywords(builder.mKeywords);
        intentAction = builder.mIntentAction;
        packageName = builder.mPackageName;
        authority = builder.mAuthority;
        intentTargetPackage = builder.mIntentTargetPackage;
        intentTargetClass = builder.mIntentTargetClass;
        enabled = builder.mEnabled;
@@ -155,6 +157,7 @@ public class IndexData {
        private String mChildClassName;
        private String mScreenTitle;
        private String mPackageName;
        private String mAuthority;
        private int mIconResId;
        private String mKeywords;
        private String mIntentAction;
@@ -213,6 +216,11 @@ public class IndexData {
            return this;
        }

        public Builder setAuthority(String authority) {
            mAuthority = authority;
            return this;
        }

        public Builder setIconResId(int iconResId) {
            mIconResId = iconResId;
            return this;
+32 −24
Original line number Diff line number Diff line
@@ -20,11 +20,11 @@ package com.android.settings.intelligence.search.indexing;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.AsyncTask;
import android.provider.SearchIndexableData;
import android.provider.SearchIndexableResource;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.collection.ArraySet;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
@@ -42,7 +42,6 @@ import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -74,27 +73,34 @@ public class IndexDataConverter {
     */
    public List<IndexData> convertPreIndexDataToIndexData(PreIndexData preIndexData) {
        final long startConversion = System.currentTimeMillis();
        final List<SearchIndexableData> indexableData = preIndexData.getDataToUpdate();
        final Map<String, List<SearchIndexableData>> indexableDataMap =
                preIndexData.getDataToUpdate();
        final Map<String, Set<String>> nonIndexableKeys = preIndexData.getNonIndexableKeys();
        final List<IndexData> indexData = new ArrayList<>();

        for (Map.Entry<String, List<SearchIndexableData>> entry : indexableDataMap.entrySet()) {
            final String authority = entry.getKey();
            final List<SearchIndexableData> indexableData = entry.getValue();

            for (SearchIndexableData data : indexableData) {
                if (data instanceof SearchIndexableRaw) {
                    final SearchIndexableRaw rawData = (SearchIndexableRaw) data;
                final Set<String> rawNonIndexableKeys = nonIndexableKeys.get(
                        rawData.intentTargetPackage);
                final IndexData convertedRaw = convertRaw(mContext, rawData, rawNonIndexableKeys);
                    final Set<String> rawNonIndexableKeys = nonIndexableKeys.get(authority);
                    final IndexData convertedRaw = convertRaw(mContext, authority, rawData,
                            rawNonIndexableKeys);
                    if (convertedRaw != null) {
                        indexData.add(convertedRaw);
                    }
                } else if (data instanceof SearchIndexableResource) {
                    final SearchIndexableResource sir = (SearchIndexableResource) data;
                    final Set<String> resourceNonIndexableKeys =
                        getNonIndexableKeysForResource(nonIndexableKeys, sir.packageName);
                final List<IndexData> resourceData = convertResource(sir, resourceNonIndexableKeys);
                            getNonIndexableKeysForResource(nonIndexableKeys, authority);
                    final List<IndexData> resourceData = convertResource(sir, authority,
                            resourceNonIndexableKeys);
                    indexData.addAll(resourceData);
                }
            }
        }

        final long endConversion = System.currentTimeMillis();
        Log.d(TAG, "Converting pre-index data to index data took: "
@@ -150,10 +156,10 @@ public class IndexDataConverter {
     * and there is some data sanitization in the conversion.
     */
    @Nullable
    private IndexData convertRaw(Context context, SearchIndexableRaw raw,
    private IndexData convertRaw(Context context, String authority, SearchIndexableRaw raw,
            Set<String> nonIndexableKeys) {
        if (TextUtils.isEmpty(raw.key)) {
            Log.w(TAG, "Skipping null key for raw indexable " + raw.packageName + "/" + raw.title);
            Log.w(TAG, "Skipping null key for raw indexable " + authority + "/" + raw.title);
            return null;
        }
        // A row is enabled if it does not show up as an nonIndexableKey
@@ -172,6 +178,7 @@ public class IndexDataConverter {
                .setIntentTargetClass(raw.intentTargetClass)
                .setEnabled(enabled)
                .setPackageName(raw.packageName)
                .setAuthority(authority)
                .setKey(raw.key);

        return builder.build(context);
@@ -184,7 +191,7 @@ public class IndexDataConverter {
     *
     * TODO (b/33577327) simplify this method.
     */
    private List<IndexData> convertResource(SearchIndexableResource sir,
    private List<IndexData> convertResource(SearchIndexableResource sir, String authority,
            Set<String> nonIndexableKeys) {
        final Context context = sir.context;
        XmlResourceParser parser = null;
@@ -244,6 +251,7 @@ public class IndexDataConverter {
                    .setKeywords(headerKeywords)
                    .setClassName(sir.className)
                    .setPackageName(sir.packageName)
                    .setAuthority(authority)
                    .setIntentAction(sir.intentAction)
                    .setIntentTargetPackage(sir.intentTargetPackage)
                    .setIntentTargetClass(sir.intentTargetClass)
@@ -285,6 +293,7 @@ public class IndexDataConverter {
                        .setScreenTitle(screenTitle)
                        .setIconResId(iconResId)
                        .setPackageName(sir.packageName)
                        .setAuthority(authority)
                        .setIntentAction(sir.intentAction)
                        .setIntentTargetPackage(sir.intentTargetPackage)
                        .setIntentTargetClass(sir.intentTargetClass)
@@ -352,9 +361,8 @@ public class IndexDataConverter {
    }

    private Set<String> getNonIndexableKeysForResource(Map<String, Set<String>> nonIndexableKeys,
            String packageName) {
        return nonIndexableKeys.containsKey(packageName)
                ? nonIndexableKeys.get(packageName)
                : new HashSet<String>();
            String authority) {
        final Set<String> result = nonIndexableKeys.get(authority);
        return result != null ? result : new ArraySet<>();
    }
}
+4 −1
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ public class IndexDatabaseHelper extends SQLiteOpenHelper {
    private static final String TAG = "IndexDatabaseHelper";

    private static final String DATABASE_NAME = "search_index.db";
    private static final int DATABASE_VERSION = 119;
    private static final int DATABASE_VERSION = 120;

    @VisibleForTesting
    static final String SHARED_PREFS_TAG = "indexing_manager";
@@ -61,6 +61,7 @@ public class IndexDatabaseHelper extends SQLiteOpenHelper {
        String DATA_ENTRIES = "data_entries";
        String DATA_KEYWORDS = "data_keywords";
        String DATA_PACKAGE = "package";
        String DATA_AUTHORITY = "authority";
        String CLASS_NAME = "class_name";
        String SCREEN_TITLE = "screen_title";
        String INTENT_ACTION = "intent_action";
@@ -111,6 +112,8 @@ public class IndexDatabaseHelper extends SQLiteOpenHelper {
                    ", " +
                    IndexColumns.DATA_PACKAGE +
                    ", " +
                    IndexColumns.DATA_AUTHORITY +
                    ", " +
                    IndexColumns.SCREEN_TITLE +
                    ", " +
                    IndexColumns.CLASS_NAME +
+14 −5
Original line number Diff line number Diff line
@@ -32,12 +32,12 @@ import java.util.Set;
 */
public class PreIndexData {

    private final List<SearchIndexableData> mDataToUpdate;
    private final Map<String, List<SearchIndexableData>> mDataToUpdate;
    private final Map<String, Set<String>> mNonIndexableKeys;
    private final List<Pair<String, String>> mSiteMapPairs;

    public PreIndexData() {
        mDataToUpdate = new ArrayList<>();
        mDataToUpdate = new HashMap<>();
        mNonIndexableKeys = new HashMap<>();
        mSiteMapPairs = new ArrayList<>();
    }
@@ -46,7 +46,7 @@ public class PreIndexData {
        return mNonIndexableKeys;
    }

    public List<SearchIndexableData> getDataToUpdate() {
    public Map<String, List<SearchIndexableData>> getDataToUpdate() {
        return mDataToUpdate;
    }

@@ -58,8 +58,17 @@ public class PreIndexData {
        mNonIndexableKeys.put(authority, keys);
    }

    public void addDataToUpdate(List<? extends SearchIndexableData> data) {
        mDataToUpdate.addAll(data);
    public void addDataToUpdate(String authority, List<? extends SearchIndexableData> data) {
        if (data.isEmpty()) {
            return;
        }

        List<SearchIndexableData> indexableData = mDataToUpdate.get(authority);
        if (indexableData == null) {
            mDataToUpdate.put(authority, new ArrayList<>(data));
        } else {
            indexableData.addAll(data);
        }
    }

    public void addSiteMapPairs(List<Pair<String, String>> siteMapPairs) {
Loading