Gets wallpaper color from live wallpaper to be used for workspace preview

- Curretly we are using the wallpaper color of wallpaper thumbnail asset for static and live wallpaper, but the content of live wallpaper may be different from thumbnail, so it would show the wrong color for the workspace preview.
- For live wallpaper, we have to use the wallpaper color from live wallpaper instead of thumbnail. The static wallpaper would still keep using the wallpaper color from thumbnail.

Before:
https://screenshot.googleplex.com/yJCGCw2A03L.png
https://screenshot.googleplex.com/RdRQeoG0AnB.png
https://screenshot.googleplex.com/84KmcO3rTOJ.png
After:
https://screenshot.googleplex.com/5wVOM72CJTD.png
https://screenshot.googleplex.com/Psr9uLE396f.png
https://screenshot.googleplex.com/AxMsoo3F7Vc.png

Test: Manually
Fixes: 159195012
Change-Id: I439e06ff2cb19254455a698742798fd0eb8f7b39
diff --git a/src/com/android/customization/picker/WallpaperPreviewer.java b/src/com/android/customization/picker/WallpaperPreviewer.java
index 34688c5..4993a56 100644
--- a/src/com/android/customization/picker/WallpaperPreviewer.java
+++ b/src/com/android/customization/picker/WallpaperPreviewer.java
@@ -19,6 +19,7 @@
 import static android.view.View.MeasureSpec.makeMeasureSpec;
 
 import android.app.Activity;
+import android.app.WallpaperColors;
 import android.content.Intent;
 import android.graphics.Rect;
 import android.graphics.RectF;
@@ -31,6 +32,7 @@
 import android.widget.ImageView;
 
 import androidx.annotation.MainThread;
+import androidx.annotation.Nullable;
 import androidx.cardview.widget.CardView;
 import androidx.core.content.ContextCompat;
 import androidx.lifecycle.Lifecycle;
@@ -43,7 +45,9 @@
 import com.android.wallpaper.util.ScreenSizeCalculator;
 import com.android.wallpaper.util.SizeCalculator;
 import com.android.wallpaper.util.WallpaperConnection;
+import com.android.wallpaper.util.WallpaperConnection.WallpaperConnectionListener;
 import com.android.wallpaper.widget.LiveTileOverlay;
+import com.android.wallpaper.widget.WallpaperColorsLoader;
 
 /** A class to load the wallpaper to the view. */
 public class WallpaperPreviewer implements LifecycleObserver {
@@ -63,6 +67,13 @@
     // Home workspace surface is behind the app window, and so must the home image wallpaper like
     // the live wallpaper. This view is rendered on mWallpaperSurface for home image wallpaper.
     private ImageView mHomeImageWallpaper;
+    @Nullable private WallpaperColorsListener mWallpaperColorsListener;
+
+    /** Interface for getting {@link WallpaperColors} from wallpaper. */
+    public interface WallpaperColorsListener {
+        /** Gets called when wallpaper color is available or updated. */
+        void onWallpaperColorsChanged(WallpaperColors colors);
+    }
 
     public WallpaperPreviewer(Lifecycle lifecycle, Activity activity, ImageView homePreview,
                               SurfaceView wallpaperSurface) {
@@ -129,9 +140,16 @@
         cardView.setRadius(SizeCalculator.getPreviewCornerRadius(mActivity, cardWidth));
     }
 
-    /** Loads the wallpaper. */
-    public void setWallpaper(WallpaperInfo wallpaperInfo) {
+    /**
+     * Sets a wallpaper to be shown on preview screen.
+     *
+     * @param wallpaperInfo the wallpaper to preview
+     * @param listener the listener for getting the wallpaper color of {@param wallpaperInfo}
+     */
+    public void setWallpaper(WallpaperInfo wallpaperInfo,
+                             @Nullable WallpaperColorsListener listener) {
         mWallpaper =  wallpaperInfo;
+        mWallpaperColorsListener = listener;
         setUpWallpaperPreview();
     }
 
@@ -151,10 +169,21 @@
                                 mActivity.getColor(R.color.secondary_color));
                 setUpLiveWallpaperPreview(mWallpaper);
             } else {
+                // Ensure live wallpaper connection is disconnected.
                 if (mWallpaperConnection != null) {
                     mWallpaperConnection.disconnect();
                     mWallpaperConnection = null;
                 }
+
+                // Load wallpaper color for static wallpaper.
+                WallpaperColorsLoader.getWallpaperColors(
+                        mActivity,
+                        mWallpaper.getThumbAsset(mActivity),
+                        colors -> {
+                            if (mWallpaperColorsListener != null) {
+                                mWallpaperColorsListener.onWallpaperColorsChanged(colors);
+                            }
+                        });
             }
         }
     }
