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