Launcher3: import reversed google now lib

* Imported from https://github.com/patriksletmo/launcherclient/

Change-Id: I84582ae812b8ecb0d694ae2396843effdcf1219c

Launcher3: support google now tab

Change-Id: I6efc1d5186e0f1bd4fa78ae7231999b984ce7222

LauncherClient: make sure service is connected before trying to unbind

Change-Id: If092db391be12db4f89ed292d1a2f5f9daef8c17

Launcher3: animate workspace when animating the Google now page

Change-Id: I6306d8eeee5ef87b3faf1d9dcb6825e67eb65d45

Launcher3: make Google now left page optional

* Always destroy the launcher client when disabled, don't just disable the scrolling page.

Change-Id: Ic9a990c03c29e3ef280242c7a2d8b3bb0d049566

Launcher3: cleanup launcher tab preference

 Remove preference is search package is not available
 Consolidate BroadcastReceiver and IntentFilter

[OmniChange]

Launcher3: Add OnStart and OnStop to LuncherClient

Launcher3: Add ChangeListener for GoogleFeed

Change-Id: Idcdf7380ce25a66f1dc6c3cd993ae76d089cdd36

[On A11]
Updated to use the new LauncherOverlay plugin

[On A12]
Replace isAppInstalled with isAvailableApp
Remove previous Launchertab
Remove previous Google LauncherClient.
No more needed to edit the Launcher.java :-)

Add prebuilt libGoogleFeed.jar for the new LauncherClient with:
 * ClientOptions
 * dump(String, PrintWriter)
 * hideOverlay(Int)
 * reattachOverlay
 * showOverlay(boolean)

Imported from https://github.com/LineageOS/android_packages_apps_Trebuchet/commit/100eaee2247996010137dc5a1422e65c629d34f6
Added the new OverlayCallbackImpl from LOS.

Launcher3: move libGoogleFeed.jar to vendor/gapps

and only GAPPS builds will add it
for all other there is a mock impl

Launcher3: move overlay lib to common repo

Change-Id: Ief90e95a657aea1182f76c656eb8dfdafc3db505

fixup! QuickStep: Google Feed integration

Change-Id: Ic9ae500a93b930590e22eddb0fefe04deb584300

