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: I7c9496949d35061049e380f4d21621610590e6b7
diff --git a/src/com/android/wallpaper/picker/CategoryFragment.java b/src/com/android/wallpaper/picker/CategoryFragment.java
index da81111..4150493 100755
--- a/src/com/android/wallpaper/picker/CategoryFragment.java
+++ b/src/com/android/wallpaper/picker/CategoryFragment.java
@@ -663,11 +663,9 @@
             }
 
             WallpaperColorsLoader.getWallpaperColors(
-                    getContext(),
-                    wallpaperInfo.getThumbAsset(getContext()),
-                    thumbnailView.getMeasuredWidth(),
-                    thumbnailView.getMeasuredHeight(),
-                    mLockScreenOverlayUpdater::setColor);
+                        activity,
+                        wallpaperInfo.getThumbAsset(activity),
+                        mLockScreenOverlayUpdater::setColor);
         }
 
         thumbnailView.setOnClickListener(view -> {
diff --git a/src/com/android/wallpaper/picker/ImagePreviewFragment.java b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
index fc57026..93bfa33 100755
--- a/src/com/android/wallpaper/picker/ImagePreviewFragment.java
+++ b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
@@ -212,12 +212,8 @@
     @Override
     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
-
-        WallpaperColorsLoader.getWallpaperColors(
-                getContext(),
-                mWallpaperAsset,
-                mWallpaperSurface.getMeasuredWidth(),
-                mWallpaperSurface.getMeasuredHeight(),
+        WallpaperColorsLoader.getWallpaperColors(getContext(),
+                mWallpaper.getThumbAsset(getContext()),
                 mLockScreenOverlayUpdater::setColor);
     }
 
diff --git a/src/com/android/wallpaper/widget/LockScreenOverlayUpdater.java b/src/com/android/wallpaper/widget/LockScreenOverlayUpdater.java
index 83b2bb7..7323701 100644
--- a/src/com/android/wallpaper/widget/LockScreenOverlayUpdater.java
+++ b/src/com/android/wallpaper/widget/LockScreenOverlayUpdater.java
@@ -26,6 +26,7 @@
 import android.widget.TextView;
 
 import androidx.annotation.MainThread;
+import androidx.annotation.Nullable;
 import androidx.lifecycle.Lifecycle;
 import androidx.lifecycle.LifecycleObserver;
 import androidx.lifecycle.OnLifecycleEvent;
@@ -98,13 +99,12 @@
      * Sets the content's color based on the wallpaper's {@link WallpaperColors}.
      *
      * @param colors the {@link WallpaperColors} of the wallpaper which the lock screen overlay
-     *               will attach to
+     *               will attach to, or {@code null} to use light color as default
      */
-    public void setColor(WallpaperColors colors) {
-        int color = mContext.getColor(
-                (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0
-                        ? R.color.text_color_light
-                        : R.color.text_color_dark);
+    public void setColor(@Nullable WallpaperColors colors) {
+        int color = mContext.getColor(colors == null
+                || (colors.getColorHints() & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) == 0
+                ? R.color.text_color_light : R.color.text_color_dark);
         mLockIcon.setImageTintList(ColorStateList.valueOf(color));
         mLockDate.setTextColor(color);
         mLockTime.setTextColor(color);
diff --git a/src/com/android/wallpaper/widget/WallpaperColorsLoader.java b/src/com/android/wallpaper/widget/WallpaperColorsLoader.java
index 22d1ab2..c900763 100644
--- a/src/com/android/wallpaper/widget/WallpaperColorsLoader.java
+++ b/src/com/android/wallpaper/widget/WallpaperColorsLoader.java
@@ -18,45 +18,47 @@
 import android.app.WallpaperColors;
 import android.content.Context;
 import android.graphics.Bitmap;
+import android.graphics.Point;
 import android.util.Log;
+import android.view.Display;
+import android.view.WindowManager;
 
 import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
 
 import com.android.wallpaper.asset.Asset;
 import com.android.wallpaper.asset.BitmapCachingAsset;
+import com.android.wallpaper.util.ScreenSizeCalculator;
 
 /** A class to load the {@link WallpaperColors} from wallpaper {@link Asset}. */
 public class WallpaperColorsLoader {
     private static final String TAG = "WallpaperColorsLoader";
 
-    /** Callback of loading {@link WallpaperColors}. */
+    /** Callback of loading a {@link WallpaperColors}. */
     public interface Callback {
-        /** Gets called when {@link WallpaperColors} parsing is succeed. */
-        void onSuccess(WallpaperColors colors);
-
-        /** Gets called when {@link WallpaperColors} parsing is failed. */
-        default void onFailure() {
-            Log.i(TAG, "Can't get wallpaper colors from a null bitmap.");
-        }
+        /** Gets called when a {@link WallpaperColors} is loaded. */
+        void onLoaded(@Nullable WallpaperColors colors);
     }
 
     /** Gets the {@link WallpaperColors} from the wallpaper {@link Asset}. */
     public static void getWallpaperColors(Context context, @NonNull Asset asset,
-                                          int targetWidth, int targetHeight,
                                           @NonNull Callback callback) {
-        new BitmapCachingAsset(context, asset).decodeBitmap(targetWidth, targetHeight, bitmap -> {
+        Display display = context.getSystemService(WindowManager.class).getDefaultDisplay();
+        Point screen = ScreenSizeCalculator.getInstance().getScreenSize(display);
+        new BitmapCachingAsset(context, asset).decodeBitmap(screen.y / 2, screen.x / 2, bitmap -> {
             if (bitmap != null) {
                 boolean shouldRecycle = false;
                 if (bitmap.getConfig() == Bitmap.Config.HARDWARE) {
                     bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false);
                     shouldRecycle = true;
                 }
-                callback.onSuccess(WallpaperColors.fromBitmap(bitmap));
+                callback.onLoaded(WallpaperColors.fromBitmap(bitmap));
                 if (shouldRecycle) {
                     bitmap.recycle();
                 }
             } else {
-                callback.onFailure();
+                Log.i(TAG, "Can't get wallpaper colors from a null bitmap, uses null color.");
+                callback.onLoaded(null);
             }
         });
     }