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.test.taskembed;
18 
19 import android.content.Context;
20 import android.view.SurfaceControl;
21 import android.view.SurfaceHolder;
22 import android.view.SurfaceView;
23 import android.window.WindowContainerToken;
24 
25 /**
26  * Simple SurfaceView wrapper which registers a TaskOrganizer
27  * after it's Surface is ready.
28  */
29 class TaskView extends SurfaceView implements SurfaceHolder.Callback {
30     WindowContainerToken mWc;
31     private SurfaceControl mLeash;
32 
33     private boolean mSurfaceCreated = false;
34     private boolean mNeedsReparent;
35 
TaskView(Context c)36     TaskView(Context c) {
37         super(c);
38         getHolder().addCallback(this);
39         setZOrderOnTop(true);
40     }
41 
42     @Override
surfaceCreated(SurfaceHolder holder)43     public void surfaceCreated(SurfaceHolder holder) {
44         mSurfaceCreated = true;
45         if (mNeedsReparent) {
46             mNeedsReparent = false;
47             reparentLeash();
48         }
49     }
50 
51     @Override
surfaceChanged(SurfaceHolder holder, int format, int width, int height)52     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
53     }
54 
55     @Override
surfaceDestroyed(SurfaceHolder holder)56     public void surfaceDestroyed(SurfaceHolder holder) {
57     }
58 
reparentTask(WindowContainerToken wc, SurfaceControl leash)59     void reparentTask(WindowContainerToken wc, SurfaceControl leash) {
60         mWc = wc;
61         mLeash = leash;
62         if (!mSurfaceCreated) {
63             mNeedsReparent = true;
64         } else {
65             reparentLeash();
66         }
67     }
68 
reparentLeash()69     private void reparentLeash() {
70         SurfaceControl.Transaction t = new SurfaceControl.Transaction();
71         if (mLeash == null) {
72             return;
73         }
74 
75         t.reparent(mLeash, getSurfaceControl())
76             .setPosition(mLeash, 0, 0)
77             .show(mLeash)
78             .apply();
79     }
80 }
81