Change-Id: Ief90e95a657aea1182f76c656eb8dfdafc3db505
diff --git a/Android.bp b/Android.bp
index c661986..9aeb3d4 100644
--- a/Android.bp
+++ b/Android.bp
@@ -272,8 +272,9 @@
 
 // Library with all the source code and dependencies for building Quickstep
 android_library {
-    name: "Launcher3QuickStepLib",
+    name: "Launcher3QuickStepLibGoogle",
     srcs: [
+        "src_overlay/google/com/android/launcher3/overlay/OverlayCallbackImpl.java",
         ":launcher-src",
         ":launcher-quickstep-src",
         ":launcher-build-config",
@@ -287,6 +288,37 @@
     // in QuickstepResLib to take precendece, so it should be the final
     // dependency. See b/208647810 for how this can go wrong.
     static_libs: [
+        "OmniLib",
+        "//vendor/omni:libGoogleFeed",
+        "SystemUI-statsd",
+        "QuickstepResLib",
+    ],
+    manifest: "quickstep/AndroidManifest.xml",
+    platform_apis: true,
+    min_sdk_version: "current",
+    // TODO(b/319712088): re-enable use_resource_processor
+    use_resource_processor: false,
+}
+
+android_library {
+    name: "Launcher3QuickStepLibMock",
+    srcs: [
+        "src_overlay/mock/com/android/launcher3/overlay/OverlayCallbackImpl.java",
+        "src_overlay/mock/com/google/android/libraries/gsa/launcherclient/LauncherClientCallbacks.java",
+        ":launcher-src",
+        ":launcher-quickstep-src",
+        ":launcher-build-config",
+    ],
+    resource_dirs: [],
+    libs: [
+        "framework-statsd",
+    ],
+    // Note the ordering here is important when it comes to resource
+    // overriding. We want the most specific resource overrides defined
+    // in QuickstepResLib to take precendece, so it should be the final
+    // dependency. See b/208647810 for how this can go wrong.
+    static_libs: [
+        "OmniLib",
         "SystemUI-statsd",
         "QuickstepResLib",
     ],
@@ -301,7 +333,7 @@
 android_app {
     name: "Launcher3QuickStep",
 
-    static_libs: ["Launcher3QuickStepLib"],
+    defaults: ["omni_launcher3_defaults"],
     optimize: {
         enabled: false,
     },
diff --git a/quickstep/src/com/android/launcher3/uioverrides/QuickstepLauncher.java b/quickstep/src/com/android/launcher3/uioverrides/QuickstepLauncher.java
index 2168f7a..183a496 100644
--- a/quickstep/src/com/android/launcher3/uioverrides/QuickstepLauncher.java
+++ b/quickstep/src/com/android/launcher3/uioverrides/QuickstepLauncher.java
@@ -130,6 +130,7 @@
 import com.android.launcher3.model.BgDataModel.FixedContainerItems;
 import com.android.launcher3.model.WellbeingModel;
 import com.android.launcher3.model.data.ItemInfo;
+import com.android.launcher3.overlay.OverlayCallbackImpl;
 import com.android.launcher3.popup.SystemShortcut;
 import com.android.launcher3.proxy.ProxyActivityStarter;
 import com.android.launcher3.statehandlers.DepthController;
@@ -187,6 +188,7 @@
 import com.android.quickstep.views.RecentsView;
 import com.android.quickstep.views.RecentsViewContainer;
 import com.android.quickstep.views.TaskView;
+import com.android.systemui.plugins.shared.LauncherOverlayManager;
 import com.android.systemui.shared.recents.model.Task;
 import com.android.systemui.shared.system.ActivityManagerWrapper;
 import com.android.systemui.unfold.RemoteUnfoldSharedComponent;
@@ -258,6 +260,11 @@
     }
 
     @Override
+    protected LauncherOverlayManager getDefaultOverlay() {
+        return new OverlayCallbackImpl(this);
+    }
+
+    @Override
     protected void setupViews() {
         super.setupViews();
 
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
index e161967..a063d2f 100644
--- a/res/values/custom_strings.xml
+++ b/res/values/custom_strings.xml
@@ -26,5 +26,7 @@
     <string name="pref_qsb_location_workspace">Workspace</string>
     <string name="pref_qsb_location_workspace_value" translatable="false">1</string>
     <string name="pref_grid_title">Grid size</string>
+    <string name="left_tab_label">Google feed tab</string>
+    <string name="left_tab_description">Toggle Google feed intergration</string>
 
 </resources>
diff --git a/res/xml/launcher_preferences.xml b/res/xml/launcher_preferences.xml
index 94617d1..6c161fe 100644
--- a/res/xml/launcher_preferences.xml
+++ b/res/xml/launcher_preferences.xml
@@ -57,7 +57,6 @@
         android:summary="@string/pref_qsb_show_summary"
         android:title="@string/pref_qsb_show_title"/>
 
-
     <ListPreference
         android:key="pref_qsb_location"
         android:title="@string/pref_qsb_location_title"
@@ -67,6 +66,12 @@
         android:entryValues="@array/prefs_qsb_location_values"
         android:defaultValue="@string/pref_qsb_location_workspace_value"/>
 
+    <SwitchPreference
+        android:key="pref_left_tab"
+        android:title="@string/left_tab_label"
+        android:summary="@string/left_tab_description"
+        android:persistent="true" />
+
     <ListPreference
         android:key="pref_grid"
         android:title="@string/pref_grid_title"
diff --git a/src/com/android/launcher3/settings/SettingsActivity.java b/src/com/android/launcher3/settings/SettingsActivity.java
index 46995b2..815ea4b 100644
--- a/src/com/android/launcher3/settings/SettingsActivity.java
+++ b/src/com/android/launcher3/settings/SettingsActivity.java
@@ -63,6 +63,7 @@
 import com.android.launcher3.util.DisplayController;
 import com.android.launcher3.util.SettingsCache;
 
+import org.omnirom.omnilib.utils.PackageUtils;
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 
@@ -95,6 +96,9 @@
     private static final int DELAY_HIGHLIGHT_DURATION_MILLIS = 600;
     public static final String SAVE_HIGHLIGHTED_KEY = "android:preference_highlighted";
 
+    private static final String SEARCH_PACKAGE = "com.google.android.googlequicksearchbox";
+    private static final String SHOW_LEFT_TAB_PREFERENCE_KEY = "pref_left_tab";
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -229,6 +233,17 @@
                 }
             });
 
