Merge "Get rid of setZOrderOnTop for theme preview" into ub-launcher3-master
diff --git a/src/com/android/customization/model/theme/DefaultThemeProvider.java b/src/com/android/customization/model/theme/DefaultThemeProvider.java
index c0937b4..60eb66d 100644
--- a/src/com/android/customization/model/theme/DefaultThemeProvider.java
+++ b/src/com/android/customization/model/theme/DefaultThemeProvider.java
@@ -28,6 +28,7 @@
 import static com.android.customization.model.ResourceConstants.SYSUI_PACKAGE;
 
 import android.content.Context;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.res.Resources.NotFoundException;
 import android.text.TextUtils;
@@ -212,6 +213,11 @@
             try {
                 builder.addShapePreviewIcon(
                         mContext.getPackageManager().getApplicationIcon(packageName));
+                // Add the shape icon app name.
+                ApplicationInfo appInfo = mContext.getPackageManager()
+                        .getApplicationInfo(packageName, /* flag= */ 0);
+                builder.addShapePreviewIconName(
+                        String.valueOf(mContext.getPackageManager().getApplicationLabel(appInfo)));
             } catch (NameNotFoundException e) {
                 Log.d(TAG, "Couldn't find app " + packageName + ", won't use it for icon shape"
                         + "preview");
diff --git a/src/com/android/customization/model/theme/OverlayThemeExtractor.java b/src/com/android/customization/model/theme/OverlayThemeExtractor.java
index 9c85fa4..f1698f6 100644
--- a/src/com/android/customization/model/theme/OverlayThemeExtractor.java
+++ b/src/com/android/customization/model/theme/OverlayThemeExtractor.java
@@ -8,6 +8,7 @@
 import android.content.Context;
 import android.content.om.OverlayInfo;
 import android.content.om.OverlayManager;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.res.Resources;
 import android.content.res.Resources.NotFoundException;
@@ -102,6 +103,11 @@
                 builder.addShapePreviewIcon(
                         mContext.getPackageManager().getApplicationIcon(
                                 packageName));
+                // Add the shape icon app name.
+                ApplicationInfo appInfo = mContext.getPackageManager()
+                        .getApplicationInfo(packageName, /* flag= */ 0);
+                builder.addShapePreviewIconName(
+                        String.valueOf(mContext.getPackageManager().getApplicationLabel(appInfo)));
             } catch (NameNotFoundException e) {
                 Log.d(TAG, "Couldn't find app " + packageName
                         + ", won't use it for icon shape preview");
diff --git a/src/com/android/customization/model/theme/ThemeBundle.java b/src/com/android/customization/model/theme/ThemeBundle.java
index 14b2dcd..9928c76 100644
--- a/src/com/android/customization/model/theme/ThemeBundle.java
+++ b/src/com/android/customization/model/theme/ThemeBundle.java
@@ -251,12 +251,13 @@
         public final List<Drawable> icons;
         public final Drawable shapeDrawable;
         public final List<Drawable> shapeAppIcons;
+        public final List<String> shapeAppIconNames;
         @Dimension public final int bottomSheeetCornerRadius;
 
         private PreviewInfo(Context context, Typeface bodyFontFamily, Typeface headlineFontFamily,
                 int colorAccentLight, int colorAccentDark, List<Drawable> icons,
                 Drawable shapeDrawable, @Dimension int cornerRadius,
-                List<Drawable> shapeAppIcons) {
+                List<Drawable> shapeAppIcons, List<String> shapeAppIconNames) {
             this.bodyFontFamily = bodyFontFamily;
             this.headlineFontFamily = headlineFontFamily;
             this.colorAccentLight = colorAccentLight;
@@ -265,6 +266,7 @@
             this.shapeDrawable = shapeDrawable;
             this.bottomSheeetCornerRadius = cornerRadius;
             this.shapeAppIcons = shapeAppIcons;
+            this.shapeAppIconNames = shapeAppIconNames;
         }
 
         /**
@@ -292,6 +294,7 @@
         @Dimension private int mCornerRadius;
         protected Map<String, String> mPackages = new HashMap<>();
         private List<Drawable> mAppIcons = new ArrayList<>();
+        private List<String> mAppIconNames = new ArrayList<>();
 
         public ThemeBundle build(Context context) {
             return new ThemeBundle(mTitle, mPackages, mIsDefault, createPreviewInfo(context));
@@ -322,7 +325,8 @@
                 }
             }
             return new PreviewInfo(context, mBodyFontFamily, mHeadlineFontFamily, mColorAccentLight,
-                    mColorAccentDark, mIcons, shapeDrawable, mCornerRadius, shapeIcons);
+                    mColorAccentDark, mIcons, shapeDrawable, mCornerRadius, shapeIcons,
+                    mAppIconNames);
         }
 
         public Map<String, String> getPackages() {
@@ -388,6 +392,11 @@
             return this;
         }
 
+        public Builder addShapePreviewIconName(String appIconName) {
+            mAppIconNames.add(appIconName);
+            return this;
+        }
+
         public Builder setBottomSheetCornerRadius(@Dimension int radius) {
             mCornerRadius = radius;
             return this;
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..efaa38c 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;
@@ -150,12 +151,38 @@
         setHeadlineFont(previewInfo.headlineFontFamily);
         setTopBarIcons(previewInfo.icons);
         setAppIconShape(previewInfo.shapeAppIcons);
+        setAppIconName(previewInfo.shapeAppIconNames);
         setColorAndIconsSection(previewInfo.icons, previewInfo.shapeDrawable,
                 previewInfo.resolveAccentColor(mContext.getResources()));
         setColorAndIconsBoxRadius(previewInfo.bottomSheeetCornerRadius);
         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() {
@@ -172,20 +199,22 @@
     }
 
     private void setHeadlineFont(Typeface headlineFont) {
+        // Update font of status bar clock.
         mClock.setTypeface(headlineFont);
 
+        // Update font of the smart space date.
         TextView date = mContentView.findViewById(R.id.smart_space_date);
         date.setTypeface(headlineFont);
         // TODO(chihhangchuang): Use real date.
         date.setText("Friday, Nov 12");
 
-        // TODO(chihhangchuang): Query the app name for icon shapes, we can get package name from
-        // res/values/override.xml to query the app name.
+        // Update font of app names.
         for (int id : mShapeIconAppNameIds) {
             TextView appName = mContentView.findViewById(id);
             appName.setTypeface(headlineFont);
         }
 
+        // Update font of color/icons section title.
         TextView colorIconsSectionTitle = mContentView.findViewById(R.id.color_icons_section_title);
         colorIconsSectionTitle.setTypeface(headlineFont);
     }
@@ -211,6 +240,13 @@
         }
     }
 
+    private void setAppIconName(List<String> appIconNames) {
+        for (int i = 0; i < mShapeIconAppNameIds.length && i < appIconNames.size(); i++) {
+            TextView appName = mContentView.findViewById(mShapeIconAppNameIds[i]);
+            appName.setText(appIconNames.get(i));
+        }
+    }
+
     private void setColorAndIconsSection(List<Drawable> icons, Drawable shapeDrawable,
                                          int accentColor) {
         // Set QS icons and background.