@@ -176,7 +205,14 @@
 
         mWallpaperConnection = new WallpaperConnection(
                 getWallpaperIntent(homeWallpaper.getWallpaperComponent()), mActivity,
-                /* listener= */ null, mPreviewGlobalRect);
+                new WallpaperConnectionListener() {
+                    @Override
+                    public void onWallpaperColorsChanged(WallpaperColors colors, int displayId) {
+                        if (mWallpaperColorsListener != null) {
+                            mWallpaperColorsListener.onWallpaperColorsChanged(colors);
+                        }
+                    }
+                }, mPreviewGlobalRect);
 
         LiveTileOverlay.INSTANCE.update(new RectF(mPreviewLocalRect),
                 ((CardView) mHomePreview.getParent()).getRadius());
diff --git a/src/com/android/customization/picker/grid/GridFragment.java b/src/com/android/customization/picker/grid/GridFragment.java
index b6a749e..dbce456 100644
--- a/src/com/android/customization/picker/grid/GridFragment.java
+++ b/src/com/android/customization/picker/grid/GridFragment.java
@@ -143,7 +143,7 @@
                 .getCurrentWallpaperFactory(getContext().getApplicationContext());
         factory.createCurrentWallpaperInfos((homeWallpaper, lockWallpaper, presentationMode) -> {
             mHomeWallpaper = homeWallpaper;
-            wallpaperPreviewer.setWallpaper(mHomeWallpaper);
+            wallpaperPreviewer.setWallpaper(mHomeWallpaper, /* listener= */ null);
         }, false);
 
         mGridOptionPreviewer = new GridOptionPreviewer(mGridManager,
diff --git a/src/com/android/customization/picker/grid/GridFullPreviewFragment.java b/src/com/android/customization/picker/grid/GridFullPreviewFragment.java
index 27293d6..ae3370c 100644
--- a/src/com/android/customization/picker/grid/GridFullPreviewFragment.java
+++ b/src/com/android/customization/picker/grid/GridFullPreviewFragment.java
@@ -119,7 +119,7 @@
     @Override
     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
-        mWallpaperPreviewer.setWallpaper(mWallpaper);
+        mWallpaperPreviewer.setWallpaper(mWallpaper, /* listener= */ null);
         mGridOptionPreviewer.setGridOption(mGridOption);
     }
 
diff --git a/src/com/android/customization/picker/theme/CustomThemeNameFragment.java b/src/com/android/customization/picker/theme/CustomThemeNameFragment.java
index f4a1106..750c405 100644
--- a/src/com/android/customization/picker/theme/CustomThemeNameFragment.java
+++ b/src/com/android/customization/picker/theme/CustomThemeNameFragment.java
@@ -20,7 +20,6 @@
 import static com.android.customization.picker.theme.ThemeFullPreviewFragment.EXTRA_THEME_OPTION_TITLE;
 import static com.android.customization.picker.theme.ThemeFullPreviewFragment.EXTRA_WALLPAPER_INFO;
 
-import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.text.TextUtils;
@@ -45,7 +44,6 @@
 import com.android.wallpaper.module.CurrentWallpaperInfoFactory;
 import com.android.wallpaper.module.InjectorProvider;
 import com.android.wallpaper.picker.AppbarFragment;
-import com.android.wallpaper.widget.WallpaperColorsLoader;
 
 import org.json.JSONArray;
 import org.json.JSONException;
@@ -101,14 +99,8 @@
         currentWallpaperFactory.createCurrentWallpaperInfos(
                 (homeWallpaper, lockWallpaper, presentationMode) -> {
                     mCurrentHomeWallpaper = homeWallpaper;
-                    wallpaperPreviewer.setWallpaper(homeWallpaper);
-                    Context context =  getContext();
-                    if (context != null) {
-                        WallpaperColorsLoader.getWallpaperColors(
-                                context,
-                                mCurrentHomeWallpaper.getThumbAsset(context),
-                                mThemeOptionPreviewer::updateColorForLauncherWidgets);
-                    }
+                    wallpaperPreviewer.setWallpaper(homeWallpaper,
+                            mThemeOptionPreviewer::updateColorForLauncherWidgets);
                 }, false);
 
         // Set theme default name.
