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

Commit 4169ac2f authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Simplify IndexData builder"

parents b3576c9f 6bbc459f
Loading
Loading
Loading
Loading
+0 −32
Original line number Diff line number Diff line
@@ -17,16 +17,10 @@

package com.android.settings.search;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;

@@ -37,10 +31,8 @@ import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;

import java.lang.reflect.Field;
import java.text.Normalizer;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Utility class for {@like DatabaseIndexingManager} to handle the mapping between Payloads
@@ -53,15 +45,6 @@ public class DatabaseIndexingUtils {
    private static final String FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER =
            "SEARCH_INDEX_DATA_PROVIDER";

    private static final String NON_BREAKING_HYPHEN = "\u2011";
    private static final String EMPTY = "";
    private static final String LIST_DELIMITERS = "[,]\\s*";
    private static final String HYPHEN = "-";
    private static final String SPACE = " ";

    private static final Pattern REMOVE_DIACRITICALS_PATTERN
            = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");

    /**
     * Builds intent into a subsetting.
     */
@@ -173,19 +156,4 @@ public class DatabaseIndexingUtils {
        }
        return null;
    }

    public static String normalizeHyphen(String input) {
        return (input != null) ? input.replaceAll(NON_BREAKING_HYPHEN, HYPHEN) : EMPTY;
    }

    public static String normalizeString(String input) {
        final String nohyphen = (input != null) ? input.replaceAll(HYPHEN, EMPTY) : EMPTY;
        final String normalized = Normalizer.normalize(nohyphen, Normalizer.Form.NFD);

        return REMOVE_DIACRITICALS_PATTERN.matcher(normalized).replaceAll("").toLowerCase();
    }

    public static String normalizeKeywords(String input) {
        return (input != null) ? input.replaceAll(LIST_DELIMITERS, SPACE) : EMPTY;
    }
}
+61 −52
Original line number Diff line number Diff line
@@ -27,7 +27,9 @@ import com.android.settings.search.ResultPayload;
import com.android.settings.search.ResultPayloadUtils;
import com.android.settings.search.SearchIndexableResources;

