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

Commit 40ce0fab authored by Matthew Fritze's avatar Matthew Fritze
Browse files

Sync search result loaders

The loaders should be syncronized so the results can be
properly ranked without sudden insertions.

This means InstalledAppsLoader needs to finish faster,
which is accomplished by delaying icon loading to bind time
rather than as the apps are queried.

Bug: 34772522
Test: make RunSettingsRoboTests
Change-Id: I7f5244c574d37c6cfd8bbd0d3d40488f38211be3
parent 249077a0
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
/*
 * 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.search2;

import android.content.pm.ApplicationInfo;

public class AppSearchResult extends SearchResult {
    /**
     * Installed app's ApplicationInfo for delayed loading of icons
     */
    public final ApplicationInfo info;

    public AppSearchResult(Builder builder) {
        super(builder);
        info = builder.mInfo;
    }

    public static class Builder extends SearchResult.Builder {
        protected ApplicationInfo mInfo;

        public SearchResult.Builder setAppInfo(ApplicationInfo info) {
            mInfo = info;
            return this;
        }

        public AppSearchResult build() {
            return new AppSearchResult(this);
        }
    }
}
+10 −9
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ import static com.android.settings.search.IndexDatabaseHelper.Tables.TABLE_PREFS
/**
 * AsyncTask to retrieve Settings, First party app and any intent based results.
 */
public class DatabaseResultLoader extends AsyncLoader<List<SearchResult>> {
public class DatabaseResultLoader extends AsyncLoader<List<? extends SearchResult>> {
    private static final String LOG = "DatabaseResultLoader";

    /* These indices are used to match the columns of the this loader's SELECT statement.
@@ -98,26 +98,25 @@ public class DatabaseResultLoader extends AsyncLoader<List<SearchResult>> {
    private static final int[] BASE_RANKS = {1, 4, 7};

    private final String mQueryText;
    private final SQLiteDatabase mDatabase;
    private final Context mContext;
    private final CursorToSearchResultConverter mConverter;
    private final SiteMapManager mSiteMapManager;

    public DatabaseResultLoader(Context context, String queryText) {
    public DatabaseResultLoader(Context context, String queryText, SiteMapManager mapManager) {
        super(context);
        mSiteMapManager = FeatureFactory.getFactory(context)
                .getSearchFeatureProvider().getSiteMapManager();
        mDatabase = IndexDatabaseHelper.getInstance(context).getReadableDatabase();
        mSiteMapManager = mapManager;
        mContext = context;
        mQueryText = cleanQuery(queryText);
        mConverter = new CursorToSearchResultConverter(context, mQueryText);
    }

    @Override
    protected void onDiscardResult(List<SearchResult> result) {
    protected void onDiscardResult(List<? extends SearchResult> result) {
        // TODO Search
    }

    @Override
    public List<SearchResult> loadInBackground() {
    public List<? extends SearchResult> loadInBackground() {
        if (mQueryText == null || mQueryText.isEmpty()) {
            return null;
        }
@@ -144,7 +143,9 @@ public class DatabaseResultLoader extends AsyncLoader<List<SearchResult>> {
        final String whereClause = buildWhereClause(matchColumns);
        final String[] selection = buildQuerySelection(matchColumns.length * 2);

        final Cursor resultCursor = mDatabase.query(TABLE_PREFS_INDEX, SELECT_COLUMNS, whereClause,
        final SQLiteDatabase database = IndexDatabaseHelper.getInstance(mContext)
                .getReadableDatabase();
        final Cursor resultCursor = database.query(TABLE_PREFS_INDEX, SELECT_COLUMNS, whereClause,
                selection, null, null, null);
        return mConverter.convertCursor(mSiteMapManager, resultCursor, baseRank);
    }
+21 −10
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ import java.util.List;
/**
 * Search loader for installed apps.
 */
public class InstalledAppResultLoader extends AsyncLoader<List<SearchResult>> {
public class InstalledAppResultLoader extends AsyncLoader<List<? extends SearchResult>> {

