Snap for 7439149 from b95d02ea6783a7bcf6b751eac4afb1dc41549887 to sc-d1-release

Change-Id: I3c764eb4a24ceea15b641e6aee17bd14f3e67391
diff --git a/res/layout/grid_item_image.xml b/res/layout/grid_item_image.xml
index f111062..7a00b59 100755
--- a/res/layout/grid_item_image.xml
+++ b/res/layout/grid_item_image.xml
@@ -21,21 +21,6 @@
     android:layout_height="wrap_content"
     android:orientation="vertical">
 
-    <TextView
-        android:id="@+id/title"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:layout_marginTop="@dimen/grid_item_individual_label_padding_top"
-        android:layout_marginBottom="@dimen/grid_item_individual_label_padding_bottom"
-        android:background="?android:colorSecondary"
-        android:ellipsize="end"
-        android:gravity="center"
-        android:maxLines="1"
-        android:minHeight="@dimen/grid_item_category_label_minimum_height"
-        android:textAppearance="@style/OptionTitleTextAppearance"
-        android:visibility="gone"
-        tools:text="Wallpaper" />
-
     <androidx.cardview.widget.CardView
         android:id="@+id/wallpaper_container"
         android:layout_width="match_parent"
@@ -89,4 +74,18 @@
         </RelativeLayout>
 
     </androidx.cardview.widget.CardView>
+
+    <TextView
+        android:id="@+id/title"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="@dimen/grid_item_individual_label_padding_top"
+        android:layout_marginBottom="@dimen/grid_item_individual_label_padding_bottom"
+        android:ellipsize="end"
+        android:gravity="center"
+        android:maxLines="1"
+        android:minHeight="@dimen/grid_item_category_label_minimum_height"
+        android:textAppearance="@style/OptionTitleTextAppearance"
+        android:visibility="gone"
+        tools:text="Wallpaper" />
 </LinearLayout>
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index fa8fee1..83d35c0 100755
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -34,7 +34,7 @@
     <!-- Dimensions for individual wallpaper tiles. -->
     <dimen name="grid_item_individual_padding_horizontal">4dp</dimen>
     <dimen name="grid_item_individual_padding_bottom">8dp</dimen>
-    <dimen name="grid_item_individual_label_padding_top">4dp</dimen>
+    <dimen name="grid_item_individual_label_padding_top">6dp</dimen>
     <dimen name="grid_item_individual_label_padding_bottom">4dp</dimen>
     <dimen name="wallpaper_grid_edge_space">20dp</dimen>
     <dimen name="wallpaper_grid_padding_top">32dp</dimen>
diff --git a/src/com/android/wallpaper/picker/ImagePreviewFragment.java b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
index 8ce50f9..28a8d25 100755
--- a/src/com/android/wallpaper/picker/ImagePreviewFragment.java
+++ b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
@@ -76,10 +76,8 @@
 import com.bumptech.glide.MemoryCategory;
 import com.davemorrissey.labs.subscaleview.ImageSource;
 import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
-import com.google.android.material.tabs.TabLayout;
 
 import java.util.Locale;
