Fix flash when setting photo as wallpaper

1.Hide the workSpaceSurface if the surface isn’t created.
2.Assign to background color if placeHolderColor is wrong.

Videos:
  - Before: https://drive.google.com/file/d/1J-DYMjv-NKM_5FLv0mI-PfpiNk-uVprr/view?usp=sharing&resourcekey=0-wDM3vTXJptH3ZKpcFB8oZQ
  - After: https://drive.google.com/file/d/1HRw9rVkwKw_5Sr7DoPiIVG8ccgDpsoGy/view?usp=sharing&resourcekey=0-c_VjQBem_WgqGPPBY8R5xw

Bug: 197564856
Bug: 197827700
Test: Manual
Change-Id: I6709fc5ad304eb4caec5006c17094c71676e2e1d
diff --git a/src/com/android/wallpaper/picker/ImagePreviewFragment.java b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
index 9005262..acfcc16 100755
--- a/src/com/android/wallpaper/picker/ImagePreviewFragment.java
+++ b/src/com/android/wallpaper/picker/ImagePreviewFragment.java
@@ -110,6 +110,7 @@
     private TouchForwardingLayout mTouchForwardingLayout;
     private ConstraintLayout mContainer;
     private SurfaceView mWallpaperSurface;
+    private boolean mIsSurfaceCreated = false;
     private WallpaperColors mWallpaperColors;
 
     protected SurfaceView mWorkspaceSurface;
@@ -557,17 +558,22 @@
                 // Load a low-res placeholder image if there's a thumbnail available from the asset
                 // that can be shown to the user more quickly than the full-sized image.
                 Activity activity = requireActivity();
+                // Change to background color if colorValue is Color.TRANSPARENT
                 int placeHolderColor = ResourceUtils.getColorAttr(activity,
                         android.R.attr.colorBackground);
                 if (mPlaceholderColorFuture.isDone()) {
                     try {
-                        placeHolderColor = mWallpaper.computePlaceholderColor(context).get();
+                        int colorValue = mWallpaper.computePlaceholderColor(context).get();
+                        if (colorValue != Color.TRANSPARENT) {
+                            placeHolderColor = colorValue;
+                        }
                     } catch (InterruptedException | ExecutionException e) {
                         // Do nothing
                     }
                 }
                 mWallpaperSurface.setResizeBackgroundColor(placeHolderColor);
                 mWallpaperSurface.setBackgroundColor(placeHolderColor);
+                mLowResImageView.setBackgroundColor(placeHolderColor);
 
                 mWallpaperAsset.loadLowResDrawable(activity, mLowResImageView, placeHolderColor,
                         mPreviewBitmapTransformation);
@@ -584,6 +590,9 @@
                 mHost.setView(wallpaperPreviewContainer, wallpaperPreviewContainer.getWidth(),
                         wallpaperPreviewContainer.getHeight());
                 mWallpaperSurface.setChildSurfacePackage(mHost.getSurfacePackage());
+                // After surface creating, update workspaceSurface.
+                mIsSurfaceCreated = true;
+                updateScreenPreview(mLastSelectedTabPositionOptional.orElse(0) == 0);
             }
         }
 
@@ -598,14 +607,18 @@
                 mHost.release();
                 mHost = null;
             }
+            mIsSurfaceCreated = false;
         }
     }
 
     @Override
     protected void updateScreenPreview(boolean isHomeSelected) {
-        mWorkspaceSurface.setVisibility(isHomeSelected ? View.VISIBLE : View.INVISIBLE);
+        // Use View.GONE for WorkspaceSurface's visibility before its surface is created.
+        mWorkspaceSurface.setVisibility(isHomeSelected && mIsSurfaceCreated ? View.VISIBLE :
+                View.GONE);
 
-        mLockPreviewContainer.setVisibility(isHomeSelected ? View.INVISIBLE : View.VISIBLE);
+        mLockPreviewContainer.setVisibility(isHomeSelected && mIsSurfaceCreated ? View.VISIBLE :
+                View.GONE);
 
         mFullScreenAnimation.setIsHomeSelected(isHomeSelected);
     }
diff --git a/src/com/android/wallpaper/picker/PreviewFragment.java b/src/com/android/wallpaper/picker/PreviewFragment.java
index d0626f0..172bacd 100755
--- a/src/com/android/wallpaper/picker/PreviewFragment.java
+++ b/src/com/android/wallpaper/picker/PreviewFragment.java
@@ -143,7 +143,7 @@
 
     protected SetWallpaperViewModel mSetWallpaperViewModel;
     protected ViewModelProvider mViewModelProvider;
-    private Optional<Integer> mLastSelectedTabPositionOptional = Optional.empty();
+    protected Optional<Integer> mLastSelectedTabPositionOptional = Optional.empty();
     private OnBackPressedCallback mOnBackPressedCallback;
 
     /**