    private static final int NAME_NO_MATCH = -1;
    private static final Intent LAUNCHER_PROBE = new Intent(Intent.ACTION_MAIN)
@@ -56,18 +56,17 @@ public class InstalledAppResultLoader extends AsyncLoader<List<SearchResult>> {


    public InstalledAppResultLoader(Context context, PackageManagerWrapper pmWrapper,
            String query) {
            String query, SiteMapManager mapManager) {
        super(context);
        mSiteMapManager = FeatureFactory.getFactory(context)
                .getSearchFeatureProvider().getSiteMapManager();
        mSiteMapManager = mapManager;
        mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
        mPackageManager = pmWrapper;
        mQuery = query;
    }

    @Override
    public List<SearchResult> loadInBackground() {
        final List<SearchResult> results = new ArrayList<>();
    public List<? extends SearchResult> loadInBackground() {
        final List<AppSearchResult> results = new ArrayList<>();
        final PackageManager pm = mPackageManager.getPackageManager();

        for (UserInfo user : getUsersToCount()) {
@@ -90,10 +89,10 @@ public class InstalledAppResultLoader extends AsyncLoader<List<SearchResult>> {
                        .setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                        .setData(Uri.fromParts("package", info.packageName, null));

                final SearchResult.Builder builder = new SearchResult.Builder();
                builder.addIcon(info.loadIcon(pm))
                final AppSearchResult.Builder builder = new AppSearchResult.Builder();
                builder.setAppInfo(info)
                        .addTitle(info.loadLabel(pm))
                        .addRank(wordDiff)
                        .addRank(getRank(wordDiff))
                        .addBreadcrumbs(getBreadCrumb())
                        .addPayload(new IntentPayload(intent));
                results.add(builder.build());
@@ -120,7 +119,7 @@ public class InstalledAppResultLoader extends AsyncLoader<List<SearchResult>> {
    }

    @Override
    protected void onDiscardResult(List<SearchResult> result) {
    protected void onDiscardResult(List<? extends SearchResult> result) {

    }

@@ -200,4 +199,16 @@ public class InstalledAppResultLoader extends AsyncLoader<List<SearchResult>> {
        }
        return mBreadcrumb;
    }

    /**
     * A temporary ranking scheme for installed apps.
     * @param wordDiff difference between query length and app name length.
     * @return the ranking.
     */
    private int getRank(int wordDiff) {
        if (wordDiff < 6) {
            return 3;
        }
        return 4;
    }
}
+3 −3
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import java.util.List;
/**
 * Loader for recently searched queries.
 */
public class SavedQueryLoader extends AsyncLoader<List<SearchResult>> {
public class SavedQueryLoader extends AsyncLoader<List<? extends SearchResult>> {

    // Max number of proposed suggestions
    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
@@ -45,12 +45,12 @@ public class SavedQueryLoader extends AsyncLoader<List<SearchResult>> {
    }

    @Override
    protected void onDiscardResult(List<SearchResult> result) {
    protected void onDiscardResult(List<? extends SearchResult> result) {

    }

    @Override
    public List<SearchResult> loadInBackground() {
    public List<? extends SearchResult> loadInBackground() {
        Cursor cursor = mDatabase.query(IndexDatabaseHelper.Tables.TABLE_SAVED_QUERIES /* table */,
                new String[]{SavedQueriesColumns.QUERY} /* columns */,
                null /* selection */,
+3 −2
Original line number Diff line number Diff line
@@ -65,13 +65,14 @@ public class SearchFeatureProviderImpl implements SearchFeatureProvider {

    @Override
    public DatabaseResultLoader getDatabaseSearchLoader(Context context, String query) {
        return new DatabaseResultLoader(context, query);
        return new DatabaseResultLoader(context, query, getSiteMapManager());
    }

    @Override
    public InstalledAppResultLoader getInstalledAppSearchLoader(Context context, String query) {
        return new InstalledAppResultLoader(
                context, new PackageManagerWrapperImpl(context.getPackageManager()), query);
                context, new PackageManagerWrapperImpl(context.getPackageManager()), query,
                getSiteMapManager());
    }

    @Override
Loading