Only update the applied wallpaper's tile instead of updating all wallpaper tiles when swiping the preview pager.

Bug: 150840985
Change-Id: Ia71a14c72d92f0ff4c55be6a34486bbf7b0a9cde
diff --git a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
index c30b564..506f3a0 100755
--- a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
+++ b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
@@ -98,6 +98,7 @@
 import java.util.Date;
 import java.util.EnumSet;
 import java.util.List;
+import java.util.Optional;
 import java.util.Random;
 
 /**
@@ -266,8 +267,8 @@
      */
     public void highlightAppliedWallpaper(@Destination int wallpaperDestination) {
         mWallpaperDestination = wallpaperDestination;
-        if (mAdapter != null) {
-            mAdapter.notifyDataSetChanged();
+        if (mWallpapers != null) {
+            refreshAppliedWallpaper();
         }
     }
 
@@ -826,9 +827,7 @@
                 public void onSuccess(WallpaperInfo wallpaperInfo) {
                     // TODO(b/150913705): Show the snack bar.
                     mBottomActionBar.enableActions();
-                    updateAppliedStatus(mAppliedWallpaperInfo, false);
-                    updateAppliedStatus(wallpaperInfo, true);
-                    mAppliedWallpaperInfo = wallpaperInfo;
+                    refreshAppliedWallpaper();
 
                     mWallpaperPersister.onLiveWallpaperSet();
                 }
@@ -952,6 +951,45 @@
         return wallpaperComponent == null || wallpaperComponent.getShowMetadataInPreview();
     }
 
+    private void refreshAppliedWallpaper() {
+        // Clear the check mark and blue border(if it shows) of the old applied wallpaper.
+        showCheckMarkAndBorderForAppliedWallpaper(false);
+
+        // Update to the new applied wallpaper.
+        String appliedWallpaperId = getAppliedWallpaperId();
+        Optional<WallpaperInfo> wallpaperInfoOptional = mWallpapers
+                .stream()
+                .filter(wallpaper -> wallpaper.getWallpaperId() != null)
+                .filter(wallpaper -> wallpaper.getWallpaperId().equals(appliedWallpaperId))
+                .findFirst();
+        mAppliedWallpaperInfo = wallpaperInfoOptional.orElse(null);
+
+        // Set the check mark and blue border(if user doesn't select) of the new applied wallpaper.
+        showCheckMarkAndBorderForAppliedWallpaper(true);
+    }
+
+    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 void showCheckMarkAndBorderForAppliedWallpaper(boolean show) {
+        updateAppliedStatus(mAppliedWallpaperInfo, show);
+        if (mSelectedWallpaperInfo == null) {
+            updateActivatedStatus(mAppliedWallpaperInfo, show);
+        }
+    }
+
     /**
      * ViewHolder subclass for "daily refresh" tile in the RecyclerView, only shown if rotation is
      * enabled for this category.
@@ -1302,21 +1340,6 @@
                         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 {