[automerger skipped] [automerged blank] Import translations. DO NOT MERGE ANYWHERE 2p: a4bc08b309 am: a6469e3a10 -s ours

am skip reason: subject contains skip directive

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/WallpaperPicker2/+/15699396

Change-Id: Id32dd63bca2a55934589ffb6d300569e289ba58b
diff --git a/res/values-zh-rHK/strings.xml b/res/values-zh-rHK/strings.xml
index 94f4912..27930bf 100644
--- a/res/values-zh-rHK/strings.xml
+++ b/res/values-zh-rHK/strings.xml
@@ -32,10 +32,10 @@
     <string name="home_screen_message" msgid="106444102822522813">"主畫面"</string>
     <string name="lock_screen_message" msgid="1534506081955058013">"上鎖畫面"</string>
     <string name="home_and_lock_short_label" msgid="2937922943541927983">"主畫面及上鎖畫面"</string>
-    <string name="set_wallpaper_dialog_message" msgid="2110475703996853076">"設定以下畫面的桌布:"</string>
+    <string name="set_wallpaper_dialog_message" msgid="2110475703996853076">"設定桌布"</string>
     <string name="set_wallpaper_home_screen_destination" msgid="7315594722013109354">"主畫面"</string>
     <string name="set_wallpaper_lock_screen_destination" msgid="6224685559375417945">"上鎖畫面"</string>
-    <string name="set_wallpaper_both_destination" msgid="233216830256235288">"主畫面和螢幕鎖定畫面"</string>
+    <string name="set_wallpaper_both_destination" msgid="233216830256235288">"主畫面和上鎖畫面"</string>
     <string name="no_backup_image_wallpaper_label" msgid="6316627676107284851">"正在輪替顯示圖片桌布"</string>
     <string name="permission_needed_explanation" msgid="139166837541426823">"如要在此顯示目前的桌布,「<xliff:g id="APP_NAME">%1$s</xliff:g>」需要存取您裝置的儲存空間。"</string>
     <string name="permission_needed_explanation_go_to_settings" msgid="3923551582092599609">"如要在此顯示目前的桌布,「桌布」需要存取您裝置的儲存空間。\n\n如要變更此設定,請前往「桌布」應用程式資訊的「權限」部分。"</string>
diff --git a/src/com/android/wallpaper/asset/LiveWallpaperThumbAsset.java b/src/com/android/wallpaper/asset/LiveWallpaperThumbAsset.java
index 227f8b8..259788f 100755
--- a/src/com/android/wallpaper/asset/LiveWallpaperThumbAsset.java
+++ b/src/com/android/wallpaper/asset/LiveWallpaperThumbAsset.java
@@ -267,8 +267,21 @@
             Drawable thumb = mInfo.loadThumbnail(mPackageManager);
 
             // Live wallpaper components may or may not specify a thumbnail drawable.