-import java.util.Optional;
 import java.util.concurrent.atomic.AtomicInteger;
 
 /**
@@ -103,7 +101,6 @@
     private ConstraintLayout mContainer;
     private SurfaceView mWallpaperSurface;
     private WallpaperInfoView mWallpaperInfoView;
-    private Optional<Integer> mLastSelectedTabPositionOptional = Optional.empty();
     private AtomicInteger mImageScaleChangeCounter = new AtomicInteger(0);
 
     protected SurfaceView mWorkspaceSurface;
@@ -285,31 +282,6 @@
         }
     }
 
-    protected void setUpTabs(TabLayout tabs) {
-        tabs.addTab(tabs.newTab().setText(getContext().getString(R.string.home_screen_message)));
-        tabs.addTab(tabs.newTab().setText(getContext().getString(R.string.lock_screen_message)));
-        tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
-            @Override
-            public void onTabSelected(TabLayout.Tab tab) {
-                mLastSelectedTabPositionOptional = Optional.of(tab.getPosition());
-                updateScreenPreview(/* isHomeSelected= */ tab.getPosition() == 0);
-            }
-
-            @Override
-            public void onTabUnselected(TabLayout.Tab tab) {}
-
-            @Override
-            public void onTabReselected(TabLayout.Tab tab) {}
-        });
-
-        // The TabLayout only contains below tabs
-        // 0. Home tab
-        // 1. Lock tab
-        int tabPosition = mLastSelectedTabPositionOptional.orElseGet(() -> mViewAsHome ? 0 : 1);
-        tabs.getTabAt(tabPosition).select();
-        updateScreenPreview(/* isHomeSelected= */ tabPosition == 0);
-    }
-
     /**
      * Initializes MosaicView by initializing tiling, setting a fallback page bitmap, and
      * initializing a zoom-scroll observer and click listener.
@@ -633,8 +605,10 @@
         }
     }
 
-    private void updateScreenPreview(boolean isHomeSelected) {
+    @Override
+    protected void updateScreenPreview(boolean isHomeSelected) {
         mWorkspaceSurface.setVisibility(isHomeSelected ? View.VISIBLE : View.INVISIBLE);
+
         mLockPreviewContainer.setVisibility(isHomeSelected ? View.INVISIBLE : View.VISIBLE);
 
         mFullScreenAnimation.setIsHomeSelected(isHomeSelected);
diff --git a/src/com/android/wallpaper/picker/LivePreviewFragment.java b/src/com/android/wallpaper/picker/LivePreviewFragment.java
index 07f85b6..a7b45fa 100644
--- a/src/com/android/wallpaper/picker/LivePreviewFragment.java
+++ b/src/com/android/wallpaper/picker/LivePreviewFragment.java
@@ -80,10 +80,7 @@
 import com.android.wallpaper.widget.WallpaperColorsLoader;
 import com.android.wallpaper.widget.WallpaperInfoView;
 
-import com.google.android.material.tabs.TabLayout;
-
 import java.util.Locale;
-import java.util.Optional;
 
 /**
  * Fragment which displays the UI for previewing an individual live wallpaper, its attribution
@@ -121,7 +118,6 @@
     private ViewGroup mPreviewContainer;
     private TouchForwardingLayout mTouchForwardingLayout;
     private SurfaceView mWallpaperSurface;
-    private Optional<Integer> mLastSelectedTabPositionOptional = Optional.empty();
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -229,38 +225,17 @@
         renderWorkspaceSurface();
     }
 
-    protected void setUpTabs(TabLayout tabs) {
-        tabs.addTab(tabs.newTab().setText(getContext().getString(R.string.home_screen_message)));
-        tabs.addTab(tabs.newTab().setText(getContext().getString(R.string.lock_screen_message)));
-        tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
-            @Override
-            public void onTabSelected(TabLayout.Tab tab) {
-                mLastSelectedTabPositionOptional = Optional.of(tab.getPosition());
-                updateScreenPreview(/* isHomeSelected= */ tab.getPosition() == 0);
-            }
-
-            @Override
-            public void onTabUnselected(TabLayout.Tab tab) {}
-
-            @Override
-            public void onTabReselected(TabLayout.Tab tab) {}
-        });
-
-        // The TabLayout only contains below tabs
-        // 0. Home tab
-        // 1. Lock tab
-        int tabPosition = mLastSelectedTabPositionOptional.orElseGet(() -> mViewAsHome ? 0 : 1);
-        tabs.getTabAt(tabPosition).select();
-        updateScreenPreview(/* isHomeSelected= */ tabPosition == 0);
-    }
-
     private void updateWallpaperSurface() {
         mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback);
         mWallpaperSurface.setZOrderMediaOverlay(true);
     }
 
