Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 19 | import android.app.ActivityManager.TaskDescription; |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 20 | import android.app.ActivityManager.TaskSnapshot; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 21 | import android.content.res.Configuration; |
| 22 | import android.graphics.Rect; |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.os.Looper; |
| 25 | import android.os.Message; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 26 | import android.util.EventLog; |
| 27 | import android.util.Slog; |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 28 | import com.android.internal.annotations.VisibleForTesting; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 29 | |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 30 | import java.lang.ref.WeakReference; |
| 31 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 32 | import static com.android.server.EventLogTags.WM_TASK_CREATED; |
| 33 | import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_DOCKED_DIVIDER; |
| 34 | import static com.android.server.wm.WindowContainer.POSITION_BOTTOM; |
| 35 | import static com.android.server.wm.WindowContainer.POSITION_TOP; |
| 36 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK; |
| 37 | import 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 | */ |
| 45 | public class TaskWindowContainerController |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 46 | extends WindowContainerController<Task, TaskWindowContainerListener> { |
| 47 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 48 | private final int mTaskId; |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 49 | private final H mHandler; |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 50 | |
| 51 | public TaskWindowContainerController(int taskId, TaskWindowContainerListener listener, |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 52 | StackWindowController stackController, int userId, Rect bounds, |
| 53 | Configuration overrideConfig, int resizeMode, boolean supportsPictureInPicture, |
Wale Ogunwale | 069bbd3 | 2017-02-03 07:58:14 -0800 | [diff] [blame] | 54 | boolean homeTask, boolean toTop, boolean showForAllUsers, |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 55 | TaskDescription taskDescription) { |
| 56 | this(taskId, listener, stackController, userId, bounds, overrideConfig, resizeMode, |
Wale Ogunwale | 069bbd3 | 2017-02-03 07:58:14 -0800 | [diff] [blame] | 57 | supportsPictureInPicture, homeTask, toTop, showForAllUsers, taskDescription, |
| 58 | WindowManagerService.getInstance()); |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | public TaskWindowContainerController(int taskId, TaskWindowContainerListener listener, |
| 62 | StackWindowController stackController, int userId, Rect bounds, |
| 63 | Configuration overrideConfig, int resizeMode, boolean supportsPictureInPicture, |
Wale Ogunwale | 069bbd3 | 2017-02-03 07:58:14 -0800 | [diff] [blame] | 64 | boolean homeTask, boolean toTop, boolean showForAllUsers, |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 65 | TaskDescription taskDescription, WindowManagerService service) { |
| 66 | super(listener, service); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 67 | mTaskId = taskId; |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 68 | mHandler = new H(new WeakReference<>(this), service.mH.getLooper()); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 69 | |
| 70 | synchronized(mWindowMap) { |
| 71 | if (DEBUG_STACK) Slog.i(TAG_WM, "TaskWindowContainerController: taskId=" + taskId |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 72 | + " stack=" + stackController + " bounds=" + bounds); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 73 | |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 74 | final TaskStack stack = stackController.mContainer; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 75 | if (stack == null) { |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 76 | throw new IllegalArgumentException("TaskWindowContainerController: invalid stack=" |
| 77 | + stackController); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 78 | } |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 79 | EventLog.writeEvent(WM_TASK_CREATED, taskId, stack.mStackId); |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 80 | final Task task = createTask(taskId, stack, userId, bounds, overrideConfig, resizeMode, |
Wale Ogunwale | 069bbd3 | 2017-02-03 07:58:14 -0800 | [diff] [blame] | 81 | supportsPictureInPicture, homeTask, taskDescription); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 82 | final int position = toTop ? POSITION_TOP : POSITION_BOTTOM; |
| 83 | stack.addTask(task, position, showForAllUsers, true /* moveParents */); |
| 84 | } |
| 85 | } |
| 86 | |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 87 | @VisibleForTesting |
| 88 | Task createTask(int taskId, TaskStack stack, int userId, Rect bounds, |
Winson Chung | d339538 | 2016-12-13 11:49:09 -0800 | [diff] [blame] | 89 | Configuration overrideConfig, int resizeMode, boolean supportsPictureInPicture, |
Wale Ogunwale | 069bbd3 | 2017-02-03 07:58:14 -0800 | [diff] [blame] | 90 | boolean homeTask, TaskDescription taskDescription) { |
| 91 | return new Task(taskId, stack, userId, mService, bounds, overrideConfig, resizeMode, |
| 92 | supportsPictureInPicture, homeTask, taskDescription, this); |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 95 | @Override |
| 96 | public void removeContainer() { |
| 97 | synchronized(mWindowMap) { |
| 98 | if (mContainer == null) { |
| 99 | if (DEBUG_STACK) Slog.i(TAG_WM, "removeTask: could not find taskId=" + mTaskId); |
| 100 | return; |
| 101 | } |
| 102 | mContainer.removeIfPossible(); |
| 103 | super.removeContainer(); |
| 104 | } |
| 105 | } |
| 106 | |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 107 | public void positionChildAt(AppWindowContainerController childController, int position) { |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 108 | synchronized(mService.mWindowMap) { |
| 109 | final AppWindowToken aToken = childController.mContainer; |
| 110 | if (aToken == null) { |
| 111 | Slog.w(TAG_WM, |
| 112 | "Attempted to position of non-existing app : " + childController); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | final Task task = mContainer; |
| 117 | if (task == null) { |
| 118 | throw new IllegalArgumentException("positionChildAt: invalid task=" + this); |
| 119 | } |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 120 | task.positionChildAt(position, aToken, false /* includeParents */); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Wale Ogunwale | 2719cc1 | 2017-04-14 09:45:27 -0700 | [diff] [blame^] | 124 | public void reparent(StackWindowController stackController, int position, boolean moveParents) { |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 125 | synchronized (mWindowMap) { |
| 126 | if (DEBUG_STACK) Slog.i(TAG_WM, "reparent: moving taskId=" + mTaskId |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 127 | + " to stack=" + stackController + " at " + position); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 128 | if (mContainer == null) { |
| 129 | if (DEBUG_STACK) Slog.i(TAG_WM, |
| 130 | "reparent: could not find taskId=" + mTaskId); |
| 131 | return; |
| 132 | } |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 133 | final TaskStack stack = stackController.mContainer; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 134 | if (stack == null) { |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 135 | throw new IllegalArgumentException("reparent: could not find stack=" |
| 136 | + stackController); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 137 | } |
Wale Ogunwale | 2719cc1 | 2017-04-14 09:45:27 -0700 | [diff] [blame^] | 138 | mContainer.reparent(stack, position, moveParents); |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 139 | mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded(); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | public void setResizeable(int resizeMode) { |
| 144 | synchronized (mWindowMap) { |
| 145 | if (mContainer != null) { |
| 146 | mContainer.setResizeable(resizeMode); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public void resize(Rect bounds, Configuration overrideConfig, boolean relayout, |
| 152 | boolean forced) { |
| 153 | synchronized (mWindowMap) { |
| 154 | if (mContainer == null) { |
| 155 | throw new IllegalArgumentException("resizeTask: taskId " + mTaskId + " not found."); |
| 156 | } |
| 157 | |
| 158 | if (mContainer.resizeLocked(bounds, overrideConfig, forced) && relayout) { |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 159 | mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded(); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 164 | public void getBounds(Rect bounds) { |
| 165 | synchronized (mWindowMap) { |
| 166 | if (mContainer != null) { |
| 167 | mContainer.getBounds(bounds); |
| 168 | return; |
| 169 | } |
| 170 | bounds.setEmpty(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Puts this task into docked drag resizing mode. See {@link DragResizeMode}. |
| 176 | * |
| 177 | * @param resizing Whether to put the task into drag resize mode. |
| 178 | */ |
| 179 | public void setTaskDockedResizing(boolean resizing) { |
| 180 | synchronized (mWindowMap) { |
| 181 | if (mContainer == null) { |
| 182 | Slog.w(TAG_WM, "setTaskDockedResizing: taskId " + mTaskId + " not found."); |
| 183 | return; |
| 184 | } |
| 185 | mContainer.setDragResizing(resizing, DRAG_RESIZE_MODE_DOCKED_DIVIDER); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | public void cancelWindowTransition() { |
| 190 | synchronized (mWindowMap) { |
| 191 | if (mContainer == null) { |
| 192 | Slog.w(TAG_WM, "cancelWindowTransition: taskId " + mTaskId + " not found."); |
| 193 | return; |
| 194 | } |
| 195 | mContainer.cancelTaskWindowTransition(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | public void cancelThumbnailTransition() { |
| 200 | synchronized (mWindowMap) { |
| 201 | if (mContainer == null) { |
| 202 | Slog.w(TAG_WM, "cancelThumbnailTransition: taskId " + mTaskId + " not found."); |
| 203 | return; |
| 204 | } |
| 205 | mContainer.cancelTaskThumbnailTransition(); |
| 206 | } |
| 207 | } |
| 208 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 209 | public void setTaskDescription(TaskDescription taskDescription) { |
| 210 | synchronized (mWindowMap) { |
| 211 | if (mContainer == null) { |
| 212 | Slog.w(TAG_WM, "setTaskDescription: taskId " + mTaskId + " not found."); |
| 213 | return; |
| 214 | } |
| 215 | mContainer.setTaskDescription(taskDescription); |
| 216 | } |
| 217 | } |
| 218 | |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 219 | void reportSnapshotChanged(TaskSnapshot snapshot) { |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 220 | mHandler.obtainMessage(H.REPORT_SNAPSHOT_CHANGED, snapshot).sendToTarget(); |
| 221 | } |
| 222 | |
| 223 | void requestResize(Rect bounds, int resizeMode) { |
| 224 | mHandler.obtainMessage(H.REQUEST_RESIZE, resizeMode, 0, bounds).sendToTarget(); |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 227 | @Override |
| 228 | public String toString() { |
| 229 | return "{TaskWindowContainerController taskId=" + mTaskId + "}"; |
| 230 | } |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 231 | |
| 232 | private static final class H extends Handler { |
| 233 | |
| 234 | static final int REPORT_SNAPSHOT_CHANGED = 0; |
| 235 | static final int REQUEST_RESIZE = 1; |
| 236 | |
| 237 | private final WeakReference<TaskWindowContainerController> mController; |
| 238 | |
| 239 | H(WeakReference<TaskWindowContainerController> controller, Looper looper) { |
| 240 | super(looper); |
| 241 | mController = controller; |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public void handleMessage(Message msg) { |
| 246 | final TaskWindowContainerController controller = mController.get(); |
| 247 | final TaskWindowContainerListener listener = (controller != null) |
| 248 | ? controller.mListener : null; |
| 249 | if (listener == null) { |
| 250 | return; |
| 251 | } |
| 252 | switch (msg.what) { |
| 253 | case REPORT_SNAPSHOT_CHANGED: |
| 254 | listener.onSnapshotChanged((TaskSnapshot) msg.obj); |
| 255 | break; |
| 256 | case REQUEST_RESIZE: |
| 257 | listener.requestResize((Rect) msg.obj, msg.arg1); |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | } |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 262 | } |