Merge "Fades in preview of feathers to avoid flicker" into tm-qpr-dev
diff --git a/res/layout/grid_preview_card.xml b/res/layout/grid_preview_card.xml
index e333ca7..6df8508 100644
--- a/res/layout/grid_preview_card.xml
+++ b/res/layout/grid_preview_card.xml
@@ -38,4 +38,13 @@
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:importantForAccessibility="noHideDescendants" />
+
+    <View
+        android:id="@+id/grid_fadein_scrim"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:background="?android:colorSecondary"
+        android:forceHasOverlappingRendering="false"
+        android:importantForAccessibility="no"
+        android:visibility="invisible" />
 </androidx.cardview.widget.CardView>
diff --git a/src/com/android/customization/picker/WallpaperPreviewer.java b/src/com/android/customization/picker/WallpaperPreviewer.java
index 354eec2..ef16895 100644
--- a/src/com/android/customization/picker/WallpaperPreviewer.java
+++ b/src/com/android/customization/picker/WallpaperPreviewer.java
@@ -38,6 +38,7 @@
 import com.android.wallpaper.util.ResourceUtils;
 import com.android.wallpaper.util.ScreenSizeCalculator;
 import com.android.wallpaper.util.SizeCalculator;
+import com.android.wallpaper.util.VideoWallpaperUtils;
 import com.android.wallpaper.util.WallpaperConnection;
 import com.android.wallpaper.util.WallpaperConnection.WallpaperConnectionListener;
 import com.android.wallpaper.util.WallpaperSurfaceCallback;
@@ -53,6 +54,7 @@
     private final Activity mActivity;
     private final ImageView mHomePreview;
     private final SurfaceView mWallpaperSurface;
+    @Nullable private final View mFadeInScrim;
 
     private WallpaperSurfaceCallback mWallpaperSurfaceCallback;
     private WallpaperInfo mWallpaper;
@@ -67,11 +69,17 @@
 
     public WallpaperPreviewer(Lifecycle lifecycle, Activity activity, ImageView homePreview,
                               SurfaceView wallpaperSurface) {
+        this(lifecycle, activity, homePreview, wallpaperSurface, null);
+    }
+
+    public WallpaperPreviewer(Lifecycle lifecycle, Activity activity, ImageView homePreview,
+                              SurfaceView wallpaperSurface, @Nullable View fadeInScrim) {
         lifecycle.addObserver(this);
 
         mActivity = activity;
         mHomePreview = homePreview;
         mWallpaperSurface = wallpaperSurface;
+        mFadeInScrim = fadeInScrim;
         mWallpaperSurfaceCallback = new WallpaperSurfaceCallback(activity, mHomePreview,
                 mWallpaperSurface, this::setUpWallpaperPreview);
         mWallpaperSurface.setZOrderMediaOverlay(true);
@@ -139,6 +147,11 @@
                              @Nullable WallpaperColorsListener listener) {
         mWallpaper = wallpaperInfo;
         mWallpaperColorsListener = listener;
+        if (mFadeInScrim != null && VideoWallpaperUtils.needsFadeIn(wallpaperInfo)) {
+            mFadeInScrim.animate().cancel();
+            mFadeInScrim.setAlpha(1f);
+            mFadeInScrim.setVisibility(View.VISIBLE);
+        }
         setUpWallpaperPreview();
     }
 
@@ -209,6 +222,17 @@
                                 mWallpaperColorsListener.onWallpaperColorsChanged(colors);
                             }
                         }
+
+                        @Override
+                        public void onEngineShown() {
+                            if (mFadeInScrim != null && VideoWallpaperUtils.needsFadeIn(
+                                    homeWallpaper)) {
+                                mFadeInScrim.animate().alpha(0.0f)
+                                        .setDuration(VideoWallpaperUtils.TRANSITION_MILLIS)
+                                        .withEndAction(
+                                                () -> mFadeInScrim.setVisibility(View.INVISIBLE));
+                            }
+                        }
                     }, mWallpaperSurface);
 
             mWallpaperConnection.setVisibility(true);
diff --git a/src/com/android/customization/picker/grid/GridFragment.java b/src/com/android/customization/picker/grid/GridFragment.java
index b5ed7ee..4de1dab 100644
--- a/src/com/android/customization/picker/grid/GridFragment.java
+++ b/src/com/android/customization/picker/grid/GridFragment.java
@@ -158,7 +158,8 @@
 
         SurfaceView wallpaperSurface = view.findViewById(R.id.wallpaper_preview_surface);
         WallpaperPreviewer wallpaperPreviewer = new WallpaperPreviewer(getLifecycle(),
-                getActivity(), view.findViewById(R.id.wallpaper_preview_image), wallpaperSurface);
+                getActivity(), view.findViewById(R.id.wallpaper_preview_image), wallpaperSurface,
+                view.findViewById(R.id.grid_fadein_scrim));
         // Loads current Wallpaper.
         CurrentWallpaperInfoFactory factory = InjectorProvider.getInjector()
                 .getCurrentWallpaperInfoFactory(getContext().getApplicationContext());