-    private void updateScreenPreview(boolean isHomeSelected) {
+    @Override
+    protected void updateScreenPreview(boolean isHomeSelected) {
         mWorkspaceSurface.setVisibility(isHomeSelected ? View.VISIBLE : View.INVISIBLE);
+        if (!isHomeSelected) {
+            mWorkspaceSurfaceCallback.cleanUp();
+        }
         mLockPreviewContainer.setVisibility(isHomeSelected ? View.INVISIBLE : View.VISIBLE);
 
         mFullScreenAnimation.setIsHomeSelected(isHomeSelected);
diff --git a/src/com/android/wallpaper/picker/PreviewFragment.java b/src/com/android/wallpaper/picker/PreviewFragment.java
index 22cc6da..72d656f 100755
--- a/src/com/android/wallpaper/picker/PreviewFragment.java
+++ b/src/com/android/wallpaper/picker/PreviewFragment.java
@@ -55,8 +55,11 @@
 import com.android.wallpaper.util.FullScreenAnimation;
 import com.android.wallpaper.widget.BottomActionBar;
 
+import com.google.android.material.tabs.TabLayout;
+
 import java.util.Date;
 import java.util.List;
+import java.util.Optional;
 
 /**
  * Base Fragment to display the UI for previewing an individual wallpaper
@@ -76,6 +79,7 @@
      * wallpaper with pan and crop position to the device.
      */
     static final int MODE_CROP_AND_SET_WALLPAPER = 1;
+    private Optional<Integer> mLastSelectedTabPositionOptional = Optional.empty();
 
     /**
      * Possible preview modes for the fragment.
@@ -383,6 +387,33 @@
         startActivity(mExploreIntent);
     }
 
+    protected void setUpTabs(TabLayout tabs) {
+        tabs.addTab(tabs.newTab().setText(getContext().getString(R.string.home_screen_message)));
+        tabs.addTab(tabs.newTab().setText(getContext().getString(R.string.lock_screen_message)));
+        tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
+            @Override
+            public void onTabSelected(TabLayout.Tab tab) {
+                mLastSelectedTabPositionOptional = Optional.of(tab.getPosition());
+                updateScreenPreview(/* isHomeSelected= */ tab.getPosition() == 0);
+            }
+
+            @Override
+            public void onTabUnselected(TabLayout.Tab tab) {}
+
+            @Override
+            public void onTabReselected(TabLayout.Tab tab) {}
+        });
+
+        // The TabLayout only contains below tabs
+        // 0. Home tab
+        // 1. Lock tab
+        int tabPosition = mLastSelectedTabPositionOptional.orElseGet(() -> mViewAsHome ? 0 : 1);
+        tabs.getTabAt(tabPosition).select();
+        updateScreenPreview(/* isHomeSelected= */ tabPosition == 0);
+    }
+
+    protected abstract void updateScreenPreview(boolean isHomeSelected);
+
     /**
      * Sets current wallpaper to the device based on current zoom and scroll state.
      *
diff --git a/src/com/android/wallpaper/picker/WorkspaceSurfaceHolderCallback.java b/src/com/android/wallpaper/picker/WorkspaceSurfaceHolderCallback.java
index ec67242..c469820 100644
--- a/src/com/android/wallpaper/picker/WorkspaceSurfaceHolderCallback.java
+++ b/src/com/android/wallpaper/picker/WorkspaceSurfaceHolderCallback.java
@@ -22,7 +22,6 @@
 import android.os.RemoteException;
 import android.util.Log;
 import android.view.Surface;
-import android.view.SurfaceControlViewHost;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;
 
@@ -61,7 +60,6 @@
     private WorkspaceRenderListener mListener;
 
     private boolean mNeedsToCleanUp;
-    private SurfaceControlViewHost.SurfacePackage mLastPackage;
 
     public WorkspaceSurfaceHolderCallback(SurfaceView workspaceSurface, Context context) {
         this(workspaceSurface, context, false);
@@ -117,20 +115,13 @@
         if ((mShouldUseWallpaperColors && !mIsWallpaperColorsReady) || mLastSurface == null) {
             return;
         }
-        if (mLastPackage != null) {
-            mWorkspaceSurface.setChildSurfacePackage(mLastPackage);
-            if (mListener != null) {
-                mListener.onWorkspaceRendered();
-            }
-            return;
-        }
 
         mRequestPending.set(true);
         requestPreview(mWorkspaceSurface, (result) -> {
             mRequestPending.set(false);
             if (result != null && mLastSurface != null) {
-                mLastPackage = SurfaceViewUtils.getSurfacePackage(result);
-                mWorkspaceSurface.setChildSurfacePackage(mLastPackage);
+                mWorkspaceSurface.setChildSurfacePackage(
+                        SurfaceViewUtils.getSurfacePackage(result));
 
                 mCallback = SurfaceViewUtils.getCallback(result);
 
@@ -148,11 +139,9 @@
 
     @Override
     public void surfaceDestroyed(SurfaceHolder holder) {
-        mLastSurface = null;
     }
 
     public void cleanUp() {
-        mLastPackage = null;
         if (mCallback != null) {
             try {
                 mCallback.replyTo.send(mCallback);