Handles onFailure case of getting wallpaper color

- Use a fixed bitmap size to be stored, so that the full preview won't decode the asset, uses tha catched result.
- If returns null wallpaper color, uses light color as default.

Test: Manually
Fixes: 157889638
Change-Id: Iaf513d79eb2d6154b0bf50e333f607205c3eea9d
diff --git a/src/com/android/customization/picker/theme/CustomThemeNameFragment.java b/src/com/android/customization/picker/theme/CustomThemeNameFragment.java
index 3411542..9bc6772 100644
--- a/src/com/android/customization/picker/theme/CustomThemeNameFragment.java
+++ b/src/com/android/customization/picker/theme/CustomThemeNameFragment.java
@@ -20,6 +20,7 @@
 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;
@@ -84,6 +85,14 @@
         CustomizationInjector injector = (CustomizationInjector) InjectorProvider.getInjector();
         mCustomizationPreferences = injector.getCustomizationPreferences(getContext());
 
+        // Set theme option.
+        ViewGroup previewContainer = view.findViewById(R.id.theme_preview_container);
+        previewContainer.setOnClickListener(v -> showFullPreview());
+        mThemeOptionPreviewer = new ThemeOptionPreviewer(getLifecycle(), getContext(),
+                previewContainer);
+        PreviewInfo previewInfo = mCustomThemeManager.buildCustomThemePreviewInfo(getContext());
+        mThemeOptionPreviewer.setPreviewInfo(previewInfo);
+
         // Set wallpaper background.
         mWallpaperImage = view.findViewById(R.id.wallpaper_preview_image);
         final WallpaperPreviewer wallpaperPreviewer = new WallpaperPreviewer(
@@ -95,17 +104,15 @@
                 (homeWallpaper, lockWallpaper, presentationMode) -> {
                     mCurrentHomeWallpaper = homeWallpaper;
                     wallpaperPreviewer.setWallpaper(homeWallpaper);
-                    updateThemePreviewColorPerWallpaper();
+                    Context context =  getContext();
+                    if (context != null) {
+                        WallpaperColorsLoader.getWallpaperColors(
+                                context,
+                                mCurrentHomeWallpaper.getThumbAsset(context),
+                                mThemeOptionPreviewer::updateColorForLauncherWidgets);
+                    }
                 }, false);
 
-        // Set theme option.
-        ViewGroup previewContainer = view.findViewById(R.id.theme_preview_container);
-        previewContainer.setOnClickListener(v -> showFullPreview());
-        mThemeOptionPreviewer = new ThemeOptionPreviewer(getLifecycle(), getContext(),
-                previewContainer);
-        PreviewInfo previewInfo = mCustomThemeManager.buildCustomThemePreviewInfo(getContext());
-        mThemeOptionPreviewer.setPreviewInfo(previewInfo);
-
         // Set theme default name.
         mNameEditor = view.findViewById(R.id.custom_theme_name);
         mNameEditor.setText(
@@ -116,25 +123,12 @@
             public void onLayoutChange(View v, int left, int top, int right, int bottom,
                                        int oldLeft, int oldTop, int oldRight, int oldBottom) {
                 wallpaperPreviewer.updatePreviewCardRadius();
-                updateThemePreviewColorPerWallpaper();
                 view.removeOnLayoutChangeListener(this);
             }
         });
         return view;
     }
 
