Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [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 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 17 | package com.android.server.wm; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 18 | |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 19 | import android.app.ActivityManager; |
| 20 | import android.app.ActivityManager.RunningTaskInfo; |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 21 | import android.app.ActivityManager.TaskSnapshot; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 22 | import android.app.ITaskStackListener; |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 23 | import android.app.TaskInfo; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 24 | import android.content.ComponentName; |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 25 | import android.os.Binder; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 26 | import android.os.Handler; |
Riddle Hsu | 7b766fd | 2019-01-28 21:14:59 +0800 | [diff] [blame] | 27 | import android.os.IBinder; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 28 | import android.os.Looper; |
| 29 | import android.os.Message; |
| 30 | import android.os.RemoteCallbackList; |
| 31 | import android.os.RemoteException; |
| 32 | |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 33 | import java.util.ArrayList; |
| 34 | |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 35 | class TaskChangeNotificationController { |
Wale Ogunwale | f1b2ec9 | 2017-06-05 07:09:35 -0700 | [diff] [blame] | 36 | private static final int LOG_STACK_STATE_MSG = 1; |
| 37 | private static final int NOTIFY_TASK_STACK_CHANGE_LISTENERS_MSG = 2; |
| 38 | private static final int NOTIFY_ACTIVITY_PINNED_LISTENERS_MSG = 3; |
| 39 | private static final int NOTIFY_PINNED_ACTIVITY_RESTART_ATTEMPT_LISTENERS_MSG = 4; |
| 40 | private static final int NOTIFY_PINNED_STACK_ANIMATION_ENDED_LISTENERS_MSG = 5; |
| 41 | private static final int NOTIFY_FORCED_RESIZABLE_MSG = 6; |
| 42 | private static final int NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG = 7; |
| 43 | private static final int NOTIFY_TASK_ADDED_LISTENERS_MSG = 8; |
| 44 | private static final int NOTIFY_TASK_REMOVED_LISTENERS_MSG = 9; |
| 45 | private static final int NOTIFY_TASK_MOVED_TO_FRONT_LISTENERS_MSG = 10; |
| 46 | private static final int NOTIFY_TASK_DESCRIPTION_CHANGED_LISTENERS_MSG = 11; |
| 47 | private static final int NOTIFY_ACTIVITY_REQUESTED_ORIENTATION_CHANGED_LISTENERS = 12; |
| 48 | private static final int NOTIFY_TASK_REMOVAL_STARTED_LISTENERS = 13; |
| 49 | private static final int NOTIFY_TASK_PROFILE_LOCKED_LISTENERS_MSG = 14; |
| 50 | private static final int NOTIFY_TASK_SNAPSHOT_CHANGED_LISTENERS_MSG = 15; |
| 51 | private static final int NOTIFY_PINNED_STACK_ANIMATION_STARTED_LISTENERS_MSG = 16; |
| 52 | private static final int NOTIFY_ACTIVITY_UNPINNED_LISTENERS_MSG = 17; |
| 53 | private static final int NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_FAILED_MSG = 18; |
Jeff Chang | bf29986 | 2019-02-26 20:07:22 +0800 | [diff] [blame] | 54 | private static final int NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_REROUTED_MSG = 19; |
Riddle Hsu | 7b766fd | 2019-01-28 21:14:59 +0800 | [diff] [blame] | 55 | private static final int NOTIFY_SIZE_COMPAT_MODE_ACTIVITY_CHANGED_MSG = 20; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 56 | |
| 57 | // Delay in notifying task stack change listeners (in millis) |
Wale Ogunwale | f1b2ec9 | 2017-06-05 07:09:35 -0700 | [diff] [blame] | 58 | private static final int NOTIFY_TASK_STACK_CHANGE_LISTENERS_DELAY = 100; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 59 | |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 60 | // Global lock used by the service the instantiate objects of this class. |
| 61 | private final Object mServiceLock; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 62 | private final ActivityStackSupervisor mStackSupervisor; |
| 63 | private final Handler mHandler; |
| 64 | |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 65 | // Task stack change listeners in a remote process. |
| 66 | private final RemoteCallbackList<ITaskStackListener> mRemoteTaskStackListeners = |
| 67 | new RemoteCallbackList<>(); |
| 68 | |
| 69 | /* |
| 70 | * Task stack change listeners in a local process. Tracked separately so that they can be |
| 71 | * called on the same thread. |
| 72 | */ |
| 73 | private final ArrayList<ITaskStackListener> mLocalTaskStackListeners = new ArrayList<>(); |
| 74 | |
| 75 | private final TaskStackConsumer mNotifyTaskStackChanged = (l, m) -> { |
| 76 | l.onTaskStackChanged(); |
| 77 | }; |
| 78 | |
| 79 | private final TaskStackConsumer mNotifyTaskCreated = (l, m) -> { |
| 80 | l.onTaskCreated(m.arg1, (ComponentName) m.obj); |
| 81 | }; |
| 82 | |
| 83 | private final TaskStackConsumer mNotifyTaskRemoved = (l, m) -> { |
| 84 | l.onTaskRemoved(m.arg1); |
| 85 | }; |
| 86 | |
| 87 | private final TaskStackConsumer mNotifyTaskMovedToFront = (l, m) -> { |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 88 | l.onTaskMovedToFront((RunningTaskInfo) m.obj); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | private final TaskStackConsumer mNotifyTaskDescriptionChanged = (l, m) -> { |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 92 | l.onTaskDescriptionChanged((RunningTaskInfo) m.obj); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | private final TaskStackConsumer mNotifyActivityRequestedOrientationChanged = (l, m) -> { |
| 96 | l.onActivityRequestedOrientationChanged(m.arg1, m.arg2); |
| 97 | }; |
| 98 | |
| 99 | private final TaskStackConsumer mNotifyTaskRemovalStarted = (l, m) -> { |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 100 | l.onTaskRemovalStarted((RunningTaskInfo) m.obj); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | private final TaskStackConsumer mNotifyActivityPinned = (l, m) -> { |
Wale Ogunwale | b7cf063 | 2017-11-07 13:20:53 -0800 | [diff] [blame] | 104 | l.onActivityPinned((String) m.obj /* packageName */, m.sendingUid /* userId */, |
| 105 | m.arg1 /* taskId */, m.arg2 /* stackId */); |
Winson Chung | c81c0ce | 2017-03-17 12:27:30 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | private final TaskStackConsumer mNotifyActivityUnpinned = (l, m) -> { |
| 109 | l.onActivityUnpinned(); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | private final TaskStackConsumer mNotifyPinnedActivityRestartAttempt = (l, m) -> { |
Winson Chung | e6385a2 | 2017-05-02 18:15:16 -0700 | [diff] [blame] | 113 | l.onPinnedActivityRestartAttempt(m.arg1 != 0); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 116 | private final TaskStackConsumer mNotifyPinnedStackAnimationStarted = (l, m) -> { |
| 117 | l.onPinnedStackAnimationStarted(); |
| 118 | }; |
| 119 | |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 120 | private final TaskStackConsumer mNotifyPinnedStackAnimationEnded = (l, m) -> { |
| 121 | l.onPinnedStackAnimationEnded(); |
| 122 | }; |
| 123 | |
| 124 | private final TaskStackConsumer mNotifyActivityForcedResizable = (l, m) -> { |
Andrii Kulian | 036e3ad | 2017-04-19 10:55:10 -0700 | [diff] [blame] | 125 | l.onActivityForcedResizable((String) m.obj, m.arg1, m.arg2); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | private final TaskStackConsumer mNotifyActivityDismissingDockedStack = (l, m) -> { |
| 129 | l.onActivityDismissingDockedStack(); |
| 130 | }; |
| 131 | |
Andrii Kulian | 036e3ad | 2017-04-19 10:55:10 -0700 | [diff] [blame] | 132 | private final TaskStackConsumer mNotifyActivityLaunchOnSecondaryDisplayFailed = (l, m) -> { |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 133 | l.onActivityLaunchOnSecondaryDisplayFailed((RunningTaskInfo) m.obj, m.arg1); |
Andrii Kulian | 036e3ad | 2017-04-19 10:55:10 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Jeff Chang | bf29986 | 2019-02-26 20:07:22 +0800 | [diff] [blame] | 136 | private final TaskStackConsumer mNotifyActivityLaunchOnSecondaryDisplayRerouted = (l, m) -> { |
| 137 | l.onActivityLaunchOnSecondaryDisplayRerouted((RunningTaskInfo) m.obj, m.arg1); |
| 138 | }; |
| 139 | |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 140 | private final TaskStackConsumer mNotifyTaskProfileLocked = (l, m) -> { |
| 141 | l.onTaskProfileLocked(m.arg1, m.arg2); |
| 142 | }; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 143 | |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 144 | private final TaskStackConsumer mNotifyTaskSnapshotChanged = (l, m) -> { |
| 145 | l.onTaskSnapshotChanged(m.arg1, (TaskSnapshot) m.obj); |
| 146 | }; |
| 147 | |
Riddle Hsu | 7b766fd | 2019-01-28 21:14:59 +0800 | [diff] [blame] | 148 | private final TaskStackConsumer mOnSizeCompatModeActivityChanged = (l, m) -> { |
| 149 | l.onSizeCompatModeActivityChanged(m.arg1, (IBinder) m.obj); |
| 150 | }; |
| 151 | |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 152 | @FunctionalInterface |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 153 | public interface TaskStackConsumer { |
| 154 | void accept(ITaskStackListener t, Message m) throws RemoteException; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private class MainHandler extends Handler { |
| 158 | public MainHandler(Looper looper) { |
| 159 | super(looper); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void handleMessage(Message msg) { |
| 164 | switch (msg.what) { |
| 165 | case LOG_STACK_STATE_MSG: { |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 166 | synchronized (mServiceLock) { |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 167 | mStackSupervisor.logStackState(); |
| 168 | } |
| 169 | break; |
| 170 | } |
| 171 | case NOTIFY_TASK_STACK_CHANGE_LISTENERS_MSG: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 172 | forAllRemoteListeners(mNotifyTaskStackChanged, msg); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 173 | break; |
| 174 | case NOTIFY_TASK_ADDED_LISTENERS_MSG: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 175 | forAllRemoteListeners(mNotifyTaskCreated, msg); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 176 | break; |
| 177 | case NOTIFY_TASK_REMOVED_LISTENERS_MSG: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 178 | forAllRemoteListeners(mNotifyTaskRemoved, msg); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 179 | break; |
| 180 | case NOTIFY_TASK_MOVED_TO_FRONT_LISTENERS_MSG: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 181 | forAllRemoteListeners(mNotifyTaskMovedToFront, msg); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 182 | break; |
| 183 | case NOTIFY_TASK_DESCRIPTION_CHANGED_LISTENERS_MSG: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 184 | forAllRemoteListeners(mNotifyTaskDescriptionChanged, msg); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 185 | break; |
| 186 | case NOTIFY_ACTIVITY_REQUESTED_ORIENTATION_CHANGED_LISTENERS: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 187 | forAllRemoteListeners(mNotifyActivityRequestedOrientationChanged, msg); |
Yorke Lee | 64fd1ce | 2017-01-05 17:15:20 -0800 | [diff] [blame] | 188 | break; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 189 | case NOTIFY_TASK_REMOVAL_STARTED_LISTENERS: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 190 | forAllRemoteListeners(mNotifyTaskRemovalStarted, msg); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 191 | break; |
| 192 | case NOTIFY_ACTIVITY_PINNED_LISTENERS_MSG: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 193 | forAllRemoteListeners(mNotifyActivityPinned, msg); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 194 | break; |
Winson Chung | c81c0ce | 2017-03-17 12:27:30 -0700 | [diff] [blame] | 195 | case NOTIFY_ACTIVITY_UNPINNED_LISTENERS_MSG: |
| 196 | forAllRemoteListeners(mNotifyActivityUnpinned, msg); |
| 197 | break; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 198 | case NOTIFY_PINNED_ACTIVITY_RESTART_ATTEMPT_LISTENERS_MSG: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 199 | forAllRemoteListeners(mNotifyPinnedActivityRestartAttempt, msg); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 200 | break; |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 201 | case NOTIFY_PINNED_STACK_ANIMATION_STARTED_LISTENERS_MSG: |
| 202 | forAllRemoteListeners(mNotifyPinnedStackAnimationStarted, msg); |
| 203 | break; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 204 | case NOTIFY_PINNED_STACK_ANIMATION_ENDED_LISTENERS_MSG: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 205 | forAllRemoteListeners(mNotifyPinnedStackAnimationEnded, msg); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 206 | break; |
| 207 | case NOTIFY_FORCED_RESIZABLE_MSG: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 208 | forAllRemoteListeners(mNotifyActivityForcedResizable, msg); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 209 | break; |
| 210 | case NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 211 | forAllRemoteListeners(mNotifyActivityDismissingDockedStack, msg); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 212 | break; |
Andrii Kulian | 036e3ad | 2017-04-19 10:55:10 -0700 | [diff] [blame] | 213 | case NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_FAILED_MSG: |
| 214 | forAllRemoteListeners(mNotifyActivityLaunchOnSecondaryDisplayFailed, msg); |
| 215 | break; |
Jeff Chang | bf29986 | 2019-02-26 20:07:22 +0800 | [diff] [blame] | 216 | case NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_REROUTED_MSG: |
| 217 | forAllRemoteListeners(mNotifyActivityLaunchOnSecondaryDisplayRerouted, msg); |
| 218 | break; |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 219 | case NOTIFY_TASK_PROFILE_LOCKED_LISTENERS_MSG: |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 220 | forAllRemoteListeners(mNotifyTaskProfileLocked, msg); |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 221 | break; |
| 222 | case NOTIFY_TASK_SNAPSHOT_CHANGED_LISTENERS_MSG: |
| 223 | forAllRemoteListeners(mNotifyTaskSnapshotChanged, msg); |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 224 | break; |
Riddle Hsu | 7b766fd | 2019-01-28 21:14:59 +0800 | [diff] [blame] | 225 | case NOTIFY_SIZE_COMPAT_MODE_ACTIVITY_CHANGED_MSG: |
| 226 | forAllRemoteListeners(mOnSizeCompatModeActivityChanged, msg); |
| 227 | break; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 232 | public TaskChangeNotificationController(Object serviceLock, |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 233 | ActivityStackSupervisor stackSupervisor, Handler handler) { |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 234 | mServiceLock = serviceLock; |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 235 | mStackSupervisor = stackSupervisor; |
| 236 | mHandler = new MainHandler(handler.getLooper()); |
| 237 | } |
| 238 | |
| 239 | public void registerTaskStackListener(ITaskStackListener listener) { |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 240 | synchronized (mServiceLock) { |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 241 | if (listener != null) { |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 242 | if (Binder.getCallingPid() == android.os.Process.myPid()) { |
| 243 | if (!mLocalTaskStackListeners.contains(listener)) { |
| 244 | mLocalTaskStackListeners.add(listener); |
| 245 | } |
| 246 | } else { |
| 247 | mRemoteTaskStackListeners.register(listener); |
| 248 | } |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | public void unregisterTaskStackListener(ITaskStackListener listener) { |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 254 | synchronized (mServiceLock) { |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 255 | if (listener != null) { |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 256 | if (Binder.getCallingPid() == android.os.Process.myPid()) { |
| 257 | mLocalTaskStackListeners.remove(listener); |
| 258 | } else { |
| 259 | mRemoteTaskStackListeners.unregister(listener); |
| 260 | } |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
Wale Ogunwale | f1b2ec9 | 2017-06-05 07:09:35 -0700 | [diff] [blame] | 265 | private void forAllRemoteListeners(TaskStackConsumer callback, Message message) { |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 266 | synchronized (mServiceLock) { |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 267 | for (int i = mRemoteTaskStackListeners.beginBroadcast() - 1; i >= 0; i--) { |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 268 | try { |
| 269 | // Make a one-way callback to the listener |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 270 | callback.accept(mRemoteTaskStackListeners.getBroadcastItem(i), message); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 271 | } catch (RemoteException e) { |
| 272 | // Handled by the RemoteCallbackList. |
| 273 | } |
| 274 | } |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 275 | mRemoteTaskStackListeners.finishBroadcast(); |
| 276 | } |
| 277 | } |
| 278 | |
Wale Ogunwale | f1b2ec9 | 2017-06-05 07:09:35 -0700 | [diff] [blame] | 279 | private void forAllLocalListeners(TaskStackConsumer callback, Message message) { |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 280 | synchronized (mServiceLock) { |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 281 | for (int i = mLocalTaskStackListeners.size() - 1; i >= 0; i--) { |
| 282 | try { |
| 283 | callback.accept(mLocalTaskStackListeners.get(i), message); |
| 284 | } catch (RemoteException e) { |
| 285 | // Never thrown since this is called locally. |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 290 | |
| 291 | /** Notifies all listeners when the task stack has changed. */ |
| 292 | void notifyTaskStackChanged() { |
| 293 | mHandler.sendEmptyMessage(LOG_STACK_STATE_MSG); |
| 294 | mHandler.removeMessages(NOTIFY_TASK_STACK_CHANGE_LISTENERS_MSG); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 295 | final Message msg = mHandler.obtainMessage(NOTIFY_TASK_STACK_CHANGE_LISTENERS_MSG); |
| 296 | forAllLocalListeners(mNotifyTaskStackChanged, msg); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 297 | // Only the main task stack change notification requires a delay. |
| 298 | mHandler.sendMessageDelayed(msg, NOTIFY_TASK_STACK_CHANGE_LISTENERS_DELAY); |
| 299 | } |
| 300 | |
| 301 | /** Notifies all listeners when an Activity is pinned. */ |
Wale Ogunwale | 89be576 | 2017-10-04 13:27:49 -0700 | [diff] [blame] | 302 | void notifyActivityPinned(ActivityRecord r) { |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 303 | mHandler.removeMessages(NOTIFY_ACTIVITY_PINNED_LISTENERS_MSG); |
Wale Ogunwale | b7cf063 | 2017-11-07 13:20:53 -0800 | [diff] [blame] | 304 | final Message msg = mHandler.obtainMessage(NOTIFY_ACTIVITY_PINNED_LISTENERS_MSG, |
Wale Ogunwale | 8b19de9 | 2018-11-29 19:58:26 -0800 | [diff] [blame] | 305 | r.getTaskRecord().taskId, r.getStackId(), r.packageName); |
| 306 | msg.sendingUid = r.mUserId; |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 307 | forAllLocalListeners(mNotifyActivityPinned, msg); |
| 308 | msg.sendToTarget(); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Winson Chung | c81c0ce | 2017-03-17 12:27:30 -0700 | [diff] [blame] | 311 | /** Notifies all listeners when an Activity is unpinned. */ |
| 312 | void notifyActivityUnpinned() { |
| 313 | mHandler.removeMessages(NOTIFY_ACTIVITY_UNPINNED_LISTENERS_MSG); |
| 314 | final Message msg = mHandler.obtainMessage(NOTIFY_ACTIVITY_UNPINNED_LISTENERS_MSG); |
| 315 | forAllLocalListeners(mNotifyActivityUnpinned, msg); |
| 316 | msg.sendToTarget(); |
| 317 | } |
| 318 | |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 319 | /** |
| 320 | * Notifies all listeners when an attempt was made to start an an activity that is already |
| 321 | * running in the pinned stack and the activity was not actually started, but the task is |
| 322 | * either brought to the front or a new Intent is delivered to it. |
| 323 | */ |
Winson Chung | e6385a2 | 2017-05-02 18:15:16 -0700 | [diff] [blame] | 324 | void notifyPinnedActivityRestartAttempt(boolean clearedTask) { |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 325 | mHandler.removeMessages(NOTIFY_PINNED_ACTIVITY_RESTART_ATTEMPT_LISTENERS_MSG); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 326 | final Message msg = |
Winson Chung | e6385a2 | 2017-05-02 18:15:16 -0700 | [diff] [blame] | 327 | mHandler.obtainMessage(NOTIFY_PINNED_ACTIVITY_RESTART_ATTEMPT_LISTENERS_MSG, |
| 328 | clearedTask ? 1 : 0, 0); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 329 | forAllLocalListeners(mNotifyPinnedActivityRestartAttempt, msg); |
| 330 | msg.sendToTarget(); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 333 | /** Notifies all listeners when the pinned stack animation starts. */ |
| 334 | void notifyPinnedStackAnimationStarted() { |
| 335 | mHandler.removeMessages(NOTIFY_PINNED_STACK_ANIMATION_STARTED_LISTENERS_MSG); |
| 336 | final Message msg = |
| 337 | mHandler.obtainMessage(NOTIFY_PINNED_STACK_ANIMATION_STARTED_LISTENERS_MSG); |
| 338 | forAllLocalListeners(mNotifyPinnedStackAnimationStarted, msg); |
| 339 | msg.sendToTarget(); |
| 340 | } |
| 341 | |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 342 | /** Notifies all listeners when the pinned stack animation ends. */ |
| 343 | void notifyPinnedStackAnimationEnded() { |
| 344 | mHandler.removeMessages(NOTIFY_PINNED_STACK_ANIMATION_ENDED_LISTENERS_MSG); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 345 | final Message msg = |
| 346 | mHandler.obtainMessage(NOTIFY_PINNED_STACK_ANIMATION_ENDED_LISTENERS_MSG); |
| 347 | forAllLocalListeners(mNotifyPinnedStackAnimationEnded, msg); |
| 348 | msg.sendToTarget(); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void notifyActivityDismissingDockedStack() { |
| 352 | mHandler.removeMessages(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG); |
Wale Ogunwale | f1b2ec9 | 2017-06-05 07:09:35 -0700 | [diff] [blame] | 353 | final Message msg = mHandler.obtainMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG); |
| 354 | forAllLocalListeners(mNotifyActivityDismissingDockedStack, msg); |
| 355 | msg.sendToTarget(); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Andrii Kulian | 036e3ad | 2017-04-19 10:55:10 -0700 | [diff] [blame] | 358 | void notifyActivityForcedResizable(int taskId, int reason, String packageName) { |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 359 | mHandler.removeMessages(NOTIFY_FORCED_RESIZABLE_MSG); |
Andrii Kulian | 036e3ad | 2017-04-19 10:55:10 -0700 | [diff] [blame] | 360 | final Message msg = mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, taskId, reason, |
| 361 | packageName); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 362 | forAllLocalListeners(mNotifyActivityForcedResizable, msg); |
| 363 | msg.sendToTarget(); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 366 | void notifyActivityLaunchOnSecondaryDisplayFailed(TaskInfo ti, int requestedDisplayId) { |
Andrii Kulian | 036e3ad | 2017-04-19 10:55:10 -0700 | [diff] [blame] | 367 | mHandler.removeMessages(NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_FAILED_MSG); |
| 368 | final Message msg = mHandler.obtainMessage( |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 369 | NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_FAILED_MSG, requestedDisplayId, |
| 370 | 0 /* unused */, ti); |
Andrii Kulian | 036e3ad | 2017-04-19 10:55:10 -0700 | [diff] [blame] | 371 | forAllLocalListeners(mNotifyActivityLaunchOnSecondaryDisplayFailed, msg); |
| 372 | msg.sendToTarget(); |
| 373 | } |
| 374 | |
Jeff Chang | bf29986 | 2019-02-26 20:07:22 +0800 | [diff] [blame] | 375 | void notifyActivityLaunchOnSecondaryDisplayRerouted(TaskInfo ti, int requestedDisplayId) { |
| 376 | mHandler.removeMessages(NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_REROUTED_MSG); |
| 377 | final Message msg = mHandler.obtainMessage( |
| 378 | NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_REROUTED_MSG, requestedDisplayId, |
| 379 | 0 /* unused */, ti); |
| 380 | forAllLocalListeners(mNotifyActivityLaunchOnSecondaryDisplayRerouted, msg); |
| 381 | msg.sendToTarget(); |
| 382 | } |
| 383 | |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 384 | void notifyTaskCreated(int taskId, ComponentName componentName) { |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 385 | final Message msg = mHandler.obtainMessage(NOTIFY_TASK_ADDED_LISTENERS_MSG, |
| 386 | taskId, 0 /* unused */, componentName); |
| 387 | forAllLocalListeners(mNotifyTaskCreated, msg); |
| 388 | msg.sendToTarget(); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | void notifyTaskRemoved(int taskId) { |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 392 | final Message msg = mHandler.obtainMessage(NOTIFY_TASK_REMOVED_LISTENERS_MSG, |
| 393 | taskId, 0 /* unused */); |
| 394 | forAllLocalListeners(mNotifyTaskRemoved, msg); |
| 395 | msg.sendToTarget(); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 398 | void notifyTaskMovedToFront(TaskInfo ti) { |
| 399 | final Message msg = mHandler.obtainMessage(NOTIFY_TASK_MOVED_TO_FRONT_LISTENERS_MSG, ti); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 400 | forAllLocalListeners(mNotifyTaskMovedToFront, msg); |
| 401 | msg.sendToTarget(); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 404 | void notifyTaskDescriptionChanged(TaskInfo taskInfo) { |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 405 | final Message msg = mHandler.obtainMessage(NOTIFY_TASK_DESCRIPTION_CHANGED_LISTENERS_MSG, |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 406 | taskInfo); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 407 | forAllLocalListeners(mNotifyTaskDescriptionChanged, msg); |
| 408 | msg.sendToTarget(); |
| 409 | |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void notifyActivityRequestedOrientationChanged(int taskId, int orientation) { |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 413 | final Message msg = mHandler.obtainMessage( |
| 414 | NOTIFY_ACTIVITY_REQUESTED_ORIENTATION_CHANGED_LISTENERS, taskId, orientation); |
| 415 | forAllLocalListeners(mNotifyActivityRequestedOrientationChanged, msg); |
| 416 | msg.sendToTarget(); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Notify listeners that the task is about to be finished before its surfaces are removed from |
| 421 | * the window manager. This allows interested parties to perform relevant animations before |
| 422 | * the window disappears. |
| 423 | */ |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 424 | void notifyTaskRemovalStarted(ActivityManager.RunningTaskInfo taskInfo) { |
| 425 | final Message msg = mHandler.obtainMessage(NOTIFY_TASK_REMOVAL_STARTED_LISTENERS, taskInfo); |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 426 | forAllLocalListeners(mNotifyTaskRemovalStarted, msg); |
| 427 | msg.sendToTarget(); |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 428 | } |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 429 | |
| 430 | /** |
| 431 | * Notify listeners that the task has been put in a locked state because one or more of the |
| 432 | * activities inside it belong to a managed profile user that has been locked. |
| 433 | */ |
| 434 | void notifyTaskProfileLocked(int taskId, int userId) { |
Yorke Lee | 1329407 | 2017-01-06 14:59:58 -0800 | [diff] [blame] | 435 | final Message msg = mHandler.obtainMessage(NOTIFY_TASK_PROFILE_LOCKED_LISTENERS_MSG, taskId, |
| 436 | userId); |
| 437 | forAllLocalListeners(mNotifyTaskProfileLocked, msg); |
| 438 | msg.sendToTarget(); |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 439 | } |
Jorim Jaggi | fb9d78a | 2017-01-05 18:57:12 +0100 | [diff] [blame] | 440 | |
| 441 | /** |
| 442 | * Notify listeners that the snapshot of a task has changed. |
| 443 | */ |
| 444 | void notifyTaskSnapshotChanged(int taskId, TaskSnapshot snapshot) { |
| 445 | final Message msg = mHandler.obtainMessage(NOTIFY_TASK_SNAPSHOT_CHANGED_LISTENERS_MSG, |
| 446 | taskId, 0, snapshot); |
| 447 | forAllLocalListeners(mNotifyTaskSnapshotChanged, msg); |
| 448 | msg.sendToTarget(); |
| 449 | } |
Riddle Hsu | 7b766fd | 2019-01-28 21:14:59 +0800 | [diff] [blame] | 450 | |
| 451 | /** |
| 452 | * Notify listeners that whether a size compatibility mode activity is using the override |
| 453 | * bounds which is not fit its parent. |
| 454 | */ |
| 455 | void notifySizeCompatModeActivityChanged(int displayId, IBinder activityToken) { |
| 456 | final Message msg = mHandler.obtainMessage(NOTIFY_SIZE_COMPAT_MODE_ACTIVITY_CHANGED_MSG, |
| 457 | displayId, 0 /* unused */, activityToken); |
| 458 | forAllLocalListeners(mOnSizeCompatModeActivityChanged, msg); |
| 459 | msg.sendToTarget(); |
| 460 | } |
Yorke Lee | bd54c2a | 2016-10-25 13:49:23 -0700 | [diff] [blame] | 461 | } |