+            Preference leftTabPage = findPreference(SHOW_LEFT_TAB_PREFERENCE_KEY);
+            if (!isSearchInstalled()) {
+                getPreferenceScreen().removePreference(leftTabPage);
+            }
+            leftTabPage.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+                public boolean onPreferenceChange(Preference preference, Object newValue) {
+                    new Handler().postDelayed(() -> Utilities.restart(getActivity()), Utilities.WAIT_BEFORE_RESTART);
+                    return true;
+                }
+            });
+
             final ListPreference grid = (ListPreference) findPreference(GRID_SIZE_PREFERENCE_KEY);
             InvariantDeviceProfile idp = InvariantDeviceProfile.INSTANCE.get(getContext());
             ArrayList<String> entries = new ArrayList<>();
@@ -425,5 +440,9 @@
             }
             return result;
         }
+
+        private boolean isSearchInstalled() {
+            return PackageUtils.isAvailableApp(SEARCH_PACKAGE, getActivity());
+        }
     }
 }
diff --git a/src_overlay/google/com/android/launcher3/overlay/OverlayCallbackImpl.java b/src_overlay/google/com/android/launcher3/overlay/OverlayCallbackImpl.java
new file mode 100644
index 0000000..d068cf5
--- /dev/null
+++ b/src_overlay/google/com/android/launcher3/overlay/OverlayCallbackImpl.java
@@ -0,0 +1,171 @@
+/*
+ * Copyright (C) 2016 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.overlay;
+
+import android.app.Activity;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+
+import com.android.launcher3.Launcher;
+import com.android.launcher3.LauncherPrefs;
+import com.android.systemui.plugins.shared.LauncherOverlayManager;
+import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlay;
+import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlayCallbacks;
+
+import com.google.android.libraries.gsa.launcherclient.LauncherClient;
+import com.google.android.libraries.gsa.launcherclient.LauncherClientCallbacks;
+
+import java.io.PrintWriter;
+
+/**
+ * Implements {@link LauncherOverlay} and passes all the corresponding events to {@link
+ * LauncherClient}. {@see setClient}
+ *
+ * <p>Implements {@link LauncherClientCallbacks} and sends all the corresponding callbacks to {@link
+ * Launcher}.
+ */
+public class OverlayCallbackImpl
+        implements LauncherOverlay, LauncherClientCallbacks, LauncherOverlayManager,
+        SharedPreferences.OnSharedPreferenceChangeListener {
+
+    private static final String SHOW_LEFT_TAB_PREFERENCE_KEY = "pref_left_tab";
+
+    private final Launcher mLauncher;
+    private final LauncherClient mClient;
+
+    private LauncherOverlayCallbacks mLauncherOverlayCallbacks;
+    private boolean mWasOverlayAttached = false;
+
+    public OverlayCallbackImpl(Launcher launcher) {
+        SharedPreferences prefs = LauncherPrefs.getPrefs(launcher);
+
+        mLauncher = launcher;
+        mClient = new LauncherClient(mLauncher, this, getClientOptions(prefs));
+        prefs.registerOnSharedPreferenceChangeListener(this);
+    }
+
+    @Override
+    public void onDeviceProvideChanged() {
+        mClient.reattachOverlay();
+    }
+
+    @Override
+    public void onAttachedToWindow() {
+        mClient.onAttachedToWindow();
+    }
+
+    @Override
+    public void onDetachedFromWindow() {
+        mClient.onDetachedFromWindow();
+    }
+
+    @Override
+    public void dump(String prefix, PrintWriter w) {
+        mClient.dump(prefix, w);
+    }
+
+    @Override
+    public void openOverlay() {
+        mClient.showOverlay(true);
+    }
+
+    @Override
+    public void hideOverlay(boolean animate) {
+        mClient.hideOverlay(animate);
+    }
+
+    @Override
+    public void hideOverlay(int duration) {
+        mClient.hideOverlay(duration);
+    }
+
+    @Override
+    public void onActivityStarted() {
+        mClient.onStart();
+    }
+
+    @Override
+    public void onActivityResumed() {
+        mClient.onResume();
+    }
+
+    @Override
+    public void onActivityPaused() {
+        mClient.onPause();
+    }
+
+    @Override
+    public void onActivityStopped() {
+        mClient.onStop();
+    }
+
+    @Override
+    public void onActivityDestroyed() {
+        mClient.onDestroy();
+        mLauncher.getSharedPrefs().unregisterOnSharedPreferenceChangeListener(this);
+    }
+
+    @Override
+    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
+        if (SHOW_LEFT_TAB_PREFERENCE_KEY.equals(key)) {
+            mClient.setClientOptions(getClientOptions(prefs));
+        }
+    }
+
+    @Override
+    public void onServiceStateChanged(boolean overlayAttached, boolean hotwordActive) {
+        if (overlayAttached != mWasOverlayAttached) {
+            mWasOverlayAttached = overlayAttached;
+            mLauncher.setLauncherOverlay(overlayAttached ? this : null);
+        }
+    }
+
+    @Override
+    public void onOverlayScrollChanged(float progress) {
+        if (mLauncherOverlayCallbacks != null) {
+            mLauncherOverlayCallbacks.onOverlayScrollChanged(progress);
+        }
+    }
+
+    @Override
+    public void onScrollInteractionBegin() {
+        mClient.startMove();
+    }
+
+    @Override
+    public void onScrollInteractionEnd() {
+        mClient.endMove();
+    }
+
+    @Override
+    public void onScrollChange(float progress, boolean rtl) {
+        mClient.updateMove(progress);
+    }
+
+    @Override
+    public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks) {
+        mLauncherOverlayCallbacks = callbacks;
+    }
+
+    private LauncherClient.ClientOptions getClientOptions(SharedPreferences prefs) {
+        return new LauncherClient.ClientOptions(
+                prefs.getBoolean(SHOW_LEFT_TAB_PREFERENCE_KEY, false),
+                true, /* enableHotword */
+                true /* enablePrewarming */
+        );
+    }
+}
diff --git a/src_overlay/mock/com/android/launcher3/overlay/OverlayCallbackImpl.java b/src_overlay/mock/com/android/launcher3/overlay/OverlayCallbackImpl.java
new file mode 100644
index 0000000..449cef8
--- /dev/null
+++ b/src_overlay/mock/com/android/launcher3/overlay/OverlayCallbackImpl.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2016 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.overlay;
+
+import android.app.Activity;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+
+import com.android.launcher3.Launcher;
+import com.android.systemui.plugins.shared.LauncherOverlayManager;
+import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlay;
+import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlayCallbacks;
+
+import com.google.android.libraries.gsa.launcherclient.LauncherClientCallbacks;
+
+import java.io.PrintWriter;
+
+/**
+ * Implements {@link LauncherOverlay} and passes all the corresponding events to {@link
+ * LauncherClient}. {@see setClient}
+ *
+ * <p>Implements {@link LauncherClientCallbacks} and sends all the corresponding callbacks to {@link
+ * Launcher}.
+ */
+public class OverlayCallbackImpl
+        implements LauncherOverlay, LauncherClientCallbacks, LauncherOverlayManager,
+        SharedPreferences.OnSharedPreferenceChangeListener {
+
+    public OverlayCallbackImpl(Launcher launcher) {
+    }
+
+    @Override
+    public void onDeviceProvideChanged() {
+    }
+
+    @Override
+    public void onAttachedToWindow() {
+    }
+
+    @Override
+    public void onDetachedFromWindow() {
+    }
+
+    @Override
+    public void dump(String prefix, PrintWriter w) {
+    }
+
+    @Override
+    public void openOverlay() {
+    }
+
+    @Override
+    public void hideOverlay(boolean animate) {
+    }
+
+    @Override
+    public void hideOverlay(int duration) {
+    }
+
+    @Override
+    public void onActivityStarted() {
+    }
+
+    @Override
+    public void onActivityResumed() {
+    }
+
+    @Override
+    public void onActivityPaused() {
+    }
+
+    @Override
+    public void onActivityStopped() {
+    }
+
+    @Override
+    public void onActivityDestroyed() {
+    }
+
+    @Override
+    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
+    }
+
+    @Override
+    public void onServiceStateChanged(boolean overlayAttached, boolean hotwordActive) {
+    }
+
+    @Override
+    public void onOverlayScrollChanged(float progress) {
+    }
+
+    @Override
+    public void onScrollInteractionBegin() {
+    }
+
+    @Override
+    public void onScrollInteractionEnd() {
+    }
+
+    @Override
+    public void onScrollChange(float progress, boolean rtl) {
+    }
+
+    @Override
+    public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks) {
+    }
+}
diff --git a/src_overlay/mock/com/google/android/libraries/gsa/launcherclient/LauncherClientCallbacks.java b/src_overlay/mock/com/google/android/libraries/gsa/launcherclient/LauncherClientCallbacks.java
new file mode 100644
index 0000000..3f9e6d1
--- /dev/null
+++ b/src_overlay/mock/com/google/android/libraries/gsa/launcherclient/LauncherClientCallbacks.java
@@ -0,0 +1,5 @@
+package com.google.android.libraries.gsa.launcherclient;
+public interface LauncherClientCallbacks {
+    void onOverlayScrollChanged(float progress);
+    void onServiceStateChanged(boolean overlayAttached, boolean hotwordActive);
+}
\ No newline at end of file