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); |
| 120 | final DisplayContent displayContent = stack.getDisplayContent(); |
| 121 | displayContent.setLayoutNeeded(); |
| 122 | mService.mWindowPlacerLocked.performSurfacePlacement(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | public void setResizeable(int resizeMode) { |
| 127 | synchronized (mWindowMap) { |
| 128 | if (mContainer != null) { |
| 129 | mContainer.setResizeable(resizeMode); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | public void resize(Rect bounds, Configuration overrideConfig, boolean relayout, |
| 135 | boolean forced) { |
| 136 | synchronized (mWindowMap) { |
| 137 | if (mContainer == null) { |
| 138 | throw new IllegalArgumentException("resizeTask: taskId " + mTaskId + " not found."); |
| 139 | } |
| 140 | |
| 141 | if (mContainer.resizeLocked(bounds, overrideConfig, forced) && relayout) { |
| 142 | mContainer.getDisplayContent().setLayoutNeeded(); |
| 143 | mService.mWindowPlacerLocked.performSurfacePlacement(); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // 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^] | 149 | public void positionAt(int position, Rect bounds, Configuration overrideConfig) { |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 150 | synchronized (mWindowMap) { |
| 151 | if (DEBUG_STACK) Slog.i(TAG_WM, "positionChildAt: positioning taskId=" + mTaskId |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame^] | 152 | + " at " + position); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 153 | if (mContainer == null) { |
| 154 | if (DEBUG_STACK) Slog.i(TAG_WM, |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame^] | 155 | "positionAt: could not find taskId=" + mTaskId); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 156 | return; |
| 157 | } |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame^] | 158 | final TaskStack stack = mContainer.mStack; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 159 | if (stack == null) { |
| 160 | if (DEBUG_STACK) Slog.i(TAG_WM, |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame^] | 161 | "positionAt: could not find stack for task=" + mContainer); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 162 | return; |
| 163 | } |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame^] | 164 | mContainer.positionAt(position, bounds, overrideConfig); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 165 | final DisplayContent displayContent = stack.getDisplayContent(); |
| 166 | displayContent.setLayoutNeeded(); |
| 167 | mService.mWindowPlacerLocked.performSurfacePlacement(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // TODO: Replace with moveChildToTop in stack controller? |
| 172 | public void moveToTop(boolean includingParents) { |
| 173 | synchronized(mWindowMap) { |
| 174 | if (mContainer == null) { |
| 175 | Slog.e(TAG_WM, "moveToTop: taskId=" + mTaskId + " not found"); |
| 176 | return; |
| 177 | } |
| 178 | final TaskStack stack = mContainer.mStack; |
| 179 | stack.positionChildAt(POSITION_TOP, mContainer, includingParents); |
| 180 | |
| 181 | if (mService.mAppTransition.isTransitionSet()) { |
| 182 | mContainer.setSendingToBottom(false); |
| 183 | } |
| 184 | stack.getDisplayContent().layoutAndAssignWindowLayersIfNeeded(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // TODO: Replace with moveChildToBottom in stack controller? |
| 189 | public void moveToBottom() { |
| 190 | synchronized(mWindowMap) { |
| 191 | if (mContainer == null) { |
| 192 | Slog.e(TAG_WM, "moveTaskToBottom: taskId=" + mTaskId + " not found"); |
| 193 | return; |
| 194 | } |
| 195 | final TaskStack stack = mContainer.mStack; |
| 196 | stack.positionChildAt(POSITION_BOTTOM, mContainer, false /* includingParents */); |
| 197 | if (mService.mAppTransition.isTransitionSet()) { |
| 198 | mContainer.setSendingToBottom(true); |
| 199 | } |
| 200 | stack.getDisplayContent().layoutAndAssignWindowLayersIfNeeded(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | public void getBounds(Rect bounds) { |
| 205 | synchronized (mWindowMap) { |
| 206 | if (mContainer != null) { |
| 207 | mContainer.getBounds(bounds); |
| 208 | return; |
| 209 | } |
| 210 | bounds.setEmpty(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Puts this task into docked drag resizing mode. See {@link DragResizeMode}. |
| 216 | * |
| 217 | * @param resizing Whether to put the task into drag resize mode. |
| 218 | */ |
| 219 | public void setTaskDockedResizing(boolean resizing) { |
| 220 | synchronized (mWindowMap) { |
| 221 | if (mContainer == null) { |
| 222 | Slog.w(TAG_WM, "setTaskDockedResizing: taskId " + mTaskId + " not found."); |
| 223 | return; |
| 224 | } |
| 225 | mContainer.setDragResizing(resizing, DRAG_RESIZE_MODE_DOCKED_DIVIDER); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | public void cancelWindowTransition() { |
| 230 | synchronized (mWindowMap) { |
| 231 | if (mContainer == null) { |
| 232 | Slog.w(TAG_WM, "cancelWindowTransition: taskId " + mTaskId + " not found."); |
| 233 | return; |
| 234 | } |
| 235 | mContainer.cancelTaskWindowTransition(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | public void cancelThumbnailTransition() { |
| 240 | synchronized (mWindowMap) { |
| 241 | if (mContainer == null) { |
| 242 | Slog.w(TAG_WM, "cancelThumbnailTransition: taskId " + mTaskId + " not found."); |
| 243 | return; |
| 244 | } |
| 245 | mContainer.cancelTaskThumbnailTransition(); |
| 246 | } |
| 247 | } |
| 248 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 249 | /** |
| 250 | * @return a graphic buffer representing a screenshot of a task |
| 251 | */ |
Jorim Jaggi | e2c77f9 | 2016-12-29 14:57:22 +0100 | [diff] [blame] | 252 | public TaskSnapshot getSnapshot() { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 253 | synchronized (mWindowMap) { |
| 254 | if (mContainer == null) { |
| 255 | Slog.w(TAG_WM, "getSnapshot: taskId " + mTaskId + " not found."); |
| 256 | return null; |
| 257 | } |
| 258 | return mService.mTaskSnapshotController.getSnapshot(mContainer); |
| 259 | } |
| 260 | } |
| 261 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 262 | @Override |
| 263 | public String toString() { |
| 264 | return "{TaskWindowContainerController taskId=" + mTaskId + "}"; |
| 265 | } |
| 266 | } |