Gets the wallpaper color to update the preview text/icon

Screenshot:
    Before: https://screenshot.googleplex.com/fm9UEBrtv9C.png
    After: https://screenshot.googleplex.com/gp7RzO1Q9uH.png
    Launcher: https://screenshot.googleplex.com/eeVKg3La3bK.png

Video for lockscreen preview: https://drive.google.com/file/d/1jc2GC9BYwnla1Ici1REkb1a-LtlIitcw/view?usp=sharing

Test: Manually
Bug: 146475648
Bug: 156129610
Change-Id: Ica4b9df30e277a5d4d2b4e1838f3cdaf93147ff8
diff --git a/src/com/android/customization/picker/theme/ThemeFragment.java b/src/com/android/customization/picker/theme/ThemeFragment.java
index 4cb330e..09fc9c2 100644
--- a/src/com/android/customization/picker/theme/ThemeFragment.java
+++ b/src/com/android/customization/picker/theme/ThemeFragment.java
@@ -32,6 +32,7 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.ImageView;
 import android.widget.Toast;
 
 import androidx.annotation.NonNull;
@@ -55,6 +56,7 @@
 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;
 
@@ -91,6 +93,7 @@
     private CurrentWallpaperInfoFactory mCurrentWallpaperFactory;
     private BottomActionBar mBottomActionBar;
     private WallpaperPreviewer mWallpaperPreviewer;
+    private ImageView mWallpaperImage;
     private ThemeOptionPreviewer mThemeOptionPreviewer;
     private ThemeInfoView mThemeInfoView;
 
@@ -118,24 +121,18 @@
         mOptionsContainer = view.findViewById(R.id.options_container);
 
         // Set Wallpaper background.
+        mWallpaperImage = view.findViewById(R.id.wallpaper_preview_image);
         mWallpaperPreviewer = new WallpaperPreviewer(
                 getLifecycle(),
                 getActivity(),
-                view.findViewById(R.id.wallpaper_preview_image),
+                mWallpaperImage,
                 view.findViewById(R.id.wallpaper_preview_surface));
         mCurrentWallpaperFactory.createCurrentWallpaperInfos(
                 (homeWallpaper, lockWallpaper, presentationMode) -> {
                     mCurrentHomeWallpaper = homeWallpaper;
                     mWallpaperPreviewer.setWallpaper(mCurrentHomeWallpaper);
+                    updateThemePreviewColorPerWallpaper();
                 }, false);
-        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) {
-                mWallpaperPreviewer.updatePreviewCardRadius();
-                view.removeOnLayoutChangeListener(this);
-            }
-        });
 
         ViewGroup previewContainer = view.findViewById(R.id.theme_preview_container);
         previewContainer.setOnClickListener(v -> showFullPreview());
@@ -143,6 +140,16 @@
                 getLifecycle(),
                 getContext(),
                 previewContainer);
+
+        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) {
+                mWallpaperPreviewer.updatePreviewCardRadius();
+                updateThemePreviewColorPerWallpaper();
+                view.removeOnLayoutChangeListener(this);
+            }
+        });
         return view;
     }
 
@@ -168,6 +175,17 @@
         setUpOptions(savedInstanceState);
     }
 
