Switch to public API for surface view when rendering preview

Bug: 152003916
Test: Manual
Change-Id: I54d2f58c723a486af3292eb955ac296440c5ad41
diff --git a/src/com/android/wallpaper/picker/CategoryFragment.java b/src/com/android/wallpaper/picker/CategoryFragment.java
index 13ebef0..2ff7f29 100755
--- a/src/com/android/wallpaper/picker/CategoryFragment.java
+++ b/src/com/android/wallpaper/picker/CategoryFragment.java
@@ -33,6 +33,8 @@
 import android.os.Build.VERSION;
 import android.os.Build.VERSION_CODES;
 import android.os.Bundle;
+import android.os.Message;
+import android.os.RemoteException;
 import android.provider.Settings;
 import android.service.wallpaper.WallpaperService;
 import android.util.DisplayMetrics;
@@ -61,7 +63,6 @@
 import androidx.viewpager.widget.PagerAdapter;
 import androidx.viewpager.widget.ViewPager;
 
-import com.android.systemui.shared.system.SurfaceViewRequestUtils;
 import com.android.wallpaper.R;
 import com.android.wallpaper.config.Flags;
 import com.android.wallpaper.model.Category;
@@ -84,6 +85,7 @@
 import com.android.wallpaper.util.DisplayMetricsRetriever;
 import com.android.wallpaper.util.PreviewUtils;
 import com.android.wallpaper.util.ScreenSizeCalculator;
+import com.android.wallpaper.util.SurfaceViewUtils;
 import com.android.wallpaper.util.TileSizeCalculator;
 import com.android.wallpaper.util.WallpaperConnection;
 import com.android.wallpaper.util.WallpaperConnection.WallpaperConnectionListener;
@@ -720,20 +722,26 @@
         }
 
         @Override
-        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
-        }
+        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
 
         @Override
-        public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
-        }
+        public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) { }
     };
 
     private final SurfaceHolder.Callback mWorkspaceSurfaceCallback = new SurfaceHolder.Callback() {
+
+        private Message mCallback;
+
         @Override
         public void surfaceCreated(SurfaceHolder holder) {
-            Bundle bundle = SurfaceViewRequestUtils.createSurfaceBundle(mWorkspaceSurface);
             if (mPreviewUtils.supportsPreview()) {
-                mPreviewUtils.renderPreview(bundle);
+                Bundle result = mPreviewUtils.renderPreview(
+                        SurfaceViewUtils.createSurfaceViewRequest(mWorkspaceSurface));
+                if (result != null) {
+                    mWorkspaceSurface.setChildSurfacePackage(
+                            SurfaceViewUtils.getSurfacePackage(result));
+                    mCallback = SurfaceViewUtils.getCallback(result);
+                }
             }
         }
 
@@ -741,7 +749,17 @@
         public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
 
         @Override
-        public void surfaceDestroyed(SurfaceHolder holder) { }
+        public void surfaceDestroyed(SurfaceHolder holder) {
+            if (mCallback != null) {
+                try {
+                    mCallback.replyTo.send(mCallback);
+                } catch (RemoteException e) {
+                    e.printStackTrace();
+                } finally {
+                    mCallback = null;
+                }
+            }
+        }
     };
 
     private interface MetadataHolder {
diff --git a/src/com/android/wallpaper/util/PreviewUtils.java b/src/com/android/wallpaper/util/PreviewUtils.java
index 14b9cde..5b1d857 100644
--- a/src/com/android/wallpaper/util/PreviewUtils.java
+++ b/src/com/android/wallpaper/util/PreviewUtils.java
@@ -52,8 +52,9 @@
     }
 
     /** Render preview under the current grid option. */
-    public void renderPreview(Bundle bundle) {
-        mContext.getContentResolver().call(getUri(PREVIEW), METHOD_GET_PREVIEW, null, bundle);
+    public Bundle renderPreview(Bundle bundle) {
+        return mContext.getContentResolver().call(getUri(PREVIEW), METHOD_GET_PREVIEW, null,
+                bundle);
     }
 
     /** Easy way to generate a Uri with the provider info from this class. */
diff --git a/src/com/android/wallpaper/util/SurfaceViewUtils.java b/src/com/android/wallpaper/util/SurfaceViewUtils.java
new file mode 100644
index 0000000..1656b1b
--- /dev/null
+++ b/src/com/android/wallpaper/util/SurfaceViewUtils.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.wallpaper.util;
+
+import android.os.Bundle;
+import android.os.Message;
+import android.view.SurfaceControlViewHost;
+import android.view.SurfaceView;
+
+/** Util class to generate surface view requests and parse responses */
+public class SurfaceViewUtils {
+
+    private static final String KEY_HOST_TOKEN = "host_token";
+    private static final String KEY_VIEW_WIDTH = "width";
+    private static final String KEY_VIEW_HEIGHT = "height";
+    private static final String KEY_DISPLAY_ID = "display_id";
+    private static final String KEY_SURFACE_PACKAGE = "surface_package";
+    private static final String KEY_CALLBACK = "callback";
+
+    /** Create a surface view request. */
+    public static Bundle createSurfaceViewRequest(SurfaceView surfaceView) {
+        Bundle bundle = new Bundle();
+        bundle.putBinder(KEY_HOST_TOKEN, surfaceView.getHostToken());
+        bundle.putInt(KEY_DISPLAY_ID, surfaceView.getDisplay().getDisplayId());
+        bundle.putInt(KEY_VIEW_WIDTH, surfaceView.getWidth());
+        bundle.putInt(KEY_VIEW_HEIGHT, surfaceView.getHeight());
+        return bundle;
+    }
+
+    /** Return the surface package. */
+    public static SurfaceControlViewHost.SurfacePackage getSurfacePackage(Bundle bundle) {
+        return bundle.getParcelable(KEY_SURFACE_PACKAGE);
+    }
+
+    /** Return the message callback. */
+    public static Message getCallback(Bundle bundle) {
+        return bundle.getParcelable(KEY_CALLBACK);
+    }
+}