Release SurfaceControlViewHost used to render wallpaper surface we are done

Fixes: 157709599
Test: manual
Change-Id: I9f8ae44f94e42e98aa8e0f5b03bca15cbb5abf5e
diff --git a/src/com/android/customization/picker/WallpaperPreviewer.java b/src/com/android/customization/picker/WallpaperPreviewer.java
index 03e7031..34688c5 100644
--- a/src/com/android/customization/picker/WallpaperPreviewer.java
+++ b/src/com/android/customization/picker/WallpaperPreviewer.java
@@ -55,6 +55,8 @@
     private final Activity mActivity;
     private final ImageView mHomePreview;
     private final SurfaceView mWallpaperSurface;
+    private final WallpaperSurfaceCallback mWallpaperSurfaceCallback =
+            new WallpaperSurfaceCallback();
 
     private WallpaperInfo mWallpaper;
     private WallpaperConnection mWallpaperConnection;
@@ -70,7 +72,7 @@
         mHomePreview = homePreview;
         mWallpaperSurface = wallpaperSurface;
         mWallpaperSurface.setZOrderMediaOverlay(false);
-        mWallpaperSurface.getHolder().addCallback(mSurfaceCallback);
+        mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback);
     }
 
     @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
@@ -107,7 +109,8 @@
             mWallpaperConnection = null;
         }
 
-        mWallpaperSurface.getHolder().removeCallback(mSurfaceCallback);
+        mWallpaperSurfaceCallback.cleanUp();
+        mWallpaperSurface.getHolder().removeCallback(mWallpaperSurfaceCallback);
         Surface surface = mWallpaperSurface.getHolder().getSurface();
         if (surface != null) {
             surface.release();
@@ -187,9 +190,10 @@
         });
     }
 
-    private final SurfaceHolder.Callback mSurfaceCallback = new SurfaceHolder.Callback() {
+    private class WallpaperSurfaceCallback implements SurfaceHolder.Callback {
 
         private Surface mLastSurface;
+        private SurfaceControlViewHost mHost;
 
         @Override
         public void surfaceCreated(SurfaceHolder holder) {
@@ -202,11 +206,12 @@
                         makeMeasureSpec(mHomePreview.getHeight(), EXACTLY));
                 mHomeImageWallpaper.layout(0, 0, mHomePreview.getWidth(), mHomePreview.getHeight());
 
-                SurfaceControlViewHost host = new SurfaceControlViewHost(mActivity,
+                cleanUp();
+                mHost = new SurfaceControlViewHost(mActivity,
                         mActivity.getDisplay(), mWallpaperSurface.getHostToken());
-                host.setView(mHomeImageWallpaper, mHomeImageWallpaper.getWidth(),
+                mHost.setView(mHomeImageWallpaper, mHomeImageWallpaper.getWidth(),
                         mHomeImageWallpaper.getHeight());
-                mWallpaperSurface.setChildSurfacePackage(host.getSurfacePackage());
+                mWallpaperSurface.setChildSurfacePackage(mHost.getSurfacePackage());
             }
             setUpWallpaperPreview();
         }
@@ -216,6 +221,13 @@
 
         @Override
         public void surfaceDestroyed(SurfaceHolder holder) {}
+
+        public void cleanUp() {
+            if (mHost != null) {
+                mHost.release();
+                mHost = null;
+            }
+        }
     };
 
     private static Intent getWallpaperIntent(android.app.WallpaperInfo info) {