Implement the behavior when switching between Home and Lock preview page.

1.Set a checkmark to the corresponding applied wallpaper in Home and Lock preview.
2.Switch to the corresponding preview page when applying a wallpaper to Home or Lock screen.

Video:
https://drive.google.com/a/google.com/file/d/1-B9bAx9s2S3FWKEJkLDalv2ox90-poIn/view?usp=sharing

Bug: 150840985
Change-Id: I24d0c22ec9d9520da7ff03432f8babd803cfc307
diff --git a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
index 4392ca8..c30b564 100755
--- a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
+++ b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
@@ -22,6 +22,7 @@
 
 import android.app.Activity;
 import android.app.ProgressDialog;
+import android.app.WallpaperManager;
 import android.content.Context;
 import android.content.res.Configuration;
 import android.content.res.Resources.NotFoundException;
@@ -143,6 +144,19 @@
         void restoreThumbnails();
     }
 
+    /**
+     * An interface for receiving the destination of the new applied wallpaper.
+     */
+    public interface WallpaperDestinationCallback {
+        /**
+         * Called when the destination of the wallpaper is set.
+         *
+         * @param destination the destination which a wallpaper may be set.
+         *                    See {@link Destination} for more details.
+         */
+        void onDestinationSet(@Destination int destination);
+    }
+
     WallpaperPreferences mWallpaperPreferences;
     WallpaperChangedNotifier mWallpaperChangedNotifier;
     RecyclerView mImageGrid;
@@ -230,6 +244,8 @@
     private WallpaperPersister mWallpaperPersister;
     @Nullable private WallpaperInfo mSelectedWallpaperInfo;
     private WallpaperInfo mAppliedWallpaperInfo;
+    private WallpaperManager mWallpaperManager;
+    private int mWallpaperDestination;
 
     public static IndividualPickerFragment newInstance(String collectionId) {
         Bundle args = new Bundle();
@@ -240,6 +256,21 @@
         return fragment;
     }
 
+    /**
+     * Highlights the applied wallpaper (if it exists) according to the destination a wallpaper
+     * would be set.
+     *
+     * @param wallpaperDestination the destination a wallpaper would be set.
+     *                             It will be either {@link Destination#DEST_HOME_SCREEN}
+     *                             or {@link Destination#DEST_LOCK_SCREEN}.
+     */
+    public void highlightAppliedWallpaper(@Destination int wallpaperDestination) {
+        mWallpaperDestination = wallpaperDestination;
+        if (mAdapter != null) {
+            mAdapter.notifyDataSetChanged();
+        }
+    }
+
     private static int getResIdForRotationState(@RotationInitializationState int rotationState) {
         switch (rotationState) {
             case WallpaperRotationInitializer.ROTATION_NOT_INITIALIZED:
@@ -284,6 +315,8 @@
         mWallpaperChangedNotifier = WallpaperChangedNotifier.getInstance();
         mWallpaperChangedNotifier.registerListener(mWallpaperChangedListener);
 
+        mWallpaperManager = WallpaperManager.getInstance(appContext);
+
         mFormFactor = injector.getFormFactorChecker(appContext).getFormFactor();
 
         mPackageStatusNotifier = injector.getPackageStatusNotifier(appContext);
@@ -784,6 +817,7 @@
             mWallpaperSetter.setCurrentWallpaper(
                     getActivity(), mSelectedWallpaperInfo, destination, mSetWallpaperCallback);
         }
+        onWallpaperDestinationSet(destination);
     }
 
     private WallpaperPersister.SetWallpaperCallback mSetWallpaperCallback =
@@ -848,6 +882,16 @@
         }
     }
 
+    private void onWallpaperDestinationSet(int destination) {
+        WallpaperDestinationCallback wallpaperDestinationCallback =
+                (WallpaperDestinationCallback) getParentFragment();
+        if (wallpaperDestinationCallback == null) {
+            return;
+        }
+
+        wallpaperDestinationCallback.onDestinationSet(destination);
+    }
+
     private void onWallpaperSelected(@Nullable WallpaperInfo newSelectedWallpaperInfo) {
         if (mSelectedWallpaperInfo == newSelectedWallpaperInfo) {
             return;
@@ -1239,8 +1283,7 @@
                     ? position - 1 : position;
             WallpaperInfo wallpaper = mWallpapers.get(wallpaperIndex);
             ((IndividualHolder) holder).bindWallpaper(wallpaper);
-            WallpaperPreferences prefs = InjectorProvider.getInjector().getPreferences(getContext());
-            String appliedWallpaperId = prefs.getHomeWallpaperRemoteId();
+            String appliedWallpaperId = getAppliedWallpaperId();
             boolean isWallpaperApplied = wallpaper.getWallpaperId().equals(appliedWallpaperId);
             boolean isWallpaperSelected = wallpaper.equals(mSelectedWallpaperInfo);
             boolean hasUserSelectedWallpaper = mSelectedWallpaperInfo != null;
@@ -1259,6 +1302,21 @@
                         view -> onWallpaperSelected(wallpaper));
             }
         }
+
+        private String getAppliedWallpaperId() {
+            WallpaperPreferences prefs =
+                    InjectorProvider.getInjector().getPreferences(getContext());
+            android.app.WallpaperInfo wallpaperInfo = mWallpaperManager.getWallpaperInfo();
+            boolean isDestinationBoth =
+                    mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_LOCK) < 0;
+
+            if (isDestinationBoth || mWallpaperDestination == WallpaperPersister.DEST_HOME_SCREEN) {
+                return wallpaperInfo != null
+                        ? wallpaperInfo.getServiceName() : prefs.getHomeWallpaperRemoteId();
+            } else {
+                return prefs.getLockWallpaperRemoteId();
+            }
+        }
     }
 
     private class GridPaddingDecoration extends RecyclerView.ItemDecoration {