• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
20 import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED;
21 
22 import android.app.ActivityOptions;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.graphics.Rect;
26 import android.os.Bundle;
27 import android.view.SurfaceControl;
28 import android.view.SurfaceHolder;
29 import android.view.SurfaceView;
30 import android.window.WindowContainerToken;
31 import android.window.WindowContainerTransaction;
32 
33 /**
34  * Simple SurfaceView wrapper which registers a TaskOrganizer
35  * after it's Surface is ready.
36  */
37 class TaskView extends SurfaceView {
38     private WindowContainerToken mWc;
39     private Context mContext;
40     private SurfaceControl mLeash;
41     private TaskOrganizerMultiWindowTest.Organizer mOrganizer;
42     private Intent mIntent;
43     private boolean mLaunched = false;
44 
TaskView(Context c, TaskOrganizerMultiWindowTest.Organizer organizer, Intent intent)45     TaskView(Context c, TaskOrganizerMultiWindowTest.Organizer organizer,
46             Intent intent) {
47         super(c);
48         mContext = c;
49         mOrganizer = organizer;
50         mIntent = intent;
51         getHolder().addCallback(
52                 new SurfaceHolder.Callback() {
53                     @Override
54                     public void surfaceCreated(SurfaceHolder holder) {}
55 
56                     @Override
57                     public void surfaceChanged(SurfaceHolder holder,
58                             int format, int width, int height) {
59                         if (!mLaunched) {
60                             launchOrganizedActivity(mIntent, width, height);
61                             mLaunched = true;
62                         } else {
63                             resizeTask(width, height);
64                         }
65                     }
66 
67                     @Override
68                     public void surfaceDestroyed(SurfaceHolder holder) {}
69                 }
70         );
71         setZOrderOnTop(true);
72     }
73 
launchOrganizedActivity(Intent i, int width, int height)74     private void launchOrganizedActivity(Intent i, int width, int height) {
75         mContext.startActivity(i, makeLaunchOptions(width, height));
76     }
77 
makeLaunchOptions(int width, int height)78     private Bundle makeLaunchOptions(int width, int height) {
79         ActivityOptions o = ActivityOptions.makeBasic();
80         o.setLaunchWindowingMode(WINDOWING_MODE_MULTI_WINDOW);
81         o.setLaunchBounds(new Rect(0, 0, width, height));
82         o.setTaskOverlay(true, true);
83         o.setTaskAlwaysOnTop(true);
84         return o.toBundle();
85     }
86 
resizeTask(int width, int height)87     void resizeTask(int width, int height) {
88         final WindowContainerTransaction wct = new WindowContainerTransaction();
89         wct.setBounds(mWc, new Rect(0, 0, width, height)).setHidden(mWc, false);
90         try {
91             mOrganizer.applySyncTransaction(wct, mOrganizer.mTransactionCallback);
92         } catch (Exception e) {
93             // Oh well
94         }
95     }
96 
hideTask()97     void hideTask() {
98         if (mWc == null) {
99             return;
100         }
101         final WindowContainerTransaction wct = new WindowContainerTransaction();
102         wct.setWindowingMode(mWc, WINDOWING_MODE_UNDEFINED).setHidden(mWc, true);
103         try {
104             mOrganizer.applySyncTransaction(wct, mOrganizer.mTransactionCallback);
105         } catch (Exception e) {
106             // Oh well
107         }
108         releaseLeash();
109     }
110 
reparentTask(WindowContainerToken wc, SurfaceControl leash)111     void reparentTask(WindowContainerToken wc, SurfaceControl leash) {
112         mWc = wc;
113         mLeash = leash;
114         reparentLeash();
115     }
116 
reparentLeash()117     void reparentLeash() {
118         SurfaceControl.Transaction t = new SurfaceControl.Transaction();
119         t.reparent(mLeash, getSurfaceControl())
120             .show(mLeash)
121             .apply();
122     }
123 
releaseLeash()124     void releaseLeash() {
125         SurfaceControl.Transaction t = new SurfaceControl.Transaction();
126         t.remove(mLeash).apply();
127     }
128 }
129