Hookup existing AllAppsSearchPlugin interface to SearchBarController
Bug: 161801331

Change-Id: Ied575f78ad2139c6818ae5a13467b7399b9ab17a
diff --git a/src/com/android/launcher3/allapps/search/AllAppsSearchBarController.java b/src/com/android/launcher3/allapps/search/AllAppsSearchBarController.java
index 06faaac..0d87481 100644
--- a/src/com/android/launcher3/allapps/search/AllAppsSearchBarController.java
+++ b/src/com/android/launcher3/allapps/search/AllAppsSearchBarController.java
@@ -15,15 +15,23 @@
  */
 package com.android.launcher3.allapps.search;
 
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.os.Bundle;
 import android.text.Editable;
 import android.text.TextUtils;
 import android.text.TextWatcher;
+import android.util.Log;
 import android.view.KeyEvent;
 import android.view.View;
 import android.view.View.OnFocusChangeListener;
 import android.view.inputmethod.EditorInfo;
 import android.widget.TextView;
 import android.widget.TextView.OnEditorActionListener;
+import android.widget.Toast;
+
+import androidx.core.app.ActivityCompat;
+import androidx.core.content.ContextCompat;
 
 import com.android.launcher3.BaseDraggingActivity;
 import com.android.launcher3.ExtendedEditText;
@@ -32,23 +40,31 @@
 import com.android.launcher3.allapps.AlphabeticalAppsList;
 import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.model.data.ItemInfo;
+import com.android.launcher3.uioverrides.plugins.PluginManagerWrapper;
 import com.android.launcher3.util.PackageManagerHelper;
+import com.android.systemui.plugins.AllAppsSearchPlugin;
+import com.android.systemui.plugins.PluginListener;
 
 import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Consumer;
 
 /**
  * An interface to a search box that AllApps can command.
  */
 public class AllAppsSearchBarController
         implements TextWatcher, OnEditorActionListener, ExtendedEditText.OnBackKeyListener,
-        OnFocusChangeListener {
+        OnFocusChangeListener, PluginListener<AllAppsSearchPlugin> {
 
+    private static final String TAG = "AllAppsSearchBarContoller";
     protected BaseDraggingActivity mLauncher;
     protected Callbacks mCb;
     protected ExtendedEditText mInput;
     protected String mQuery;
 
     protected SearchAlgorithm mSearchAlgorithm;
+    private AllAppsSearchPlugin mPlugin;
+    private Consumer mPlubinCb;
 
     public void setVisibility(int visibility) {
         mInput.setVisibility(visibility);
@@ -59,7 +75,7 @@
      */
     public final void initialize(
             SearchAlgorithm searchAlgorithm, ExtendedEditText input,
-            BaseDraggingActivity launcher, Callbacks cb) {
+            BaseDraggingActivity launcher, Callbacks cb, Consumer<List<Bundle>> secondaryCb) {
         mCb = cb;
         mLauncher = launcher;
 
@@ -69,11 +85,19 @@
         mInput.setOnBackKeyListener(this);
         mInput.setOnFocusChangeListener(this);
         mSearchAlgorithm = searchAlgorithm;
+
+        PluginManagerWrapper.INSTANCE.get(launcher).addPluginListener(this,
+                AllAppsSearchPlugin.class);
+        mPlubinCb = secondaryCb;
     }
 
     @Override
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
-        // Do nothing
+        if (mPlugin != null) {
+            if (s.length() == 0) {
+                mPlugin.startedTyping();
+            }
+        }
     }
 
     @Override
@@ -90,6 +114,9 @@
         } else {
             mSearchAlgorithm.cancel(false);
             mSearchAlgorithm.doSearch(mQuery, mCb);
+            if (mPlugin != null) {
+                mPlugin.performSearch(mQuery, mPlubinCb);
+            }
         }
     }
 
@@ -170,13 +197,52 @@
         return mInput.isFocused();
     }
 
+    @Override
+    public void onPluginConnected(AllAppsSearchPlugin allAppsSearchPlugin, Context context) {
+        if (FeatureFlags.ENABLE_DEVICE_SEARCH.get()) {
+            mPlugin = allAppsSearchPlugin;
+            checkCallPermission();
+        }
+    }
+
+    /**
+     * Check call permissions.
+     */
+    public void checkCallPermission() {
+        final String[] permission = {"android.permission.CALL_PHONE",
+                "android.permission.READ_CONTACTS"};
+        boolean request = false;
+        for (String p : permission) {
+            int permissionCheck = ContextCompat.checkSelfPermission(mLauncher, p);
+            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
+                request = true;
+            }
+        }
+
+        if (!request) return;
+        boolean rationale = false;
+        for (String p : permission) {
+            if (mLauncher.shouldShowRequestPermissionRationale(p)) {
+                rationale = true;
+            }
+            if (rationale) {
+                Log.e(TAG, p + " Show rationale");
+                Toast.makeText(mLauncher, "Requesting Permissions", Toast.LENGTH_SHORT).show();
+            } else {
+                ActivityCompat.requestPermissions(mLauncher,  permission,  123);
+                Log.e(TAG, p + " request permission");
+            }
+        }
+
+    }
+
     /**
      * Callback for getting search results.
      */
     public interface Callbacks {
 
         /**
-         * Called when the search is complete.
+         * Called when the search from primary source is complete.
          *
          * @param items sorted list of search result adapter items.
          */
@@ -187,5 +253,4 @@
          */
         void clearSearchResult();
     }