-    private void updateThemePreviewColorPerWallpaper() {
-        if (mCurrentHomeWallpaper != null && mWallpaperImage.getMeasuredWidth() > 0
-                && mWallpaperImage.getMeasuredHeight() > 0) {
-            WallpaperColorsLoader.getWallpaperColors(
-                    getContext(),
-                    mCurrentHomeWallpaper.getThumbAsset(getContext()),
-                    mWallpaperImage.getMeasuredWidth(),
-                    mWallpaperImage.getMeasuredHeight(),
-                    mThemeOptionPreviewer::updateColorForLauncherWidgets);
-        }
-    }
-
     private String getCustomThemeDefaultName(boolean mIsDefinedTheme) {
         if (mIsDefinedTheme) {
             // For new custom theme. use custom themes amount plus 1 as default naming.
diff --git a/src/com/android/customization/picker/theme/ThemeFragment.java b/src/com/android/customization/picker/theme/ThemeFragment.java
index 5c5dcdd..fa9d533 100644
--- a/src/com/android/customization/picker/theme/ThemeFragment.java
+++ b/src/com/android/customization/picker/theme/ThemeFragment.java
@@ -123,6 +123,13 @@
                 .getCurrentWallpaperFactory(getActivity().getApplicationContext());
         mOptionsContainer = view.findViewById(R.id.options_container);
 
+        ViewGroup previewContainer = view.findViewById(R.id.theme_preview_container);
+        previewContainer.setOnClickListener(v -> showFullPreview());
+        mThemeOptionPreviewer = new ThemeOptionPreviewer(
+                getLifecycle(),
+                getContext(),
+                previewContainer);
+
         // Set Wallpaper background.
         mWallpaperImage = view.findViewById(R.id.wallpaper_preview_image);
         mWallpaperPreviewer = new WallpaperPreviewer(
@@ -134,22 +141,20 @@
                 (homeWallpaper, lockWallpaper, presentationMode) -> {
                     mCurrentHomeWallpaper = homeWallpaper;
                     mWallpaperPreviewer.setWallpaper(mCurrentHomeWallpaper);
-                    updateThemePreviewColorPerWallpaper();
+                    Context context = getContext();
+                    if (context != null) {
+                        WallpaperColorsLoader.getWallpaperColors(
+                                context,
+                                mCurrentHomeWallpaper.getThumbAsset(context),
+                                mThemeOptionPreviewer::updateColorForLauncherWidgets);
+                    }
                 }, false);
 
-        ViewGroup previewContainer = view.findViewById(R.id.theme_preview_container);
-        previewContainer.setOnClickListener(v -> showFullPreview());
-        mThemeOptionPreviewer = new ThemeOptionPreviewer(
-                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);
             }
         });
@@ -178,21 +183,6 @@
         setUpOptions(savedInstanceState);
     }
 
-    private void updateThemePreviewColorPerWallpaper() {
-        if (getContext() == null) {
-            return;
-        }
-        if (mCurrentHomeWallpaper != null && mWallpaperImage.getMeasuredWidth() > 0
-                && mWallpaperImage.getMeasuredHeight() > 0) {
-            WallpaperColorsLoader.getWallpaperColors(
-                    getContext(),
-                    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 253720e..1821f84 100644
--- a/src/com/android/customization/picker/theme/ThemeFullPreviewFragment.java
+++ b/src/com/android/customization/picker/theme/ThemeFullPreviewFragment.java
@@ -21,6 +21,7 @@
 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;
@@ -125,13 +126,13 @@
             public void onLayoutChange(View v, int left, int top, int right, int bottom,
                                        int oldLeft, int oldTop, int oldRight, int oldBottom) {
                 wallpaperPreviewer.updatePreviewCardRadius();
-                // Let's use half size of full preview card to reduce memory and loading time.
-                WallpaperColorsLoader.getWallpaperColors(
-                        getContext(),
-                        mWallpaper.getThumbAsset(getContext()),
-                        wallpaperImageView.getMeasuredWidth() / 2,
-                        wallpaperImageView.getMeasuredHeight() / 2,
-                        themeOptionPreviewer::updateColorForLauncherWidgets);
+                Context context = getContext();
+                if (context != null) {
+                    WallpaperColorsLoader.getWallpaperColors(
+                            context,
+                            mWallpaper.getThumbAsset(context),
+                            themeOptionPreviewer::updateColorForLauncherWidgets);
+                }
                 view.removeOnLayoutChangeListener(this);
             }
         });
diff --git a/src/com/android/customization/picker/theme/ThemeOptionPreviewer.java b/src/com/android/customization/picker/theme/ThemeOptionPreviewer.java
index f24cf64..1f99c1e 100644
--- a/src/com/android/customization/picker/theme/ThemeOptionPreviewer.java
+++ b/src/com/android/customization/picker/theme/ThemeOptionPreviewer.java
@@ -38,6 +38,7 @@
 import android.widget.TextView;
 
 import androidx.annotation.MainThread;
+import androidx.annotation.Nullable;
 import androidx.cardview.widget.CardView;
 import androidx.lifecycle.Lifecycle;
 import androidx.lifecycle.LifecycleObserver;
@@ -172,10 +173,13 @@
     /**
      * 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.
+     *
+     * @param colors the {@link WallpaperColors} of the wallpaper, or {@code null} to use light
+     *               color as default
      */
-    public void updateColorForLauncherWidgets(WallpaperColors colors) {
-        boolean useLightTextColor =
-                (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0;
+    public void updateColorForLauncherWidgets(@Nullable WallpaperColors colors) {
+        boolean useLightTextColor = colors == null
+                || (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0;
         int textColor = mContext.getColor(useLightTextColor
                 ? R.color.text_color_light
                 : R.color.text_color_dark);