Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 18 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 19 | import static android.app.ActivityManager.FLAG_AND_UNLOCKED; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 20 | import static android.app.ActivityManager.RECENT_IGNORE_UNAVAILABLE; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 21 | import static android.app.ActivityManager.RECENT_WITH_EXCLUDED; |
Garfield Tan | 891146c | 2018-10-09 12:14:00 -0700 | [diff] [blame] | 22 | import static android.app.ActivityTaskManager.INVALID_TASK_ID; |
Vadim Tryshev | eff42d3 | 2018-03-05 18:33:48 -0800 | [diff] [blame] | 23 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT; |
Galia Peycheva | 45f48d3 | 2020-02-20 15:39:10 +0100 | [diff] [blame] | 24 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_DREAM; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 25 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME; |
| 26 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS; |
Winson Chung | 27f8188 | 2018-04-19 14:45:03 -0700 | [diff] [blame] | 27 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED; |
Winson Chung | e5ab017 | 2020-03-21 23:00:24 -0700 | [diff] [blame] | 28 | import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 29 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
| 30 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY; |
Winson Chung | 27f8188 | 2018-04-19 14:45:03 -0700 | [diff] [blame] | 31 | import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 32 | import static android.content.Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS; |
Wale Ogunwale | e8d5f65 | 2016-04-22 16:27:39 -0700 | [diff] [blame] | 33 | import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK; |
| 34 | import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT; |
| 35 | import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 36 | import static android.os.Process.SYSTEM_UID; |
Louis Chang | bd48dca | 2018-08-29 17:44:34 +0800 | [diff] [blame] | 37 | |
Garfield Tan | 891146c | 2018-10-09 12:14:00 -0700 | [diff] [blame] | 38 | import static com.android.server.wm.ActivityStackSupervisor.REMOVE_FROM_RECENTS; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 39 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RECENTS; |
| 40 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_RECENTS_TRIM_TASKS; |
| 41 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_TASKS; |
| 42 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_RECENTS; |
| 43 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_TASKS; |
| 44 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM; |
| 45 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME; |
Wale Ogunwale | e23149f | 2015-03-06 15:39:44 -0800 | [diff] [blame] | 46 | |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame] | 47 | import android.annotation.Nullable; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 48 | import android.app.ActivityManager; |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 49 | import android.app.ActivityTaskManager; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 50 | import android.app.AppGlobals; |
| 51 | import android.content.ComponentName; |
| 52 | import android.content.Intent; |
| 53 | import android.content.pm.ActivityInfo; |
| 54 | import android.content.pm.ApplicationInfo; |
| 55 | import android.content.pm.IPackageManager; |
| 56 | import android.content.pm.PackageManager; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 57 | import android.content.pm.ParceledListSlice; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 58 | import android.content.pm.UserInfo; |
| 59 | import android.content.res.Resources; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 60 | import android.graphics.Bitmap; |
| 61 | import android.os.Environment; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 62 | import android.os.IBinder; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 63 | import android.os.RemoteException; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 64 | import android.os.SystemProperties; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 65 | import android.os.UserHandle; |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 66 | import android.text.TextUtils; |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 67 | import android.util.ArraySet; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 68 | import android.util.Slog; |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 69 | import android.util.SparseArray; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 70 | import android.util.SparseBooleanArray; |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 71 | import android.view.MotionEvent; |
| 72 | import android.view.WindowManagerPolicyConstants.PointerEventListener; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 73 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 74 | import com.android.internal.annotations.VisibleForTesting; |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 75 | import com.android.internal.util.function.pooled.PooledLambda; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 76 | import com.android.server.am.ActivityManagerService; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 77 | |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 78 | import com.google.android.collect.Sets; |
| 79 | |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 80 | import java.io.File; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 81 | import java.io.PrintWriter; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 82 | import java.util.ArrayList; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 83 | import java.util.Arrays; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 84 | import java.util.Collections; |
| 85 | import java.util.Comparator; |
| 86 | import java.util.HashMap; |
Wale Ogunwale | 86b7446 | 2018-07-02 08:42:43 -0700 | [diff] [blame] | 87 | import java.util.List; |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 88 | import java.util.Set; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 89 | import java.util.concurrent.TimeUnit; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 90 | |
| 91 | /** |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 92 | * Class for managing the recent tasks list. The list is ordered by most recent (index 0) to the |
| 93 | * least recent. |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 94 | * |
| 95 | * The trimming logic can be boiled down to the following. For recent task list with a number of |
| 96 | * tasks, the visible tasks are an interleaving subset of tasks that would normally be presented to |
| 97 | * the user. Non-visible tasks are not considered for trimming. Of the visible tasks, only a |
| 98 | * sub-range are presented to the user, based on the device type, last task active time, or other |
| 99 | * task state. Tasks that are not in the visible range and are not returnable from the SystemUI |
| 100 | * (considering the back stack) are considered trimmable. If the device does not support recent |
| 101 | * tasks, then trimming is completely disabled. |
| 102 | * |
| 103 | * eg. |
| 104 | * L = [TTTTTTTTTTTTTTTTTTTTTTTTTT] // list of tasks |
| 105 | * [VVV VV VVVV V V V ] // Visible tasks |
| 106 | * [RRR RR XXXX X X X ] // Visible range tasks, eg. if the device only shows 5 tasks, |
| 107 | * // 'X' tasks are trimmed. |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 108 | */ |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 109 | class RecentTasks { |
Wale Ogunwale | 9887561 | 2018-10-12 07:53:02 -0700 | [diff] [blame] | 110 | private static final String TAG = TAG_WITH_CLASS_NAME ? "RecentTasks" : TAG_ATM; |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 111 | private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS; |
| 112 | private static final String TAG_TASKS = TAG + POSTFIX_TASKS; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 113 | |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 114 | private static final int DEFAULT_INITIAL_CAPACITY = 5; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 115 | |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 116 | // The duration of time after freezing the recent tasks list where getRecentTasks() will return |
| 117 | // a stable ordering of the tasks. Upon the next call to getRecentTasks() beyond this duration, |
| 118 | // the task list will be unfrozen and committed (the current top task will be moved to the |
| 119 | // front of the list) |
| 120 | private static final long FREEZE_TASK_LIST_TIMEOUT_MS = TimeUnit.SECONDS.toMillis(5); |
Winson | 6976f7b | 2016-05-03 14:58:12 -0700 | [diff] [blame] | 121 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 122 | // Comparator to sort by taskId |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 123 | private static final Comparator<Task> TASK_ID_COMPARATOR = |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 124 | (lhs, rhs) -> rhs.mTaskId - lhs.mTaskId; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 125 | |
| 126 | // Placeholder variables to keep track of activities/apps that are no longer avialble while |
| 127 | // iterating through the recents list |
| 128 | private static final ActivityInfo NO_ACTIVITY_INFO_TOKEN = new ActivityInfo(); |
| 129 | private static final ApplicationInfo NO_APPLICATION_INFO_TOKEN = new ApplicationInfo(); |
Vinit Nayak | 3e73749 | 2019-07-24 13:03:15 -0700 | [diff] [blame] | 130 | private TaskChangeNotificationController mTaskNotificationController; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Callbacks made when manipulating the list. |
| 134 | */ |
| 135 | interface Callbacks { |
| 136 | /** |
| 137 | * Called when a task is added to the recent tasks list. |
| 138 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 139 | void onRecentTaskAdded(Task task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * Called when a task is removed from the recent tasks list. |
| 143 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 144 | void onRecentTaskRemoved(Task task, boolean wasTrimmed, boolean killProcess); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 147 | /** |
| 148 | * Save recent tasks information across reboots. |
| 149 | */ |
| 150 | private final TaskPersister mTaskPersister; |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 151 | private final ActivityTaskManagerService mService; |
| 152 | private final ActivityStackSupervisor mSupervisor; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 153 | |
| 154 | /** |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 155 | * Keeps track of the static recents package/component which is granted additional permissions |
| 156 | * to call recents-related APIs. |
| 157 | */ |
| 158 | private int mRecentsUid = -1; |
| 159 | private ComponentName mRecentsComponent = null; |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame] | 160 | private @Nullable String mFeatureId; |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 161 | |
| 162 | /** |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 163 | * Mapping of user id -> whether recent tasks have been loaded for that user. |
| 164 | */ |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 165 | private final SparseBooleanArray mUsersWithRecentsLoaded = new SparseBooleanArray( |
| 166 | DEFAULT_INITIAL_CAPACITY); |
| 167 | |
| 168 | /** |
| 169 | * Stores for each user task ids that are taken by tasks residing in persistent storage. These |
| 170 | * tasks may or may not currently be in memory. |
| 171 | */ |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 172 | private final SparseArray<SparseBooleanArray> mPersistedTaskIds = new SparseArray<>( |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 173 | DEFAULT_INITIAL_CAPACITY); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 174 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 175 | // List of all active recent tasks |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 176 | private final ArrayList<Task> mTasks = new ArrayList<>(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 177 | private final ArrayList<Callbacks> mCallbacks = new ArrayList<>(); |
| 178 | |
Riddle Hsu | 4e361e9 | 2019-12-24 16:51:09 +0800 | [diff] [blame] | 179 | /** The non-empty tasks that are removed from recent tasks (see {@link #removeForAddTask}). */ |
| 180 | private final ArrayList<Task> mHiddenTasks = new ArrayList<>(); |
| 181 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 182 | // These values are generally loaded from resources, but can be set dynamically in the tests |
| 183 | private boolean mHasVisibleRecentTasks; |
| 184 | private int mGlobalMaxNumTasks; |
| 185 | private int mMinNumVisibleTasks; |
| 186 | private int mMaxNumVisibleTasks; |
| 187 | private long mActiveTasksSessionDurationMs; |
| 188 | |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 189 | // When set, the task list will not be reordered as tasks within the list are moved to the |
| 190 | // front. Newly created tasks, or tasks that are removed from the list will continue to change |
| 191 | // the list. This does not affect affiliated tasks. |
| 192 | private boolean mFreezeTaskListReordering; |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 193 | private long mFreezeTaskListTimeoutMs = FREEZE_TASK_LIST_TIMEOUT_MS; |
| 194 | |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 195 | // Mainly to avoid object recreation on multiple calls. |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 196 | private final ArrayList<Task> mTmpRecents = new ArrayList<>(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 197 | private final HashMap<ComponentName, ActivityInfo> mTmpAvailActCache = new HashMap<>(); |
| 198 | private final HashMap<String, ApplicationInfo> mTmpAvailAppCache = new HashMap<>(); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 199 | private final SparseBooleanArray mTmpQuietProfileUserIds = new SparseBooleanArray(); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 200 | |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 201 | // TODO(b/127498985): This is currently a rough heuristic for interaction inside an app |
| 202 | private final PointerEventListener mListener = new PointerEventListener() { |
| 203 | @Override |
| 204 | public void onPointerEvent(MotionEvent ev) { |
| 205 | if (!mFreezeTaskListReordering || ev.getAction() != MotionEvent.ACTION_DOWN) { |
| 206 | // Skip if we aren't freezing or starting a gesture |
| 207 | return; |
| 208 | } |
| 209 | int displayId = ev.getDisplayId(); |
| 210 | int x = (int) ev.getX(); |
| 211 | int y = (int) ev.getY(); |
| 212 | mService.mH.post(PooledLambda.obtainRunnable((nonArg) -> { |
| 213 | synchronized (mService.mGlobalLock) { |
| 214 | // Unfreeze the task list once we touch down in a task |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 215 | final RootWindowContainer rac = mService.mRootWindowContainer; |
Louis Chang | 677921f | 2019-12-06 16:44:24 +0800 | [diff] [blame] | 216 | final DisplayContent dc = rac.getDisplayContent(displayId).mDisplayContent; |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 217 | if (dc.pointWithinAppWindow(x, y)) { |
| 218 | final ActivityStack stack = mService.getTopDisplayFocusedStack(); |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 219 | final Task topTask = stack != null ? stack.getTopMostTask() : null; |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 220 | resetFreezeTaskListReordering(topTask); |
| 221 | } |
| 222 | } |
| 223 | }, null).recycleOnUse()); |
| 224 | } |
| 225 | }; |
| 226 | |
Winson Chung | 1fe58e9 | 2019-04-10 14:18:06 -0700 | [diff] [blame] | 227 | private final Runnable mResetFreezeTaskListOnTimeoutRunnable = |
| 228 | this::resetFreezeTaskListReorderingOnTimeout; |
| 229 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 230 | @VisibleForTesting |
Wale Ogunwale | 86b7446 | 2018-07-02 08:42:43 -0700 | [diff] [blame] | 231 | RecentTasks(ActivityTaskManagerService service, TaskPersister taskPersister) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 232 | mService = service; |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 233 | mSupervisor = mService.mStackSupervisor; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 234 | mTaskPersister = taskPersister; |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 235 | mGlobalMaxNumTasks = ActivityTaskManager.getMaxRecentTasksStatic(); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 236 | mHasVisibleRecentTasks = true; |
Vinit Nayak | 3e73749 | 2019-07-24 13:03:15 -0700 | [diff] [blame] | 237 | mTaskNotificationController = service.getTaskChangeNotificationController(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 240 | RecentTasks(ActivityTaskManagerService service, ActivityStackSupervisor stackSupervisor) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 241 | final File systemDir = Environment.getDataSystemDirectory(); |
| 242 | final Resources res = service.mContext.getResources(); |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 243 | mService = service; |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 244 | mSupervisor = mService.mStackSupervisor; |
Garfield Tan | 891146c | 2018-10-09 12:14:00 -0700 | [diff] [blame] | 245 | mTaskPersister = new TaskPersister(systemDir, stackSupervisor, service, this, |
| 246 | stackSupervisor.mPersisterQueue); |
Wale Ogunwale | 65ebd95 | 2018-04-25 15:41:44 -0700 | [diff] [blame] | 247 | mGlobalMaxNumTasks = ActivityTaskManager.getMaxRecentTasksStatic(); |
Vinit Nayak | 3e73749 | 2019-07-24 13:03:15 -0700 | [diff] [blame] | 248 | mTaskNotificationController = service.getTaskChangeNotificationController(); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 249 | mHasVisibleRecentTasks = res.getBoolean(com.android.internal.R.bool.config_hasRecents); |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 250 | loadParametersFromResources(res); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @VisibleForTesting |
| 254 | void setParameters(int minNumVisibleTasks, int maxNumVisibleTasks, |
| 255 | long activeSessionDurationMs) { |
| 256 | mMinNumVisibleTasks = minNumVisibleTasks; |
| 257 | mMaxNumVisibleTasks = maxNumVisibleTasks; |
| 258 | mActiveTasksSessionDurationMs = activeSessionDurationMs; |
| 259 | } |
| 260 | |
| 261 | @VisibleForTesting |
| 262 | void setGlobalMaxNumTasks(int globalMaxNumTasks) { |
| 263 | mGlobalMaxNumTasks = globalMaxNumTasks; |
| 264 | } |
| 265 | |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 266 | @VisibleForTesting |
Winson Chung | 1fe58e9 | 2019-04-10 14:18:06 -0700 | [diff] [blame] | 267 | void setFreezeTaskListTimeout(long timeoutMs) { |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 268 | mFreezeTaskListTimeoutMs = timeoutMs; |
| 269 | } |
| 270 | |
| 271 | PointerEventListener getInputListener() { |
| 272 | return mListener; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Freezes the current recent task list order until either a user interaction with the current |
| 277 | * app, or a timeout occurs. |
| 278 | */ |
| 279 | void setFreezeTaskListReordering() { |
Vinit Nayak | 8e54796 | 2019-10-07 10:36:41 -0700 | [diff] [blame] | 280 | // Only fire the callback once per quickswitch session, not on every individual switch |
| 281 | if (!mFreezeTaskListReordering) { |
| 282 | mTaskNotificationController.notifyTaskListFrozen(true); |
| 283 | mFreezeTaskListReordering = true; |
| 284 | } |
| 285 | |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 286 | // Always update the reordering time when this is called to ensure that the timeout |
| 287 | // is reset |
Winson Chung | 1fe58e9 | 2019-04-10 14:18:06 -0700 | [diff] [blame] | 288 | mService.mH.removeCallbacks(mResetFreezeTaskListOnTimeoutRunnable); |
| 289 | mService.mH.postDelayed(mResetFreezeTaskListOnTimeoutRunnable, mFreezeTaskListTimeoutMs); |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Commits the frozen recent task list order, moving the provided {@param topTask} to the |
| 294 | * front of the list. |
| 295 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 296 | void resetFreezeTaskListReordering(Task topTask) { |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 297 | if (!mFreezeTaskListReordering) { |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | // Once we end freezing the task list, reset the existing task order to the stable state |
| 302 | mFreezeTaskListReordering = false; |
Winson Chung | 1fe58e9 | 2019-04-10 14:18:06 -0700 | [diff] [blame] | 303 | mService.mH.removeCallbacks(mResetFreezeTaskListOnTimeoutRunnable); |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 304 | |
| 305 | // If the top task is provided, then restore the top task to the front of the list |
| 306 | if (topTask != null) { |
| 307 | mTasks.remove(topTask); |
| 308 | mTasks.add(0, topTask); |
| 309 | } |
| 310 | |
| 311 | // Resume trimming tasks |
| 312 | trimInactiveRecentTasks(); |
Winson Chung | 1fe58e9 | 2019-04-10 14:18:06 -0700 | [diff] [blame] | 313 | |
Vinit Nayak | 3e73749 | 2019-07-24 13:03:15 -0700 | [diff] [blame] | 314 | mTaskNotificationController.notifyTaskStackChanged(); |
Vinit Nayak | 8e54796 | 2019-10-07 10:36:41 -0700 | [diff] [blame] | 315 | mTaskNotificationController.notifyTaskListFrozen(false); |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Resets the frozen recent task list order if the timeout has passed. This should be called |
| 320 | * before we need to iterate the task list in order (either for purposes of returning the list |
| 321 | * to SystemUI or if we need to trim tasks in order) |
| 322 | */ |
Winson Chung | 1fe58e9 | 2019-04-10 14:18:06 -0700 | [diff] [blame] | 323 | @VisibleForTesting |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 324 | void resetFreezeTaskListReorderingOnTimeout() { |
chaviw | a13051f | 2019-06-14 16:41:17 -0700 | [diff] [blame] | 325 | synchronized (mService.mGlobalLock) { |
| 326 | final ActivityStack focusedStack = mService.getTopDisplayFocusedStack(); |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 327 | final Task topTask = focusedStack != null ? focusedStack.getTopMostTask() : null; |
chaviw | a13051f | 2019-06-14 16:41:17 -0700 | [diff] [blame] | 328 | resetFreezeTaskListReordering(topTask); |
| 329 | } |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | @VisibleForTesting |
| 333 | boolean isFreezeTaskListReorderingSet() { |
| 334 | return mFreezeTaskListReordering; |
| 335 | } |
| 336 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 337 | /** |
| 338 | * Loads the parameters from the system resources. |
| 339 | */ |
| 340 | @VisibleForTesting |
| 341 | void loadParametersFromResources(Resources res) { |
| 342 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 343 | mMinNumVisibleTasks = res.getInteger( |
| 344 | com.android.internal.R.integer.config_minNumVisibleRecentTasks_lowRam); |
| 345 | mMaxNumVisibleTasks = res.getInteger( |
| 346 | com.android.internal.R.integer.config_maxNumVisibleRecentTasks_lowRam); |
| 347 | } else if (SystemProperties.getBoolean("ro.recents.grid", false)) { |
| 348 | mMinNumVisibleTasks = res.getInteger( |
| 349 | com.android.internal.R.integer.config_minNumVisibleRecentTasks_grid); |
| 350 | mMaxNumVisibleTasks = res.getInteger( |
| 351 | com.android.internal.R.integer.config_maxNumVisibleRecentTasks_grid); |
| 352 | } else { |
| 353 | mMinNumVisibleTasks = res.getInteger( |
| 354 | com.android.internal.R.integer.config_minNumVisibleRecentTasks); |
| 355 | mMaxNumVisibleTasks = res.getInteger( |
| 356 | com.android.internal.R.integer.config_maxNumVisibleRecentTasks); |
| 357 | } |
| 358 | final int sessionDurationHrs = res.getInteger( |
| 359 | com.android.internal.R.integer.config_activeTaskDurationHours); |
| 360 | mActiveTasksSessionDurationMs = (sessionDurationHrs > 0) |
| 361 | ? TimeUnit.HOURS.toMillis(sessionDurationHrs) |
| 362 | : -1; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 365 | /** |
| 366 | * Loads the static recents component. This is called after the system is ready, but before |
| 367 | * any dependent services (like SystemUI) is started. |
| 368 | */ |
| 369 | void loadRecentsComponent(Resources res) { |
| 370 | final String rawRecentsComponent = res.getString( |
| 371 | com.android.internal.R.string.config_recentsComponentName); |
| 372 | if (TextUtils.isEmpty(rawRecentsComponent)) { |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | final ComponentName cn = ComponentName.unflattenFromString(rawRecentsComponent); |
| 377 | if (cn != null) { |
| 378 | try { |
| 379 | final ApplicationInfo appInfo = AppGlobals.getPackageManager() |
| 380 | .getApplicationInfo(cn.getPackageName(), 0, mService.mContext.getUserId()); |
| 381 | if (appInfo != null) { |
| 382 | mRecentsUid = appInfo.uid; |
| 383 | mRecentsComponent = cn; |
| 384 | } |
| 385 | } catch (RemoteException e) { |
| 386 | Slog.w(TAG, "Could not load application info for recents component: " + cn); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * @return whether the current caller has the same uid as the recents component. |
| 393 | */ |
| 394 | boolean isCallerRecents(int callingUid) { |
| 395 | return UserHandle.isSameApp(callingUid, mRecentsUid); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * @return whether the given component is the recents component and shares the same uid as the |
| 400 | * recents component. |
| 401 | */ |
| 402 | boolean isRecentsComponent(ComponentName cn, int uid) { |
| 403 | return cn.equals(mRecentsComponent) && UserHandle.isSameApp(uid, mRecentsUid); |
| 404 | } |
| 405 | |
Winson Chung | ec1ef09 | 2017-10-25 16:22:34 -0700 | [diff] [blame] | 406 | /** |
Winson Chung | c167427 | 2018-02-21 10:15:17 -0800 | [diff] [blame] | 407 | * @return whether the home app is also the active handler of recent tasks. |
| 408 | */ |
| 409 | boolean isRecentsComponentHomeActivity(int userId) { |
Wale Ogunwale | 906f9c6 | 2018-07-23 11:23:44 -0700 | [diff] [blame] | 410 | final ComponentName defaultHomeActivity = mService.getPackageManagerInternalLocked() |
Winson Chung | c167427 | 2018-02-21 10:15:17 -0800 | [diff] [blame] | 411 | .getDefaultHomeActivity(userId); |
Alice Sheng | 1de98fc | 2018-03-16 09:45:58 -0700 | [diff] [blame] | 412 | return defaultHomeActivity != null && mRecentsComponent != null && |
Winson Chung | c167427 | 2018-02-21 10:15:17 -0800 | [diff] [blame] | 413 | defaultHomeActivity.getPackageName().equals(mRecentsComponent.getPackageName()); |
| 414 | } |
| 415 | |
| 416 | /** |
Winson Chung | ec1ef09 | 2017-10-25 16:22:34 -0700 | [diff] [blame] | 417 | * @return the recents component. |
| 418 | */ |
| 419 | ComponentName getRecentsComponent() { |
| 420 | return mRecentsComponent; |
| 421 | } |
| 422 | |
| 423 | /** |
Philip P. Moltmann | ee29509 | 2020-02-10 08:46:26 -0800 | [diff] [blame] | 424 | * @return the featureId for the recents component. |
| 425 | */ |
| 426 | @Nullable String getRecentsComponentFeatureId() { |
| 427 | return mFeatureId; |
| 428 | } |
| 429 | |
| 430 | /** |
Winson Chung | ec1ef09 | 2017-10-25 16:22:34 -0700 | [diff] [blame] | 431 | * @return the uid for the recents component. |
| 432 | */ |
| 433 | int getRecentsComponentUid() { |
| 434 | return mRecentsUid; |
| 435 | } |
| 436 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 437 | void registerCallback(Callbacks callback) { |
| 438 | mCallbacks.add(callback); |
| 439 | } |
| 440 | |
| 441 | void unregisterCallback(Callbacks callback) { |
| 442 | mCallbacks.remove(callback); |
| 443 | } |
| 444 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 445 | private void notifyTaskAdded(Task task) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 446 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 447 | mCallbacks.get(i).onRecentTaskAdded(task); |
| 448 | } |
Vinit Nayak | 3e73749 | 2019-07-24 13:03:15 -0700 | [diff] [blame] | 449 | mTaskNotificationController.notifyTaskListUpdated(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 452 | private void notifyTaskRemoved(Task task, boolean wasTrimmed, boolean killProcess) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 453 | for (int i = 0; i < mCallbacks.size(); i++) { |
Winson Chung | e643910 | 2018-07-30 15:48:01 -0700 | [diff] [blame] | 454 | mCallbacks.get(i).onRecentTaskRemoved(task, wasTrimmed, killProcess); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 455 | } |
Vinit Nayak | 3e73749 | 2019-07-24 13:03:15 -0700 | [diff] [blame] | 456 | mTaskNotificationController.notifyTaskListUpdated(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
Suprabh Shukla | f5bf0cb | 2016-01-19 17:42:03 -0800 | [diff] [blame] | 460 | * Loads the persistent recentTasks for {@code userId} into this list from persistent storage. |
| 461 | * Does nothing if they are already loaded. |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 462 | * |
| 463 | * @param userId the user Id |
| 464 | */ |
| 465 | void loadUserRecentsLocked(int userId) { |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 466 | if (mUsersWithRecentsLoaded.get(userId)) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 467 | // User already loaded, return early |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 468 | return; |
| 469 | } |
| 470 | |
| 471 | // Load the task ids if not loaded. |
| 472 | loadPersistedTaskIdsForUserLocked(userId); |
| 473 | |
| 474 | // Check if any tasks are added before recents is loaded |
| 475 | final SparseBooleanArray preaddedTasks = new SparseBooleanArray(); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 476 | for (final Task task : mTasks) { |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 477 | if (task.mUserId == userId && shouldPersistTaskLocked(task)) { |
| 478 | preaddedTasks.put(task.mTaskId, true); |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | Slog.i(TAG, "Loading recents for user " + userId + " into memory."); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 483 | List<Task> tasks = mTaskPersister.restoreTasksForUserLocked(userId, preaddedTasks); |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 484 | mTasks.addAll(tasks); |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 485 | cleanupLocked(userId); |
| 486 | mUsersWithRecentsLoaded.put(userId, true); |
| 487 | |
| 488 | // If we have tasks added before loading recents, we need to update persistent task IDs. |
| 489 | if (preaddedTasks.size() > 0) { |
| 490 | syncPersistentTaskIdsLocked(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 494 | private void loadPersistedTaskIdsForUserLocked(int userId) { |
| 495 | // An empty instead of a null set here means that no persistent taskIds were present |
| 496 | // on file when we loaded them. |
| 497 | if (mPersistedTaskIds.get(userId) == null) { |
| 498 | mPersistedTaskIds.put(userId, mTaskPersister.loadPersistedTaskIdsForUser(userId)); |
Fyodor Kupolov | c4b46dd | 2016-03-17 12:57:36 -0700 | [diff] [blame] | 499 | Slog.i(TAG, "Loaded persisted task ids for user " + userId); |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 503 | /** |
| 504 | * @return whether the {@param taskId} is currently in use for the given user. |
| 505 | */ |
| 506 | boolean containsTaskId(int taskId, int userId) { |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 507 | loadPersistedTaskIdsForUserLocked(userId); |
| 508 | return mPersistedTaskIds.get(userId).get(taskId); |
| 509 | } |
| 510 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 511 | /** |
| 512 | * @return all the task ids for the user with the given {@param userId}. |
| 513 | */ |
| 514 | SparseBooleanArray getTaskIdsForUser(int userId) { |
| 515 | loadPersistedTaskIdsForUserLocked(userId); |
| 516 | return mPersistedTaskIds.get(userId); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Kicks off the task persister to write any pending tasks to disk. |
| 521 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 522 | void notifyTaskPersisterLocked(Task task, boolean flush) { |
Andrii Kulian | 02b7a83 | 2016-10-06 23:11:56 -0700 | [diff] [blame] | 523 | final ActivityStack stack = task != null ? task.getStack() : null; |
Matthew Ng | ae1ff4f | 2016-11-10 15:49:14 -0800 | [diff] [blame] | 524 | if (stack != null && stack.isHomeOrRecentsStack()) { |
| 525 | // Never persist the home or recents stack. |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 526 | return; |
| 527 | } |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 528 | syncPersistentTaskIdsLocked(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 529 | mTaskPersister.wakeup(task, flush); |
| 530 | } |
| 531 | |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 532 | private void syncPersistentTaskIdsLocked() { |
| 533 | for (int i = mPersistedTaskIds.size() - 1; i >= 0; i--) { |
| 534 | int userId = mPersistedTaskIds.keyAt(i); |
| 535 | if (mUsersWithRecentsLoaded.get(userId)) { |
| 536 | // Recents are loaded only after task ids are loaded. Therefore, the set of taskids |
| 537 | // referenced here should not be null. |
| 538 | mPersistedTaskIds.valueAt(i).clear(); |
| 539 | } |
| 540 | } |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 541 | for (int i = mTasks.size() - 1; i >= 0; i--) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 542 | final Task task = mTasks.get(i); |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 543 | if (shouldPersistTaskLocked(task)) { |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 544 | // Set of persisted taskIds for task.userId should not be null here |
Fyodor Kupolov | c4b46dd | 2016-03-17 12:57:36 -0700 | [diff] [blame] | 545 | // TODO Investigate why it can happen. For now initialize with an empty set |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 546 | if (mPersistedTaskIds.get(task.mUserId) == null) { |
| 547 | Slog.wtf(TAG, "No task ids found for userId " + task.mUserId + ". task=" + task |
Fyodor Kupolov | c4b46dd | 2016-03-17 12:57:36 -0700 | [diff] [blame] | 548 | + " mPersistedTaskIds=" + mPersistedTaskIds); |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 549 | mPersistedTaskIds.put(task.mUserId, new SparseBooleanArray()); |
Fyodor Kupolov | c4b46dd | 2016-03-17 12:57:36 -0700 | [diff] [blame] | 550 | } |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 551 | mPersistedTaskIds.get(task.mUserId).put(task.mTaskId, true); |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 552 | } |
| 553 | } |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 554 | } |
| 555 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 556 | private static boolean shouldPersistTaskLocked(Task task) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 557 | final ActivityStack stack = task.getStack(); |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 558 | return task.isPersistable && (stack == null || !stack.isHomeOrRecentsStack()); |
| 559 | } |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 560 | |
| 561 | void onSystemReadyLocked() { |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 562 | loadRecentsComponent(mService.mContext.getResources()); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 563 | mTasks.clear(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | Bitmap getTaskDescriptionIcon(String path) { |
| 567 | return mTaskPersister.getTaskDescriptionIcon(path); |
| 568 | } |
| 569 | |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 570 | void saveImage(Bitmap image, String path) { |
| 571 | mTaskPersister.saveImage(image, path); |
| 572 | } |
| 573 | |
| 574 | void flush() { |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 575 | synchronized (mService.mGlobalLock) { |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 576 | syncPersistentTaskIdsLocked(); |
| 577 | } |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 578 | mTaskPersister.flush(); |
| 579 | } |
| 580 | |
| 581 | /** |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 582 | * Returns all userIds for which recents from persistent storage are loaded into this list. |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 583 | * |
| 584 | * @return an array of userIds. |
| 585 | */ |
| 586 | int[] usersWithRecentsLoadedLocked() { |
| 587 | int[] usersWithRecentsLoaded = new int[mUsersWithRecentsLoaded.size()]; |
| 588 | int len = 0; |
| 589 | for (int i = 0; i < usersWithRecentsLoaded.length; i++) { |
| 590 | int userId = mUsersWithRecentsLoaded.keyAt(i); |
| 591 | if (mUsersWithRecentsLoaded.valueAt(i)) { |
| 592 | usersWithRecentsLoaded[len++] = userId; |
| 593 | } |
| 594 | } |
| 595 | if (len < usersWithRecentsLoaded.length) { |
| 596 | // should never happen. |
| 597 | return Arrays.copyOf(usersWithRecentsLoaded, len); |
| 598 | } |
| 599 | return usersWithRecentsLoaded; |
| 600 | } |
| 601 | |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 602 | /** |
| 603 | * Removes recent tasks and any other state kept in memory for the passed in user. Does not |
| 604 | * touch the information present on persistent storage. |
| 605 | * |
| 606 | * @param userId the id of the user |
| 607 | */ |
| 608 | void unloadUserDataFromMemoryLocked(int userId) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 609 | if (mUsersWithRecentsLoaded.get(userId)) { |
| 610 | Slog.i(TAG, "Unloading recents for user " + userId + " from memory."); |
| 611 | mUsersWithRecentsLoaded.delete(userId); |
| 612 | removeTasksForUserLocked(userId); |
| 613 | } |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 614 | mPersistedTaskIds.delete(userId); |
| 615 | mTaskPersister.unloadUserDataFromMemory(userId); |
| 616 | } |
| 617 | |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 618 | /** Remove recent tasks for a user. */ |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 619 | private void removeTasksForUserLocked(int userId) { |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 620 | if(userId <= 0) { |
| 621 | Slog.i(TAG, "Can't remove recent task on user " + userId); |
| 622 | return; |
| 623 | } |
| 624 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 625 | for (int i = mTasks.size() - 1; i >= 0; --i) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 626 | Task task = mTasks.get(i); |
| 627 | if (task.mUserId == userId) { |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 628 | if(DEBUG_TASKS) Slog.i(TAG_TASKS, |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 629 | "remove RecentTask " + task + " when finishing user" + userId); |
| 630 | remove(task); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 631 | } |
| 632 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 635 | void onPackagesSuspendedChanged(String[] packages, boolean suspended, int userId) { |
| 636 | final Set<String> packageNames = Sets.newHashSet(packages); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 637 | for (int i = mTasks.size() - 1; i >= 0; --i) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 638 | final Task task = mTasks.get(i); |
| 639 | if (task.realActivity != null |
| 640 | && packageNames.contains(task.realActivity.getPackageName()) |
| 641 | && task.mUserId == userId |
| 642 | && task.realActivitySuspended != suspended) { |
| 643 | task.realActivitySuspended = suspended; |
Suprabh Shukla | 021b57a | 2018-03-08 18:21:50 -0800 | [diff] [blame] | 644 | if (suspended) { |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 645 | mSupervisor.removeTask(task, false, REMOVE_FROM_RECENTS, "suspended-package"); |
Suprabh Shukla | 021b57a | 2018-03-08 18:21:50 -0800 | [diff] [blame] | 646 | } |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 647 | notifyTaskPersisterLocked(task, false); |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 648 | } |
| 649 | } |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 650 | } |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 651 | |
Charles He | d62f965 | 2017-11-01 10:05:51 +0000 | [diff] [blame] | 652 | void onLockTaskModeStateChanged(int lockTaskModeState, int userId) { |
| 653 | if (lockTaskModeState != ActivityManager.LOCK_TASK_MODE_LOCKED) { |
| 654 | return; |
| 655 | } |
| 656 | for (int i = mTasks.size() - 1; i >= 0; --i) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 657 | final Task task = mTasks.get(i); |
| 658 | if (task.mUserId == userId && !mService.getLockTaskController().isTaskWhitelisted(task)) { |
| 659 | remove(task); |
Charles He | d62f965 | 2017-11-01 10:05:51 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 664 | void removeTasksByPackageName(String packageName, int userId) { |
Winson Chung | 6308584 | 2017-10-13 17:18:58 -0700 | [diff] [blame] | 665 | for (int i = mTasks.size() - 1; i >= 0; --i) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 666 | final Task task = mTasks.get(i); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 667 | final String taskPackageName = |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 668 | task.getBaseIntent().getComponent().getPackageName(); |
| 669 | if (task.mUserId != userId) continue; |
Winson Chung | 99720d4 | 2018-04-09 11:28:46 -0700 | [diff] [blame] | 670 | if (!taskPackageName.equals(packageName)) continue; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 671 | |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 672 | mSupervisor.removeTask(task, true, REMOVE_FROM_RECENTS, "remove-package-task"); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
Winson Chung | 42fa21f | 2019-04-02 16:23:46 -0700 | [diff] [blame] | 676 | void removeAllVisibleTasks(int userId) { |
Winson Chung | cdd4c3f | 2019-04-17 15:40:11 -0700 | [diff] [blame] | 677 | Set<Integer> profileIds = getProfileIds(userId); |
Winson Chung | e643910 | 2018-07-30 15:48:01 -0700 | [diff] [blame] | 678 | for (int i = mTasks.size() - 1; i >= 0; --i) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 679 | final Task task = mTasks.get(i); |
| 680 | if (!profileIds.contains(task.mUserId)) continue; |
| 681 | if (isVisibleRecentTask(task)) { |
Winson Chung | e643910 | 2018-07-30 15:48:01 -0700 | [diff] [blame] | 682 | mTasks.remove(i); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 683 | notifyTaskRemoved(task, true /* wasTrimmed */, true /* killProcess */); |
Winson Chung | e643910 | 2018-07-30 15:48:01 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 688 | void cleanupDisabledPackageTasksLocked(String packageName, Set<String> filterByClasses, |
| 689 | int userId) { |
Winson Chung | 6308584 | 2017-10-13 17:18:58 -0700 | [diff] [blame] | 690 | for (int i = mTasks.size() - 1; i >= 0; --i) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 691 | final Task task = mTasks.get(i); |
| 692 | if (userId != UserHandle.USER_ALL && task.mUserId != userId) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 693 | continue; |
| 694 | } |
| 695 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 696 | ComponentName cn = task.intent != null ? task.intent.getComponent() : null; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 697 | final boolean sameComponent = cn != null && cn.getPackageName().equals(packageName) |
| 698 | && (filterByClasses == null || filterByClasses.contains(cn.getClassName())); |
| 699 | if (sameComponent) { |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 700 | mSupervisor.removeTask(task, false, REMOVE_FROM_RECENTS, "disabled-package"); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 705 | /** |
| 706 | * Update the recent tasks lists: make sure tasks should still be here (their |
| 707 | * applications / activities still exist), update their availability, fix-up ordering |
| 708 | * of affiliations. |
| 709 | */ |
| 710 | void cleanupLocked(int userId) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 711 | int recentsCount = mTasks.size(); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 712 | if (recentsCount == 0) { |
| 713 | // Happens when called from the packagemanager broadcast before boot, |
| 714 | // or just any empty list. |
| 715 | return; |
| 716 | } |
| 717 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 718 | // Clear the temp lists |
| 719 | mTmpAvailActCache.clear(); |
| 720 | mTmpAvailAppCache.clear(); |
| 721 | |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 722 | final IPackageManager pm = AppGlobals.getPackageManager(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 723 | for (int i = recentsCount - 1; i >= 0; i--) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 724 | final Task task = mTasks.get(i); |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 725 | if (userId != UserHandle.USER_ALL && task.mUserId != userId) { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 726 | // Only look at tasks for the user ID of interest. |
| 727 | continue; |
| 728 | } |
Wale Ogunwale | 21e0648 | 2019-11-18 05:14:15 -0800 | [diff] [blame] | 729 | if (task.autoRemoveRecents && task.getTopNonFinishingActivity() == null) { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 730 | // This situation is broken, and we should just get rid of it now. |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 731 | remove(task); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 732 | Slog.w(TAG, "Removing auto-remove without activity: " + task); |
| 733 | continue; |
| 734 | } |
| 735 | // Check whether this activity is currently available. |
| 736 | if (task.realActivity != null) { |
| 737 | ActivityInfo ai = mTmpAvailActCache.get(task.realActivity); |
| 738 | if (ai == null) { |
| 739 | try { |
| 740 | // At this first cut, we're only interested in |
| 741 | // activities that are fully runnable based on |
| 742 | // current system state. |
| 743 | ai = pm.getActivityInfo(task.realActivity, |
Philip P. Moltmann | cff8f0f | 2018-03-27 12:51:51 -0700 | [diff] [blame] | 744 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING |
| 745 | | ActivityManagerService.STOCK_PM_FLAGS, userId); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 746 | } catch (RemoteException e) { |
| 747 | // Will never happen. |
| 748 | continue; |
| 749 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 750 | if (ai == null) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 751 | ai = NO_ACTIVITY_INFO_TOKEN; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 752 | } |
| 753 | mTmpAvailActCache.put(task.realActivity, ai); |
| 754 | } |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 755 | if (ai == NO_ACTIVITY_INFO_TOKEN) { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 756 | // This could be either because the activity no longer exists, or the |
| 757 | // app is temporarily gone. For the former we want to remove the recents |
| 758 | // entry; for the latter we want to mark it as unavailable. |
| 759 | ApplicationInfo app = mTmpAvailAppCache |
| 760 | .get(task.realActivity.getPackageName()); |
| 761 | if (app == null) { |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 762 | try { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 763 | app = pm.getApplicationInfo(task.realActivity.getPackageName(), |
| 764 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 765 | } catch (RemoteException e) { |
| 766 | // Will never happen. |
| 767 | continue; |
| 768 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 769 | if (app == null) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 770 | app = NO_APPLICATION_INFO_TOKEN; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 771 | } |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 772 | mTmpAvailAppCache.put(task.realActivity.getPackageName(), app); |
| 773 | } |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 774 | if (app == NO_APPLICATION_INFO_TOKEN |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 775 | || (app.flags & ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 776 | // Doesn't exist any more! Good-bye. |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 777 | remove(task); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 778 | Slog.w(TAG, "Removing no longer valid recent: " + task); |
| 779 | continue; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 780 | } else { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 781 | // Otherwise just not available for now. |
| 782 | if (DEBUG_RECENTS && task.isAvailable) Slog.d(TAG_RECENTS, |
| 783 | "Making recent unavailable: " + task); |
| 784 | task.isAvailable = false; |
| 785 | } |
| 786 | } else { |
| 787 | if (!ai.enabled || !ai.applicationInfo.enabled |
| 788 | || (ai.applicationInfo.flags |
| 789 | & ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 790 | if (DEBUG_RECENTS && task.isAvailable) Slog.d(TAG_RECENTS, |
| 791 | "Making recent unavailable: " + task |
| 792 | + " (enabled=" + ai.enabled + "/" |
| 793 | + ai.applicationInfo.enabled |
| 794 | + " flags=" |
| 795 | + Integer.toHexString(ai.applicationInfo.flags) |
| 796 | + ")"); |
| 797 | task.isAvailable = false; |
| 798 | } else { |
| 799 | if (DEBUG_RECENTS && !task.isAvailable) Slog.d(TAG_RECENTS, |
| 800 | "Making recent available: " + task); |
| 801 | task.isAvailable = true; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | // Verify the affiliate chain for each task. |
| 808 | int i = 0; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 809 | recentsCount = mTasks.size(); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 810 | while (i < recentsCount) { |
| 811 | i = processNextAffiliateChainLocked(i); |
| 812 | } |
| 813 | // recent tasks are now in sorted, affiliated order. |
| 814 | } |
| 815 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 816 | /** |
| 817 | * @return whether the given {@param task} can be added to the list without causing another |
| 818 | * task to be trimmed as a result of that add. |
| 819 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 820 | private boolean canAddTaskWithoutTrim(Task task) { |
Winson Chung | 079221f | 2017-12-13 17:43:34 -0800 | [diff] [blame] | 821 | return findRemoveIndexForAddTask(task) == -1; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /** |
| 825 | * Returns the list of {@link ActivityManager.AppTask}s. |
| 826 | */ |
| 827 | ArrayList<IBinder> getAppTasksList(int callingUid, String callingPackage) { |
| 828 | final ArrayList<IBinder> list = new ArrayList<>(); |
| 829 | final int size = mTasks.size(); |
| 830 | for (int i = 0; i < size; i++) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 831 | final Task task = mTasks.get(i); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 832 | // Skip tasks that do not match the caller. We don't need to verify |
| 833 | // callingPackage, because we are also limiting to callingUid and know |
| 834 | // that will limit to the correct security sandbox. |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 835 | if (task.effectiveUid != callingUid) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 836 | continue; |
| 837 | } |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 838 | Intent intent = task.getBaseIntent(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 839 | if (intent == null || !callingPackage.equals(intent.getComponent().getPackageName())) { |
| 840 | continue; |
| 841 | } |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 842 | AppTaskImpl taskImpl = new AppTaskImpl(mService, task.mTaskId, callingUid); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 843 | list.add(taskImpl.asBinder()); |
| 844 | } |
| 845 | return list; |
| 846 | } |
| 847 | |
Wale Ogunwale | 86b7446 | 2018-07-02 08:42:43 -0700 | [diff] [blame] | 848 | @VisibleForTesting |
| 849 | Set<Integer> getProfileIds(int userId) { |
| 850 | Set<Integer> userIds = new ArraySet<>(); |
Sunny Goyal | e3de30e | 2020-03-11 22:42:44 -0700 | [diff] [blame] | 851 | int[] profileIds = mService.getUserManager().getProfileIds(userId, false /* enabledOnly */); |
| 852 | for (int i = 0; i < profileIds.length; i++) { |
| 853 | userIds.add(Integer.valueOf(profileIds[i])); |
Wale Ogunwale | 86b7446 | 2018-07-02 08:42:43 -0700 | [diff] [blame] | 854 | } |
| 855 | return userIds; |
| 856 | } |
| 857 | |
| 858 | @VisibleForTesting |
| 859 | UserInfo getUserInfo(int userId) { |
| 860 | return mService.getUserManager().getUserInfo(userId); |
| 861 | } |
| 862 | |
| 863 | @VisibleForTesting |
| 864 | int[] getCurrentProfileIds() { |
| 865 | return mService.mAmInternal.getCurrentProfileIds(); |
| 866 | } |
| 867 | |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 868 | @VisibleForTesting |
| 869 | boolean isUserRunning(int userId, int flags) { |
| 870 | return mService.mAmInternal.isUserRunning(userId, flags); |
| 871 | } |
| 872 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 873 | /** |
| 874 | * @return the list of recent tasks for presentation. |
| 875 | */ |
| 876 | ParceledListSlice<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, int flags, |
Winson Chung | 66b08f0 | 2020-03-03 14:32:35 -0800 | [diff] [blame] | 877 | boolean getTasksAllowed, int userId, int callingUid) { |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 878 | return new ParceledListSlice<>(getRecentTasksImpl(maxNum, flags, getTasksAllowed, |
Winson Chung | 66b08f0 | 2020-03-03 14:32:35 -0800 | [diff] [blame] | 879 | userId, callingUid)); |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 882 | /** |
| 883 | * @return the list of recent tasks for presentation. |
| 884 | */ |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 885 | private ArrayList<ActivityManager.RecentTaskInfo> getRecentTasksImpl(int maxNum, int flags, |
Winson Chung | 66b08f0 | 2020-03-03 14:32:35 -0800 | [diff] [blame] | 886 | boolean getTasksAllowed, int userId, int callingUid) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 887 | final boolean withExcluded = (flags & RECENT_WITH_EXCLUDED) != 0; |
| 888 | |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 889 | if (!isUserRunning(userId, FLAG_AND_UNLOCKED)) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 890 | Slog.i(TAG, "user " + userId + " is still locked. Cannot load recents"); |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 891 | return new ArrayList<>(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 892 | } |
| 893 | loadUserRecentsLocked(userId); |
| 894 | |
Wale Ogunwale | 86b7446 | 2018-07-02 08:42:43 -0700 | [diff] [blame] | 895 | final Set<Integer> includedUsers = getProfileIds(userId); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 896 | includedUsers.add(Integer.valueOf(userId)); |
| 897 | |
| 898 | final ArrayList<ActivityManager.RecentTaskInfo> res = new ArrayList<>(); |
| 899 | final int size = mTasks.size(); |
Winson Chung | fb44d21 | 2017-10-04 11:39:10 -0700 | [diff] [blame] | 900 | int numVisibleTasks = 0; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 901 | for (int i = 0; i < size; i++) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 902 | final Task task = mTasks.get(i); |
Winson Chung | fb44d21 | 2017-10-04 11:39:10 -0700 | [diff] [blame] | 903 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 904 | if (isVisibleRecentTask(task)) { |
Winson Chung | fb44d21 | 2017-10-04 11:39:10 -0700 | [diff] [blame] | 905 | numVisibleTasks++; |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 906 | if (isInVisibleRange(task, i, numVisibleTasks, withExcluded)) { |
Winson Chung | fb44d21 | 2017-10-04 11:39:10 -0700 | [diff] [blame] | 907 | // Fall through |
| 908 | } else { |
| 909 | // Not in visible range |
| 910 | continue; |
| 911 | } |
| 912 | } else { |
| 913 | // Not visible |
| 914 | continue; |
| 915 | } |
| 916 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 917 | // Skip remaining tasks once we reach the requested size |
| 918 | if (res.size() >= maxNum) { |
| 919 | continue; |
| 920 | } |
| 921 | |
| 922 | // Only add calling user or related users recent tasks |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 923 | if (!includedUsers.contains(Integer.valueOf(task.mUserId))) { |
| 924 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Skipping, not user: " + task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 925 | continue; |
| 926 | } |
| 927 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 928 | if (task.realActivitySuspended) { |
| 929 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Skipping, activity suspended: " + task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 930 | continue; |
| 931 | } |
| 932 | |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 933 | if (!getTasksAllowed) { |
| 934 | // If the caller doesn't have the GET_TASKS permission, then only |
| 935 | // allow them to see a small subset of tasks -- their own and home. |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 936 | if (!task.isActivityTypeHome() && task.effectiveUid != callingUid) { |
| 937 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Skipping, not allowed: " + task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 938 | continue; |
| 939 | } |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 940 | } |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 941 | |
Wale Ogunwale | 21e0648 | 2019-11-18 05:14:15 -0800 | [diff] [blame] | 942 | if (task.autoRemoveRecents && task.getTopNonFinishingActivity() == null) { |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 943 | // Don't include auto remove tasks that are finished or finishing. |
| 944 | if (DEBUG_RECENTS) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 945 | Slog.d(TAG_RECENTS, "Skipping, auto-remove without activity: " + task); |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 946 | } |
| 947 | continue; |
| 948 | } |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 949 | if ((flags & RECENT_IGNORE_UNAVAILABLE) != 0 && !task.isAvailable) { |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 950 | if (DEBUG_RECENTS) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 951 | Slog.d(TAG_RECENTS, "Skipping, unavail real act: " + task); |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 952 | } |
| 953 | continue; |
| 954 | } |
| 955 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 956 | if (!task.mUserSetupComplete) { |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 957 | // Don't include task launched while user is not done setting-up. |
| 958 | if (DEBUG_RECENTS) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 959 | Slog.d(TAG_RECENTS, "Skipping, user setup not complete: " + task); |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 960 | } |
| 961 | continue; |
| 962 | } |
| 963 | |
Winson Chung | 0275771 | 2020-06-23 10:41:44 -0700 | [diff] [blame] | 964 | res.add(createRecentTaskInfo(task, true /* stripExtras */)); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 965 | } |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 966 | return res; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /** |
| 970 | * @return the list of persistable task ids. |
| 971 | */ |
| 972 | void getPersistableTaskIds(ArraySet<Integer> persistentTaskIds) { |
| 973 | final int size = mTasks.size(); |
| 974 | for (int i = 0; i < size; i++) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 975 | final Task task = mTasks.get(i); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 976 | if (TaskPersister.DEBUG) Slog.d(TAG, "LazyTaskWriter: task=" + task |
| 977 | + " persistable=" + task.isPersistable); |
| 978 | final ActivityStack stack = task.getStack(); |
| 979 | if ((task.isPersistable || task.inRecents) |
| 980 | && (stack == null || !stack.isHomeOrRecentsStack())) { |
| 981 | if (TaskPersister.DEBUG) Slog.d(TAG, "adding to persistentTaskIds task=" + task); |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 982 | persistentTaskIds.add(task.mTaskId); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 983 | } else { |
| 984 | if (TaskPersister.DEBUG) Slog.d(TAG, "omitting from persistentTaskIds task=" |
| 985 | + task); |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 990 | @VisibleForTesting |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 991 | ArrayList<Task> getRawTasks() { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 992 | return mTasks; |
| 993 | } |
| 994 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 995 | /** |
Vadim Tryshev | 593e956 | 2018-03-08 17:15:45 -0800 | [diff] [blame] | 996 | * @return ids of tasks that are presented in Recents UI. |
| 997 | */ |
| 998 | SparseBooleanArray getRecentTaskIds() { |
| 999 | final SparseBooleanArray res = new SparseBooleanArray(); |
| 1000 | final int size = mTasks.size(); |
| 1001 | int numVisibleTasks = 0; |
| 1002 | for (int i = 0; i < size; i++) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1003 | final Task task = mTasks.get(i); |
| 1004 | if (isVisibleRecentTask(task)) { |
Vadim Tryshev | 593e956 | 2018-03-08 17:15:45 -0800 | [diff] [blame] | 1005 | numVisibleTasks++; |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1006 | if (isInVisibleRange(task, i, numVisibleTasks, false /* skipExcludedCheck */)) { |
| 1007 | res.put(task.mTaskId, true); |
Vadim Tryshev | 593e956 | 2018-03-08 17:15:45 -0800 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | return res; |
| 1012 | } |
| 1013 | |
| 1014 | /** |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1015 | * @return the task in the task list with the given {@param id} if one exists. |
| 1016 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1017 | Task getTask(int id) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1018 | final int recentsCount = mTasks.size(); |
| 1019 | for (int i = 0; i < recentsCount; i++) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1020 | Task task = mTasks.get(i); |
| 1021 | if (task.mTaskId == id) { |
| 1022 | return task; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | return null; |
| 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * Add a new task to the recent tasks list. |
| 1030 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1031 | void add(Task task) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1032 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "add: task=" + task); |
| 1033 | |
Riddle Hsu | f2ff25f | 2020-04-29 19:15:33 +0800 | [diff] [blame] | 1034 | // Only allow trimming task if it is not updating visibility for activities, so the caller |
| 1035 | // doesn't need to handle unexpected size and index when looping task containers. |
| 1036 | final boolean canTrimTask = !mSupervisor.inActivityVisibilityUpdate(); |
| 1037 | |
Riddle Hsu | 4e361e9 | 2019-12-24 16:51:09 +0800 | [diff] [blame] | 1038 | // Clean up the hidden tasks when going to home because the user may not be unable to return |
| 1039 | // to the task from recents. |
Riddle Hsu | f2ff25f | 2020-04-29 19:15:33 +0800 | [diff] [blame] | 1040 | if (canTrimTask && !mHiddenTasks.isEmpty() && task.isActivityTypeHome()) { |
Riddle Hsu | 4e361e9 | 2019-12-24 16:51:09 +0800 | [diff] [blame] | 1041 | removeUnreachableHiddenTasks(task.getWindowingMode()); |
| 1042 | } |
| 1043 | |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 1044 | final boolean isAffiliated = task.mAffiliatedTaskId != task.mTaskId |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1045 | || task.mNextAffiliateTaskId != INVALID_TASK_ID |
| 1046 | || task.mPrevAffiliateTaskId != INVALID_TASK_ID; |
| 1047 | |
| 1048 | int recentsCount = mTasks.size(); |
| 1049 | // Quick case: never add voice sessions. |
| 1050 | // TODO: VI what about if it's just an activity? |
| 1051 | // Probably nothing to do here |
| 1052 | if (task.voiceSession != null) { |
| 1053 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 1054 | "addRecent: not adding voice interaction " + task); |
| 1055 | return; |
| 1056 | } |
| 1057 | // Another quick case: check if the top-most recent task is the same. |
| 1058 | if (!isAffiliated && recentsCount > 0 && mTasks.get(0) == task) { |
| 1059 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: already at top: " + task); |
| 1060 | return; |
| 1061 | } |
| 1062 | // Another quick case: check if this is part of a set of affiliated |
| 1063 | // tasks that are at the top. |
| 1064 | if (isAffiliated && recentsCount > 0 && task.inRecents |
| 1065 | && task.mAffiliatedTaskId == mTasks.get(0).mAffiliatedTaskId) { |
| 1066 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: affiliated " + mTasks.get(0) |
| 1067 | + " at top when adding " + task); |
| 1068 | return; |
| 1069 | } |
| 1070 | |
| 1071 | boolean needAffiliationFix = false; |
| 1072 | |
| 1073 | // Slightly less quick case: the task is already in recents, so all we need |
| 1074 | // to do is move it. |
| 1075 | if (task.inRecents) { |
| 1076 | int taskIndex = mTasks.indexOf(task); |
| 1077 | if (taskIndex >= 0) { |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 1078 | if (!isAffiliated) { |
| 1079 | if (!mFreezeTaskListReordering) { |
| 1080 | // Simple case: this is not an affiliated task, so we just move it to the |
| 1081 | // front unless overridden by the provided activity options |
| 1082 | mTasks.remove(taskIndex); |
| 1083 | mTasks.add(0, task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1084 | |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 1085 | if (DEBUG_RECENTS) { |
| 1086 | Slog.d(TAG_RECENTS, "addRecent: moving to top " + task |
| 1087 | + " from " + taskIndex); |
| 1088 | } |
| 1089 | } |
| 1090 | notifyTaskPersisterLocked(task, false); |
| 1091 | return; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1092 | } |
| 1093 | } else { |
| 1094 | Slog.wtf(TAG, "Task with inRecent not in recents: " + task); |
| 1095 | needAffiliationFix = true; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: trimming tasks for " + task); |
Winson Chung | 079221f | 2017-12-13 17:43:34 -0800 | [diff] [blame] | 1100 | removeForAddTask(task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1101 | |
| 1102 | task.inRecents = true; |
| 1103 | if (!isAffiliated || needAffiliationFix) { |
| 1104 | // If this is a simple non-affiliated task, or we had some failure trying to |
| 1105 | // handle it as part of an affilated task, then just place it at the top. |
| 1106 | mTasks.add(0, task); |
| 1107 | notifyTaskAdded(task); |
| 1108 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: adding " + task); |
| 1109 | } else if (isAffiliated) { |
| 1110 | // If this is a new affiliated task, then move all of the affiliated tasks |
| 1111 | // to the front and insert this new one. |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1112 | Task other = task.mNextAffiliate; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1113 | if (other == null) { |
| 1114 | other = task.mPrevAffiliate; |
| 1115 | } |
| 1116 | if (other != null) { |
| 1117 | int otherIndex = mTasks.indexOf(other); |
| 1118 | if (otherIndex >= 0) { |
| 1119 | // Insert new task at appropriate location. |
| 1120 | int taskIndex; |
| 1121 | if (other == task.mNextAffiliate) { |
| 1122 | // We found the index of our next affiliation, which is who is |
| 1123 | // before us in the list, so add after that point. |
| 1124 | taskIndex = otherIndex+1; |
| 1125 | } else { |
| 1126 | // We found the index of our previous affiliation, which is who is |
| 1127 | // after us in the list, so add at their position. |
| 1128 | taskIndex = otherIndex; |
| 1129 | } |
| 1130 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 1131 | "addRecent: new affiliated task added at " + taskIndex + ": " + task); |
| 1132 | mTasks.add(taskIndex, task); |
| 1133 | notifyTaskAdded(task); |
| 1134 | |
| 1135 | // Now move everything to the front. |
| 1136 | if (moveAffiliatedTasksToFront(task, taskIndex)) { |
| 1137 | // All went well. |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | // Uh oh... something bad in the affiliation chain, try to rebuild |
| 1142 | // everything and then go through our general path of adding a new task. |
| 1143 | needAffiliationFix = true; |
| 1144 | } else { |
| 1145 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 1146 | "addRecent: couldn't find other affiliation " + other); |
| 1147 | needAffiliationFix = true; |
| 1148 | } |
| 1149 | } else { |
| 1150 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 1151 | "addRecent: adding affiliated task without next/prev:" + task); |
| 1152 | needAffiliationFix = true; |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | if (needAffiliationFix) { |
| 1157 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: regrouping affiliations"); |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 1158 | cleanupLocked(task.mUserId); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1159 | } |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1160 | |
| 1161 | // Trim the set of tasks to the active set |
Riddle Hsu | f2ff25f | 2020-04-29 19:15:33 +0800 | [diff] [blame] | 1162 | if (canTrimTask) { |
| 1163 | trimInactiveRecentTasks(); |
| 1164 | } |
Winson Chung | 081676e | 2019-12-11 13:55:19 -0800 | [diff] [blame] | 1165 | notifyTaskPersisterLocked(task, false /* flush */); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * Add the task to the bottom if possible. |
| 1170 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1171 | boolean addToBottom(Task task) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1172 | if (!canAddTaskWithoutTrim(task)) { |
| 1173 | // Adding this task would cause the task to be removed (since it's appended at |
| 1174 | // the bottom and would be trimmed) so just return now |
| 1175 | return false; |
| 1176 | } |
| 1177 | |
| 1178 | add(task); |
| 1179 | return true; |
| 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * Remove a task from the recent tasks list. |
| 1184 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1185 | void remove(Task task) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1186 | mTasks.remove(task); |
Winson Chung | e643910 | 2018-07-30 15:48:01 -0700 | [diff] [blame] | 1187 | notifyTaskRemoved(task, false /* wasTrimmed */, false /* killProcess */); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | /** |
| 1191 | * Trims the recents task list to the global max number of recents. |
| 1192 | */ |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1193 | private void trimInactiveRecentTasks() { |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 1194 | if (mFreezeTaskListReordering) { |
| 1195 | // Defer trimming inactive recent tasks until we are unfrozen |
| 1196 | return; |
| 1197 | } |
| 1198 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1199 | int recentsCount = mTasks.size(); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1200 | |
| 1201 | // Remove from the end of the list until we reach the max number of recents |
| 1202 | while (recentsCount > mGlobalMaxNumTasks) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1203 | final Task task = mTasks.remove(recentsCount - 1); |
| 1204 | notifyTaskRemoved(task, true /* wasTrimmed */, false /* killProcess */); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1205 | recentsCount--; |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1206 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "Trimming over max-recents task=" + task |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1207 | + " max=" + mGlobalMaxNumTasks); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1208 | } |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1209 | |
| 1210 | // Remove any tasks that belong to currently quiet profiles |
Wale Ogunwale | 86b7446 | 2018-07-02 08:42:43 -0700 | [diff] [blame] | 1211 | final int[] profileUserIds = getCurrentProfileIds(); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1212 | mTmpQuietProfileUserIds.clear(); |
| 1213 | for (int userId : profileUserIds) { |
Wale Ogunwale | 86b7446 | 2018-07-02 08:42:43 -0700 | [diff] [blame] | 1214 | final UserInfo userInfo = getUserInfo(userId); |
Winson Chung | 129771a0 | 2017-10-18 13:49:42 -0700 | [diff] [blame] | 1215 | if (userInfo != null && userInfo.isManagedProfile() && userInfo.isQuietModeEnabled()) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1216 | mTmpQuietProfileUserIds.put(userId, true); |
| 1217 | } |
| 1218 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "User: " + userInfo |
| 1219 | + " quiet=" + mTmpQuietProfileUserIds.get(userId)); |
| 1220 | } |
| 1221 | |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 1222 | // Remove any inactive tasks, calculate the latest set of visible tasks. |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1223 | int numVisibleTasks = 0; |
| 1224 | for (int i = 0; i < mTasks.size();) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1225 | final Task task = mTasks.get(i); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1226 | |
| 1227 | if (isActiveRecentTask(task, mTmpQuietProfileUserIds)) { |
| 1228 | if (!mHasVisibleRecentTasks) { |
| 1229 | // Keep all active tasks if visible recent tasks is not supported |
| 1230 | i++; |
| 1231 | continue; |
| 1232 | } |
| 1233 | |
| 1234 | if (!isVisibleRecentTask(task)) { |
| 1235 | // Keep all active-but-invisible tasks |
| 1236 | i++; |
| 1237 | continue; |
| 1238 | } else { |
| 1239 | numVisibleTasks++; |
Winson Chung | 0a10c3a | 2019-04-23 15:53:08 -0700 | [diff] [blame] | 1240 | if (isInVisibleRange(task, i, numVisibleTasks, false /* skipExcludedCheck */) |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 1241 | || !isTrimmable(task)) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1242 | // Keep visible tasks in range |
| 1243 | i++; |
| 1244 | continue; |
| 1245 | } else { |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 1246 | // Fall through to trim visible tasks that are no longer in range and |
| 1247 | // trimmable |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1248 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, |
| 1249 | "Trimming out-of-range visible task=" + task); |
| 1250 | } |
| 1251 | } |
| 1252 | } else { |
| 1253 | // Fall through to trim inactive tasks |
| 1254 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "Trimming inactive task=" + task); |
| 1255 | } |
| 1256 | |
| 1257 | // Task is no longer active, trim it from the list |
| 1258 | mTasks.remove(task); |
Winson Chung | e643910 | 2018-07-30 15:48:01 -0700 | [diff] [blame] | 1259 | notifyTaskRemoved(task, true /* wasTrimmed */, false /* killProcess */); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1260 | notifyTaskPersisterLocked(task, false /* flush */); |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | /** |
| 1265 | * @return whether the given task should be considered active. |
| 1266 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1267 | private boolean isActiveRecentTask(Task task, SparseBooleanArray quietProfileUserIds) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1268 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "isActiveRecentTask: task=" + task |
| 1269 | + " globalMax=" + mGlobalMaxNumTasks); |
| 1270 | |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 1271 | if (quietProfileUserIds.get(task.mUserId)) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1272 | // Quiet profile user's tasks are never active |
| 1273 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "\tisQuietProfileTask=true"); |
| 1274 | return false; |
| 1275 | } |
| 1276 | |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 1277 | if (task.mAffiliatedTaskId != INVALID_TASK_ID && task.mAffiliatedTaskId != task.mTaskId) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1278 | // Keep the task active if its affiliated task is also active |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1279 | final Task affiliatedTask = getTask(task.mAffiliatedTaskId); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1280 | if (affiliatedTask != null) { |
| 1281 | if (!isActiveRecentTask(affiliatedTask, quietProfileUserIds)) { |
| 1282 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, |
| 1283 | "\taffiliatedWithTask=" + affiliatedTask + " is not active"); |
| 1284 | return false; |
| 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | // All other tasks are considered active |
| 1290 | return true; |
| 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * @return whether the given active task should be presented to the user through SystemUI. |
| 1295 | */ |
Mark Renouf | 2aaf4cd | 2019-02-19 17:24:43 -0500 | [diff] [blame] | 1296 | @VisibleForTesting |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1297 | boolean isVisibleRecentTask(Task task) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1298 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "isVisibleRecentTask: task=" + task |
| 1299 | + " minVis=" + mMinNumVisibleTasks + " maxVis=" + mMaxNumVisibleTasks |
| 1300 | + " sessionDuration=" + mActiveTasksSessionDurationMs |
| 1301 | + " inactiveDuration=" + task.getInactiveDuration() |
| 1302 | + " activityType=" + task.getActivityType() |
Vadim Tryshev | eff42d3 | 2018-03-05 18:33:48 -0800 | [diff] [blame] | 1303 | + " windowingMode=" + task.getWindowingMode() |
| 1304 | + " intentFlags=" + task.getBaseIntent().getFlags()); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1305 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1306 | switch (task.getActivityType()) { |
| 1307 | case ACTIVITY_TYPE_HOME: |
| 1308 | case ACTIVITY_TYPE_RECENTS: |
Galia Peycheva | 45f48d3 | 2020-02-20 15:39:10 +0100 | [diff] [blame] | 1309 | case ACTIVITY_TYPE_DREAM: |
Vadim Tryshev | eff42d3 | 2018-03-05 18:33:48 -0800 | [diff] [blame] | 1310 | // Ignore certain activity types completely |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1311 | return false; |
Vadim Tryshev | eff42d3 | 2018-03-05 18:33:48 -0800 | [diff] [blame] | 1312 | case ACTIVITY_TYPE_ASSISTANT: |
| 1313 | // Ignore assistant that chose to be excluded from Recents, even if it's a top |
| 1314 | // task. |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 1315 | if ((task.getBaseIntent().getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) |
| 1316 | == FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) { |
Vadim Tryshev | eff42d3 | 2018-03-05 18:33:48 -0800 | [diff] [blame] | 1317 | return false; |
| 1318 | } |
Winson Chung | e5ab017 | 2020-03-21 23:00:24 -0700 | [diff] [blame] | 1319 | break; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | // Ignore certain windowing modes |
| 1323 | switch (task.getWindowingMode()) { |
| 1324 | case WINDOWING_MODE_PINNED: |
| 1325 | return false; |
| 1326 | case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: |
Winson Chung | e5ab017 | 2020-03-21 23:00:24 -0700 | [diff] [blame] | 1327 | if (DEBUG_RECENTS_TRIM_TASKS) { |
| 1328 | Slog.d(TAG, "\ttop=" + task.getStack().getTopMostTask()); |
| 1329 | } |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1330 | final ActivityStack stack = task.getStack(); |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 1331 | if (stack != null && stack.getTopMostTask() == task) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1332 | // Only the non-top task of the primary split screen mode is visible |
| 1333 | return false; |
| 1334 | } |
Winson Chung | e5ab017 | 2020-03-21 23:00:24 -0700 | [diff] [blame] | 1335 | break; |
| 1336 | case WINDOWING_MODE_MULTI_WINDOW: |
| 1337 | // Ignore tasks that are always on top |
| 1338 | if (task.isAlwaysOnTop()) { |
| 1339 | return false; |
| 1340 | } |
| 1341 | break; |
Mark Renouf | 2aaf4cd | 2019-02-19 17:24:43 -0500 | [diff] [blame] | 1342 | } |
| 1343 | |
Winson Chung | 3e405ad | 2020-06-10 15:25:46 -0700 | [diff] [blame] | 1344 | // Tasks managed by/associated with an ActivityView should be excluded from recents. |
| 1345 | // singleTaskInstance is set on the VirtualDisplay managed by ActivityView |
| 1346 | // TODO(b/126185105): Find a different signal to use besides isSingleTaskInstance |
| 1347 | final ActivityStack stack = task.getStack(); |
| 1348 | if (stack != null) { |
| 1349 | DisplayContent display = stack.getDisplay(); |
| 1350 | if (display != null && display.isSingleTaskInstance()) { |
| 1351 | return false; |
| 1352 | } |
| 1353 | } |
| 1354 | |
Benjamin Franz | 7dcbfb0 | 2018-01-16 15:16:16 +0000 | [diff] [blame] | 1355 | // If we're in lock task mode, ignore the root task |
Wale Ogunwale | d95c06b | 2018-05-08 10:35:38 -0700 | [diff] [blame] | 1356 | if (task == mService.getLockTaskController().getRootTask()) { |
Benjamin Franz | 7dcbfb0 | 2018-01-16 15:16:16 +0000 | [diff] [blame] | 1357 | return false; |
| 1358 | } |
| 1359 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1360 | return true; |
| 1361 | } |
| 1362 | |
| 1363 | /** |
| 1364 | * @return whether the given visible task is within the policy range. |
| 1365 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1366 | private boolean isInVisibleRange(Task task, int taskIndex, int numVisibleTasks, |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 1367 | boolean skipExcludedCheck) { |
| 1368 | if (!skipExcludedCheck) { |
| 1369 | // Keep the most recent task even if it is excluded from recents |
| 1370 | final boolean isExcludeFromRecents = |
| 1371 | (task.getBaseIntent().getFlags() & FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) |
| 1372 | == FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS; |
| 1373 | if (isExcludeFromRecents) { |
| 1374 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "\texcludeFromRecents=true"); |
Winson Chung | 0a10c3a | 2019-04-23 15:53:08 -0700 | [diff] [blame] | 1375 | return taskIndex == 0; |
Winson Chung | d273007 | 2019-02-22 14:10:27 -0800 | [diff] [blame] | 1376 | } |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | if (mMinNumVisibleTasks >= 0 && numVisibleTasks <= mMinNumVisibleTasks) { |
| 1380 | // Always keep up to the min number of recent tasks, after that fall through to the |
| 1381 | // checks below |
| 1382 | return true; |
| 1383 | } |
| 1384 | |
| 1385 | if (mMaxNumVisibleTasks >= 0) { |
| 1386 | // Always keep up to the max number of recent tasks, but return false afterwards |
| 1387 | return numVisibleTasks <= mMaxNumVisibleTasks; |
| 1388 | } |
| 1389 | |
| 1390 | if (mActiveTasksSessionDurationMs > 0) { |
| 1391 | // Keep the task if the inactive time is within the session window, this check must come |
| 1392 | // after the checks for the min/max visible task range |
| 1393 | if (task.getInactiveDuration() <= mActiveTasksSessionDurationMs) { |
| 1394 | return true; |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | return false; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
Charles Chen | 7d550c4 | 2020-05-15 17:32:03 +0800 | [diff] [blame] | 1401 | /** @return whether the given task can be trimmed even if it is outside the visible range. */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1402 | protected boolean isTrimmable(Task task) { |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 1403 | final ActivityStack stack = task.getStack(); |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 1404 | |
| 1405 | // No stack for task, just trim it |
| 1406 | if (stack == null) { |
| 1407 | return true; |
| 1408 | } |
| 1409 | |
| 1410 | // Ignore tasks from different displays |
Louis Chang | bd48dca | 2018-08-29 17:44:34 +0800 | [diff] [blame] | 1411 | // TODO (b/115289124): No Recents on non-default displays. |
Wale Ogunwale | 8f93b64 | 2019-12-26 12:10:52 -0800 | [diff] [blame] | 1412 | if (!stack.isOnHomeDisplay()) { |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 1413 | return false; |
| 1414 | } |
| 1415 | |
Charles Chen | 7d550c4 | 2020-05-15 17:32:03 +0800 | [diff] [blame] | 1416 | final ActivityStack rootHomeTask = stack.getDisplayArea().getRootHomeTask(); |
| 1417 | // Home stack does not exist. Don't trim the task. |
| 1418 | if (rootHomeTask == null) { |
| 1419 | return false; |
| 1420 | } |
Wale Ogunwale | 0db64ac | 2020-04-11 10:00:42 -0700 | [diff] [blame] | 1421 | // Trim tasks that are behind the home task. |
Charles Chen | 7d550c4 | 2020-05-15 17:32:03 +0800 | [diff] [blame] | 1422 | return task.compareTo(rootHomeTask) < 0; |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Riddle Hsu | 4e361e9 | 2019-12-24 16:51:09 +0800 | [diff] [blame] | 1425 | /** Remove the tasks that user may not be able to return. */ |
| 1426 | private void removeUnreachableHiddenTasks(int windowingMode) { |
| 1427 | for (int i = mHiddenTasks.size() - 1; i >= 0; i--) { |
| 1428 | final Task hiddenTask = mHiddenTasks.get(i); |
| 1429 | if (!hiddenTask.hasChild()) { |
| 1430 | // The task was removed by other path. |
| 1431 | mHiddenTasks.remove(i); |
| 1432 | continue; |
| 1433 | } |
| 1434 | if (hiddenTask.getWindowingMode() != windowingMode |
| 1435 | || hiddenTask.getTopVisibleActivity() != null) { |
| 1436 | // The task may be reachable from the back stack of other windowing mode or it is |
| 1437 | // currently in use. Keep the task in the hidden list to avoid losing track, e.g. |
| 1438 | // after dismissing primary split screen. |
| 1439 | continue; |
| 1440 | } |
| 1441 | mHiddenTasks.remove(i); |
| 1442 | mSupervisor.removeTask(hiddenTask, false /* killProcess */, |
| 1443 | !REMOVE_FROM_RECENTS, "remove-hidden-task"); |
| 1444 | } |
| 1445 | } |
| 1446 | |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 1447 | /** |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1448 | * If needed, remove oldest existing entries in recents that are for the same kind |
| 1449 | * of task as the given one. |
| 1450 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1451 | private void removeForAddTask(Task task) { |
Winson Chung | 079221f | 2017-12-13 17:43:34 -0800 | [diff] [blame] | 1452 | final int removeIndex = findRemoveIndexForAddTask(task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1453 | if (removeIndex == -1) { |
| 1454 | // Nothing to trim |
| 1455 | return; |
| 1456 | } |
| 1457 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1458 | // There is a similar task that will be removed for the addition of {@param task}, but it |
| 1459 | // can be the same task, and if so, the task will be re-added in add(), so skip the |
| 1460 | // callbacks here. |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1461 | final Task removedTask = mTasks.remove(removeIndex); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1462 | if (removedTask != task) { |
Riddle Hsu | 4e361e9 | 2019-12-24 16:51:09 +0800 | [diff] [blame] | 1463 | // The added task is in recents so it is not hidden. |
| 1464 | mHiddenTasks.remove(task); |
| 1465 | if (removedTask.hasChild()) { |
| 1466 | // A non-empty task is replaced by a new task. Because the removed task is no longer |
| 1467 | // managed by the recent tasks list, add it to the hidden list to prevent the task |
| 1468 | // from becoming dangling. |
| 1469 | mHiddenTasks.add(removedTask); |
| 1470 | } |
Winson Chung | e643910 | 2018-07-30 15:48:01 -0700 | [diff] [blame] | 1471 | notifyTaskRemoved(removedTask, false /* wasTrimmed */, false /* killProcess */); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1472 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "Trimming task=" + removedTask |
| 1473 | + " for addition of task=" + task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1474 | } |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1475 | notifyTaskPersisterLocked(removedTask, false /* flush */); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /** |
| 1479 | * Find the task that would be removed if the given {@param task} is added to the recent tasks |
| 1480 | * list (if any). |
| 1481 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1482 | private int findRemoveIndexForAddTask(Task task) { |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 1483 | if (mFreezeTaskListReordering) { |
| 1484 | // Defer removing tasks due to the addition of new tasks until the task list is unfrozen |
| 1485 | return -1; |
| 1486 | } |
| 1487 | |
Winson Chung | 6c9dcad | 2018-03-13 16:57:55 -0700 | [diff] [blame] | 1488 | final int recentsCount = mTasks.size(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1489 | final Intent intent = task.intent; |
| 1490 | final boolean document = intent != null && intent.isDocument(); |
| 1491 | int maxRecents = task.maxRecents - 1; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1492 | for (int i = 0; i < recentsCount; i++) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1493 | final Task t = mTasks.get(i); |
| 1494 | if (task != t) { |
| 1495 | if (!hasCompatibleActivityTypeAndWindowingMode(task, t) |
| 1496 | || task.mUserId != t.mUserId) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1497 | continue; |
| 1498 | } |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1499 | final Intent trIntent = t.intent; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1500 | final boolean sameAffinity = |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1501 | task.affinity != null && task.affinity.equals(t.affinity); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1502 | final boolean sameIntent = intent != null && intent.filterEquals(trIntent); |
| 1503 | boolean multiTasksAllowed = false; |
| 1504 | final int flags = intent.getFlags(); |
| 1505 | if ((flags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NEW_DOCUMENT)) != 0 |
| 1506 | && (flags & FLAG_ACTIVITY_MULTIPLE_TASK) != 0) { |
| 1507 | multiTasksAllowed = true; |
| 1508 | } |
| 1509 | final boolean trIsDocument = trIntent != null && trIntent.isDocument(); |
| 1510 | final boolean bothDocuments = document && trIsDocument; |
| 1511 | if (!sameAffinity && !sameIntent && !bothDocuments) { |
| 1512 | continue; |
| 1513 | } |
| 1514 | |
| 1515 | if (bothDocuments) { |
| 1516 | // Do these documents belong to the same activity? |
| 1517 | final boolean sameActivity = task.realActivity != null |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1518 | && t.realActivity != null |
| 1519 | && task.realActivity.equals(t.realActivity); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1520 | if (!sameActivity) { |
| 1521 | // If the document is open in another app or is not the same document, we |
| 1522 | // don't need to trim it. |
| 1523 | continue; |
| 1524 | } else if (maxRecents > 0) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1525 | --maxRecents; |
| 1526 | if (!sameIntent || multiTasksAllowed) { |
| 1527 | // We don't want to trim if we are not over the max allowed entries and |
| 1528 | // the tasks are not of the same intent filter, or multiple entries for |
| 1529 | // the task is allowed. |
| 1530 | continue; |
| 1531 | } |
| 1532 | } |
| 1533 | // Hit the maximum number of documents for this task. Fall through |
| 1534 | // and remove this document from recents. |
| 1535 | } else if (document || trIsDocument) { |
| 1536 | // Only one of these is a document. Not the droid we're looking for. |
| 1537 | continue; |
| 1538 | } |
| 1539 | } |
| 1540 | return i; |
| 1541 | } |
| 1542 | return -1; |
| 1543 | } |
| 1544 | |
| 1545 | // Extract the affiliates of the chain containing recent at index start. |
| 1546 | private int processNextAffiliateChainLocked(int start) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1547 | final Task startTask = mTasks.get(start); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1548 | final int affiliateId = startTask.mAffiliatedTaskId; |
| 1549 | |
| 1550 | // Quick identification of isolated tasks. I.e. those not launched behind. |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 1551 | if (startTask.mTaskId == affiliateId && startTask.mPrevAffiliate == null && |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1552 | startTask.mNextAffiliate == null) { |
| 1553 | // There is still a slim chance that there are other tasks that point to this task |
| 1554 | // and that the chain is so messed up that this task no longer points to them but |
| 1555 | // the gain of this optimization outweighs the risk. |
| 1556 | startTask.inRecents = true; |
| 1557 | return start + 1; |
| 1558 | } |
| 1559 | |
| 1560 | // Remove all tasks that are affiliated to affiliateId and put them in mTmpRecents. |
| 1561 | mTmpRecents.clear(); |
| 1562 | for (int i = mTasks.size() - 1; i >= start; --i) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1563 | final Task task = mTasks.get(i); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1564 | if (task.mAffiliatedTaskId == affiliateId) { |
| 1565 | mTasks.remove(i); |
| 1566 | mTmpRecents.add(task); |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | // Sort them all by taskId. That is the order they were create in and that order will |
| 1571 | // always be correct. |
| 1572 | Collections.sort(mTmpRecents, TASK_ID_COMPARATOR); |
| 1573 | |
| 1574 | // Go through and fix up the linked list. |
| 1575 | // The first one is the end of the chain and has no next. |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1576 | final Task first = mTmpRecents.get(0); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1577 | first.inRecents = true; |
| 1578 | if (first.mNextAffiliate != null) { |
| 1579 | Slog.w(TAG, "Link error 1 first.next=" + first.mNextAffiliate); |
| 1580 | first.setNextAffiliate(null); |
| 1581 | notifyTaskPersisterLocked(first, false); |
| 1582 | } |
| 1583 | // Everything in the middle is doubly linked from next to prev. |
| 1584 | final int tmpSize = mTmpRecents.size(); |
| 1585 | for (int i = 0; i < tmpSize - 1; ++i) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1586 | final Task next = mTmpRecents.get(i); |
| 1587 | final Task prev = mTmpRecents.get(i + 1); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1588 | if (next.mPrevAffiliate != prev) { |
| 1589 | Slog.w(TAG, "Link error 2 next=" + next + " prev=" + next.mPrevAffiliate + |
| 1590 | " setting prev=" + prev); |
| 1591 | next.setPrevAffiliate(prev); |
| 1592 | notifyTaskPersisterLocked(next, false); |
| 1593 | } |
| 1594 | if (prev.mNextAffiliate != next) { |
| 1595 | Slog.w(TAG, "Link error 3 prev=" + prev + " next=" + prev.mNextAffiliate + |
| 1596 | " setting next=" + next); |
| 1597 | prev.setNextAffiliate(next); |
| 1598 | notifyTaskPersisterLocked(prev, false); |
| 1599 | } |
| 1600 | prev.inRecents = true; |
| 1601 | } |
| 1602 | // The last one is the beginning of the list and has no prev. |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1603 | final Task last = mTmpRecents.get(tmpSize - 1); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1604 | if (last.mPrevAffiliate != null) { |
| 1605 | Slog.w(TAG, "Link error 4 last.prev=" + last.mPrevAffiliate); |
| 1606 | last.setPrevAffiliate(null); |
| 1607 | notifyTaskPersisterLocked(last, false); |
| 1608 | } |
| 1609 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1610 | // Insert the group back into mTmpTasks at start. |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1611 | mTasks.addAll(start, mTmpRecents); |
| 1612 | mTmpRecents.clear(); |
| 1613 | |
| 1614 | // Let the caller know where we left off. |
| 1615 | return start + tmpSize; |
| 1616 | } |
| 1617 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1618 | private boolean moveAffiliatedTasksToFront(Task task, int taskIndex) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1619 | int recentsCount = mTasks.size(); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1620 | Task top = task; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1621 | int topIndex = taskIndex; |
| 1622 | while (top.mNextAffiliate != null && topIndex > 0) { |
| 1623 | top = top.mNextAffiliate; |
| 1624 | topIndex--; |
| 1625 | } |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 1626 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: adding affilliates starting at " |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1627 | + topIndex + " from intial " + taskIndex); |
| 1628 | // Find the end of the chain, doing a sanity check along the way. |
| 1629 | boolean sane = top.mAffiliatedTaskId == task.mAffiliatedTaskId; |
| 1630 | int endIndex = topIndex; |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1631 | Task prev = top; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1632 | while (endIndex < recentsCount) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1633 | Task cur = mTasks.get(endIndex); |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 1634 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: looking at next chain @" |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1635 | + endIndex + " " + cur); |
| 1636 | if (cur == top) { |
| 1637 | // Verify start of the chain. |
| 1638 | if (cur.mNextAffiliate != null || cur.mNextAffiliateTaskId != INVALID_TASK_ID) { |
| 1639 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 1640 | + ": first task has next affiliate: " + prev); |
| 1641 | sane = false; |
| 1642 | break; |
| 1643 | } |
| 1644 | } else { |
| 1645 | // Verify middle of the chain's next points back to the one before. |
| 1646 | if (cur.mNextAffiliate != prev |
Wale Ogunwale | 4e79a1c | 2019-10-05 20:52:40 -0700 | [diff] [blame] | 1647 | || cur.mNextAffiliateTaskId != prev.mTaskId) { |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1648 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 1649 | + ": middle task " + cur + " @" + endIndex |
| 1650 | + " has bad next affiliate " |
| 1651 | + cur.mNextAffiliate + " id " + cur.mNextAffiliateTaskId |
| 1652 | + ", expected " + prev); |
| 1653 | sane = false; |
| 1654 | break; |
| 1655 | } |
| 1656 | } |
| 1657 | if (cur.mPrevAffiliateTaskId == INVALID_TASK_ID) { |
| 1658 | // Chain ends here. |
| 1659 | if (cur.mPrevAffiliate != null) { |
| 1660 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 1661 | + ": last task " + cur + " has previous affiliate " |
| 1662 | + cur.mPrevAffiliate); |
| 1663 | sane = false; |
| 1664 | } |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 1665 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: end of chain @" + endIndex); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1666 | break; |
| 1667 | } else { |
| 1668 | // Verify middle of the chain's prev points to a valid item. |
| 1669 | if (cur.mPrevAffiliate == null) { |
| 1670 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 1671 | + ": task " + cur + " has previous affiliate " |
| 1672 | + cur.mPrevAffiliate + " but should be id " |
| 1673 | + cur.mPrevAffiliate); |
| 1674 | sane = false; |
| 1675 | break; |
| 1676 | } |
| 1677 | } |
| 1678 | if (cur.mAffiliatedTaskId != task.mAffiliatedTaskId) { |
| 1679 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 1680 | + ": task " + cur + " has affiliated id " |
| 1681 | + cur.mAffiliatedTaskId + " but should be " |
| 1682 | + task.mAffiliatedTaskId); |
| 1683 | sane = false; |
| 1684 | break; |
| 1685 | } |
| 1686 | prev = cur; |
| 1687 | endIndex++; |
| 1688 | if (endIndex >= recentsCount) { |
| 1689 | Slog.wtf(TAG, "Bad chain ran off index " + endIndex |
| 1690 | + ": last task " + prev); |
| 1691 | sane = false; |
| 1692 | break; |
| 1693 | } |
| 1694 | } |
| 1695 | if (sane) { |
| 1696 | if (endIndex < taskIndex) { |
| 1697 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 1698 | + ": did not extend to task " + task + " @" + taskIndex); |
| 1699 | sane = false; |
| 1700 | } |
| 1701 | } |
| 1702 | if (sane) { |
| 1703 | // All looks good, we can just move all of the affiliated tasks |
| 1704 | // to the top. |
| 1705 | for (int i=topIndex; i<=endIndex; i++) { |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 1706 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: moving affiliated " + task |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1707 | + " from " + i + " to " + (i-topIndex)); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1708 | Task cur = mTasks.remove(i); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1709 | mTasks.add(i - topIndex, cur); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1710 | } |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 1711 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: done moving tasks " + topIndex |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1712 | + " to " + endIndex); |
| 1713 | return true; |
| 1714 | } |
| 1715 | |
| 1716 | // Whoops, couldn't do it. |
| 1717 | return false; |
| 1718 | } |
| 1719 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1720 | void dump(PrintWriter pw, boolean dumpAll, String dumpPackage) { |
| 1721 | pw.println("ACTIVITY MANAGER RECENT TASKS (dumpsys activity recents)"); |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 1722 | pw.println("mRecentsUid=" + mRecentsUid); |
| 1723 | pw.println("mRecentsComponent=" + mRecentsComponent); |
Winson Chung | c5fe7ff | 2019-02-19 14:49:25 -0800 | [diff] [blame] | 1724 | pw.println("mFreezeTaskListReordering=" + mFreezeTaskListReordering); |
Winson Chung | 1fe58e9 | 2019-04-10 14:18:06 -0700 | [diff] [blame] | 1725 | pw.println("mFreezeTaskListReorderingPendingTimeout=" |
| 1726 | + mService.mH.hasCallbacks(mResetFreezeTaskListOnTimeoutRunnable)); |
Riddle Hsu | 4e361e9 | 2019-12-24 16:51:09 +0800 | [diff] [blame] | 1727 | if (!mHiddenTasks.isEmpty()) { |
| 1728 | pw.println("mHiddenTasks=" + mHiddenTasks); |
| 1729 | } |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1730 | if (mTasks.isEmpty()) { |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1731 | return; |
| 1732 | } |
| 1733 | |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 1734 | // Dump raw recent task list |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 1735 | boolean printedAnything = false; |
| 1736 | boolean printedHeader = false; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1737 | final int size = mTasks.size(); |
| 1738 | for (int i = 0; i < size; i++) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1739 | final Task task = mTasks.get(i); |
Dianne Hackborn | 9106f2a | 2020-04-30 16:23:20 -0700 | [diff] [blame] | 1740 | if (dumpPackage != null) { |
| 1741 | boolean match = task.intent != null |
| 1742 | && task.intent.getComponent() != null |
| 1743 | && dumpPackage.equals( |
| 1744 | task.intent.getComponent().getPackageName()); |
| 1745 | if (!match) { |
| 1746 | match |= task.affinityIntent != null |
| 1747 | && task.affinityIntent.getComponent() != null |
| 1748 | && dumpPackage.equals( |
| 1749 | task.affinityIntent.getComponent().getPackageName()); |
| 1750 | } |
| 1751 | if (!match) { |
| 1752 | match |= task.origActivity != null |
| 1753 | && dumpPackage.equals(task.origActivity.getPackageName()); |
| 1754 | } |
| 1755 | if (!match) { |
| 1756 | match |= task.realActivity != null |
| 1757 | && dumpPackage.equals(task.realActivity.getPackageName()); |
| 1758 | } |
| 1759 | if (!match) { |
| 1760 | match |= dumpPackage.equals(task.mCallingPackage); |
| 1761 | } |
| 1762 | if (!match) { |
| 1763 | continue; |
| 1764 | } |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1765 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1766 | |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 1767 | if (!printedHeader) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1768 | pw.println(" Recent tasks:"); |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 1769 | printedHeader = true; |
| 1770 | printedAnything = true; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1771 | } |
| 1772 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1773 | pw.println(task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1774 | if (dumpAll) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1775 | task.dump(pw, " "); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 1779 | // Dump visible recent task list |
| 1780 | if (mHasVisibleRecentTasks) { |
| 1781 | // Reset the header flag for the next block |
| 1782 | printedHeader = false; |
| 1783 | ArrayList<ActivityManager.RecentTaskInfo> tasks = getRecentTasksImpl(Integer.MAX_VALUE, |
Winson Chung | 66b08f0 | 2020-03-03 14:32:35 -0800 | [diff] [blame] | 1784 | 0, true /* getTasksAllowed */, mService.getCurrentUserId(), SYSTEM_UID); |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 1785 | for (int i = 0; i < tasks.size(); i++) { |
| 1786 | final ActivityManager.RecentTaskInfo taskInfo = tasks.get(i); |
Dianne Hackborn | 9106f2a | 2020-04-30 16:23:20 -0700 | [diff] [blame] | 1787 | if (dumpPackage != null) { |
| 1788 | boolean match = taskInfo.baseIntent != null |
| 1789 | && taskInfo.baseIntent.getComponent() != null |
| 1790 | && dumpPackage.equals( |
| 1791 | taskInfo.baseIntent.getComponent().getPackageName()); |
| 1792 | if (!match) { |
| 1793 | match |= taskInfo.baseActivity != null |
| 1794 | && dumpPackage.equals(taskInfo.baseActivity.getPackageName()); |
| 1795 | } |
| 1796 | if (!match) { |
| 1797 | match |= taskInfo.topActivity != null |
| 1798 | && dumpPackage.equals(taskInfo.topActivity.getPackageName()); |
| 1799 | } |
| 1800 | if (!match) { |
| 1801 | match |= taskInfo.origActivity != null |
| 1802 | && dumpPackage.equals(taskInfo.origActivity.getPackageName()); |
| 1803 | } |
| 1804 | if (!match) { |
| 1805 | match |= taskInfo.realActivity != null |
| 1806 | && dumpPackage.equals(taskInfo.realActivity.getPackageName()); |
| 1807 | } |
| 1808 | if (!match) { |
| 1809 | continue; |
| 1810 | } |
| 1811 | } |
Winson Chung | b413299 | 2018-07-03 15:52:38 -0700 | [diff] [blame] | 1812 | if (!printedHeader) { |
| 1813 | if (printedAnything) { |
| 1814 | // Separate from the last block if it printed |
| 1815 | pw.println(); |
| 1816 | } |
| 1817 | pw.println(" Visible recent tasks (most recent first):"); |
| 1818 | printedHeader = true; |
| 1819 | printedAnything = true; |
| 1820 | } |
| 1821 | |
| 1822 | pw.print(" * RecentTaskInfo #"); pw.print(i); pw.print(": "); |
| 1823 | taskInfo.dump(pw, " "); |
| 1824 | } |
| 1825 | } |
| 1826 | |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 1827 | if (!printedAnything) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1828 | pw.println(" (nothing)"); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | |
| 1832 | /** |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1833 | * Creates a new RecentTaskInfo from a Task. |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1834 | */ |
Winson Chung | 0275771 | 2020-06-23 10:41:44 -0700 | [diff] [blame] | 1835 | ActivityManager.RecentTaskInfo createRecentTaskInfo(Task tr, boolean stripExtras) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1836 | ActivityManager.RecentTaskInfo rti = new ActivityManager.RecentTaskInfo(); |
Winson Chung | 0275771 | 2020-06-23 10:41:44 -0700 | [diff] [blame] | 1837 | tr.fillTaskInfo(rti, stripExtras); |
Winson Chung | abfdcce | 2018-07-02 17:23:33 -0700 | [diff] [blame] | 1838 | // Fill in some deprecated values |
| 1839 | rti.id = rti.isRunning ? rti.taskId : INVALID_TASK_ID; |
| 1840 | rti.persistentId = rti.taskId; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1841 | return rti; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1842 | } |
Winson Chung | 27f8188 | 2018-04-19 14:45:03 -0700 | [diff] [blame] | 1843 | |
| 1844 | /** |
| 1845 | * @return Whether the activity types and windowing modes of the two tasks are considered |
| 1846 | * compatible. This is necessary because we currently don't persist the activity type |
| 1847 | * or the windowing mode with the task, so they can be undefined when restored. |
| 1848 | */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 1849 | private boolean hasCompatibleActivityTypeAndWindowingMode(Task t1, Task t2) { |
Winson Chung | 27f8188 | 2018-04-19 14:45:03 -0700 | [diff] [blame] | 1850 | final int activityType = t1.getActivityType(); |
| 1851 | final int windowingMode = t1.getWindowingMode(); |
| 1852 | final boolean isUndefinedType = activityType == ACTIVITY_TYPE_UNDEFINED; |
| 1853 | final boolean isUndefinedMode = windowingMode == WINDOWING_MODE_UNDEFINED; |
| 1854 | final int otherActivityType = t2.getActivityType(); |
| 1855 | final int otherWindowingMode = t2.getWindowingMode(); |
| 1856 | final boolean isOtherUndefinedType = otherActivityType == ACTIVITY_TYPE_UNDEFINED; |
| 1857 | final boolean isOtherUndefinedMode = otherWindowingMode == WINDOWING_MODE_UNDEFINED; |
| 1858 | |
| 1859 | // An activity type and windowing mode is compatible if they are the exact same type/mode, |
| 1860 | // or if one of the type/modes is undefined |
| 1861 | final boolean isCompatibleType = activityType == otherActivityType |
| 1862 | || isUndefinedType || isOtherUndefinedType; |
| 1863 | final boolean isCompatibleMode = windowingMode == otherWindowingMode |
| 1864 | || isUndefinedMode || isOtherUndefinedMode; |
| 1865 | |
| 1866 | return isCompatibleType && isCompatibleMode; |
| 1867 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1868 | } |