blob: b3bb0b7e001dc3fd34b00cff5cad99920dd147cb [file] [log] [blame]
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -08001/*
2 * Copyright (C) 2016 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
17package com.android.server.wm;
18
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010019import android.app.ActivityManager.TaskDescription;
Jorim Jaggie2c77f92016-12-29 14:57:22 +010020import android.app.ActivityManager.TaskSnapshot;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080021import android.content.res.Configuration;
22import android.graphics.Rect;
Jorim Jaggifb9d78a2017-01-05 18:57:12 +010023import android.os.Handler;
24import android.os.Looper;
25import android.os.Message;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080026import android.util.EventLog;
27import android.util.Slog;
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080028import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080029
Wale Ogunwale1666e312016-12-16 11:27:18 -080030import java.lang.ref.WeakReference;
31
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080032import static com.android.server.EventLogTags.WM_TASK_CREATED;
33import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_DOCKED_DIVIDER;
34import static com.android.server.wm.WindowContainer.POSITION_BOTTOM;
35import static com.android.server.wm.WindowContainer.POSITION_TOP;
36import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
37import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
38
39/**
40 * Controller for the task container. This is created by activity manager to link task records to
41 * the task container they use in window manager.
42 *
43 * Test class: {@link TaskWindowContainerControllerTests}
44 */
45public class TaskWindowContainerController
Jorim Jaggifb9d78a2017-01-05 18:57:12 +010046 extends WindowContainerController<Task, TaskWindowContainerListener> {
47
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080048 private final int mTaskId;
Wale Ogunwale1666e312016-12-16 11:27:18 -080049 private final H mHandler;
Jorim Jaggifb9d78a2017-01-05 18:57:12 +010050
51 public TaskWindowContainerController(int taskId, TaskWindowContainerListener listener,
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070052 StackWindowController stackController, int userId, Rect bounds, int resizeMode,
53 boolean supportsPictureInPicture, boolean toTop, boolean showForAllUsers,
54 TaskDescription taskDescription) {
55 this(taskId, listener, stackController, userId, bounds, resizeMode,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070056 supportsPictureInPicture, toTop, showForAllUsers, taskDescription,
Wale Ogunwale069bbd32017-02-03 07:58:14 -080057 WindowManagerService.getInstance());
Wale Ogunwale1666e312016-12-16 11:27:18 -080058 }
59
60 public TaskWindowContainerController(int taskId, TaskWindowContainerListener listener,
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070061 StackWindowController stackController, int userId, Rect bounds, int resizeMode,
62 boolean supportsPictureInPicture, boolean toTop, boolean showForAllUsers,
63 TaskDescription taskDescription, WindowManagerService service) {
Wale Ogunwale1666e312016-12-16 11:27:18 -080064 super(listener, service);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080065 mTaskId = taskId;
Wale Ogunwale1666e312016-12-16 11:27:18 -080066 mHandler = new H(new WeakReference<>(this), service.mH.getLooper());
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080067
68 synchronized(mWindowMap) {
69 if (DEBUG_STACK) Slog.i(TAG_WM, "TaskWindowContainerController: taskId=" + taskId
Wale Ogunwale1666e312016-12-16 11:27:18 -080070 + " stack=" + stackController + " bounds=" + bounds);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080071
Wale Ogunwale1666e312016-12-16 11:27:18 -080072 final TaskStack stack = stackController.mContainer;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080073 if (stack == null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -080074 throw new IllegalArgumentException("TaskWindowContainerController: invalid stack="
75 + stackController);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080076 }
Wale Ogunwale1666e312016-12-16 11:27:18 -080077 EventLog.writeEvent(WM_TASK_CREATED, taskId, stack.mStackId);
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070078 final Task task = createTask(taskId, stack, userId, bounds, resizeMode,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070079 supportsPictureInPicture, taskDescription);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080080 final int position = toTop ? POSITION_TOP : POSITION_BOTTOM;
Wale Ogunwale1f5b92f2017-07-18 06:33:01 -070081 // We only want to move the parents to the parents if we are creating this task at the
82 // top of its stack.
83 stack.addTask(task, position, showForAllUsers, toTop /* moveParents */);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080084 }
85 }
86
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080087 @VisibleForTesting
Wale Ogunwale034a8ec2017-09-02 17:14:40 -070088 Task createTask(int taskId, TaskStack stack, int userId, Rect bounds, int resizeMode,
89 boolean supportsPictureInPicture, TaskDescription taskDescription) {
90 return new Task(taskId, stack, userId, mService, bounds, resizeMode,
Wale Ogunwale6fbde9f2017-08-24 07:24:12 -070091 supportsPictureInPicture, taskDescription, this);
Wale Ogunwalec5cc3012017-01-13 13:26:16 -080092 }
93
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -080094 @Override
95 public void removeContainer() {
96 synchronized(mWindowMap) {
97 if (mContainer == null) {
98 if (DEBUG_STACK) Slog.i(TAG_WM, "removeTask: could not find taskId=" + mTaskId);
99 return;
100 }
101 mContainer.removeIfPossible();
102 super.removeContainer();
103 }
104 }
105
Bryce Leed58d7b32017-09-08 15:55:22 -0700106 public void positionChildAtTop(AppWindowContainerController childController) {
107 positionChildAt(childController, POSITION_TOP);
108 }
109
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800110 public void positionChildAt(AppWindowContainerController childController, int position) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800111 synchronized(mService.mWindowMap) {
112 final AppWindowToken aToken = childController.mContainer;
113 if (aToken == null) {
114 Slog.w(TAG_WM,
115 "Attempted to position of non-existing app : " + childController);
116 return;
117 }
118
119 final Task task = mContainer;
120 if (task == null) {
121 throw new IllegalArgumentException("positionChildAt: invalid task=" + this);
122 }
Wale Ogunwalec5cc3012017-01-13 13:26:16 -0800123 task.positionChildAt(position, aToken, false /* includeParents */);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800124 }
125 }
126
Wale Ogunwale2719cc12017-04-14 09:45:27 -0700127 public void reparent(StackWindowController stackController, int position, boolean moveParents) {
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800128 synchronized (mWindowMap) {
129 if (DEBUG_STACK) Slog.i(TAG_WM, "reparent: moving taskId=" + mTaskId
Wale Ogunwale1666e312016-12-16 11:27:18 -0800130 + " to stack=" + stackController + " at " + position);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800131 if (mContainer == null) {
132 if (DEBUG_STACK) Slog.i(TAG_WM,
133 "reparent: could not find taskId=" + mTaskId);
134 return;
135 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800136 final TaskStack stack = stackController.mContainer;
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800137 if (stack == null) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800138 throw new IllegalArgumentException("reparent: could not find stack="
139 + stackController);
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800140 }
Wale Ogunwale2719cc12017-04-14 09:45:27 -0700141 mContainer.reparent(stack, position, moveParents);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800142 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800143 }
144 }
145
146 public void setResizeable(int resizeMode) {
147 synchronized (mWindowMap) {
148 if (mContainer != null) {
149 mContainer.setResizeable(resizeMode);
150 }
151 }
152 }
153
154 public void resize(Rect bounds, Configuration overrideConfig, boolean relayout,
155 boolean forced) {
156 synchronized (mWindowMap) {
157 if (mContainer == null) {
158 throw new IllegalArgumentException("resizeTask: taskId " + mTaskId + " not found.");
159 }
160
161 if (mContainer.resizeLocked(bounds, overrideConfig, forced) && relayout) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800162 mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded();
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800163 }
164 }
165 }
166
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800167 public void getBounds(Rect bounds) {
168 synchronized (mWindowMap) {
169 if (mContainer != null) {
170 mContainer.getBounds(bounds);
171 return;
172 }
173 bounds.setEmpty();
174 }
175 }
176
177 /**
178 * Puts this task into docked drag resizing mode. See {@link DragResizeMode}.
179 *
180 * @param resizing Whether to put the task into drag resize mode.
181 */
182 public void setTaskDockedResizing(boolean resizing) {
183 synchronized (mWindowMap) {
184 if (mContainer == null) {
185 Slog.w(TAG_WM, "setTaskDockedResizing: taskId " + mTaskId + " not found.");
186 return;
187 }
188 mContainer.setDragResizing(resizing, DRAG_RESIZE_MODE_DOCKED_DIVIDER);
189 }
190 }
191
192 public void cancelWindowTransition() {
193 synchronized (mWindowMap) {
194 if (mContainer == null) {
195 Slog.w(TAG_WM, "cancelWindowTransition: taskId " + mTaskId + " not found.");
196 return;
197 }
198 mContainer.cancelTaskWindowTransition();
199 }
200 }
201
202 public void cancelThumbnailTransition() {
203 synchronized (mWindowMap) {
204 if (mContainer == null) {
205 Slog.w(TAG_WM, "cancelThumbnailTransition: taskId " + mTaskId + " not found.");
206 return;
207 }
208 mContainer.cancelTaskThumbnailTransition();
209 }
210 }
211
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100212 public void setTaskDescription(TaskDescription taskDescription) {
213 synchronized (mWindowMap) {
214 if (mContainer == null) {
215 Slog.w(TAG_WM, "setTaskDescription: taskId " + mTaskId + " not found.");
216 return;
217 }
218 mContainer.setTaskDescription(taskDescription);
219 }
220 }
221
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100222 void reportSnapshotChanged(TaskSnapshot snapshot) {
Wale Ogunwale1666e312016-12-16 11:27:18 -0800223 mHandler.obtainMessage(H.REPORT_SNAPSHOT_CHANGED, snapshot).sendToTarget();
224 }
225
226 void requestResize(Rect bounds, int resizeMode) {
227 mHandler.obtainMessage(H.REQUEST_RESIZE, resizeMode, 0, bounds).sendToTarget();
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100228 }
229
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800230 @Override
231 public String toString() {
232 return "{TaskWindowContainerController taskId=" + mTaskId + "}";
233 }
Wale Ogunwale1666e312016-12-16 11:27:18 -0800234
235 private static final class H extends Handler {
236
237 static final int REPORT_SNAPSHOT_CHANGED = 0;
238 static final int REQUEST_RESIZE = 1;
239
240 private final WeakReference<TaskWindowContainerController> mController;
241
242 H(WeakReference<TaskWindowContainerController> controller, Looper looper) {
243 super(looper);
244 mController = controller;
245 }
246
247 @Override
248 public void handleMessage(Message msg) {
249 final TaskWindowContainerController controller = mController.get();
250 final TaskWindowContainerListener listener = (controller != null)
251 ? controller.mListener : null;
252 if (listener == null) {
253 return;
254 }
255 switch (msg.what) {
256 case REPORT_SNAPSHOT_CHANGED:
257 listener.onSnapshotChanged((TaskSnapshot) msg.obj);
258 break;
259 case REQUEST_RESIZE:
260 listener.requestResize((Rect) msg.obj, msg.arg1);
261 break;
262 }
263 }
264 }
Wale Ogunwalee1fe7fa22016-12-15 18:27:00 -0800265}