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 
17 package com.android.systemui.shared.system;
18 
19 import android.annotation.Nullable;
20 import android.os.Bundle;
21 import android.os.IBinder;
22 import android.view.SurfaceControl;
23 import android.view.SurfaceView;
24 
25 /** Util class that wraps a SurfaceView request into a bundle. */
26 public class SurfaceViewRequestUtils {
27     private static final String KEY_HOST_TOKEN = "host_token";
28     private static final String KEY_SURFACE_CONTROL = "surface_control";
29     private static final String KEY_DISPLAY_ID = "display_id";
30 
31     /** Creates a SurfaceView based bundle that stores the input host token and surface control. */
createSurfaceBundle(SurfaceView surfaceView)32     public static Bundle createSurfaceBundle(SurfaceView surfaceView) {
33         Bundle bundle = new Bundle();
34         bundle.putBinder(KEY_HOST_TOKEN, surfaceView.getHostToken());
35         bundle.putParcelable(KEY_SURFACE_CONTROL, surfaceView.getSurfaceControl());
36         bundle.putInt(KEY_DISPLAY_ID, surfaceView.getDisplay().getDisplayId());
37         return bundle;
38     }
39 
40     /**
41      * Retrieves the SurfaceControl from a bundle created by
42      * {@link #createSurfaceBundle(SurfaceView)}.
43      */
getSurfaceControl(Bundle bundle)44     public static SurfaceControl getSurfaceControl(Bundle bundle) {
45         return bundle.getParcelable(KEY_SURFACE_CONTROL);
46     }
47 
48     /**
49      * Retrieves the input token from a bundle created by {@link #createSurfaceBundle(SurfaceView)}.
50      */
getHostToken(Bundle bundle)51     public static @Nullable IBinder getHostToken(Bundle bundle) {
52         return bundle.getBinder(KEY_HOST_TOKEN);
53     }
54 
55     /**
56      * Retrieves the display id from a bundle created by {@link #createSurfaceBundle(SurfaceView)}.
57      */
getDisplayId(Bundle bundle)58     public static int getDisplayId(Bundle bundle) {
59         return bundle.getInt(KEY_DISPLAY_ID);
60     }
61 
SurfaceViewRequestUtils()62     private SurfaceViewRequestUtils() {}
63 }
64