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 | |
| 30 | import static com.android.server.EventLogTags.WM_TASK_CREATED; |
| 31 | import static com.android.server.wm.DragResizeMode.DRAG_RESIZE_MODE_DOCKED_DIVIDER; |
| 32 | import static com.android.server.wm.WindowContainer.POSITION_BOTTOM; |
| 33 | import static com.android.server.wm.WindowContainer.POSITION_TOP; |
| 34 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK; |
| 35 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 36 | |
| 37 | /** |
| 38 | * Controller for the task container. This is created by activity manager to link task records to |
| 39 | * the task container they use in window manager. |
| 40 | * |
| 41 | * Test class: {@link TaskWindowContainerControllerTests} |
| 42 | */ |
| 43 | public class TaskWindowContainerController |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 44 | extends WindowContainerController<Task, TaskWindowContainerListener> { |
| 45 | |
| 46 | private static final int REPORT_SNAPSHOT_CHANGED = 0; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 47 | |
| 48 | private final int mTaskId; |
| 49 | |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 50 | private final Handler mHandler = new Handler(Looper.getMainLooper()) { |
| 51 | |
| 52 | @Override |
| 53 | public void handleMessage(Message msg) { |
| 54 | switch (msg.what) { |
| 55 | case REPORT_SNAPSHOT_CHANGED: |
Jorim Jaggi | 329a583 | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 56 | if (mListener != null) { |
| 57 | mListener.onSnapshotChanged((TaskSnapshot) msg.obj); |
| 58 | } |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 59 | break; |
| 60 | } |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | public TaskWindowContainerController(int taskId, TaskWindowContainerListener listener, |
| 65 | int stackId, int userId, Rect bounds, Configuration overrideConfig, int resizeMode, |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 66 | boolean homeTask, boolean isOnTopLauncher, boolean toTop, boolean showForAllUsers, |
| 67 | TaskDescription taskDescription) { |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 68 | super(listener, WindowManagerService.getInstance()); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 69 | mTaskId = taskId; |
| 70 | |
| 71 | synchronized(mWindowMap) { |
| 72 | if (DEBUG_STACK) Slog.i(TAG_WM, "TaskWindowContainerController: taskId=" + taskId |
| 73 | + " stackId=" + stackId + " bounds=" + bounds); |
| 74 | |
| 75 | // TODO: Pass controller for the stack to get the container object when stack is |
| 76 | // switched to use controller. |
| 77 | final TaskStack stack = mService.mStackIdToStack.get(stackId); |
| 78 | if (stack == null) { |
| 79 | throw new IllegalArgumentException("TaskWindowContainerController: invalid stackId=" |
| 80 | + stackId); |
| 81 | } |
| 82 | EventLog.writeEvent(WM_TASK_CREATED, taskId, stackId); |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 83 | final Task task = createTask(taskId, stack, userId, bounds, overrideConfig, resizeMode, |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 84 | homeTask, isOnTopLauncher, taskDescription); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 85 | final int position = toTop ? POSITION_TOP : POSITION_BOTTOM; |
| 86 | stack.addTask(task, position, showForAllUsers, true /* moveParents */); |
| 87 | } |
| 88 | } |
| 89 | |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 90 | @VisibleForTesting |
| 91 | Task createTask(int taskId, TaskStack stack, int userId, Rect bounds, |
| 92 | Configuration overrideConfig, int resizeMode, boolean homeTask, |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 93 | boolean isOnTopLauncher, TaskDescription taskDescription) { |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 94 | return new Task(taskId, stack, userId, mService, bounds, overrideConfig, isOnTopLauncher, |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 95 | resizeMode, homeTask, taskDescription, this); |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 98 | @Override |
| 99 | public void removeContainer() { |
| 100 | synchronized(mWindowMap) { |
| 101 | if (mContainer == null) { |
| 102 | if (DEBUG_STACK) Slog.i(TAG_WM, "removeTask: could not find taskId=" + mTaskId); |
| 103 | return; |
| 104 | } |
| 105 | mContainer.removeIfPossible(); |
| 106 | super.removeContainer(); |
| 107 | } |
| 108 | } |
| 109 | |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 110 | public void positionChildAt(AppWindowContainerController childController, int position) { |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 111 | 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 Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 123 | task.positionChildAt(position, aToken, false /* includeParents */); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | public void reparent(int stackId, int position) { |
| 128 | synchronized (mWindowMap) { |
| 129 | if (DEBUG_STACK) Slog.i(TAG_WM, "reparent: moving taskId=" + mTaskId |
| 130 | + " to stackId=" + stackId + " at " + position); |
| 131 | if (mContainer == null) { |
| 132 | if (DEBUG_STACK) Slog.i(TAG_WM, |
| 133 | "reparent: could not find taskId=" + mTaskId); |
| 134 | return; |
| 135 | } |
| 136 | final TaskStack stack = mService.mStackIdToStack.get(stackId); |
| 137 | if (stack == null) { |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 138 | throw new IllegalArgumentException("reparent: could not find stackId=" + stackId); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 139 | } |
| 140 | mContainer.reparent(stack, position); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 141 | mService.mWindowPlacerLocked.performSurfacePlacement(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | public void setResizeable(int resizeMode) { |
| 146 | synchronized (mWindowMap) { |
| 147 | if (mContainer != null) { |
| 148 | mContainer.setResizeable(resizeMode); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | public void resize(Rect bounds, Configuration overrideConfig, boolean relayout, |
| 154 | boolean forced) { |
| 155 | synchronized (mWindowMap) { |
| 156 | if (mContainer == null) { |
| 157 | throw new IllegalArgumentException("resizeTask: taskId " + mTaskId + " not found."); |
| 158 | } |
| 159 | |
| 160 | if (mContainer.resizeLocked(bounds, overrideConfig, forced) && relayout) { |
| 161 | mContainer.getDisplayContent().setLayoutNeeded(); |
| 162 | mService.mWindowPlacerLocked.performSurfacePlacement(); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // TODO: Move to positionChildAt() in stack controller once we have a stack controller. |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 168 | public void positionAt(int position, Rect bounds, Configuration overrideConfig) { |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 169 | synchronized (mWindowMap) { |
| 170 | if (DEBUG_STACK) Slog.i(TAG_WM, "positionChildAt: positioning taskId=" + mTaskId |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 171 | + " at " + position); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 172 | if (mContainer == null) { |
| 173 | if (DEBUG_STACK) Slog.i(TAG_WM, |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 174 | "positionAt: could not find taskId=" + mTaskId); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 175 | return; |
| 176 | } |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 177 | final TaskStack stack = mContainer.mStack; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 178 | if (stack == null) { |
| 179 | if (DEBUG_STACK) Slog.i(TAG_WM, |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 180 | "positionAt: could not find stack for task=" + mContainer); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 181 | return; |
| 182 | } |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 183 | mContainer.positionAt(position, bounds, overrideConfig); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 184 | final DisplayContent displayContent = stack.getDisplayContent(); |
| 185 | displayContent.setLayoutNeeded(); |
| 186 | mService.mWindowPlacerLocked.performSurfacePlacement(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // TODO: Replace with moveChildToTop in stack controller? |
| 191 | public void moveToTop(boolean includingParents) { |
| 192 | synchronized(mWindowMap) { |
| 193 | if (mContainer == null) { |
| 194 | Slog.e(TAG_WM, "moveToTop: taskId=" + mTaskId + " not found"); |
| 195 | return; |
| 196 | } |
| 197 | final TaskStack stack = mContainer.mStack; |
| 198 | stack.positionChildAt(POSITION_TOP, mContainer, includingParents); |
| 199 | |
| 200 | if (mService.mAppTransition.isTransitionSet()) { |
| 201 | mContainer.setSendingToBottom(false); |
| 202 | } |
| 203 | stack.getDisplayContent().layoutAndAssignWindowLayersIfNeeded(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // TODO: Replace with moveChildToBottom in stack controller? |
| 208 | public void moveToBottom() { |
| 209 | synchronized(mWindowMap) { |
| 210 | if (mContainer == null) { |
| 211 | Slog.e(TAG_WM, "moveTaskToBottom: taskId=" + mTaskId + " not found"); |
| 212 | return; |
| 213 | } |
| 214 | final TaskStack stack = mContainer.mStack; |
| 215 | stack.positionChildAt(POSITION_BOTTOM, mContainer, false /* includingParents */); |
| 216 | if (mService.mAppTransition.isTransitionSet()) { |
| 217 | mContainer.setSendingToBottom(true); |
| 218 | } |
| 219 | stack.getDisplayContent().layoutAndAssignWindowLayersIfNeeded(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | public void getBounds(Rect bounds) { |
| 224 | synchronized (mWindowMap) { |
| 225 | if (mContainer != null) { |
| 226 | mContainer.getBounds(bounds); |
| 227 | return; |
| 228 | } |
| 229 | bounds.setEmpty(); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Puts this task into docked drag resizing mode. See {@link DragResizeMode}. |
| 235 | * |
| 236 | * @param resizing Whether to put the task into drag resize mode. |
| 237 | */ |
| 238 | public void setTaskDockedResizing(boolean resizing) { |
| 239 | synchronized (mWindowMap) { |
| 240 | if (mContainer == null) { |
| 241 | Slog.w(TAG_WM, "setTaskDockedResizing: taskId " + mTaskId + " not found."); |
| 242 | return; |
| 243 | } |
| 244 | mContainer.setDragResizing(resizing, DRAG_RESIZE_MODE_DOCKED_DIVIDER); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | public void cancelWindowTransition() { |
| 249 | synchronized (mWindowMap) { |
| 250 | if (mContainer == null) { |
| 251 | Slog.w(TAG_WM, "cancelWindowTransition: taskId " + mTaskId + " not found."); |
| 252 | return; |
| 253 | } |
| 254 | mContainer.cancelTaskWindowTransition(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | public void cancelThumbnailTransition() { |
| 259 | synchronized (mWindowMap) { |
| 260 | if (mContainer == null) { |
| 261 | Slog.w(TAG_WM, "cancelThumbnailTransition: taskId " + mTaskId + " not found."); |
| 262 | return; |
| 263 | } |
| 264 | mContainer.cancelTaskThumbnailTransition(); |
| 265 | } |
| 266 | } |
| 267 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 268 | public void setTaskDescription(TaskDescription taskDescription) { |
| 269 | synchronized (mWindowMap) { |
| 270 | if (mContainer == null) { |
| 271 | Slog.w(TAG_WM, "setTaskDescription: taskId " + mTaskId + " not found."); |
| 272 | return; |
| 273 | } |
| 274 | mContainer.setTaskDescription(taskDescription); |
| 275 | } |
| 276 | } |
| 277 | |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 278 | void reportSnapshotChanged(TaskSnapshot snapshot) { |
| 279 | mHandler.obtainMessage(REPORT_SNAPSHOT_CHANGED, snapshot).sendToTarget(); |
| 280 | } |
| 281 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 282 | @Override |
| 283 | public String toString() { |
| 284 | return "{TaskWindowContainerController taskId=" + mTaskId + "}"; |
| 285 | } |
| 286 | } |