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.graphics.Rect; |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 22 | import android.os.Handler; |
| 23 | import android.os.Looper; |
| 24 | import android.os.Message; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 25 | import android.util.EventLog; |
| 26 | import android.util.Slog; |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 27 | import com.android.internal.annotations.VisibleForTesting; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 28 | |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 29 | import java.lang.ref.WeakReference; |
| 30 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 31 | import static com.android.server.EventLogTags.WM_TASK_CREATED; |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame^] | 32 | import static com.android.server.wm.ConfigurationContainer.BOUNDS_CHANGE_NONE; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 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 | 034a8ec | 2017-09-02 17:14:40 -0700 | [diff] [blame] | 52 | 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 Ogunwale | 6fbde9f | 2017-08-24 07:24:12 -0700 | [diff] [blame] | 56 | supportsPictureInPicture, toTop, showForAllUsers, taskDescription, |
Wale Ogunwale | 069bbd3 | 2017-02-03 07:58:14 -0800 | [diff] [blame] | 57 | WindowManagerService.getInstance()); |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | public TaskWindowContainerController(int taskId, TaskWindowContainerListener listener, |
Wale Ogunwale | 034a8ec | 2017-09-02 17:14:40 -0700 | [diff] [blame] | 61 | StackWindowController stackController, int userId, Rect bounds, int resizeMode, |
| 62 | boolean supportsPictureInPicture, boolean toTop, boolean showForAllUsers, |
| 63 | TaskDescription taskDescription, WindowManagerService service) { |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 64 | super(listener, service); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 65 | mTaskId = taskId; |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 66 | mHandler = new H(new WeakReference<>(this), service.mH.getLooper()); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 67 | |
| 68 | synchronized(mWindowMap) { |
| 69 | if (DEBUG_STACK) Slog.i(TAG_WM, "TaskWindowContainerController: taskId=" + taskId |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 70 | + " stack=" + stackController + " bounds=" + bounds); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 71 | |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 72 | final TaskStack stack = stackController.mContainer; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 73 | if (stack == null) { |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 74 | throw new IllegalArgumentException("TaskWindowContainerController: invalid stack=" |
| 75 | + stackController); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 76 | } |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 77 | EventLog.writeEvent(WM_TASK_CREATED, taskId, stack.mStackId); |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame^] | 78 | final Task task = createTask(taskId, stack, userId, resizeMode, |
Wale Ogunwale | 6fbde9f | 2017-08-24 07:24:12 -0700 | [diff] [blame] | 79 | supportsPictureInPicture, taskDescription); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 80 | final int position = toTop ? POSITION_TOP : POSITION_BOTTOM; |
Wale Ogunwale | 1f5b92f | 2017-07-18 06:33:01 -0700 | [diff] [blame] | 81 | // 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 Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 87 | @VisibleForTesting |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame^] | 88 | Task createTask(int taskId, TaskStack stack, int userId, int resizeMode, |
Wale Ogunwale | 034a8ec | 2017-09-02 17:14:40 -0700 | [diff] [blame] | 89 | boolean supportsPictureInPicture, TaskDescription taskDescription) { |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame^] | 90 | return new Task(taskId, stack, userId, mService, resizeMode, supportsPictureInPicture, |
| 91 | taskDescription, this); |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 94 | @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 Lee | d58d7b3 | 2017-09-08 15:55:22 -0700 | [diff] [blame] | 106 | public void positionChildAtTop(AppWindowContainerController childController) { |
| 107 | positionChildAt(childController, POSITION_TOP); |
| 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 | |
Wale Ogunwale | 2719cc1 | 2017-04-14 09:45:27 -0700 | [diff] [blame] | 127 | public void reparent(StackWindowController stackController, int position, boolean moveParents) { |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 128 | synchronized (mWindowMap) { |
| 129 | if (DEBUG_STACK) Slog.i(TAG_WM, "reparent: moving taskId=" + mTaskId |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 130 | + " to stack=" + stackController + " at " + position); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 131 | if (mContainer == null) { |
| 132 | if (DEBUG_STACK) Slog.i(TAG_WM, |
| 133 | "reparent: could not find taskId=" + mTaskId); |
| 134 | return; |
| 135 | } |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 136 | final TaskStack stack = stackController.mContainer; |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 137 | if (stack == null) { |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 138 | throw new IllegalArgumentException("reparent: could not find stack=" |
| 139 | + stackController); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 140 | } |
Wale Ogunwale | 2719cc1 | 2017-04-14 09:45:27 -0700 | [diff] [blame] | 141 | mContainer.reparent(stack, position, moveParents); |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 142 | mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded(); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | public void setResizeable(int resizeMode) { |
| 147 | synchronized (mWindowMap) { |
| 148 | if (mContainer != null) { |
| 149 | mContainer.setResizeable(resizeMode); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame^] | 154 | public void resize(boolean relayout, boolean forced) { |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 155 | synchronized (mWindowMap) { |
| 156 | if (mContainer == null) { |
| 157 | throw new IllegalArgumentException("resizeTask: taskId " + mTaskId + " not found."); |
| 158 | } |
| 159 | |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame^] | 160 | if (mContainer.setBounds(mContainer.getOverrideBounds(), forced) != BOUNDS_CHANGE_NONE |
| 161 | && relayout) { |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 162 | mContainer.getDisplayContent().layoutAndAssignWindowLayersIfNeeded(); |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 167 | 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 Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 212 | 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 Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 222 | void reportSnapshotChanged(TaskSnapshot snapshot) { |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 223 | 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 Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Wale Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 230 | @Override |
| 231 | public String toString() { |
| 232 | return "{TaskWindowContainerController taskId=" + mTaskId + "}"; |
| 233 | } |
Wale Ogunwale | 1666e31 | 2016-12-16 11:27:18 -0800 | [diff] [blame] | 234 | |
| 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 Ogunwale | e1fe7fa2 | 2016-12-15 18:27:00 -0800 | [diff] [blame] | 265 | } |