1 /*
2  * Copyright (C) 2017 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.systemui.shared.system;
18 
19 import android.app.ActivityManager.RunningTaskInfo;
20 import android.content.ComponentName;
21 import android.os.IBinder;
22 import android.os.UserHandle;
23 import android.util.Log;
24 
25 import com.android.systemui.shared.recents.model.ThumbnailData;
26 
27 /**
28  * An interface to track task stack changes. Classes should implement this instead of
29  * {@link android.app.ITaskStackListener} to reduce IPC calls from system services.
30  */
31 public abstract class TaskStackChangeListener {
32 
33     // Binder thread callbacks
onTaskStackChangedBackground()34     public void onTaskStackChangedBackground() { }
35 
36     // Main thread callbacks
onTaskStackChanged()37     public void onTaskStackChanged() { }
onTaskSnapshotChanged(int taskId, ThumbnailData snapshot)38     public void onTaskSnapshotChanged(int taskId, ThumbnailData snapshot) { }
onActivityPinned(String packageName, int userId, int taskId, int stackId)39     public void onActivityPinned(String packageName, int userId, int taskId, int stackId) { }
onActivityUnpinned()40     public void onActivityUnpinned() { }
onPinnedActivityRestartAttempt(boolean clearedTask)41     public void onPinnedActivityRestartAttempt(boolean clearedTask) { }
onPinnedStackAnimationStarted()42     public void onPinnedStackAnimationStarted() { }
onPinnedStackAnimationEnded()43     public void onPinnedStackAnimationEnded() { }
onActivityForcedResizable(String packageName, int taskId, int reason)44     public void onActivityForcedResizable(String packageName, int taskId, int reason) { }
onActivityDismissingDockedStack()45     public void onActivityDismissingDockedStack() { }
onActivityLaunchOnSecondaryDisplayFailed()46     public void onActivityLaunchOnSecondaryDisplayFailed() { }
47 
onActivityLaunchOnSecondaryDisplayFailed(RunningTaskInfo taskInfo)48     public void onActivityLaunchOnSecondaryDisplayFailed(RunningTaskInfo taskInfo) {
49         onActivityLaunchOnSecondaryDisplayFailed();
50     }
51 
52     /**
53      * @see #onActivityLaunchOnSecondaryDisplayRerouted(RunningTaskInfo taskInfo)
54      */
onActivityLaunchOnSecondaryDisplayRerouted()55     public void onActivityLaunchOnSecondaryDisplayRerouted() { }
56 
57     /**
58      * Called when an activity was requested to be launched on a secondary display but was rerouted
59      * to default display.
60      *
61      * @param taskInfo info about the Activity's task
62      */
onActivityLaunchOnSecondaryDisplayRerouted(RunningTaskInfo taskInfo)63     public void onActivityLaunchOnSecondaryDisplayRerouted(RunningTaskInfo taskInfo) {
64         onActivityLaunchOnSecondaryDisplayRerouted();
65     }
66 
67     /**
68      * Called when contents are drawn for the first time on a display which can only contain one
69      * task.
70      *
71      * @param displayId the id of the display on which contents are drawn.
72      */
onSingleTaskDisplayDrawn(int displayId)73     public void onSingleTaskDisplayDrawn(int displayId) { }
74 
75     /**
76      * Called when the last task is removed from a display which can only contain one task.
77      *
78      * @param displayId the id of the display from which the window is removed.
79      */
onSingleTaskDisplayEmpty(int displayId)80     public void onSingleTaskDisplayEmpty(int displayId) {}
81 
onTaskProfileLocked(int taskId, int userId)82     public void onTaskProfileLocked(int taskId, int userId) { }
onTaskCreated(int taskId, ComponentName componentName)83     public void onTaskCreated(int taskId, ComponentName componentName) { }
onTaskRemoved(int taskId)84     public void onTaskRemoved(int taskId) { }
onTaskMovedToFront(int taskId)85     public void onTaskMovedToFront(int taskId) { }
86 
onTaskMovedToFront(RunningTaskInfo taskInfo)87     public void onTaskMovedToFront(RunningTaskInfo taskInfo) {
88         onTaskMovedToFront(taskInfo.taskId);
89     }
90 
onActivityRequestedOrientationChanged(int taskId, int requestedOrientation)91     public void onActivityRequestedOrientationChanged(int taskId, int requestedOrientation) { }
onSizeCompatModeActivityChanged(int displayId, IBinder activityToken)92     public void onSizeCompatModeActivityChanged(int displayId, IBinder activityToken) { }
93 
onBackPressedOnTaskRoot(RunningTaskInfo taskInfo)94     public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) { }
95 
96     /**
97      * Called when a task is reparented to a stack on a different display.
98      *
99      * @param taskId id of the task which was moved to a different display.
100      * @param newDisplayId id of the new display.
101      */
onTaskDisplayChanged(int taskId, int newDisplayId)102     public void onTaskDisplayChanged(int taskId, int newDisplayId) { }
103 
104     /**
105      * Checks that the current user matches the process. Since
106      * {@link android.app.ITaskStackListener} is not multi-user aware, handlers of
107      * {@link TaskStackChangeListener} should make this call to verify that we don't act on events
108      * originating from another user's interactions.
109      */
checkCurrentUserId(int currentUserId, boolean debug)110     protected final boolean checkCurrentUserId(int currentUserId, boolean debug) {
111         int processUserId = UserHandle.myUserId();
112         if (processUserId != currentUserId) {
113             if (debug) {
114                 Log.d("TaskStackChangeListener", "UID mismatch. Process is uid=" + processUserId
115                         + " and the current user is uid=" + currentUserId);
116             }
117             return false;
118         }
119         return true;
120     }
121 }
122