Merge "Simplifying all-apps seach pipeline" into sc-dev
diff --git a/quickstep/src/com/android/launcher3/uioverrides/QuickstepLauncher.java b/quickstep/src/com/android/launcher3/uioverrides/QuickstepLauncher.java
index bae97d7..b1c04b1 100644
--- a/quickstep/src/com/android/launcher3/uioverrides/QuickstepLauncher.java
+++ b/quickstep/src/com/android/launcher3/uioverrides/QuickstepLauncher.java
@@ -234,7 +234,6 @@
     @Override
     public void onDestroy() {
         super.onDestroy();
-        getAppsView().getSearchUiManager().destroySearch();
         mHotseatPredictionController.destroy();
     }
 
diff --git a/src/com/android/launcher3/allapps/SearchUiManager.java b/src/com/android/launcher3/allapps/SearchUiManager.java
index 0a2dea9..941d3af 100644
--- a/src/com/android/launcher3/allapps/SearchUiManager.java
+++ b/src/com/android/launcher3/allapps/SearchUiManager.java
@@ -49,11 +49,6 @@
     float getScrollRangeDelta(Rect insets);
 
     /**
-     * Called when activity is destroyed. Used to close search system services
-     */
-    default void destroySearch() { }
-
-    /**
      * @return the edit text object
      */
     @Nullable
diff --git a/src/com/android/launcher3/allapps/search/AppsSearchContainerLayout.java b/src/com/android/launcher3/allapps/search/AppsSearchContainerLayout.java
index bfcc1c7..c51bcf5 100644
--- a/src/com/android/launcher3/allapps/search/AppsSearchContainerLayout.java
+++ b/src/com/android/launcher3/allapps/search/AppsSearchContainerLayout.java
@@ -36,7 +36,6 @@
 import com.android.launcher3.DeviceProfile;
 import com.android.launcher3.ExtendedEditText;
 import com.android.launcher3.Insettable;
-import com.android.launcher3.LauncherAppState;
 import com.android.launcher3.R;
 import com.android.launcher3.allapps.AllAppsContainerView;
 import com.android.launcher3.allapps.AllAppsGridAdapter.AdapterItem;
@@ -136,7 +135,7 @@
         mApps = appsView.getApps();
         mAppsView = appsView;
         mSearchBarController.initialize(
-                new DefaultAppSearchAlgorithm(mLauncher, LauncherAppState.getInstance(mLauncher)),
+                new DefaultAppSearchAlgorithm(mLauncher),
                 this, mLauncher, this);
     }
 
diff --git a/src/com/android/launcher3/allapps/search/AppsSearchPipeline.java b/src/com/android/launcher3/allapps/search/AppsSearchPipeline.java
deleted file mode 100644
index 42ee4b7..0000000
--- a/src/com/android/launcher3/allapps/search/AppsSearchPipeline.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * Copyright (C) 2020 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.launcher3.allapps.search;
-
-import android.content.Context;
-import android.os.CancellationSignal;
-
-import com.android.launcher3.LauncherAppState;
-import com.android.launcher3.allapps.AllAppsGridAdapter.AdapterItem;
-import com.android.launcher3.model.AllAppsList;
-import com.android.launcher3.model.BaseModelUpdateTask;
-import com.android.launcher3.model.BgDataModel;
-import com.android.launcher3.model.data.AppInfo;
-import com.android.launcher3.search.StringMatcherUtility;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.function.Consumer;
-
-/**
- * A device search section for handling app searches
- */
-public class AppsSearchPipeline implements SearchPipeline {
-
-    private static final int MAX_RESULTS_COUNT = 5;
-
-    private final LauncherAppState mLauncherAppState;
-
-    public AppsSearchPipeline(Context context, LauncherAppState launcherAppState) {
-        mLauncherAppState = launcherAppState;
-    }
-
-    @Override
-    public void query(String input, Consumer<ArrayList<AdapterItem>> callback,
-            CancellationSignal cancellationSignal) {
-        mLauncherAppState.getModel().enqueueModelUpdateTask(new BaseModelUpdateTask() {
-            @Override
-            public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
-                List<AppInfo> matchingResults = getTitleMatchResult(apps.data, input);
-                callback.accept(getAdapterItems(matchingResults));
-            }
-        });
-    }
-
-    /**
-     * Filters {@link AppInfo}s matching specified query
-     */
-    public static ArrayList<AppInfo> getTitleMatchResult(List<AppInfo> apps, String query) {
-        // Do an intersection of the words in the query and each title, and filter out all the
-        // apps that don't match all of the words in the query.
-        final String queryTextLower = query.toLowerCase();
-        final ArrayList<AppInfo> result = new ArrayList<>();
-        StringMatcherUtility.StringMatcher matcher =
-                StringMatcherUtility.StringMatcher.getInstance();
-        for (AppInfo info : apps) {
-            if (StringMatcherUtility.matches(queryTextLower, info.title.toString(), matcher)) {
-                result.add(info);
-            }
-        }
-        return result;
-    }
-
-    private ArrayList<AdapterItem> getAdapterItems(List<AppInfo> matchingApps) {
-        ArrayList<AdapterItem> items = new ArrayList<>();
-        for (int i = 0; i < matchingApps.size() && i < MAX_RESULTS_COUNT; i++) {
-            AdapterItem appItem = AdapterItem.asApp(i, "", matchingApps.get(i), i);
-            items.add(appItem);
-        }
-
-        return items;
-    }
-}
diff --git a/src/com/android/launcher3/allapps/search/DefaultAppSearchAlgorithm.java b/src/com/android/launcher3/allapps/search/DefaultAppSearchAlgorithm.java
index a386ef8..1f854c6 100644
--- a/src/com/android/launcher3/allapps/search/DefaultAppSearchAlgorithm.java
+++ b/src/com/android/launcher3/allapps/search/DefaultAppSearchAlgorithm.java
@@ -15,25 +15,39 @@
  */
 package com.android.launcher3.allapps.search;
 
