blob: a61c908e0f6fbb420fb0cbbbd870d17ae716a95e [file] [log] [blame]
Yorke Leebd54c2a2016-10-25 13:49:23 -07001/*
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 Ogunwale59507092018-10-29 09:00:30 -070017package com.android.server.wm;
Yorke Leebd54c2a2016-10-25 13:49:23 -070018
Mark Renoufc808f062019-02-07 15:20:37 -050019import android.app.ActivityManager;
20import android.app.ActivityManager.RunningTaskInfo;
Jorim Jaggifb9d78a2017-01-05 18:57:12 +010021import android.app.ActivityManager.TaskSnapshot;
Yorke Leebd54c2a2016-10-25 13:49:23 -070022import android.app.ITaskStackListener;
Mark Renoufc808f062019-02-07 15:20:37 -050023import android.app.TaskInfo;
Yorke Leebd54c2a2016-10-25 13:49:23 -070024import android.content.ComponentName;
Yorke Lee13294072017-01-06 14:59:58 -080025import android.os.Binder;
Yorke Leebd54c2a2016-10-25 13:49:23 -070026import android.os.Handler;
Riddle Hsu7b766fd2019-01-28 21:14:59 +080027import android.os.IBinder;
Yorke Leebd54c2a2016-10-25 13:49:23 -070028import android.os.Looper;
29import android.os.Message;
30import android.os.RemoteCallbackList;
31import android.os.RemoteException;
32
Yorke Lee13294072017-01-06 14:59:58 -080033import java.util.ArrayList;
34
Yorke Leebd54c2a2016-10-25 13:49:23 -070035class TaskChangeNotificationController {
Wale Ogunwalef1b2ec92017-06-05 07:09:35 -070036 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 Changbf299862019-02-26 20:07:22 +080054 private static final int NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_REROUTED_MSG = 19;
Riddle Hsu7b766fd2019-01-28 21:14:59 +080055 private static final int NOTIFY_SIZE_COMPAT_MODE_ACTIVITY_CHANGED_MSG = 20;
Mark Renouf446251d2019-04-26 10:22:41 -040056 private static final int NOTIFY_BACK_PRESSED_ON_TASK_ROOT = 21;
Issei Suzukicac2a502019-04-16 16:52:50 +020057 private static final int NOTIFY_SINGLE_TASK_DISPLAY_DRAWN = 22;
davidln1ceedb52019-05-30 14:25:01 -070058 private static final int NOTIFY_TASK_DISPLAY_CHANGED_LISTENERS_MSG = 23;
Vinit Nayak3e737492019-07-24 13:03:15 -070059 private static final int NOTIFY_TASK_LIST_UPDATED_LISTENERS_MSG = 24;
Mady Mellor06b770c2019-08-29 18:01:00 -070060 private static final int NOTIFY_SINGLE_TASK_DISPLAY_EMPTY = 25;
Vinit Nayak8e547962019-10-07 10:36:41 -070061 private static final int NOTIFY_TASK_LIST_FROZEN_UNFROZEN_MSG = 26;
Yorke Leebd54c2a2016-10-25 13:49:23 -070062
63 // Delay in notifying task stack change listeners (in millis)
Wale Ogunwalef1b2ec92017-06-05 07:09:35 -070064 private static final int NOTIFY_TASK_STACK_CHANGE_LISTENERS_DELAY = 100;
Yorke Leebd54c2a2016-10-25 13:49:23 -070065
Wale Ogunwalec9e57de2018-05-08 14:28:07 -070066 // Global lock used by the service the instantiate objects of this class.
67 private final Object mServiceLock;
Yorke Leebd54c2a2016-10-25 13:49:23 -070068 private final ActivityStackSupervisor mStackSupervisor;
69 private final Handler mHandler;
70
Yorke Lee13294072017-01-06 14:59:58 -080071 // Task stack change listeners in a remote process.
72 private final RemoteCallbackList<ITaskStackListener> mRemoteTaskStackListeners =
73 new RemoteCallbackList<>();
74
75 /*
76 * Task stack change listeners in a local process. Tracked separately so that they can be
77 * called on the same thread.
78 */
79 private final ArrayList<ITaskStackListener> mLocalTaskStackListeners = new ArrayList<>();
80
81 private final TaskStackConsumer mNotifyTaskStackChanged = (l, m) -> {
82 l.onTaskStackChanged();
83 };
84
85 private final TaskStackConsumer mNotifyTaskCreated = (l, m) -> {
86 l.onTaskCreated(m.arg1, (ComponentName) m.obj);
87 };
88
89 private final TaskStackConsumer mNotifyTaskRemoved = (l, m) -> {
90 l.onTaskRemoved(m.arg1);
91 };
92
93 private final TaskStackConsumer mNotifyTaskMovedToFront = (l, m) -> {
Mark Renoufc808f062019-02-07 15:20:37 -050094 l.onTaskMovedToFront((RunningTaskInfo) m.obj);
Yorke Lee13294072017-01-06 14:59:58 -080095 };
96
97 private final TaskStackConsumer mNotifyTaskDescriptionChanged = (l, m) -> {
Mark Renoufc808f062019-02-07 15:20:37 -050098 l.onTaskDescriptionChanged((RunningTaskInfo) m.obj);
Yorke Lee13294072017-01-06 14:59:58 -080099 };
100
Mark Renouf446251d2019-04-26 10:22:41 -0400101 private final TaskStackConsumer mNotifyBackPressedOnTaskRoot = (l, m) -> {
102 l.onBackPressedOnTaskRoot((RunningTaskInfo) m.obj);
103 };
104
Yorke Lee13294072017-01-06 14:59:58 -0800105 private final TaskStackConsumer mNotifyActivityRequestedOrientationChanged = (l, m) -> {
106 l.onActivityRequestedOrientationChanged(m.arg1, m.arg2);
107 };
108
109 private final TaskStackConsumer mNotifyTaskRemovalStarted = (l, m) -> {
Mark Renoufc808f062019-02-07 15:20:37 -0500110 l.onTaskRemovalStarted((RunningTaskInfo) m.obj);
Yorke Lee13294072017-01-06 14:59:58 -0800111 };
112
113 private final TaskStackConsumer mNotifyActivityPinned = (l, m) -> {
Wale Ogunwaleb7cf0632017-11-07 13:20:53 -0800114 l.onActivityPinned((String) m.obj /* packageName */, m.sendingUid /* userId */,
115 m.arg1 /* taskId */, m.arg2 /* stackId */);
Winson Chungc81c0ce2017-03-17 12:27:30 -0700116 };
117
118 private final TaskStackConsumer mNotifyActivityUnpinned = (l, m) -> {
119 l.onActivityUnpinned();
Yorke Lee13294072017-01-06 14:59:58 -0800120 };
121
122 private final TaskStackConsumer mNotifyPinnedActivityRestartAttempt = (l, m) -> {
Winson Chunge6385a22017-05-02 18:15:16 -0700123 l.onPinnedActivityRestartAttempt(m.arg1 != 0);
Yorke Lee13294072017-01-06 14:59:58 -0800124 };
125
Winson Chung85d39982017-02-24 15:21:25 -0800126 private final TaskStackConsumer mNotifyPinnedStackAnimationStarted = (l, m) -> {
127 l.onPinnedStackAnimationStarted();
128 };
129
Yorke Lee13294072017-01-06 14:59:58 -0800130 private final TaskStackConsumer mNotifyPinnedStackAnimationEnded = (l, m) -> {
131 l.onPinnedStackAnimationEnded();
132 };
133
134 private final TaskStackConsumer mNotifyActivityForcedResizable = (l, m) -> {
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700135 l.onActivityForcedResizable((String) m.obj, m.arg1, m.arg2);
Yorke Lee13294072017-01-06 14:59:58 -0800136 };
137
138 private final TaskStackConsumer mNotifyActivityDismissingDockedStack = (l, m) -> {
139 l.onActivityDismissingDockedStack();
140 };
141
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700142 private final TaskStackConsumer mNotifyActivityLaunchOnSecondaryDisplayFailed = (l, m) -> {
Mark Renoufc808f062019-02-07 15:20:37 -0500143 l.onActivityLaunchOnSecondaryDisplayFailed((RunningTaskInfo) m.obj, m.arg1);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700144 };
145
Jeff Changbf299862019-02-26 20:07:22 +0800146 private final TaskStackConsumer mNotifyActivityLaunchOnSecondaryDisplayRerouted = (l, m) -> {
147 l.onActivityLaunchOnSecondaryDisplayRerouted((RunningTaskInfo) m.obj, m.arg1);
148 };
149
Yorke Lee13294072017-01-06 14:59:58 -0800150 private final TaskStackConsumer mNotifyTaskProfileLocked = (l, m) -> {
151 l.onTaskProfileLocked(m.arg1, m.arg2);
152 };
Yorke Leebd54c2a2016-10-25 13:49:23 -0700153
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100154 private final TaskStackConsumer mNotifyTaskSnapshotChanged = (l, m) -> {
155 l.onTaskSnapshotChanged(m.arg1, (TaskSnapshot) m.obj);
156 };
157
Riddle Hsu7b766fd2019-01-28 21:14:59 +0800158 private final TaskStackConsumer mOnSizeCompatModeActivityChanged = (l, m) -> {
159 l.onSizeCompatModeActivityChanged(m.arg1, (IBinder) m.obj);
160 };
161
Issei Suzukicac2a502019-04-16 16:52:50 +0200162 private final TaskStackConsumer mNotifySingleTaskDisplayDrawn = (l, m) -> {
163 l.onSingleTaskDisplayDrawn(m.arg1);
164 };
165
Issei Suzuki734bc942019-06-05 13:59:52 +0200166 private final TaskStackConsumer mNotifySingleTaskDisplayEmpty = (l, m) -> {
167 l.onSingleTaskDisplayEmpty(m.arg1);
168 };
169
davidln1ceedb52019-05-30 14:25:01 -0700170 private final TaskStackConsumer mNotifyTaskDisplayChanged = (l, m) -> {
171 l.onTaskDisplayChanged(m.arg1, m.arg2);
172 };
173
Vinit Nayak3e737492019-07-24 13:03:15 -0700174 private final TaskStackConsumer mNotifyTaskListUpdated = (l, m) -> {
175 l.onRecentTaskListUpdated();
176 };
177
Vinit Nayak8e547962019-10-07 10:36:41 -0700178 private final TaskStackConsumer mNotifyTaskListFrozen = (l, m) -> {
179 l.onRecentTaskListFrozenChanged(m.arg1 != 0);
180 };
181
Yorke Leebd54c2a2016-10-25 13:49:23 -0700182 @FunctionalInterface
Yorke Lee13294072017-01-06 14:59:58 -0800183 public interface TaskStackConsumer {
184 void accept(ITaskStackListener t, Message m) throws RemoteException;
Yorke Leebd54c2a2016-10-25 13:49:23 -0700185 }
186
187 private class MainHandler extends Handler {
188 public MainHandler(Looper looper) {
189 super(looper);
190 }
191
192 @Override
193 public void handleMessage(Message msg) {
194 switch (msg.what) {
195 case LOG_STACK_STATE_MSG: {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700196 synchronized (mServiceLock) {
Yorke Leebd54c2a2016-10-25 13:49:23 -0700197 mStackSupervisor.logStackState();
198 }
199 break;
200 }
201 case NOTIFY_TASK_STACK_CHANGE_LISTENERS_MSG:
Yorke Lee13294072017-01-06 14:59:58 -0800202 forAllRemoteListeners(mNotifyTaskStackChanged, msg);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700203 break;
204 case NOTIFY_TASK_ADDED_LISTENERS_MSG:
Yorke Lee13294072017-01-06 14:59:58 -0800205 forAllRemoteListeners(mNotifyTaskCreated, msg);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700206 break;
207 case NOTIFY_TASK_REMOVED_LISTENERS_MSG:
Yorke Lee13294072017-01-06 14:59:58 -0800208 forAllRemoteListeners(mNotifyTaskRemoved, msg);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700209 break;
210 case NOTIFY_TASK_MOVED_TO_FRONT_LISTENERS_MSG:
Yorke Lee13294072017-01-06 14:59:58 -0800211 forAllRemoteListeners(mNotifyTaskMovedToFront, msg);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700212 break;
213 case NOTIFY_TASK_DESCRIPTION_CHANGED_LISTENERS_MSG:
Yorke Lee13294072017-01-06 14:59:58 -0800214 forAllRemoteListeners(mNotifyTaskDescriptionChanged, msg);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700215 break;
216 case NOTIFY_ACTIVITY_REQUESTED_ORIENTATION_CHANGED_LISTENERS:
Yorke Lee13294072017-01-06 14:59:58 -0800217 forAllRemoteListeners(mNotifyActivityRequestedOrientationChanged, msg);
Yorke Lee64fd1ce2017-01-05 17:15:20 -0800218 break;
Yorke Leebd54c2a2016-10-25 13:49:23 -0700219 case NOTIFY_TASK_REMOVAL_STARTED_LISTENERS:
Yorke Lee13294072017-01-06 14:59:58 -0800220 forAllRemoteListeners(mNotifyTaskRemovalStarted, msg);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700221 break;
222 case NOTIFY_ACTIVITY_PINNED_LISTENERS_MSG:
Yorke Lee13294072017-01-06 14:59:58 -0800223 forAllRemoteListeners(mNotifyActivityPinned, msg);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700224 break;
Winson Chungc81c0ce2017-03-17 12:27:30 -0700225 case NOTIFY_ACTIVITY_UNPINNED_LISTENERS_MSG:
226 forAllRemoteListeners(mNotifyActivityUnpinned, msg);
227 break;
Yorke Leebd54c2a2016-10-25 13:49:23 -0700228 case NOTIFY_PINNED_ACTIVITY_RESTART_ATTEMPT_LISTENERS_MSG:
Yorke Lee13294072017-01-06 14:59:58 -0800229 forAllRemoteListeners(mNotifyPinnedActivityRestartAttempt, msg);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700230 break;
Winson Chung85d39982017-02-24 15:21:25 -0800231 case NOTIFY_PINNED_STACK_ANIMATION_STARTED_LISTENERS_MSG:
232 forAllRemoteListeners(mNotifyPinnedStackAnimationStarted, msg);
233 break;
Yorke Leebd54c2a2016-10-25 13:49:23 -0700234 case NOTIFY_PINNED_STACK_ANIMATION_ENDED_LISTENERS_MSG:
Yorke Lee13294072017-01-06 14:59:58 -0800235 forAllRemoteListeners(mNotifyPinnedStackAnimationEnded, msg);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700236 break;
237 case NOTIFY_FORCED_RESIZABLE_MSG:
Yorke Lee13294072017-01-06 14:59:58 -0800238 forAllRemoteListeners(mNotifyActivityForcedResizable, msg);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700239 break;
240 case NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG:
Yorke Lee13294072017-01-06 14:59:58 -0800241 forAllRemoteListeners(mNotifyActivityDismissingDockedStack, msg);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700242 break;
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700243 case NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_FAILED_MSG:
244 forAllRemoteListeners(mNotifyActivityLaunchOnSecondaryDisplayFailed, msg);
245 break;
Jeff Changbf299862019-02-26 20:07:22 +0800246 case NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_REROUTED_MSG:
247 forAllRemoteListeners(mNotifyActivityLaunchOnSecondaryDisplayRerouted, msg);
248 break;
Robin Leec41f6ec2017-01-10 17:02:34 +0000249 case NOTIFY_TASK_PROFILE_LOCKED_LISTENERS_MSG:
Yorke Lee13294072017-01-06 14:59:58 -0800250 forAllRemoteListeners(mNotifyTaskProfileLocked, msg);
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100251 break;
252 case NOTIFY_TASK_SNAPSHOT_CHANGED_LISTENERS_MSG:
253 forAllRemoteListeners(mNotifyTaskSnapshotChanged, msg);
Robin Leec41f6ec2017-01-10 17:02:34 +0000254 break;
Riddle Hsu7b766fd2019-01-28 21:14:59 +0800255 case NOTIFY_SIZE_COMPAT_MODE_ACTIVITY_CHANGED_MSG:
256 forAllRemoteListeners(mOnSizeCompatModeActivityChanged, msg);
257 break;
Mark Renouf446251d2019-04-26 10:22:41 -0400258 case NOTIFY_BACK_PRESSED_ON_TASK_ROOT:
259 forAllRemoteListeners(mNotifyBackPressedOnTaskRoot, msg);
260 break;
Issei Suzukicac2a502019-04-16 16:52:50 +0200261 case NOTIFY_SINGLE_TASK_DISPLAY_DRAWN:
262 forAllRemoteListeners(mNotifySingleTaskDisplayDrawn, msg);
263 break;
Issei Suzuki734bc942019-06-05 13:59:52 +0200264 case NOTIFY_SINGLE_TASK_DISPLAY_EMPTY:
265 forAllRemoteListeners(mNotifySingleTaskDisplayEmpty, msg);
266 break;
davidln1ceedb52019-05-30 14:25:01 -0700267 case NOTIFY_TASK_DISPLAY_CHANGED_LISTENERS_MSG:
268 forAllRemoteListeners(mNotifyTaskDisplayChanged, msg);
269 break;
Vinit Nayak3e737492019-07-24 13:03:15 -0700270 case NOTIFY_TASK_LIST_UPDATED_LISTENERS_MSG:
271 forAllRemoteListeners(mNotifyTaskListUpdated, msg);
272 break;
Vinit Nayak8e547962019-10-07 10:36:41 -0700273 case NOTIFY_TASK_LIST_FROZEN_UNFROZEN_MSG:
274 forAllRemoteListeners(mNotifyTaskListFrozen, msg);
275 break;
Yorke Leebd54c2a2016-10-25 13:49:23 -0700276 }
277 }
278 }
279
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700280 public TaskChangeNotificationController(Object serviceLock,
Yorke Leebd54c2a2016-10-25 13:49:23 -0700281 ActivityStackSupervisor stackSupervisor, Handler handler) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700282 mServiceLock = serviceLock;
Yorke Leebd54c2a2016-10-25 13:49:23 -0700283 mStackSupervisor = stackSupervisor;
284 mHandler = new MainHandler(handler.getLooper());
285 }
286
287 public void registerTaskStackListener(ITaskStackListener listener) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700288 synchronized (mServiceLock) {
Yorke Leebd54c2a2016-10-25 13:49:23 -0700289 if (listener != null) {
Yorke Lee13294072017-01-06 14:59:58 -0800290 if (Binder.getCallingPid() == android.os.Process.myPid()) {
291 if (!mLocalTaskStackListeners.contains(listener)) {
292 mLocalTaskStackListeners.add(listener);
293 }
294 } else {
295 mRemoteTaskStackListeners.register(listener);
296 }
Yorke Leebd54c2a2016-10-25 13:49:23 -0700297 }
298 }
299 }
300
301 public void unregisterTaskStackListener(ITaskStackListener listener) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700302 synchronized (mServiceLock) {
Yorke Leebd54c2a2016-10-25 13:49:23 -0700303 if (listener != null) {
Yorke Lee13294072017-01-06 14:59:58 -0800304 if (Binder.getCallingPid() == android.os.Process.myPid()) {
305 mLocalTaskStackListeners.remove(listener);
306 } else {
307 mRemoteTaskStackListeners.unregister(listener);
308 }
Yorke Leebd54c2a2016-10-25 13:49:23 -0700309 }
310 }
311 }
312
Wale Ogunwalef1b2ec92017-06-05 07:09:35 -0700313 private void forAllRemoteListeners(TaskStackConsumer callback, Message message) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700314 synchronized (mServiceLock) {
Yorke Lee13294072017-01-06 14:59:58 -0800315 for (int i = mRemoteTaskStackListeners.beginBroadcast() - 1; i >= 0; i--) {
Yorke Leebd54c2a2016-10-25 13:49:23 -0700316 try {
317 // Make a one-way callback to the listener
Yorke Lee13294072017-01-06 14:59:58 -0800318 callback.accept(mRemoteTaskStackListeners.getBroadcastItem(i), message);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700319 } catch (RemoteException e) {
320 // Handled by the RemoteCallbackList.
321 }
322 }
Yorke Lee13294072017-01-06 14:59:58 -0800323 mRemoteTaskStackListeners.finishBroadcast();
324 }
325 }
326
Wale Ogunwalef1b2ec92017-06-05 07:09:35 -0700327 private void forAllLocalListeners(TaskStackConsumer callback, Message message) {
Wale Ogunwalec9e57de2018-05-08 14:28:07 -0700328 synchronized (mServiceLock) {
Yorke Lee13294072017-01-06 14:59:58 -0800329 for (int i = mLocalTaskStackListeners.size() - 1; i >= 0; i--) {
330 try {
331 callback.accept(mLocalTaskStackListeners.get(i), message);
332 } catch (RemoteException e) {
333 // Never thrown since this is called locally.
334 }
335 }
336 }
337 }
Yorke Leebd54c2a2016-10-25 13:49:23 -0700338
339 /** Notifies all listeners when the task stack has changed. */
340 void notifyTaskStackChanged() {
341 mHandler.sendEmptyMessage(LOG_STACK_STATE_MSG);
342 mHandler.removeMessages(NOTIFY_TASK_STACK_CHANGE_LISTENERS_MSG);
Yorke Lee13294072017-01-06 14:59:58 -0800343 final Message msg = mHandler.obtainMessage(NOTIFY_TASK_STACK_CHANGE_LISTENERS_MSG);
344 forAllLocalListeners(mNotifyTaskStackChanged, msg);
Yorke Leebd54c2a2016-10-25 13:49:23 -0700345 // Only the main task stack change notification requires a delay.
346 mHandler.sendMessageDelayed(msg, NOTIFY_TASK_STACK_CHANGE_LISTENERS_DELAY);
347 }
348
349 /** Notifies all listeners when an Activity is pinned. */
Wale Ogunwale89be5762017-10-04 13:27:49 -0700350 void notifyActivityPinned(ActivityRecord r) {
Yorke Leebd54c2a2016-10-25 13:49:23 -0700351 mHandler.removeMessages(NOTIFY_ACTIVITY_PINNED_LISTENERS_MSG);
Wale Ogunwaleb7cf0632017-11-07 13:20:53 -0800352 final Message msg = mHandler.obtainMessage(NOTIFY_ACTIVITY_PINNED_LISTENERS_MSG,
Wale Ogunwale4e79a1c2019-10-05 20:52:40 -0700353 r.getTaskRecord().mTaskId, r.getStackId(), r.packageName);
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800354 msg.sendingUid = r.mUserId;
Yorke Lee13294072017-01-06 14:59:58 -0800355 forAllLocalListeners(mNotifyActivityPinned, msg);
356 msg.sendToTarget();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700357 }
358
Winson Chungc81c0ce2017-03-17 12:27:30 -0700359 /** Notifies all listeners when an Activity is unpinned. */
360 void notifyActivityUnpinned() {
361 mHandler.removeMessages(NOTIFY_ACTIVITY_UNPINNED_LISTENERS_MSG);
362 final Message msg = mHandler.obtainMessage(NOTIFY_ACTIVITY_UNPINNED_LISTENERS_MSG);
363 forAllLocalListeners(mNotifyActivityUnpinned, msg);
364 msg.sendToTarget();
365 }
366
Yorke Leebd54c2a2016-10-25 13:49:23 -0700367 /**
368 * Notifies all listeners when an attempt was made to start an an activity that is already
369 * running in the pinned stack and the activity was not actually started, but the task is
370 * either brought to the front or a new Intent is delivered to it.
371 */
Winson Chunge6385a22017-05-02 18:15:16 -0700372 void notifyPinnedActivityRestartAttempt(boolean clearedTask) {
Yorke Leebd54c2a2016-10-25 13:49:23 -0700373 mHandler.removeMessages(NOTIFY_PINNED_ACTIVITY_RESTART_ATTEMPT_LISTENERS_MSG);
Yorke Lee13294072017-01-06 14:59:58 -0800374 final Message msg =
Winson Chunge6385a22017-05-02 18:15:16 -0700375 mHandler.obtainMessage(NOTIFY_PINNED_ACTIVITY_RESTART_ATTEMPT_LISTENERS_MSG,
376 clearedTask ? 1 : 0, 0);
Yorke Lee13294072017-01-06 14:59:58 -0800377 forAllLocalListeners(mNotifyPinnedActivityRestartAttempt, msg);
378 msg.sendToTarget();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700379 }
380
Winson Chung85d39982017-02-24 15:21:25 -0800381 /** Notifies all listeners when the pinned stack animation starts. */
382 void notifyPinnedStackAnimationStarted() {
383 mHandler.removeMessages(NOTIFY_PINNED_STACK_ANIMATION_STARTED_LISTENERS_MSG);
384 final Message msg =
385 mHandler.obtainMessage(NOTIFY_PINNED_STACK_ANIMATION_STARTED_LISTENERS_MSG);
386 forAllLocalListeners(mNotifyPinnedStackAnimationStarted, msg);
387 msg.sendToTarget();
388 }
389
Yorke Leebd54c2a2016-10-25 13:49:23 -0700390 /** Notifies all listeners when the pinned stack animation ends. */
391 void notifyPinnedStackAnimationEnded() {
392 mHandler.removeMessages(NOTIFY_PINNED_STACK_ANIMATION_ENDED_LISTENERS_MSG);
Yorke Lee13294072017-01-06 14:59:58 -0800393 final Message msg =
394 mHandler.obtainMessage(NOTIFY_PINNED_STACK_ANIMATION_ENDED_LISTENERS_MSG);
395 forAllLocalListeners(mNotifyPinnedStackAnimationEnded, msg);
396 msg.sendToTarget();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700397 }
398
399 void notifyActivityDismissingDockedStack() {
400 mHandler.removeMessages(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
Wale Ogunwalef1b2ec92017-06-05 07:09:35 -0700401 final Message msg = mHandler.obtainMessage(NOTIFY_ACTIVITY_DISMISSING_DOCKED_STACK_MSG);
402 forAllLocalListeners(mNotifyActivityDismissingDockedStack, msg);
403 msg.sendToTarget();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700404 }
405
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700406 void notifyActivityForcedResizable(int taskId, int reason, String packageName) {
Yorke Leebd54c2a2016-10-25 13:49:23 -0700407 mHandler.removeMessages(NOTIFY_FORCED_RESIZABLE_MSG);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700408 final Message msg = mHandler.obtainMessage(NOTIFY_FORCED_RESIZABLE_MSG, taskId, reason,
409 packageName);
Yorke Lee13294072017-01-06 14:59:58 -0800410 forAllLocalListeners(mNotifyActivityForcedResizable, msg);
411 msg.sendToTarget();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700412 }
413
Mark Renoufc808f062019-02-07 15:20:37 -0500414 void notifyActivityLaunchOnSecondaryDisplayFailed(TaskInfo ti, int requestedDisplayId) {
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700415 mHandler.removeMessages(NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_FAILED_MSG);
416 final Message msg = mHandler.obtainMessage(
Mark Renoufc808f062019-02-07 15:20:37 -0500417 NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_FAILED_MSG, requestedDisplayId,
418 0 /* unused */, ti);
Andrii Kulian036e3ad2017-04-19 10:55:10 -0700419 forAllLocalListeners(mNotifyActivityLaunchOnSecondaryDisplayFailed, msg);
420 msg.sendToTarget();
421 }
422
Jeff Changbf299862019-02-26 20:07:22 +0800423 void notifyActivityLaunchOnSecondaryDisplayRerouted(TaskInfo ti, int requestedDisplayId) {
424 mHandler.removeMessages(NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_REROUTED_MSG);
425 final Message msg = mHandler.obtainMessage(
426 NOTIFY_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_REROUTED_MSG, requestedDisplayId,
427 0 /* unused */, ti);
428 forAllLocalListeners(mNotifyActivityLaunchOnSecondaryDisplayRerouted, msg);
429 msg.sendToTarget();
430 }
431
Yorke Leebd54c2a2016-10-25 13:49:23 -0700432 void notifyTaskCreated(int taskId, ComponentName componentName) {
Yorke Lee13294072017-01-06 14:59:58 -0800433 final Message msg = mHandler.obtainMessage(NOTIFY_TASK_ADDED_LISTENERS_MSG,
434 taskId, 0 /* unused */, componentName);
435 forAllLocalListeners(mNotifyTaskCreated, msg);
436 msg.sendToTarget();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700437 }
438
439 void notifyTaskRemoved(int taskId) {
Yorke Lee13294072017-01-06 14:59:58 -0800440 final Message msg = mHandler.obtainMessage(NOTIFY_TASK_REMOVED_LISTENERS_MSG,
441 taskId, 0 /* unused */);
442 forAllLocalListeners(mNotifyTaskRemoved, msg);
443 msg.sendToTarget();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700444 }
445
Mark Renoufc808f062019-02-07 15:20:37 -0500446 void notifyTaskMovedToFront(TaskInfo ti) {
447 final Message msg = mHandler.obtainMessage(NOTIFY_TASK_MOVED_TO_FRONT_LISTENERS_MSG, ti);
Yorke Lee13294072017-01-06 14:59:58 -0800448 forAllLocalListeners(mNotifyTaskMovedToFront, msg);
449 msg.sendToTarget();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700450 }
451
Mark Renoufc808f062019-02-07 15:20:37 -0500452 void notifyTaskDescriptionChanged(TaskInfo taskInfo) {
Yorke Lee13294072017-01-06 14:59:58 -0800453 final Message msg = mHandler.obtainMessage(NOTIFY_TASK_DESCRIPTION_CHANGED_LISTENERS_MSG,
Mark Renoufc808f062019-02-07 15:20:37 -0500454 taskInfo);
Yorke Lee13294072017-01-06 14:59:58 -0800455 forAllLocalListeners(mNotifyTaskDescriptionChanged, msg);
456 msg.sendToTarget();
457
Yorke Leebd54c2a2016-10-25 13:49:23 -0700458 }
459
460 void notifyActivityRequestedOrientationChanged(int taskId, int orientation) {
Yorke Lee13294072017-01-06 14:59:58 -0800461 final Message msg = mHandler.obtainMessage(
462 NOTIFY_ACTIVITY_REQUESTED_ORIENTATION_CHANGED_LISTENERS, taskId, orientation);
463 forAllLocalListeners(mNotifyActivityRequestedOrientationChanged, msg);
464 msg.sendToTarget();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700465 }
466
467 /**
468 * Notify listeners that the task is about to be finished before its surfaces are removed from
469 * the window manager. This allows interested parties to perform relevant animations before
470 * the window disappears.
471 */
Mark Renoufc808f062019-02-07 15:20:37 -0500472 void notifyTaskRemovalStarted(ActivityManager.RunningTaskInfo taskInfo) {
473 final Message msg = mHandler.obtainMessage(NOTIFY_TASK_REMOVAL_STARTED_LISTENERS, taskInfo);
Yorke Lee13294072017-01-06 14:59:58 -0800474 forAllLocalListeners(mNotifyTaskRemovalStarted, msg);
475 msg.sendToTarget();
Yorke Leebd54c2a2016-10-25 13:49:23 -0700476 }
Robin Leec41f6ec2017-01-10 17:02:34 +0000477
478 /**
479 * Notify listeners that the task has been put in a locked state because one or more of the
480 * activities inside it belong to a managed profile user that has been locked.
481 */
482 void notifyTaskProfileLocked(int taskId, int userId) {
Yorke Lee13294072017-01-06 14:59:58 -0800483 final Message msg = mHandler.obtainMessage(NOTIFY_TASK_PROFILE_LOCKED_LISTENERS_MSG, taskId,
484 userId);
485 forAllLocalListeners(mNotifyTaskProfileLocked, msg);
486 msg.sendToTarget();
Robin Leec41f6ec2017-01-10 17:02:34 +0000487 }
Jorim Jaggifb9d78a2017-01-05 18:57:12 +0100488
489 /**
490 * Notify listeners that the snapshot of a task has changed.
491 */
492 void notifyTaskSnapshotChanged(int taskId, TaskSnapshot snapshot) {
493 final Message msg = mHandler.obtainMessage(NOTIFY_TASK_SNAPSHOT_CHANGED_LISTENERS_MSG,
494 taskId, 0, snapshot);
495 forAllLocalListeners(mNotifyTaskSnapshotChanged, msg);
496 msg.sendToTarget();
497 }
Riddle Hsu7b766fd2019-01-28 21:14:59 +0800498
499 /**
500 * Notify listeners that whether a size compatibility mode activity is using the override
501 * bounds which is not fit its parent.
502 */
503 void notifySizeCompatModeActivityChanged(int displayId, IBinder activityToken) {
504 final Message msg = mHandler.obtainMessage(NOTIFY_SIZE_COMPAT_MODE_ACTIVITY_CHANGED_MSG,
505 displayId, 0 /* unused */, activityToken);
506 forAllLocalListeners(mOnSizeCompatModeActivityChanged, msg);
507 msg.sendToTarget();
508 }
Mark Renouf446251d2019-04-26 10:22:41 -0400509
510 /**
511 * Notify listeners that an activity received a back press when there are no other activities
512 * in the back stack.
513 */
514 void notifyBackPressedOnTaskRoot(TaskInfo taskInfo) {
515 final Message msg = mHandler.obtainMessage(NOTIFY_BACK_PRESSED_ON_TASK_ROOT,
516 taskInfo);
517 forAllLocalListeners(mNotifyBackPressedOnTaskRoot, msg);
518 msg.sendToTarget();
519 }
Issei Suzukicac2a502019-04-16 16:52:50 +0200520
521 /**
522 * Notify listeners that contents are drawn for the first time on a single task display.
523 */
524 void notifySingleTaskDisplayDrawn(int displayId) {
525 final Message msg = mHandler.obtainMessage(NOTIFY_SINGLE_TASK_DISPLAY_DRAWN,
526 displayId, 0 /* unused */);
527 forAllLocalListeners(mNotifySingleTaskDisplayDrawn, msg);
528 msg.sendToTarget();
529 }
davidln1ceedb52019-05-30 14:25:01 -0700530
531 /**
Issei Suzuki734bc942019-06-05 13:59:52 +0200532 * Notify listeners that the last task is removed from a single task display.
533 */
534 void notifySingleTaskDisplayEmpty(int displayId) {
535 final Message msg = mHandler.obtainMessage(
536 NOTIFY_SINGLE_TASK_DISPLAY_EMPTY,
537 displayId, 0 /* unused */);
538 forAllLocalListeners(mNotifySingleTaskDisplayEmpty, msg);
539 msg.sendToTarget();
540 }
davidlneaeae182019-05-30 14:25:01 -0700541
542 /**
davidln1ceedb52019-05-30 14:25:01 -0700543 * Notify listeners that a task is reparented to another display.
544 */
545 void notifyTaskDisplayChanged(int taskId, int newDisplayId) {
546 final Message msg = mHandler.obtainMessage(NOTIFY_TASK_DISPLAY_CHANGED_LISTENERS_MSG,
547 taskId, newDisplayId);
548 forAllLocalListeners(mNotifyTaskStackChanged, msg);
549 msg.sendToTarget();
550 }
Vinit Nayak3e737492019-07-24 13:03:15 -0700551
552 /**
553 * Called when any additions or deletions to the recent tasks list have been made.
554 */
555 void notifyTaskListUpdated() {
556 final Message msg = mHandler.obtainMessage(NOTIFY_TASK_LIST_UPDATED_LISTENERS_MSG);
557 forAllLocalListeners(mNotifyTaskListUpdated, msg);
558 msg.sendToTarget();
559 }
Vinit Nayak8e547962019-10-07 10:36:41 -0700560
561 /** @see ITaskStackListener#onRecentTaskListFrozenChanged(boolean) */
562 void notifyTaskListFrozen(boolean frozen) {
563 final Message msg = mHandler.obtainMessage(NOTIFY_TASK_LIST_FROZEN_UNFROZEN_MSG,
564 frozen ? 1 : 0, 0 /* unused */);
565 forAllLocalListeners(mNotifyTaskListFrozen, msg);
566 msg.sendToTarget();
567 }
Yorke Leebd54c2a2016-10-25 13:49:23 -0700568}