-            if (thumb != null && thumb instanceof BitmapDrawable) {
+            if (thumb instanceof BitmapDrawable) {
                 return ((BitmapDrawable) thumb).getBitmap();
+            } else if (thumb != null) {
+                Bitmap bitmap;
+                if (thumb.getIntrinsicWidth() > 0 && thumb.getIntrinsicHeight() > 0) {
+                    bitmap = Bitmap.createBitmap(thumb.getIntrinsicWidth(),
+                            thumb.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
+                } else {
+                    return null;
+                }
+
+                Canvas canvas = new Canvas(bitmap);
+                thumb.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
+                thumb.draw(canvas);
+                return bitmap;
             }
 
             // If no thumbnail was specified, return a null bitmap.
diff --git a/src/com/android/wallpaper/model/LiveWallpaperInfo.java b/src/com/android/wallpaper/model/LiveWallpaperInfo.java
index 26d2daf..dd9537c 100755
--- a/src/com/android/wallpaper/model/LiveWallpaperInfo.java
+++ b/src/com/android/wallpaper/model/LiveWallpaperInfo.java
@@ -77,7 +77,7 @@
     public static final String ATTR_SERVICE = "service";
 
     /**
-     * Create a new {@link LiveWallpaperInfo} from an XML {@link AttributeSet}
+     * Creates a new {@link LiveWallpaperInfo} from an XML {@link AttributeSet}
      * @param context used to construct the {@link android.app.WallpaperInfo} associated with the
      *                new {@link LiveWallpaperInfo}
      * @param categoryId Id of the category the new wallpaper will belong to
@@ -94,6 +94,17 @@
         }
         String packageName = attrs.getAttributeValue(null, ATTR_PACKAGE);
         String serviceName = attrs.getAttributeValue(null, ATTR_SERVICE);
+        return fromPackageAndServiceName(context, categoryId, wallpaperId, packageName,
+                serviceName);
+    }
+
+    /**
+     * Creates a new {@link LiveWallpaperInfo} from its individual components
+     * @return a newly created {@link LiveWallpaperInfo} or {@code null} if one couldn't be created.
+     */
+    @Nullable
+    public static LiveWallpaperInfo fromPackageAndServiceName(Context context, String categoryId,
+            String wallpaperId, String packageName, String serviceName) {
         if (TextUtils.isEmpty(serviceName)) {
             Log.w(TAG, "Live wallpaper declaration without service: " + wallpaperId);
             return null;
@@ -309,7 +320,10 @@
         return wallpaperInfos;
     }
 
-    private static boolean isSystemApp(ApplicationInfo appInfo) {
+    /**
+     * @return whether the given app is a system app
+     */
+    public static boolean isSystemApp(ApplicationInfo appInfo) {
         return (appInfo.flags & (ApplicationInfo.FLAG_SYSTEM
                 | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
     }
diff --git a/src/com/android/wallpaper/module/DefaultWallpaperPersister.java b/src/com/android/wallpaper/module/DefaultWallpaperPersister.java
index bf86585..abe1728 100755
--- a/src/com/android/wallpaper/module/DefaultWallpaperPersister.java
+++ b/src/com/android/wallpaper/module/DefaultWallpaperPersister.java
@@ -17,6 +17,7 @@
 
 import android.annotation.SuppressLint;
 import android.app.Activity;
+import android.app.WallpaperColors;
 import android.app.WallpaperManager;
 import android.content.Context;
 import android.content.res.Resources;
@@ -29,6 +30,7 @@
 import android.graphics.drawable.BitmapDrawable;
 import android.os.AsyncTask;
 import android.os.ParcelFileDescriptor;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.Display;
 import android.view.WindowManager;
@@ -347,7 +349,7 @@
     @Override
     public boolean finalizeWallpaperForNextRotation(List<String> attributions, String actionUrl,
             int actionLabelRes, int actionIconRes, String collectionId, int wallpaperId) {
-        return finalizeWallpaperForRotatingComponent(attributions, actionUrl, actionLabelRes,
+        return saveStaticWallpaperMetadata(attributions, actionUrl, actionLabelRes,
                 actionIconRes, collectionId, wallpaperId);
     }
 
@@ -364,18 +366,12 @@
             return false;
         }
 
-        return finalizeWallpaperForRotatingComponent(attributions, actionUrl, actionLabelRes,
+        return saveStaticWallpaperMetadata(attributions, actionUrl, actionLabelRes,
                 actionIconRes, collectionId, wallpaperId);
     }
 
-    /**
-     * Finalizes wallpaper metadata by persisting them to SharedPreferences and finalizes the
-     * wallpaper image for live rotating components by copying the "preview" image to the "final"
-     * image file location.
-     *
-     * @return Whether the operation was successful.
-     */
-    private boolean finalizeWallpaperForRotatingComponent(List<String> attributions,
+    @Override
+    public boolean saveStaticWallpaperMetadata(List<String> attributions,
             String actionUrl,
             int actionLabelRes,
             int actionIconRes,
@@ -461,27 +457,25 @@
                 scaledCropRect.top,
                 scaledCropRect.width(),
                 scaledCropRect.height());
-
-        // Set wallpaper to home-only instead of both home and lock if there's a distinct lock-only
-        // static wallpaper set so we don't override the lock wallpaper.
-        boolean isLockWallpaperSet = isSeparateLockScreenWallpaperSet();
-
-        int whichWallpaper = (isLockWallpaperSet)
-                ? WallpaperManagerCompat.FLAG_SYSTEM
-                : WallpaperManagerCompat.FLAG_SYSTEM | WallpaperManagerCompat.FLAG_LOCK;
+        int whichWallpaper = getDefaultWhichWallpaper();
 
         return setBitmapToWallpaperManagerCompat(wallpaperBitmap, false /* allowBackup */,
                 whichWallpaper);
     }
 
-    /**
-     * Sets a wallpaper bitmap to the {@link WallpaperManagerCompat}.
-     *
-     * @return an integer wallpaper ID. This is an actual wallpaper ID on N and later versions of
-     * Android, otherwise on pre-N versions of Android will return a positive integer when the
-     * operation was successful and zero if the operation encountered an error.
+    /*
+     * Note: this method will return use home-only (FLAG_SYSTEM) instead of both home and lock
+     * if there's a distinct lock-only static wallpaper set so we don't override the lock wallpaper.
      */
-    private int setBitmapToWallpaperManagerCompat(Bitmap wallpaperBitmap, boolean allowBackup,
+    @Override
+    public int getDefaultWhichWallpaper() {
+        return isSeparateLockScreenWallpaperSet()
+                ? WallpaperManagerCompat.FLAG_SYSTEM
+                : WallpaperManagerCompat.FLAG_SYSTEM | WallpaperManagerCompat.FLAG_LOCK;
+    }
+
+    @Override
+    public int setBitmapToWallpaperManagerCompat(Bitmap wallpaperBitmap, boolean allowBackup,
             int whichWallpaper) {
         ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();
         if (wallpaperBitmap.compress(CompressFormat.PNG, DEFAULT_COMPRESS_QUALITY, tmpOut)) {
@@ -797,6 +791,7 @@
             mWallpaperManager.forgetLoadedWallpaper();
             mBitmap = ((BitmapDrawable) mWallpaperManagerCompat.getDrawable()).getBitmap();
             long bitmapHash = BitmapUtils.generateHashCode(mBitmap);
+            WallpaperColors colors = WallpaperColors.fromBitmap(mBitmap);
 
             mWallpaperPreferences.setHomeWallpaperHashCode(bitmapHash);
 
@@ -811,6 +806,10 @@
             mWallpaperPreferences.setHomeWallpaperCollectionId(
                     mWallpaper.getCollectionId(mAppContext));
             mWallpaperPreferences.setHomeWallpaperRemoteId(mWallpaper.getWallpaperId());
+            mWallpaperPreferences.storeLatestHomeWallpaper(
+                    TextUtils.isEmpty(mWallpaper.getWallpaperId()) ? String.valueOf(bitmapHash)
+                            : mWallpaper.getWallpaperId(),
+                    mWallpaper, mBitmap, colors);
         }
 
         private void setImageWallpaperLockMetadata(int lockWallpaperId) {
diff --git a/src/com/android/wallpaper/module/WallpaperPersister.java b/src/com/android/wallpaper/module/WallpaperPersister.java
index ec26065..07ccbcd 100755
--- a/src/com/android/wallpaper/module/WallpaperPersister.java
+++ b/src/com/android/wallpaper/module/WallpaperPersister.java
@@ -23,6 +23,7 @@
 import androidx.annotation.Nullable;
 
 import com.android.wallpaper.asset.Asset;
+import com.android.wallpaper.compat.WallpaperManagerCompat;
 import com.android.wallpaper.model.WallpaperInfo;
 
 import java.util.List;
@@ -113,6 +114,43 @@
                                              String collectionId, int wallpaperId);
 
     /**
+     * Finalizes wallpaper metadata by persisting them to SharedPreferences and finalizes the
+     * wallpaper image for live rotating components by copying the "preview" image to the "final"
+     * image file location.
+     *
+     * @param attributions List of attribution items.
+     * @param actionUrl    The action or "explore" URL for the wallpaper.
+     * @param actionLabelRes Resource ID of the action label
+     * @param actionIconRes Resource ID of the action icon
+     * @param collectionId ID of this wallpaper's collection.
+     * @param wallpaperId  Wallpaper ID that uniquely identifies the wallpaper image.
+     * @return Whether the operation was successful.
+     */
+    boolean saveStaticWallpaperMetadata(List<String> attributions,
+            String actionUrl,
+            int actionLabelRes,
+            int actionIconRes,
+            String collectionId,
+            int wallpaperId);
+
+    /**
+     * @return the flag indicating which wallpaper to set when we're trying to set a wallpaper with
+     * no user intervention. The idea is that if there's a static wallpaper on lock, we will only
+     * override home, otherwise both
+     */
+    int getDefaultWhichWallpaper();
+
+    /**
+     * Sets a wallpaper bitmap to the {@link WallpaperManagerCompat}.
+     *
+     * @return an integer wallpaper ID. This is an actual wallpaper ID on N and later versions of
+     * Android, otherwise on pre-N versions of Android will return a positive integer when the
+     * operation was successful and zero if the operation encountered an error.
+     */
+    int setBitmapToWallpaperManagerCompat(Bitmap wallpaperBitmap, boolean allowBackup,
+            int whichWallpaper);
+
+    /**
      * Saves the last wallpaper which showed a preview from this app.
      */
     void setWallpaperInfoInPreview(WallpaperInfo wallpaper);
diff --git a/src/com/android/wallpaper/module/WallpaperPreferences.java b/src/com/android/wallpaper/module/WallpaperPreferences.java
index 2a0fc39..e1dd60d 100755
--- a/src/com/android/wallpaper/module/WallpaperPreferences.java
+++ b/src/com/android/wallpaper/module/WallpaperPreferences.java
@@ -16,11 +16,16 @@
 package com.android.wallpaper.module;
 
 import android.annotation.TargetApi;
+import android.app.WallpaperColors;
+import android.graphics.Bitmap;
 import android.os.Build;
 
 import androidx.annotation.IntDef;
+import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
+import com.android.wallpaper.model.LiveWallpaperInfo;
+import com.android.wallpaper.model.WallpaperInfo;
 import com.android.wallpaper.module.WallpaperPersister.Destination;
 
 import java.util.List;
@@ -512,4 +517,27 @@
             DAILY_WALLPAPER_UPDATE_PENDING})
     @interface PendingDailyWallpaperUpdateStatus {
     }
+
+    /**
+     * Stores the given live wallpaper in the recent wallpapers list
+     * @param wallpaperId unique identifier for this wallpaper
+     * @param wallpaper {@link LiveWallpaperInfo} for the applied wallpaper
+     * @param colors WallpaperColors to be used as placeholder for quickswitching
+     */
+    default void storeLatestHomeWallpaper(String wallpaperId,
+            @NonNull LiveWallpaperInfo wallpaper, WallpaperColors colors) {
+        // Do nothing in the default case.
+    }
+
+    /**
+     * Stores the given static wallpaper data in the recent wallpapers list.
+     * @param wallpaperId unique identifier for this wallpaper
+     * @param wallpaper {@link WallpaperInfo} for the applied wallpaper
+     * @param croppedWallpaperBitmap wallpaper bitmap exactly as applied to WallaperManager
+     * @param colors WallpaperColors to be used as placeholder for quickswitching
+     */
+    default void storeLatestHomeWallpaper(String wallpaperId, @NonNull WallpaperInfo wallpaper,
+            @NonNull Bitmap croppedWallpaperBitmap, WallpaperColors colors) {
+        // Do nothing in the default case.
+    }
 }
diff --git a/src/com/android/wallpaper/module/WallpaperSetter.java b/src/com/android/wallpaper/module/WallpaperSetter.java
index 7f2be7c..2bc2f5c 100644
--- a/src/com/android/wallpaper/module/WallpaperSetter.java
+++ b/src/com/android/wallpaper/module/WallpaperSetter.java
@@ -2,6 +2,7 @@
 
 import android.app.Activity;
 import android.app.ProgressDialog;
+import android.app.WallpaperColors;
 import android.app.WallpaperManager;
 import android.content.pm.ActivityInfo;
 import android.graphics.Point;
@@ -97,7 +98,7 @@
                     dimensions, visibleRawWallpaperRect, wallpaperScale);
 
             setCurrentWallpaper(containerActivity, wallpaper, wallpaperAsset, destination,
-                    wallpaperScale, cropRect, callback);
+                    wallpaperScale, cropRect, null, callback);
         });
     }
 