-
 }
\ No newline at end of file
diff --git a/src/com/android/launcher3/allapps/search/AppsSearchContainerLayout.java b/src/com/android/launcher3/allapps/search/AppsSearchContainerLayout.java
index 16a1efd..e8a0d7a 100644
--- a/src/com/android/launcher3/allapps/search/AppsSearchContainerLayout.java
+++ b/src/com/android/launcher3/allapps/search/AppsSearchContainerLayout.java
@@ -24,6 +24,7 @@
 
 import android.content.Context;
 import android.graphics.Rect;
+import android.os.Bundle;
 import android.text.Selection;
 import android.text.SpannableStringBuilder;
 import android.text.method.TextKeyListener;
@@ -47,13 +48,15 @@
 import com.android.launcher3.anim.PropertySetter;
 
 import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Consumer;
 
 /**
  * Layout to contain the All-apps search UI.
  */
 public class AppsSearchContainerLayout extends ExtendedEditText
         implements SearchUiManager, AllAppsSearchBarController.Callbacks,
-        AllAppsStore.OnUpdateListener, Insettable {
+        AllAppsStore.OnUpdateListener, Insettable, Consumer<List<Bundle>> {
 
     private final BaseDraggingActivity mLauncher;
     private final AllAppsSearchBarController mSearchBarController;
@@ -136,7 +139,7 @@
         mAppsView = appsView;
         mSearchBarController.initialize(
                 new DefaultAppSearchAlgorithm(LauncherAppState.getInstance(mLauncher)), this,
-                mLauncher, this);
+                mLauncher, this, this);
     }
 
     @Override
@@ -220,4 +223,9 @@
     public EditText getEditText() {
         return this;
     }
+
+    @Override
+    public void accept(List<Bundle> bundles) {
+        // TODO: Render the result on mAppsView object
+    }
 }
diff --git a/src_plugins/com/android/systemui/plugins/AllAppsSearchPlugin.java b/src_plugins/com/android/systemui/plugins/AllAppsSearchPlugin.java
index c57f07d..be20e2d 100644
--- a/src_plugins/com/android/systemui/plugins/AllAppsSearchPlugin.java
+++ b/src_plugins/com/android/systemui/plugins/AllAppsSearchPlugin.java
@@ -16,35 +16,28 @@
 
 package com.android.systemui.plugins;
 
-import android.app.Activity;
-import android.view.ViewGroup;
-import android.widget.EditText;
+import android.os.Bundle;
 
 import com.android.systemui.plugins.annotations.ProvidesInterface;
 
+import java.util.List;
+import java.util.function.Consumer;
+
 /**
- * Implement this plugin interface to replace the all apps recycler view of the all apps drawer.
+ * Implement this plugin interface to fetch search result data from the plugin side.
  */
 @ProvidesInterface(action = AllAppsSearchPlugin.ACTION, version = AllAppsSearchPlugin.VERSION)
 public interface AllAppsSearchPlugin extends Plugin {
     String ACTION = "com.android.systemui.action.PLUGIN_ALL_APPS_SEARCH_ACTIONS";
-    int VERSION = 3;
-
-    /** Following are the order that these methods should be called. */
-    void setup(ViewGroup parent, Activity activity, float allAppsContainerHeight);
+    int VERSION = 4;
 
     /**
-     * When drag starts, pass window inset related fields and the progress to indicate
-     * whether user is swiping down or swiping up
+     * Send signal when user starts typing.
      */
-    void onDragStart(float progress);
+    void startedTyping();
 
-    /** progress is between [0, 1] 1: down, 0: up */
-    void setProgress(float progress);
-
-    /** Called when container animation stops, so that plugin can perform cleanups */
-    void onAnimationEnd(float progress);
-
-    /** pass over the search box object */
-    void setEditText(EditText editText);
+    /**
+     * Send over the query and get the search results.
+     */
+    void performSearch(String query, Consumer<List<Bundle>> results);
 }