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