+import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
+
 import android.content.Context;
 import android.os.Handler;
 
+import androidx.annotation.AnyThread;
+
 import com.android.launcher3.LauncherAppState;
 import com.android.launcher3.allapps.AllAppsGridAdapter.AdapterItem;
+import com.android.launcher3.model.AllAppsList;
+import com.android.launcher3.model.BaseModelUpdateTask;
+import com.android.launcher3.model.BgDataModel;
+import com.android.launcher3.model.data.AppInfo;
 import com.android.launcher3.search.SearchAlgorithm;
 import com.android.launcher3.search.SearchCallback;
+import com.android.launcher3.search.StringMatcherUtility;
+
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * The default search implementation.
  */
 public class DefaultAppSearchAlgorithm implements SearchAlgorithm<AdapterItem> {
 
-    protected final Handler mResultHandler;
-    private final AppsSearchPipeline mAppsSearchPipeline;
+    private static final int MAX_RESULTS_COUNT = 5;
 
-    public DefaultAppSearchAlgorithm(Context context, LauncherAppState launcherAppState) {
-        mResultHandler = new Handler();
-        mAppsSearchPipeline = new AppsSearchPipeline(context, launcherAppState);
+    private final LauncherAppState mAppState;
+    private final Handler mResultHandler;
+
+    public DefaultAppSearchAlgorithm(Context context) {
+        mAppState = LauncherAppState.getInstance(context);
+        mResultHandler = new Handler(MAIN_EXECUTOR.getLooper());
     }
 
     @Override
@@ -44,11 +58,38 @@
     }
 
     @Override
-    public void doSearch(final String query,
-            final SearchCallback<AdapterItem> callback) {
-        mAppsSearchPipeline.query(query,
-                results -> mResultHandler.post(
-                        () -> callback.onSearchResult(query, results)),
-                null);
+    public void doSearch(String query, SearchCallback<AdapterItem> callback) {
+        mAppState.getModel().enqueueModelUpdateTask(new BaseModelUpdateTask() {
+            @Override
+            public void execute(LauncherAppState app, BgDataModel dataModel, AllAppsList apps) {
+                ArrayList<AdapterItem> result = getTitleMatchResult(apps.data, query);
+                mResultHandler.post(() -> callback.onSearchResult(query, result));
+            }
+        });
+    }
+
+    /**
+     * Filters {@link AppInfo}s matching specified query
+     */
+    @AnyThread
+    public static ArrayList<AdapterItem> getTitleMatchResult(List<AppInfo> apps, String query) {
+        // Do an intersection of the words in the query and each title, and filter out all the
+        // apps that don't match all of the words in the query.
+        final String queryTextLower = query.toLowerCase();
+        final ArrayList<AdapterItem> result = new ArrayList<>();
+        StringMatcherUtility.StringMatcher matcher =
+                StringMatcherUtility.StringMatcher.getInstance();
+
+        int resultCount = 0;
+        int total = apps.size();
+        for (int i = 0; i < total && resultCount < MAX_RESULTS_COUNT; i++) {
+            AppInfo info = apps.get(i);
+            if (StringMatcherUtility.matches(queryTextLower, info.title.toString(), matcher)) {
+                AdapterItem appItem = AdapterItem.asApp(resultCount, "", info, resultCount);
+                result.add(appItem);
+                resultCount++;
+            }
+        }
+        return result;
     }
 }
diff --git a/src/com/android/launcher3/allapps/search/SearchPipeline.java b/src/com/android/launcher3/allapps/search/SearchPipeline.java
deleted file mode 100644
index 3516a41..0000000
--- a/src/com/android/launcher3/allapps/search/SearchPipeline.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright (C) 2020 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.launcher3.allapps.search;
-
-import android.os.CancellationSignal;
-
-import com.android.launcher3.allapps.AllAppsGridAdapter;
-
-import java.util.ArrayList;
-import java.util.function.Consumer;
-
-/**
- * An interface for handling search within pipeline
- */
-// Remove when System Service API is added.
-public interface SearchPipeline {
-
-    /**
-     * Perform query
-     */
-    void query(String input,
-            Consumer<ArrayList<AllAppsGridAdapter.AdapterItem>> callback,
-            CancellationSignal cancellationSignal);
-}