diff --git a/src/com/android/customization/picker/theme/ThemeFragment.java b/src/com/android/customization/picker/theme/ThemeFragment.java
index 18cc3b4..a6fdb28 100644
--- a/src/com/android/customization/picker/theme/ThemeFragment.java
+++ b/src/com/android/customization/picker/theme/ThemeFragment.java
@@ -57,7 +57,6 @@
 import com.android.wallpaper.module.InjectorProvider;
 import com.android.wallpaper.picker.AppbarFragment;
 import com.android.wallpaper.widget.BottomActionBar;
-import com.android.wallpaper.widget.WallpaperColorsLoader;
 
 import java.util.List;
 
@@ -138,14 +137,8 @@
         mCurrentWallpaperFactory.createCurrentWallpaperInfos(
                 (homeWallpaper, lockWallpaper, presentationMode) -> {
                     mCurrentHomeWallpaper = homeWallpaper;
-                    mWallpaperPreviewer.setWallpaper(mCurrentHomeWallpaper);
-                    Context context = getContext();
-                    if (context != null) {
-                        WallpaperColorsLoader.getWallpaperColors(
-                                context,
-                                mCurrentHomeWallpaper.getThumbAsset(context),
-                                mThemeOptionPreviewer::updateColorForLauncherWidgets);
-                    }
+                    mWallpaperPreviewer.setWallpaper(mCurrentHomeWallpaper,
+                            mThemeOptionPreviewer::updateColorForLauncherWidgets);
                 }, false);
 
         view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
diff --git a/src/com/android/customization/picker/theme/ThemeFullPreviewFragment.java b/src/com/android/customization/picker/theme/ThemeFullPreviewFragment.java
index 1821f84..c96c3ac 100644
--- a/src/com/android/customization/picker/theme/ThemeFullPreviewFragment.java
+++ b/src/com/android/customization/picker/theme/ThemeFullPreviewFragment.java
@@ -21,7 +21,6 @@
 import static com.android.wallpaper.widget.BottomActionBar.BottomAction.INFORMATION;
 
 import android.app.Activity;
-import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
@@ -44,7 +43,6 @@
 import com.android.wallpaper.module.InjectorProvider;
 import com.android.wallpaper.picker.AppbarFragment;
 import com.android.wallpaper.widget.BottomActionBar;
-import com.android.wallpaper.widget.WallpaperColorsLoader;
 
 import com.bumptech.glide.Glide;
 
@@ -106,6 +104,13 @@
         setUpToolbar(view);
         Glide.get(getContext()).clearMemory();
 
+        // Set theme option.
+        final ThemeOptionPreviewer themeOptionPreviewer = new ThemeOptionPreviewer(
+                getLifecycle(),
+                getContext(),
+                view.findViewById(R.id.theme_preview_container));
+        themeOptionPreviewer.setPreviewInfo(mThemeBundle.getPreviewInfo());
+
         // Set wallpaper background.
         ImageView wallpaperImageView = view.findViewById(R.id.wallpaper_preview_image);
         final WallpaperPreviewer wallpaperPreviewer = new WallpaperPreviewer(
@@ -113,26 +118,14 @@
                 getActivity(),
                 wallpaperImageView,
                 view.findViewById(R.id.wallpaper_preview_surface));
-        wallpaperPreviewer.setWallpaper(mWallpaper);
+        wallpaperPreviewer.setWallpaper(mWallpaper,
+                themeOptionPreviewer::updateColorForLauncherWidgets);
 
-        // Set theme option.
-        final ThemeOptionPreviewer themeOptionPreviewer = new ThemeOptionPreviewer(
-                getLifecycle(),
-                getContext(),
-                view.findViewById(R.id.theme_preview_container));
-        themeOptionPreviewer.setPreviewInfo(mThemeBundle.getPreviewInfo());
         view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
             @Override
             public void onLayoutChange(View v, int left, int top, int right, int bottom,
                                        int oldLeft, int oldTop, int oldRight, int oldBottom) {
                 wallpaperPreviewer.updatePreviewCardRadius();
-                Context context = getContext();
-                if (context != null) {
-                    WallpaperColorsLoader.getWallpaperColors(
-                            context,
-                            mWallpaper.getThumbAsset(context),
-                            themeOptionPreviewer::updateColorForLauncherWidgets);
-                }
                 view.removeOnLayoutChangeListener(this);
             }
         });