Extracting search algorithm in an interface to make it easier to change the search behavior

Change-Id: I0b1d1387c78d13ef749aac39d5c8167c2909716a
diff --git a/src/com/android/launcher3/allapps/search/AllAppsSearchBarController.java b/src/com/android/launcher3/allapps/search/AllAppsSearchBarController.java
index 547d9e1..63aa7be 100644
--- a/src/com/android/launcher3/allapps/search/AllAppsSearchBarController.java
+++ b/src/com/android/launcher3/allapps/search/AllAppsSearchBarController.java
@@ -31,7 +31,6 @@
 import com.android.launcher3.ExtendedEditText;
 import com.android.launcher3.Launcher;
 import com.android.launcher3.Utilities;
-import com.android.launcher3.allapps.AlphabeticalAppsList;
 import com.android.launcher3.discovery.AppDiscoveryItem;
 import com.android.launcher3.discovery.AppDiscoveryUpdateState;
 import com.android.launcher3.util.ComponentKey;
@@ -46,12 +45,11 @@
         implements TextWatcher, OnEditorActionListener, ExtendedEditText.OnBackKeyListener {
 
     protected Launcher mLauncher;
-    protected AlphabeticalAppsList mApps;
     protected Callbacks mCb;
     protected ExtendedEditText mInput;
     protected String mQuery;
 
-    protected DefaultAppSearchAlgorithm mSearchAlgorithm;
+    protected SearchAlgorithm mSearchAlgorithm;
     protected InputMethodManager mInputMethodManager;
 
     public void setVisibility(int visibility) {
@@ -61,9 +59,8 @@
      * Sets the references to the apps model and the search result callback.
      */
     public final void initialize(
-            AlphabeticalAppsList apps, ExtendedEditText input,
+            SearchAlgorithm searchAlgorithm, ExtendedEditText input,
             Launcher launcher, Callbacks cb) {
-        mApps = apps;
         mCb = cb;
         mLauncher = launcher;
 
@@ -75,22 +72,7 @@
         mInputMethodManager = (InputMethodManager)
                 mInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
 
-        mSearchAlgorithm = onInitializeSearch();
-
-        onInitialized();
-    }
-
-    /**
-     * You can override this method to perform custom initialization.
-     */
-    protected void onInitialized() {
-    }
-
-    /**
-     * This method will get called when the controller is set.
-     */
-    public DefaultAppSearchAlgorithm onInitializeSearch() {
-        return new DefaultAppSearchAlgorithm(mApps.getApps());
+        mSearchAlgorithm = searchAlgorithm;
     }
 
     @Override
diff --git a/src/com/android/launcher3/allapps/search/AppsSearchContainerLayout.java b/src/com/android/launcher3/allapps/search/AppsSearchContainerLayout.java
index 126a02c..cb3b066 100644
--- a/src/com/android/launcher3/allapps/search/AppsSearchContainerLayout.java
+++ b/src/com/android/launcher3/allapps/search/AppsSearchContainerLayout.java
@@ -118,8 +118,8 @@
         mAppsRecyclerView = recyclerView;
         mAppsRecyclerView.addOnScrollListener(mElevationController);
         mAdapter = (AllAppsGridAdapter) mAppsRecyclerView.getAdapter();
-
-        mSearchBarController.initialize(appsList, mSearchInput, mLauncher, this);
+        mSearchBarController.initialize(
+                new DefaultAppSearchAlgorithm(appsList.getApps()), mSearchInput, mLauncher, this);
     }
 
     @Override
diff --git a/src/com/android/launcher3/allapps/search/DefaultAppSearchAlgorithm.java b/src/com/android/launcher3/allapps/search/DefaultAppSearchAlgorithm.java
index 06097d0..8a0fd46 100644
--- a/src/com/android/launcher3/allapps/search/DefaultAppSearchAlgorithm.java
+++ b/src/com/android/launcher3/allapps/search/DefaultAppSearchAlgorithm.java
@@ -26,7 +26,7 @@
 /**
  * The default search implementation.
  */
-public class DefaultAppSearchAlgorithm {
+public class DefaultAppSearchAlgorithm implements SearchAlgorithm {
 
     private final List<AppInfo> mApps;
     protected final Handler mResultHandler;
@@ -36,12 +36,14 @@
         mResultHandler = new Handler();
     }
 
+    @Override
     public void cancel(boolean interruptActiveRequests) {
         if (interruptActiveRequests) {
             mResultHandler.removeCallbacksAndMessages(null);
         }
     }
 
+    @Override
     public void doSearch(final String query,
             final AllAppsSearchBarController.Callbacks callback) {
         final ArrayList<ComponentKey> result = getTitleMatchResult(query);
@@ -54,7 +56,7 @@
         });
     }
 
-    public ArrayList<ComponentKey> getTitleMatchResult(String query) {
+    private ArrayList<ComponentKey> getTitleMatchResult(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();
@@ -67,7 +69,7 @@
         return result;
     }
 
-    public boolean matches(AppInfo info, String query) {
+    public static boolean matches(AppInfo info, String query) {
         int queryLength = query.length();
 
         String title = info.title.toString();
@@ -103,7 +105,7 @@
      *      3) Any capital character after a digit or small character
      *      4) Any capital character before a small character
      */
-    protected boolean isBreak(int thisType, int prevType, int nextType) {
+    private static boolean isBreak(int thisType, int prevType, int nextType) {
         switch (thisType) {
             case Character.UPPERCASE_LETTER:
                 if (nextType == Character.UPPERCASE_LETTER) {
diff --git a/src/com/android/launcher3/allapps/search/SearchAlgorithm.java b/src/com/android/launcher3/allapps/search/SearchAlgorithm.java
new file mode 100644
index 0000000..c409b1c
--- /dev/null
+++ b/src/com/android/launcher3/allapps/search/SearchAlgorithm.java
@@ -0,0 +1,32 @@
+/*
+ * 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.launcher3.allapps.search;
+
+/**
+ * An interface for handling search.
+ */
+public interface SearchAlgorithm {
+
+    /**
+     * Performs search and sends the result to the callback.
+     */
+    void doSearch(String query, AllAppsSearchBarController.Callbacks callback);
+
+    /**
+     * Cancels any active request.
+     */
+    void cancel(boolean interruptActiveRequests);
+}