import java.text.Normalizer;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * Data class representing a single row in the Setting Search results database.
@@ -38,14 +40,11 @@ public class IndexData {
    public final String normalizedTitle;
    public final String updatedSummaryOn;
    public final String normalizedSummaryOn;
    public final String updatedSummaryOff;
    public final String normalizedSummaryOff;
    public final String entries;
    public final String className;
    public final String childClassName;
    public final String screenTitle;
    public final int iconResId;
    public final int rank;
    public final String spaceDelimitedKeywords;
    public final String intentAction;
    public final String intentTargetPackage;
@@ -56,21 +55,28 @@ public class IndexData {
    public final int payloadType;
    public final byte[] payload;

    private static final String NON_BREAKING_HYPHEN = "\u2011";
    private static final String EMPTY = "";
    private static final String HYPHEN = "-";
    private static final String SPACE = " ";
    // Regex matching a comma, and any number of subsequent white spaces.
    private static final String LIST_DELIMITERS = "[,]\\s*";

    private static final Pattern REMOVE_DIACRITICALS_PATTERN
            = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");

    private IndexData(Builder builder) {
        locale = builder.mLocale;
        updatedTitle = builder.mUpdatedTitle;
        normalizedTitle = builder.mNormalizedTitle;
        updatedSummaryOn = builder.mUpdatedSummaryOn;
        normalizedSummaryOn = builder.mNormalizedSummaryOn;
        updatedSummaryOff = builder.mUpdatedSummaryOff;
        normalizedSummaryOff = builder.mNormalizedSummaryOff;
        updatedTitle = normalizeHyphen(builder.mTitle);
        normalizedTitle = normalizeString(builder.mTitle);
        updatedSummaryOn = normalizeHyphen(builder.mSummaryOn);
        normalizedSummaryOn = normalizeString(builder.mSummaryOn);
        entries = builder.mEntries;
        className = builder.mClassName;
        childClassName = builder.mChildClassName;
        screenTitle = builder.mScreenTitle;
        iconResId = builder.mIconResId;
        rank = builder.mRank;
        spaceDelimitedKeywords = builder.mSpaceDelimitedKeywords;
        spaceDelimitedKeywords = normalizeKeywords(builder.mKeywords);
        intentAction = builder.mIntentAction;
        intentTargetPackage = builder.mIntentTargetPackage;
        intentTargetClass = builder.mIntentTargetClass;
@@ -93,21 +99,49 @@ public class IndexData {
                : key.hashCode();
    }

    @Override
    public String toString() {
        return new StringBuilder(updatedTitle)
                .append(": ")
                .append(updatedSummaryOn)
                .toString();
    }

    /**
     * In the list of keywords, replace the comma and all subsequent whitespace with a single space.
     */
    public static String normalizeKeywords(String input) {
        return (input != null) ? input.replaceAll(LIST_DELIMITERS, SPACE) : EMPTY;
    }

    /**
     * @return {@param input} where all non-standard hyphens are replaced by normal hyphens.
     */
    public static String normalizeHyphen(String input) {
        return (input != null) ? input.replaceAll(NON_BREAKING_HYPHEN, HYPHEN) : EMPTY;
    }

    /**
     * @return {@param input} with all hyphens removed, and all letters lower case.
     */
    public static String normalizeString(String input) {
        final String normalizedHypen = normalizeHyphen(input);
        final String nohyphen = (input != null) ? normalizedHypen.replaceAll(HYPHEN, EMPTY) : EMPTY;
        final String normalized = Normalizer.normalize(nohyphen, Normalizer.Form.NFD);

        return REMOVE_DIACRITICALS_PATTERN.matcher(normalized).replaceAll("").toLowerCase();
    }

    public static class Builder {
        private String mLocale;
        private String mUpdatedTitle;
        private String mNormalizedTitle;
        private String mUpdatedSummaryOn;
        private String mNormalizedSummaryOn;
        private String mUpdatedSummaryOff;
        private String mNormalizedSummaryOff;
        private String mTitle;
        private String mSummaryOn;
        private String mEntries;
        private String mClassName;
        private String mChildClassName;
        private String mScreenTitle;
        private int mIconResId;
        private int mRank;
        private String mSpaceDelimitedKeywords;
        private String mKeywords;
        private String mIntentAction;
        private String mIntentTargetPackage;
        private String mIntentTargetClass;
@@ -123,33 +157,13 @@ public class IndexData {
            return this;
        }

        public Builder setUpdatedTitle(String updatedTitle) {
            mUpdatedTitle = updatedTitle;
            return this;
        }

        public Builder setNormalizedTitle(String normalizedTitle) {
            mNormalizedTitle = normalizedTitle;
            return this;
        }

        public Builder setUpdatedSummaryOn(String updatedSummaryOn) {
            mUpdatedSummaryOn = updatedSummaryOn;
            return this;
        }

        public Builder setNormalizedSummaryOn(String normalizedSummaryOn) {
            mNormalizedSummaryOn = normalizedSummaryOn;
            return this;
        }

        public Builder setUpdatedSummaryOff(String updatedSummaryOff) {
            mUpdatedSummaryOff = updatedSummaryOff;
        public Builder setTitle(String title) {
            mTitle = title;
            return this;
        }

        public Builder setNormalizedSummaryOff(String normalizedSummaryOff) {
            this.mNormalizedSummaryOff = normalizedSummaryOff;
        public Builder setSummaryOn(String summaryOn) {
            mSummaryOn = summaryOn;
            return this;
        }

@@ -178,13 +192,8 @@ public class IndexData {
            return this;
        }

        public Builder setRank(int rank) {
            mRank = rank;
            return this;
        }

        public Builder setSpaceDelimitedKeywords(String spaceDelimitedKeywords) {
            mSpaceDelimitedKeywords = spaceDelimitedKeywords;
        public Builder setKeywords(String keywords) {
            mKeywords = keywords;
            return this;
        }

@@ -260,8 +269,8 @@ public class IndexData {
            boolean isEmptyIntentAction = TextUtils.isEmpty(mIntentAction);
            // No intent action is set, or the intent action is for a subsetting.
            if (isEmptyIntentAction
                    || (!isEmptyIntentAction && TextUtils.equals(mIntentTargetPackage,
                    SearchIndexableResources.SUBSETTING_TARGET_PACKAGE))) {
                    || TextUtils.equals(mIntentTargetPackage,
                    SearchIndexableResources.SUBSETTING_TARGET_PACKAGE)) {
                // Action is null, we will launch it as a sub-setting
                intent = DatabaseIndexingUtils.buildSubsettingIntent(context, mClassName, mKey,
                        mScreenTitle);
+27 −49
Original line number Diff line number Diff line
@@ -147,12 +147,14 @@ public class IndexDataConverter {
        }

        IndexData.Builder builder = new IndexData.Builder();
        builder.setLocale(localeStr)
        builder.setTitle(raw.title)
                .setSummaryOn(raw.summaryOn)
                .setLocale(localeStr)
                .setEntries(raw.entries)
                .setKeywords(raw.keywords)
                .setClassName(raw.className)
                .setScreenTitle(raw.screenTitle)
                .setIconResId(raw.iconResId)
                .setRank(raw.rank)
                .setIntentAction(raw.intentAction)
                .setIntentTargetPackage(raw.intentTargetPackage)
                .setIntentTargetClass(raw.intentTargetClass)
@@ -160,8 +162,7 @@ public class IndexDataConverter {
                .setKey(raw.key)
                .setUserId(raw.userId);

        updateOneRowWithFilteredData(builder, raw.title, raw.summaryOn, raw.summaryOff,
                raw.keywords);
        updateOneRow(builder.build(mContext));
    }

    @VisibleForTesting
@@ -249,7 +250,6 @@ public class IndexDataConverter {
            ResultPayload payload;
            boolean enabled;
            final String fragmentName = sir.className;
            final int rank = sir.rank;
            final String intentAction = sir.intentAction;
            final String intentTargetPackage = sir.intentTargetPackage;
            final String intentTargetClass = sir.intentTargetClass;
@@ -271,11 +271,12 @@ public class IndexDataConverter {

            // TODO: Set payload type for header results
            IndexData.Builder headerBuilder = new IndexData.Builder();
            headerBuilder.setLocale(localeStr)
                    .setEntries(null)
            headerBuilder.setTitle(headerTitle)
                    .setSummaryOn(headerSummary)
                    .setKeywords(headerKeywords)
                    .setLocale(localeStr)
                    .setClassName(fragmentName)
                    .setScreenTitle(screenTitle)
                    .setRank(rank)
                    .setIntentAction(intentAction)
                    .setIntentTargetPackage(intentTargetPackage)
                    .setIntentTargetClass(intentTargetClass)
@@ -306,11 +307,12 @@ public class IndexDataConverter {
                }

                builder = new IndexData.Builder();
                builder.setLocale(localeStr)
                builder.setTitle(title)
                        .setLocale(localeStr)
                        .setKeywords(keywords)
                        .setClassName(fragmentName)
                        .setScreenTitle(screenTitle)
                        .setIconResId(iconResId)
                        .setRank(rank)
                        .setIntentAction(intentAction)
                        .setIntentTargetPackage(intentTargetPackage)
                        .setIntentTargetClass(intentTargetClass)
@@ -331,14 +333,17 @@ public class IndexDataConverter {
                    payload = DatabaseIndexingUtils.getPayloadFromUriMap(controllerUriMap, key);
                    childFragment = XmlParserUtils.getDataChildFragment(context, attrs);

                    builder.setEntries(entries)
                    builder.setSummaryOn(summary)
                            .setEntries(entries)
                            .setChildClassName(childFragment)
                            .setPayload(payload);

                    // Insert rows for the child nodes of PreferenceScreen
                    updateOneRowWithFilteredData(builder, title, summary,
                            null /* summary off */, keywords);
                    updateOneRow(builder.build(mContext));
                } else {
                    // TODO (b/33577327) We removed summary off here. We should check if we can
                    // merge this 'else' section with the one above. Put a break point to
                    // investigate.
                    String summaryOn = XmlParserUtils.getDataSummaryOn(context, attrs);
                    String summaryOff = XmlParserUtils.getDataSummaryOff(context, attrs);

@@ -346,15 +351,15 @@ public class IndexDataConverter {
                        summaryOn = XmlParserUtils.getDataSummary(context, attrs);
                    }

                    updateOneRowWithFilteredData(builder, title, summaryOn, summaryOff,
                            keywords);
                    builder.setSummaryOn(summaryOn);

                    updateOneRow(builder.build(mContext));
                }
            }

            // The xml header's title does not match the title of one of the child settings.
            if (isHeaderUnique) {
                updateOneRowWithFilteredData(headerBuilder, headerTitle, headerSummary,
                        null /* summary off */, headerKeywords);
                updateOneRow(headerBuilder.build(mContext));
            }
        } catch (XmlPullParserException e) {
            throw new RuntimeException("Error parsing PreferenceScreen", e);
@@ -394,8 +399,11 @@ public class IndexDataConverter {
                boolean enabled = !nonIndexableKeys.contains(raw.key);

                IndexData.Builder builder = new IndexData.Builder();
                builder.setLocale(localeStr)
                builder.setTitle(raw.title)
                        .setSummaryOn(raw.summaryOn)
                        .setLocale(localeStr)
                        .setEntries(raw.entries)
                        .setKeywords(raw.keywords)
                        .setClassName(className)
                        .setScreenTitle(raw.screenTitle)
                        .setIconResId(raw.iconResId)
@@ -406,8 +414,7 @@ public class IndexDataConverter {
                        .setKey(raw.key)
                        .setUserId(raw.userId);

                updateOneRowWithFilteredData(builder, raw.title, raw.summaryOn, raw.summaryOff,
                        raw.keywords);
                updateOneRow(builder.build(mContext));
            }
        }

@@ -438,32 +445,6 @@ public class IndexDataConverter {
        }
    }

    @VisibleForTesting
    void updateOneRowWithFilteredData(IndexData.Builder builder,
            String title, String summaryOn, String summaryOff, String keywords) {

        final String updatedTitle = DatabaseIndexingUtils.normalizeHyphen(title);
        final String updatedSummaryOn = DatabaseIndexingUtils.normalizeHyphen(summaryOn);
        final String updatedSummaryOff = DatabaseIndexingUtils.normalizeHyphen(summaryOff);

        final String normalizedTitle = DatabaseIndexingUtils.normalizeString(updatedTitle);
        final String normalizedSummaryOn = DatabaseIndexingUtils.normalizeString(updatedSummaryOn);
        final String normalizedSummaryOff = DatabaseIndexingUtils
                .normalizeString(updatedSummaryOff);

        final String spaceDelimitedKeywords = DatabaseIndexingUtils.normalizeKeywords(keywords);

        builder.setUpdatedTitle(updatedTitle)
                .setUpdatedSummaryOn(updatedSummaryOn)
                .setUpdatedSummaryOff(updatedSummaryOff)
                .setNormalizedTitle(normalizedTitle)
                .setNormalizedSummaryOn(normalizedSummaryOn)
                .setNormalizedSummaryOff(normalizedSummaryOff)
                .setSpaceDelimitedKeywords(spaceDelimitedKeywords);

        updateOneRow(builder.build(mContext));
    }

    private void updateOneRow(IndexData row) {
        if (TextUtils.isEmpty(row.updatedTitle)) {
            return;
@@ -472,13 +453,10 @@ public class IndexDataConverter {
        ContentValues values = new ContentValues();
        values.put(IndexDatabaseHelper.IndexColumns.DOCID, row.getDocId());
        values.put(LOCALE, row.locale);
        values.put(DATA_RANK, row.rank);
        values.put(DATA_TITLE, row.updatedTitle);
        values.put(DATA_TITLE_NORMALIZED, row.normalizedTitle);
        values.put(DATA_SUMMARY_ON, row.updatedSummaryOn);
        values.put(DATA_SUMMARY_ON_NORMALIZED, row.normalizedSummaryOn);
        values.put(DATA_SUMMARY_OFF, row.updatedSummaryOff);
        values.put(DATA_SUMMARY_OFF_NORMALIZED, row.normalizedSummaryOff);
        values.put(DATA_ENTRIES, row.entries);
        values.put(DATA_KEYWORDS, row.spaceDelimitedKeywords);
        values.put(CLASS_NAME, row.className);
+1 −7
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import android.content.ContentValues;
@@ -287,6 +286,7 @@ public class DatabaseIndexingManagerTest {
    @Test
    public void testLocaleUpdated_afterFullIndexing_localeAdded() {
        mManager.performIndexing();

        assertThat(IndexDatabaseHelper.isLocaleAlreadyIndexed(mContext, localeStr)).isTrue();
    }

@@ -302,8 +302,6 @@ public class DatabaseIndexingManagerTest {

        // Locale
        assertThat(cursor.getString(0)).isEqualTo(localeStr);
        // Data Rank
        assertThat(cursor.getInt(1)).isEqualTo(rank);
        // Data Title
        assertThat(cursor.getString(2)).isEqualTo(updatedTitle);
        // Normalized Title
@@ -312,10 +310,6 @@ public class DatabaseIndexingManagerTest {
        assertThat(cursor.getString(4)).isEqualTo(updatedSummaryOn);
        // Summary On Normalized
        assertThat(cursor.getString(5)).isEqualTo(normalizedSummaryOn);
        // Summary Off
        assertThat(cursor.getString(6)).isEqualTo(updatedSummaryOff);
        // Summary off normalized
        assertThat(cursor.getString(7)).isEqualTo(normalizedSummaryOff);
        // Entries
        assertThat(cursor.getString(8)).isEqualTo(entries);
        // Keywords
+3 −2
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.database.sqlite.SQLiteDatabase;

import com.android.settings.TestConfig;
import com.android.settings.dashboard.SiteMapManager;
import com.android.settings.search.indexing.IndexData;
import com.android.settings.testutils.DatabaseTestUtils;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
@@ -227,8 +228,8 @@ public class DatabaseResultLoaderTest {
    }

    private void insertSpecialCase(String specialCase) {
        String normalized = DatabaseIndexingUtils.normalizeHyphen(specialCase);
        normalized = DatabaseIndexingUtils.normalizeString(normalized);
        String normalized = IndexData.normalizeHyphen(specialCase);
        normalized = IndexData.normalizeString(normalized);
        final ResultPayload payload = new ResultPayload(new Intent());

        ContentValues values = new ContentValues();
Loading