+    private void updateThemePreviewColorPerWallpaper() {
+        if (mCurrentHomeWallpaper != null && mWallpaperImage.getMeasuredWidth() > 0
+                && mWallpaperImage.getMeasuredHeight() > 0) {
+            WallpaperColorsLoader.getWallpaperColors(
+                    mCurrentHomeWallpaper.getThumbAsset(getContext()),
+                    mWallpaperImage.getMeasuredWidth(),
+                    mWallpaperImage.getMeasuredHeight(),
+                    mThemeOptionPreviewer::updateColorForLauncherWidgets);
+        }
+    }
+
     private void applyTheme() {
         mThemeManager.apply(mSelectedTheme, new Callback() {
             @Override
diff --git a/src/com/android/customization/picker/theme/ThemeFullPreviewFragment.java b/src/com/android/customization/picker/theme/ThemeFullPreviewFragment.java
index 4f6faa6..dabd9aa 100644
--- a/src/com/android/customization/picker/theme/ThemeFullPreviewFragment.java
+++ b/src/com/android/customization/picker/theme/ThemeFullPreviewFragment.java
@@ -27,6 +27,7 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.ImageView;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
@@ -42,6 +43,7 @@
 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;
 
@@ -101,20 +103,13 @@
         Glide.get(getContext()).clearMemory();
 
         // Set wallpaper background.
+        ImageView wallpaperImageView = view.findViewById(R.id.wallpaper_preview_image);
         final WallpaperPreviewer wallpaperPreviewer = new WallpaperPreviewer(
                 getLifecycle(),
                 getActivity(),
-                view.findViewById(R.id.wallpaper_preview_image),
+                wallpaperImageView,
                 view.findViewById(R.id.wallpaper_preview_surface));
         wallpaperPreviewer.setWallpaper(mWallpaper);
-        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();
-                view.removeOnLayoutChangeListener(this);
-            }
-        });
 
         // Set theme option.
         final ThemeOptionPreviewer themeOptionPreviewer = new ThemeOptionPreviewer(
@@ -122,6 +117,19 @@
                 getContext(),
                 view.findViewById(R.id.theme_preview_container));
         themeOptionPreviewer.setThemeBundle(mThemeBundle);
+        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();
+                WallpaperColorsLoader.getWallpaperColors(
+                        mWallpaper.getThumbAsset(getContext()),
+                        wallpaperImageView.getMeasuredWidth(),
+                        wallpaperImageView.getMeasuredHeight(),
+                        themeOptionPreviewer::updateColorForLauncherWidgets);
+                view.removeOnLayoutChangeListener(this);
+            }
+        });
         return view;
     }
 
diff --git a/src/com/android/customization/picker/theme/ThemeOptionPreviewer.java b/src/com/android/customization/picker/theme/ThemeOptionPreviewer.java
index 5f2cc70..4105f67 100644
--- a/src/com/android/customization/picker/theme/ThemeOptionPreviewer.java
+++ b/src/com/android/customization/picker/theme/ThemeOptionPreviewer.java
@@ -18,6 +18,7 @@
 import static android.view.View.MeasureSpec.EXACTLY;
 import static android.view.View.MeasureSpec.makeMeasureSpec;
 
+import android.app.WallpaperColors;
 import android.content.Context;
 import android.content.res.ColorStateList;
 import android.content.res.Resources;
@@ -156,6 +157,31 @@
         setQsbRadius(previewInfo.bottomSheeetCornerRadius);
     }
 
+    /**
+     * Updates the color of widgets in launcher (like top status bar, smart space, and app name
+     * text) which will change its content color according to different wallpapers.
+     */
+    public void updateColorForLauncherWidgets(WallpaperColors colors) {
+        int color = mContext.getColor(
+                (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0
+                        ? R.color.text_color_light
+                        : R.color.text_color_dark);
+        // Update the top status bar clock text color.
+        mClock.setTextColor(color);
+        // Update the top status bar icon color.
+        ViewGroup iconsContainer = mContentView.findViewById(R.id.theme_preview_top_bar_icons);
+        for (int i = 0; i < iconsContainer.getChildCount(); i++) {
+            ((ImageView) iconsContainer.getChildAt(i))
+                    .setImageTintList(ColorStateList.valueOf(color));
+        }
+        // Update smart space date color.
+        ((TextView) mContentView.findViewById(R.id.smart_space_date)).setTextColor(color);
+        // Update shape app icon name text color.
+        for (int id : mShapeIconAppNameIds) {
+            ((TextView) mContentView.findViewById(id)).setTextColor(color);
+        }
+    }
+
     @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
     @MainThread
     public void onResume() {