Fix workspace vanish when turn off/on screen

Root cause: we called mCallback.replyTo.send(mCallback) in surfaceDestroyed to disconnect launcher. surfaceDestroyed will be triggered when screen off(window get stopped).

Video: https://drive.google.com/file/d/1NKczZCcaK-1n25x8EJz9Sl3HPqdS27wz/view?usp=sharing

Test: Manually
Fixes: 158145631
Change-Id: Ib0dad7bf9b230a4b5164e0f769084e40fed1065d
diff --git a/src/com/android/customization/picker/grid/GridFragment.java b/src/com/android/customization/picker/grid/GridFragment.java
index 05f535c..fa6ab78 100644
--- a/src/com/android/customization/picker/grid/GridFragment.java
+++ b/src/com/android/customization/picker/grid/GridFragment.java
@@ -89,12 +89,10 @@
     private GridOptionsManager mGridManager;
     private GridOption mSelectedOption;
     private ContentLoadingProgressBar mLoading;
-    private ViewGroup mGridPreviewContainer;
     private View mContent;
     private View mError;
     private BottomActionBar mBottomActionBar;
     private ThemesUserEventLogger mEventLogger;
-    private boolean mReloadOptionsAfterApplying;
 
     private GridOptionPreviewer mGridOptionPreviewer;
 
@@ -130,7 +128,6 @@
         View view = inflater.inflate(
                 R.layout.fragment_grid_picker, container, /* attachToRoot */ false);
         setUpToolbar(view);
-        mGridPreviewContainer = view.findViewById(R.id.grid_preview_container);
         mContent = view.findViewById(R.id.content_section);
         mOptionsContainer = view.findViewById(R.id.options_container);
         mLoading = view.findViewById(R.id.loading_indicator);
@@ -154,6 +151,9 @@
             wallpaperPreviewer.setWallpaper(mHomeWallpaper);
         }, false);
 
+        mGridOptionPreviewer = new GridOptionPreviewer(
+                getContext(), mGridManager, view.findViewById(R.id.grid_preview_container));
+
         view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
             @Override
             public void onLayoutChange(View v, int left, int top, int right, int bottom,
@@ -207,14 +207,6 @@
     }
 
     private void updatePreview() {
-        if (mGridOptionPreviewer != null) {
-            mGridOptionPreviewer.release();
-        }
-        if (getContext() == null) {
-            return;
-        }
-        mGridOptionPreviewer = new GridOptionPreviewer(
-                getContext(), mGridManager, mGridPreviewContainer);
         mGridOptionPreviewer.setGridOption(mSelectedOption, mGridManager.usesSurfaceView());
     }
 
@@ -228,10 +220,6 @@
                 mOptionsController = new OptionSelectorController<>(mOptionsContainer, options);
                 mOptionsController.addListener(selected -> {
                     mSelectedOption = (GridOption) selected;
-                    if (mReloadOptionsAfterApplying) {
-                        mReloadOptionsAfterApplying = false;
-                        return;
-                    }
                     mBottomActionBar.show();
                     mEventLogger.logGridSelected(mSelectedOption);
                     updatePreview();
@@ -246,6 +234,7 @@
                 mSelectedOption = previouslySelectedOption != null
                         ? previouslySelectedOption
                         : getActiveOption(options);
+                // Will trigger selected listener.
                 mOptionsController.setSelectedOption(mSelectedOption);
                 boolean bottomActionBarVisibility = savedInstanceState != null
                         && savedInstanceState.getBoolean(KEY_STATE_BOTTOM_ACTION_BAR_VISIBILITY);
@@ -254,7 +243,6 @@
                 } else {
                     mBottomActionBar.hide();
                 }
-                updatePreview();
             }
 
             @Override
diff --git a/src/com/android/customization/picker/grid/GridOptionPreviewer.java b/src/com/android/customization/picker/grid/GridOptionPreviewer.java
index 6d31689..49f2a16 100644
--- a/src/com/android/customization/picker/grid/GridOptionPreviewer.java
+++ b/src/com/android/customization/picker/grid/GridOptionPreviewer.java
@@ -39,6 +39,9 @@
 
     private static final int PREVIEW_FADE_DURATION_MS = 100;
 
+    private final WorkspaceSurfaceHolderCallback mSurfaceCallback =
+            new WorkspaceSurfaceHolderCallback();
+
     private final Context mContext;
     private final GridOptionsManager mGridManager;
     private final ViewGroup mPreviewContainer;
@@ -62,11 +65,13 @@
     /** Releases the view resource. */
     public void release() {
         if (mGridOptionSurface != null) {
+            mSurfaceCallback.cleanUp();
             mGridOptionSurface.getHolder().removeCallback(mSurfaceCallback);
             Surface surface = mGridOptionSurface.getHolder().getSurface();
             if (surface != null) {
                 surface.release();
             }
+            mGridOptionSurface = null;
         }
         mPreviewContainer.removeAllViews();
     }
@@ -78,13 +83,16 @@
         mPreviewContainer.removeAllViews();
 
         if (usesSurfaceView) {
-            mGridOptionSurface = new SurfaceView(mContext);
-            setUpView(mGridOptionSurface);
-            mGridOptionSurface.setZOrderMediaOverlay(true);
-            mGridOptionSurface.getHolder().addCallback(mSurfaceCallback);
+            mSurfaceCallback.mLastSurface = null;
+            if (mGridOptionSurface == null) {
+                mGridOptionSurface = new SurfaceView(mContext);
+                mGridOptionSurface.setZOrderMediaOverlay(true);
+                mGridOptionSurface.getHolder().addCallback(mSurfaceCallback);
+            }
+            addViewToContainer(mGridOptionSurface);
         } else {
             final ImageView previewImage = new ImageView(mContext);
-            setUpView(previewImage);
+            addViewToContainer(previewImage);
             final Asset previewAsset = new ContentUriAsset(
                     mContext,
                     mGridOption.previewImageUri,
@@ -98,14 +106,14 @@
         }
     }
 
-    private void setUpView(View view) {
+    private void addViewToContainer(View view) {
         view.setLayoutParams(new ViewGroup.LayoutParams(
                 ViewGroup.LayoutParams.MATCH_PARENT,
                 ViewGroup.LayoutParams.MATCH_PARENT));
         mPreviewContainer.addView(view);
     }
 
-    private final SurfaceHolder.Callback mSurfaceCallback = new SurfaceHolder.Callback() {
+    private class WorkspaceSurfaceHolderCallback implements SurfaceHolder.Callback {
         private Surface mLastSurface;
         private Message mCallback;
 
@@ -128,17 +136,18 @@
         public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
 
         @Override
-        public void surfaceDestroyed(SurfaceHolder holder) {
+        public void surfaceDestroyed(SurfaceHolder holder) {}
+
+        public void cleanUp() {
             if (mCallback != null) {
                 try {
                     mCallback.replyTo.send(mCallback);
                 } catch (RemoteException e) {
                     e.printStackTrace();
                 } finally {
-                    mCallback.recycle();
                     mCallback = null;
                 }
             }
         }
-    };
+    }
 }