1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.wallpaper.util;
17 
18 import android.os.Bundle;
19 import android.os.Message;
20 import android.view.SurfaceControlViewHost;
21 import android.view.SurfaceView;
22 
23 import androidx.annotation.Nullable;
24 
25 /** Util class to generate surface view requests and parse responses */
26 public class SurfaceViewUtils {
27 
28     private static final String KEY_HOST_TOKEN = "host_token";
29     private static final String KEY_VIEW_WIDTH = "width";
30     private static final String KEY_VIEW_HEIGHT = "height";
31     public static final String KEY_DISPLAY_ID = "display_id";
32     private static final String KEY_SURFACE_PACKAGE = "surface_package";
33     private static final String KEY_CALLBACK = "callback";
34     public static final String KEY_WALLPAPER_COLORS = "wallpaper_colors";
35 
36     /** Create a surface view request. */
createSurfaceViewRequest( SurfaceView surfaceView, @Nullable Bundle extras)37     public static Bundle createSurfaceViewRequest(
38             SurfaceView surfaceView,
39             @Nullable Bundle extras) {
40         Bundle bundle = new Bundle();
41         bundle.putBinder(KEY_HOST_TOKEN, surfaceView.getHostToken());
42         // TODO (b/305258307): Figure out why SurfaceView.getDisplay returns null in small preview
43         if (surfaceView.getDisplay() != null) {
44             bundle.putInt(KEY_DISPLAY_ID, surfaceView.getDisplay().getDisplayId());
45         }
46         bundle.putInt(KEY_VIEW_WIDTH, surfaceView.getWidth());
47         bundle.putInt(KEY_VIEW_HEIGHT, surfaceView.getHeight());
48         if (extras != null) {
49             bundle.putAll(extras);
50         }
51         return bundle;
52     }
53 
54     /** Return the surface package. */
getSurfacePackage(Bundle bundle)55     public static SurfaceControlViewHost.SurfacePackage getSurfacePackage(Bundle bundle) {
56         return bundle.getParcelable(KEY_SURFACE_PACKAGE);
57     }
58 
59     /** Return the message callback. */
getCallback(Bundle bundle)60     public static Message getCallback(Bundle bundle) {
61         return bundle.getParcelable(KEY_CALLBACK);
62     }
63 }
64