@@ -117,10 +118,10 @@
     public void setCurrentWallpaper(Activity containerActivity, WallpaperInfo wallpaper,
             @Nullable Asset wallpaperAsset, @Destination final int destination,
             float wallpaperScale, @Nullable Rect cropRect,
-            @Nullable SetWallpaperCallback callback) {
+            @Nullable WallpaperColors wallpaperColors, @Nullable SetWallpaperCallback callback) {
         if (wallpaper instanceof LiveWallpaperInfo) {
             setCurrentLiveWallpaper(containerActivity, (LiveWallpaperInfo) wallpaper, destination,
-                    callback);
+                    wallpaperColors, callback);
             return;
         }
         mPreferences.setPendingWallpaperSetStatus(
@@ -184,7 +185,8 @@
     }
 
     public void setCurrentLiveWallpaper(Activity activity, LiveWallpaperInfo wallpaper,
-            @Destination final int destination, @Nullable SetWallpaperCallback callback) {
+            @Destination final int destination, @Nullable WallpaperColors colors,
+            @Nullable SetWallpaperCallback callback) {
         try {
             // Save current screen rotation so we can temporarily disable rotation while setting the
             // wallpaper and restore after setting the wallpaper finishes.
@@ -204,6 +206,10 @@
             if (destination == WallpaperPersister.DEST_BOTH) {
                 wallpaperManager.clear(WallpaperManager.FLAG_LOCK);
             }
+            mPreferences.storeLatestHomeWallpaper(wallpaper.getWallpaperId(), wallpaper,
+                    colors != null ? colors :
+                            WallpaperColors.fromBitmap(wallpaper.getThumbAsset(activity)
+                                    .getLowResBitmap(activity)));
             onWallpaperApplied(wallpaper, activity);
             if (callback != null) {
                 callback.onSuccess(wallpaper);
@@ -225,7 +231,6 @@
                 WallpaperPreferences.WALLPAPER_SET_NOT_PENDING);
         mUserEventLogger.logWallpaperSetResult(
                 UserEventLogger.WALLPAPER_SET_RESULT_SUCCESS);
-
         cleanUp();
         restoreScreenOrientationIfNeeded(containerActivity);
     }
diff --git a/src/com/android/wallpaper/picker/ImagePreviewFragment.java b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
index c41d394..21d5979 100755
--- a/src/com/android/wallpaper/picker/ImagePreviewFragment.java
+++ b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
@@ -111,6 +111,7 @@
     private TouchForwardingLayout mTouchForwardingLayout;
     private ConstraintLayout mContainer;
     private SurfaceView mWallpaperSurface;
+    private WallpaperColors mWallpaperColors;
 
     protected SurfaceView mWorkspaceSurface;
     protected WorkspaceSurfaceHolderCallback mWorkspaceSurfaceCallback;
@@ -188,6 +189,7 @@
         // Make it enabled since the buttons are disabled while wallpaper is moving.
         mBottomActionBar.enableActionButtonsWithBottomSheet(true);
 
+        mWallpaperColors = colors;
         mLockScreenPreviewer.setColor(colors);
 
         mFullScreenAnimation.setFullScreenTextColor(
@@ -480,7 +482,7 @@
     protected void setCurrentWallpaper(@Destination int destination) {
         mWallpaperSetter.setCurrentWallpaper(getActivity(), mWallpaper, mWallpaperAsset,
                 destination, mFullResImageView.getScale(), calculateCropRect(getContext()),
-                new SetWallpaperCallback() {
+                mWallpaperColors, new SetWallpaperCallback() {
                     @Override
                     public void onSuccess(WallpaperInfo wallpaperInfo) {
                         finishActivity(/* success= */ true);
diff --git a/src/com/android/wallpaper/picker/LivePreviewFragment.java b/src/com/android/wallpaper/picker/LivePreviewFragment.java
index ec17767..854afc9 100644
--- a/src/com/android/wallpaper/picker/LivePreviewFragment.java
+++ b/src/com/android/wallpaper/picker/LivePreviewFragment.java
@@ -118,6 +118,7 @@
     private TouchForwardingLayout mTouchForwardingLayout;
     private SurfaceView mWallpaperSurface;
     private Future<Integer> mPlaceholderColorFuture;
+    private WallpaperColors mWallpaperColors;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -326,7 +327,7 @@
             WallpaperColorsLoader.getWallpaperColors(
                     activity,
                     homeWallpaper.getThumbAsset(activity),
-                    mLockScreenPreviewer::setColor);
+                    colors -> onWallpaperColorsChanged(colors, 0));
         }
         if (mWallpaperConnection != null && !mWallpaperConnection.connect()) {
             mWallpaperConnection = null;
@@ -414,6 +415,7 @@
 
     @Override
     public void onWallpaperColorsChanged(WallpaperColors colors, int displayId) {
+        mWallpaperColors = colors;
         mLockScreenPreviewer.setColor(colors);
 
         mFullScreenAnimation.setFullScreenTextColor(
@@ -444,7 +446,8 @@
     @Override
     protected void setCurrentWallpaper(int destination) {
         mWallpaperSetter.setCurrentWallpaper(getActivity(), mWallpaper, null,
-                destination, 0, null, new SetWallpaperCallback() {
+                destination, 0, null, mWallpaperColors,
+                new SetWallpaperCallback() {
                     @Override
                     public void onSuccess(com.android.wallpaper.model.WallpaperInfo wallpaperInfo) {
                         finishActivity(/* success= */ true);
diff --git a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
index bb7a1d0..4436304 100755
--- a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
+++ b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
@@ -944,7 +944,7 @@
         mWallpaperPersister.setWallpaperInfoInPreview(mSelectedWallpaperInfo);
         if (mSelectedWallpaperInfo instanceof LiveWallpaperInfo) {
             mWallpaperSetter.setCurrentWallpaper(getActivity(), mSelectedWallpaperInfo, null,
-                    destination, 0, null, mSetWallpaperCallback);
+                    destination, 0, null, null, mSetWallpaperCallback);
         } else {
             mWallpaperSetter.setCurrentWallpaper(
                     getActivity(), mSelectedWallpaperInfo, destination, mSetWallpaperCallback);
diff --git a/tests/src/com/android/wallpaper/testing/TestWallpaperPersister.java b/tests/src/com/android/wallpaper/testing/TestWallpaperPersister.java
index 5c1ddbd..02cba0e 100644
--- a/tests/src/com/android/wallpaper/testing/TestWallpaperPersister.java
+++ b/tests/src/com/android/wallpaper/testing/TestWallpaperPersister.java
@@ -226,4 +226,18 @@
     public int getWallpaperPosition() {
         return mWallpaperPosition;
     }
+
+    public boolean saveStaticWallpaperMetadata(List<String> attributions, String actionUrl,
+            int actionLabelRes, int actionIconRes, String collectionId, int wallpaperId) {
+        return false;
+    }
+
+    public int getDefaultWhichWallpaper() {
+        return 0;
+    }
+
+    public int setBitmapToWallpaperManagerCompat(Bitmap wallpaperBitmap, boolean allowBackup,
+            int whichWallpaper) {
+        return 0;
+    }
 }