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 | |
| 17 | package com.android.server.am; |
| 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; |
Vadim Tryshev | eff42d3 | 2018-03-05 18:33:48 -0800 | [diff] [blame] | 22 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 23 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME; |
| 24 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS; |
| 25 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
| 26 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY; |
Wale Ogunwale | e8d5f65 | 2016-04-22 16:27:39 -0700 | [diff] [blame] | 27 | import static android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK; |
| 28 | import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT; |
| 29 | import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 30 | |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 31 | import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 32 | import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS_TRIM_TASKS; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 33 | import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS; |
| 34 | import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS; |
| 35 | import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS; |
| 36 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; |
| 37 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 38 | import static com.android.server.am.ActivityStackSupervisor.REMOVE_FROM_RECENTS; |
Wale Ogunwale | e23149f | 2015-03-06 15:39:44 -0800 | [diff] [blame] | 39 | import static com.android.server.am.TaskRecord.INVALID_TASK_ID; |
| 40 | |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 41 | import android.app.ActivityManager; |
| 42 | import android.app.AppGlobals; |
| 43 | import android.content.ComponentName; |
| 44 | import android.content.Intent; |
| 45 | import android.content.pm.ActivityInfo; |
| 46 | import android.content.pm.ApplicationInfo; |
| 47 | import android.content.pm.IPackageManager; |
| 48 | import android.content.pm.PackageManager; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 49 | import android.content.pm.ParceledListSlice; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 50 | import android.content.pm.UserInfo; |
| 51 | import android.content.res.Resources; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 52 | import android.graphics.Bitmap; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 53 | import android.graphics.Rect; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 54 | import android.os.Bundle; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 55 | import android.os.Environment; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 56 | import android.os.IBinder; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 57 | import android.os.RemoteException; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 58 | import android.os.SystemProperties; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 59 | import android.os.UserHandle; |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 60 | import android.text.TextUtils; |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 61 | import android.util.ArraySet; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 62 | import android.util.Slog; |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 63 | import android.util.SparseArray; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 64 | import android.util.SparseBooleanArray; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 65 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 66 | import com.android.internal.annotations.VisibleForTesting; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 67 | import com.android.server.am.TaskRecord.TaskActivitiesReport; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 68 | |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 69 | import com.google.android.collect.Sets; |
| 70 | |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 71 | import java.io.File; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 72 | import java.io.PrintWriter; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 73 | import java.util.ArrayList; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 74 | import java.util.Arrays; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 75 | import java.util.Collections; |
| 76 | import java.util.Comparator; |
| 77 | import java.util.HashMap; |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 78 | import java.util.Set; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 79 | import java.util.concurrent.TimeUnit; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 80 | |
| 81 | /** |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 82 | * Class for managing the recent tasks list. The list is ordered by most recent (index 0) to the |
| 83 | * least recent. |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 84 | * |
| 85 | * The trimming logic can be boiled down to the following. For recent task list with a number of |
| 86 | * tasks, the visible tasks are an interleaving subset of tasks that would normally be presented to |
| 87 | * the user. Non-visible tasks are not considered for trimming. Of the visible tasks, only a |
| 88 | * sub-range are presented to the user, based on the device type, last task active time, or other |
| 89 | * task state. Tasks that are not in the visible range and are not returnable from the SystemUI |
| 90 | * (considering the back stack) are considered trimmable. If the device does not support recent |
| 91 | * tasks, then trimming is completely disabled. |
| 92 | * |
| 93 | * eg. |
| 94 | * L = [TTTTTTTTTTTTTTTTTTTTTTTTTT] // list of tasks |
| 95 | * [VVV VV VVVV V V V ] // Visible tasks |
| 96 | * [RRR RR XXXX X X X ] // Visible range tasks, eg. if the device only shows 5 tasks, |
| 97 | * // 'X' tasks are trimmed. |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 98 | */ |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 99 | class RecentTasks { |
Wale Ogunwale | e23149f | 2015-03-06 15:39:44 -0800 | [diff] [blame] | 100 | private static final String TAG = TAG_WITH_CLASS_NAME ? "RecentTasks" : TAG_AM; |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 101 | private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS; |
| 102 | private static final String TAG_TASKS = TAG + POSTFIX_TASKS; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 103 | private static final boolean TRIMMED = true; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 104 | |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 105 | private static final int DEFAULT_INITIAL_CAPACITY = 5; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 106 | |
Winson | 6976f7b | 2016-05-03 14:58:12 -0700 | [diff] [blame] | 107 | // Whether or not to move all affiliated tasks to the front when one of the tasks is launched |
| 108 | private static final boolean MOVE_AFFILIATED_TASKS_TO_FRONT = false; |
| 109 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 110 | // Comparator to sort by taskId |
| 111 | private static final Comparator<TaskRecord> TASK_ID_COMPARATOR = |
| 112 | (lhs, rhs) -> rhs.taskId - lhs.taskId; |
| 113 | |
| 114 | // Placeholder variables to keep track of activities/apps that are no longer avialble while |
| 115 | // iterating through the recents list |
| 116 | private static final ActivityInfo NO_ACTIVITY_INFO_TOKEN = new ActivityInfo(); |
| 117 | private static final ApplicationInfo NO_APPLICATION_INFO_TOKEN = new ApplicationInfo(); |
| 118 | |
| 119 | /** |
| 120 | * Callbacks made when manipulating the list. |
| 121 | */ |
| 122 | interface Callbacks { |
| 123 | /** |
| 124 | * Called when a task is added to the recent tasks list. |
| 125 | */ |
| 126 | void onRecentTaskAdded(TaskRecord task); |
| 127 | |
| 128 | /** |
| 129 | * Called when a task is removed from the recent tasks list. |
| 130 | */ |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 131 | void onRecentTaskRemoved(TaskRecord task, boolean wasTrimmed); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 134 | /** |
| 135 | * Save recent tasks information across reboots. |
| 136 | */ |
| 137 | private final TaskPersister mTaskPersister; |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 138 | private final ActivityManagerService mService; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 139 | private final UserController mUserController; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 140 | |
| 141 | /** |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 142 | * Keeps track of the static recents package/component which is granted additional permissions |
| 143 | * to call recents-related APIs. |
| 144 | */ |
| 145 | private int mRecentsUid = -1; |
| 146 | private ComponentName mRecentsComponent = null; |
| 147 | |
| 148 | /** |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 149 | * Mapping of user id -> whether recent tasks have been loaded for that user. |
| 150 | */ |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 151 | private final SparseBooleanArray mUsersWithRecentsLoaded = new SparseBooleanArray( |
| 152 | DEFAULT_INITIAL_CAPACITY); |
| 153 | |
| 154 | /** |
| 155 | * Stores for each user task ids that are taken by tasks residing in persistent storage. These |
| 156 | * tasks may or may not currently be in memory. |
| 157 | */ |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 158 | private final SparseArray<SparseBooleanArray> mPersistedTaskIds = new SparseArray<>( |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 159 | DEFAULT_INITIAL_CAPACITY); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 160 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 161 | // List of all active recent tasks |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 162 | private final ArrayList<TaskRecord> mTasks = new ArrayList<>(); |
| 163 | private final ArrayList<Callbacks> mCallbacks = new ArrayList<>(); |
| 164 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 165 | // These values are generally loaded from resources, but can be set dynamically in the tests |
| 166 | private boolean mHasVisibleRecentTasks; |
| 167 | private int mGlobalMaxNumTasks; |
| 168 | private int mMinNumVisibleTasks; |
| 169 | private int mMaxNumVisibleTasks; |
| 170 | private long mActiveTasksSessionDurationMs; |
| 171 | |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 172 | // Mainly to avoid object recreation on multiple calls. |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 173 | private final ArrayList<TaskRecord> mTmpRecents = new ArrayList<>(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 174 | private final HashMap<ComponentName, ActivityInfo> mTmpAvailActCache = new HashMap<>(); |
| 175 | private final HashMap<String, ApplicationInfo> mTmpAvailAppCache = new HashMap<>(); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 176 | private final SparseBooleanArray mTmpQuietProfileUserIds = new SparseBooleanArray(); |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 177 | private final TaskActivitiesReport mTmpReport = new TaskActivitiesReport(); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 178 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 179 | @VisibleForTesting |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 180 | RecentTasks(ActivityManagerService service, TaskPersister taskPersister, |
| 181 | UserController userController) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 182 | mService = service; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 183 | mUserController = userController; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 184 | mTaskPersister = taskPersister; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 185 | mGlobalMaxNumTasks = ActivityManager.getMaxRecentTasksStatic(); |
| 186 | mHasVisibleRecentTasks = true; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | RecentTasks(ActivityManagerService service, ActivityStackSupervisor stackSupervisor) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 190 | final File systemDir = Environment.getDataSystemDirectory(); |
| 191 | final Resources res = service.mContext.getResources(); |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 192 | mService = service; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 193 | mUserController = service.mUserController; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 194 | mTaskPersister = new TaskPersister(systemDir, stackSupervisor, service, this); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 195 | mGlobalMaxNumTasks = ActivityManager.getMaxRecentTasksStatic(); |
| 196 | mHasVisibleRecentTasks = res.getBoolean(com.android.internal.R.bool.config_hasRecents); |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 197 | loadParametersFromResources(res); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @VisibleForTesting |
| 201 | void setParameters(int minNumVisibleTasks, int maxNumVisibleTasks, |
| 202 | long activeSessionDurationMs) { |
| 203 | mMinNumVisibleTasks = minNumVisibleTasks; |
| 204 | mMaxNumVisibleTasks = maxNumVisibleTasks; |
| 205 | mActiveTasksSessionDurationMs = activeSessionDurationMs; |
| 206 | } |
| 207 | |
| 208 | @VisibleForTesting |
| 209 | void setGlobalMaxNumTasks(int globalMaxNumTasks) { |
| 210 | mGlobalMaxNumTasks = globalMaxNumTasks; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Loads the parameters from the system resources. |
| 215 | */ |
| 216 | @VisibleForTesting |
| 217 | void loadParametersFromResources(Resources res) { |
| 218 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 219 | mMinNumVisibleTasks = res.getInteger( |
| 220 | com.android.internal.R.integer.config_minNumVisibleRecentTasks_lowRam); |
| 221 | mMaxNumVisibleTasks = res.getInteger( |
| 222 | com.android.internal.R.integer.config_maxNumVisibleRecentTasks_lowRam); |
| 223 | } else if (SystemProperties.getBoolean("ro.recents.grid", false)) { |
| 224 | mMinNumVisibleTasks = res.getInteger( |
| 225 | com.android.internal.R.integer.config_minNumVisibleRecentTasks_grid); |
| 226 | mMaxNumVisibleTasks = res.getInteger( |
| 227 | com.android.internal.R.integer.config_maxNumVisibleRecentTasks_grid); |
| 228 | } else { |
| 229 | mMinNumVisibleTasks = res.getInteger( |
| 230 | com.android.internal.R.integer.config_minNumVisibleRecentTasks); |
| 231 | mMaxNumVisibleTasks = res.getInteger( |
| 232 | com.android.internal.R.integer.config_maxNumVisibleRecentTasks); |
| 233 | } |
| 234 | final int sessionDurationHrs = res.getInteger( |
| 235 | com.android.internal.R.integer.config_activeTaskDurationHours); |
| 236 | mActiveTasksSessionDurationMs = (sessionDurationHrs > 0) |
| 237 | ? TimeUnit.HOURS.toMillis(sessionDurationHrs) |
| 238 | : -1; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 241 | /** |
| 242 | * Loads the static recents component. This is called after the system is ready, but before |
| 243 | * any dependent services (like SystemUI) is started. |
| 244 | */ |
| 245 | void loadRecentsComponent(Resources res) { |
| 246 | final String rawRecentsComponent = res.getString( |
| 247 | com.android.internal.R.string.config_recentsComponentName); |
| 248 | if (TextUtils.isEmpty(rawRecentsComponent)) { |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | final ComponentName cn = ComponentName.unflattenFromString(rawRecentsComponent); |
| 253 | if (cn != null) { |
| 254 | try { |
| 255 | final ApplicationInfo appInfo = AppGlobals.getPackageManager() |
| 256 | .getApplicationInfo(cn.getPackageName(), 0, mService.mContext.getUserId()); |
| 257 | if (appInfo != null) { |
| 258 | mRecentsUid = appInfo.uid; |
| 259 | mRecentsComponent = cn; |
| 260 | } |
| 261 | } catch (RemoteException e) { |
| 262 | Slog.w(TAG, "Could not load application info for recents component: " + cn); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @return whether the current caller has the same uid as the recents component. |
| 269 | */ |
| 270 | boolean isCallerRecents(int callingUid) { |
| 271 | return UserHandle.isSameApp(callingUid, mRecentsUid); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * @return whether the given component is the recents component and shares the same uid as the |
| 276 | * recents component. |
| 277 | */ |
| 278 | boolean isRecentsComponent(ComponentName cn, int uid) { |
| 279 | return cn.equals(mRecentsComponent) && UserHandle.isSameApp(uid, mRecentsUid); |
| 280 | } |
| 281 | |
Winson Chung | ec1ef09 | 2017-10-25 16:22:34 -0700 | [diff] [blame] | 282 | /** |
Winson Chung | c167427 | 2018-02-21 10:15:17 -0800 | [diff] [blame] | 283 | * @return whether the home app is also the active handler of recent tasks. |
| 284 | */ |
| 285 | boolean isRecentsComponentHomeActivity(int userId) { |
| 286 | final ComponentName defaultHomeActivity = mService.getPackageManagerInternalLocked() |
| 287 | .getDefaultHomeActivity(userId); |
Alice Sheng | 1de98fc | 2018-03-16 09:45:58 -0700 | [diff] [blame] | 288 | return defaultHomeActivity != null && mRecentsComponent != null && |
Winson Chung | c167427 | 2018-02-21 10:15:17 -0800 | [diff] [blame] | 289 | defaultHomeActivity.getPackageName().equals(mRecentsComponent.getPackageName()); |
| 290 | } |
| 291 | |
| 292 | /** |
Winson Chung | ec1ef09 | 2017-10-25 16:22:34 -0700 | [diff] [blame] | 293 | * @return the recents component. |
| 294 | */ |
| 295 | ComponentName getRecentsComponent() { |
| 296 | return mRecentsComponent; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * @return the uid for the recents component. |
| 301 | */ |
| 302 | int getRecentsComponentUid() { |
| 303 | return mRecentsUid; |
| 304 | } |
| 305 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 306 | void registerCallback(Callbacks callback) { |
| 307 | mCallbacks.add(callback); |
| 308 | } |
| 309 | |
| 310 | void unregisterCallback(Callbacks callback) { |
| 311 | mCallbacks.remove(callback); |
| 312 | } |
| 313 | |
| 314 | private void notifyTaskAdded(TaskRecord task) { |
| 315 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 316 | mCallbacks.get(i).onRecentTaskAdded(task); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | private void notifyTaskRemoved(TaskRecord task, boolean wasTrimmed) { |
| 321 | for (int i = 0; i < mCallbacks.size(); i++) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 322 | mCallbacks.get(i).onRecentTaskRemoved(task, wasTrimmed); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 323 | } |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /** |
Suprabh Shukla | f5bf0cb | 2016-01-19 17:42:03 -0800 | [diff] [blame] | 327 | * Loads the persistent recentTasks for {@code userId} into this list from persistent storage. |
| 328 | * Does nothing if they are already loaded. |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 329 | * |
| 330 | * @param userId the user Id |
| 331 | */ |
| 332 | void loadUserRecentsLocked(int userId) { |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 333 | if (mUsersWithRecentsLoaded.get(userId)) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 334 | // User already loaded, return early |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | |
| 338 | // Load the task ids if not loaded. |
| 339 | loadPersistedTaskIdsForUserLocked(userId); |
| 340 | |
| 341 | // Check if any tasks are added before recents is loaded |
| 342 | final SparseBooleanArray preaddedTasks = new SparseBooleanArray(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 343 | for (final TaskRecord task : mTasks) { |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 344 | if (task.userId == userId && shouldPersistTaskLocked(task)) { |
| 345 | preaddedTasks.put(task.taskId, true); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | Slog.i(TAG, "Loading recents for user " + userId + " into memory."); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 350 | mTasks.addAll(mTaskPersister.restoreTasksForUserLocked(userId, preaddedTasks)); |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 351 | cleanupLocked(userId); |
| 352 | mUsersWithRecentsLoaded.put(userId, true); |
| 353 | |
| 354 | // If we have tasks added before loading recents, we need to update persistent task IDs. |
| 355 | if (preaddedTasks.size() > 0) { |
| 356 | syncPersistentTaskIdsLocked(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 360 | private void loadPersistedTaskIdsForUserLocked(int userId) { |
| 361 | // An empty instead of a null set here means that no persistent taskIds were present |
| 362 | // on file when we loaded them. |
| 363 | if (mPersistedTaskIds.get(userId) == null) { |
| 364 | mPersistedTaskIds.put(userId, mTaskPersister.loadPersistedTaskIdsForUser(userId)); |
Fyodor Kupolov | c4b46dd | 2016-03-17 12:57:36 -0700 | [diff] [blame] | 365 | Slog.i(TAG, "Loaded persisted task ids for user " + userId); |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 369 | /** |
| 370 | * @return whether the {@param taskId} is currently in use for the given user. |
| 371 | */ |
| 372 | boolean containsTaskId(int taskId, int userId) { |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 373 | loadPersistedTaskIdsForUserLocked(userId); |
| 374 | return mPersistedTaskIds.get(userId).get(taskId); |
| 375 | } |
| 376 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 377 | /** |
| 378 | * @return all the task ids for the user with the given {@param userId}. |
| 379 | */ |
| 380 | SparseBooleanArray getTaskIdsForUser(int userId) { |
| 381 | loadPersistedTaskIdsForUserLocked(userId); |
| 382 | return mPersistedTaskIds.get(userId); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Kicks off the task persister to write any pending tasks to disk. |
| 387 | */ |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 388 | void notifyTaskPersisterLocked(TaskRecord task, boolean flush) { |
Andrii Kulian | 02b7a83 | 2016-10-06 23:11:56 -0700 | [diff] [blame] | 389 | final ActivityStack stack = task != null ? task.getStack() : null; |
Matthew Ng | ae1ff4f | 2016-11-10 15:49:14 -0800 | [diff] [blame] | 390 | if (stack != null && stack.isHomeOrRecentsStack()) { |
| 391 | // Never persist the home or recents stack. |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 392 | return; |
| 393 | } |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 394 | syncPersistentTaskIdsLocked(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 395 | mTaskPersister.wakeup(task, flush); |
| 396 | } |
| 397 | |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 398 | private void syncPersistentTaskIdsLocked() { |
| 399 | for (int i = mPersistedTaskIds.size() - 1; i >= 0; i--) { |
| 400 | int userId = mPersistedTaskIds.keyAt(i); |
| 401 | if (mUsersWithRecentsLoaded.get(userId)) { |
| 402 | // Recents are loaded only after task ids are loaded. Therefore, the set of taskids |
| 403 | // referenced here should not be null. |
| 404 | mPersistedTaskIds.valueAt(i).clear(); |
| 405 | } |
| 406 | } |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 407 | for (int i = mTasks.size() - 1; i >= 0; i--) { |
| 408 | final TaskRecord task = mTasks.get(i); |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 409 | if (shouldPersistTaskLocked(task)) { |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 410 | // Set of persisted taskIds for task.userId should not be null here |
Fyodor Kupolov | c4b46dd | 2016-03-17 12:57:36 -0700 | [diff] [blame] | 411 | // TODO Investigate why it can happen. For now initialize with an empty set |
| 412 | if (mPersistedTaskIds.get(task.userId) == null) { |
| 413 | Slog.wtf(TAG, "No task ids found for userId " + task.userId + ". task=" + task |
| 414 | + " mPersistedTaskIds=" + mPersistedTaskIds); |
| 415 | mPersistedTaskIds.put(task.userId, new SparseBooleanArray()); |
| 416 | } |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 417 | mPersistedTaskIds.get(task.userId).put(task.taskId, true); |
| 418 | } |
| 419 | } |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 422 | private static boolean shouldPersistTaskLocked(TaskRecord task) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 423 | final ActivityStack stack = task.getStack(); |
Daichi Hirono | 4cb941e | 2017-03-31 14:30:41 +0900 | [diff] [blame] | 424 | return task.isPersistable && (stack == null || !stack.isHomeOrRecentsStack()); |
| 425 | } |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 426 | |
| 427 | void onSystemReadyLocked() { |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 428 | loadRecentsComponent(mService.mContext.getResources()); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 429 | mTasks.clear(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 430 | mTaskPersister.startPersisting(); |
| 431 | } |
| 432 | |
| 433 | Bitmap getTaskDescriptionIcon(String path) { |
| 434 | return mTaskPersister.getTaskDescriptionIcon(path); |
| 435 | } |
| 436 | |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 437 | void saveImage(Bitmap image, String path) { |
| 438 | mTaskPersister.saveImage(image, path); |
| 439 | } |
| 440 | |
| 441 | void flush() { |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 442 | synchronized (mService) { |
| 443 | syncPersistentTaskIdsLocked(); |
| 444 | } |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 445 | mTaskPersister.flush(); |
| 446 | } |
| 447 | |
| 448 | /** |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 449 | * 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] | 450 | * |
| 451 | * @return an array of userIds. |
| 452 | */ |
| 453 | int[] usersWithRecentsLoadedLocked() { |
| 454 | int[] usersWithRecentsLoaded = new int[mUsersWithRecentsLoaded.size()]; |
| 455 | int len = 0; |
| 456 | for (int i = 0; i < usersWithRecentsLoaded.length; i++) { |
| 457 | int userId = mUsersWithRecentsLoaded.keyAt(i); |
| 458 | if (mUsersWithRecentsLoaded.valueAt(i)) { |
| 459 | usersWithRecentsLoaded[len++] = userId; |
| 460 | } |
| 461 | } |
| 462 | if (len < usersWithRecentsLoaded.length) { |
| 463 | // should never happen. |
| 464 | return Arrays.copyOf(usersWithRecentsLoaded, len); |
| 465 | } |
| 466 | return usersWithRecentsLoaded; |
| 467 | } |
| 468 | |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 469 | /** |
| 470 | * Removes recent tasks and any other state kept in memory for the passed in user. Does not |
| 471 | * touch the information present on persistent storage. |
| 472 | * |
| 473 | * @param userId the id of the user |
| 474 | */ |
| 475 | void unloadUserDataFromMemoryLocked(int userId) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 476 | if (mUsersWithRecentsLoaded.get(userId)) { |
| 477 | Slog.i(TAG, "Unloading recents for user " + userId + " from memory."); |
| 478 | mUsersWithRecentsLoaded.delete(userId); |
| 479 | removeTasksForUserLocked(userId); |
| 480 | } |
Suprabh Shukla | 4bccb46 | 2016-02-10 18:45:12 -0800 | [diff] [blame] | 481 | mPersistedTaskIds.delete(userId); |
| 482 | mTaskPersister.unloadUserDataFromMemory(userId); |
| 483 | } |
| 484 | |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 485 | /** Remove recent tasks for a user. */ |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 486 | private void removeTasksForUserLocked(int userId) { |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 487 | if(userId <= 0) { |
| 488 | Slog.i(TAG, "Can't remove recent task on user " + userId); |
| 489 | return; |
| 490 | } |
| 491 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 492 | for (int i = mTasks.size() - 1; i >= 0; --i) { |
| 493 | TaskRecord tr = mTasks.get(i); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 494 | if (tr.userId == userId) { |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 495 | if(DEBUG_TASKS) Slog.i(TAG_TASKS, |
| 496 | "remove RecentTask " + tr + " when finishing user" + userId); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 497 | remove(mTasks.get(i)); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 498 | } |
| 499 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 502 | void onPackagesSuspendedChanged(String[] packages, boolean suspended, int userId) { |
| 503 | final Set<String> packageNames = Sets.newHashSet(packages); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 504 | for (int i = mTasks.size() - 1; i >= 0; --i) { |
| 505 | final TaskRecord tr = mTasks.get(i); |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 506 | if (tr.realActivity != null |
| 507 | && packageNames.contains(tr.realActivity.getPackageName()) |
| 508 | && tr.userId == userId |
| 509 | && tr.realActivitySuspended != suspended) { |
| 510 | tr.realActivitySuspended = suspended; |
Suprabh Shukla | 021b57a | 2018-03-08 18:21:50 -0800 | [diff] [blame] | 511 | if (suspended) { |
| 512 | mService.mStackSupervisor.removeTaskByIdLocked(tr.taskId, false, |
| 513 | REMOVE_FROM_RECENTS, "suspended-package"); |
| 514 | } |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 515 | notifyTaskPersisterLocked(tr, false); |
| 516 | } |
| 517 | } |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 518 | } |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 519 | |
Charles He | d62f965 | 2017-11-01 10:05:51 +0000 | [diff] [blame] | 520 | void onLockTaskModeStateChanged(int lockTaskModeState, int userId) { |
| 521 | if (lockTaskModeState != ActivityManager.LOCK_TASK_MODE_LOCKED) { |
| 522 | return; |
| 523 | } |
| 524 | for (int i = mTasks.size() - 1; i >= 0; --i) { |
| 525 | final TaskRecord tr = mTasks.get(i); |
| 526 | if (tr.userId == userId && !mService.mLockTaskController.isTaskWhitelisted(tr)) { |
| 527 | remove(tr); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 532 | void removeTasksByPackageName(String packageName, int userId) { |
Winson Chung | 6308584 | 2017-10-13 17:18:58 -0700 | [diff] [blame] | 533 | for (int i = mTasks.size() - 1; i >= 0; --i) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 534 | final TaskRecord tr = mTasks.get(i); |
| 535 | final String taskPackageName = |
| 536 | tr.getBaseIntent().getComponent().getPackageName(); |
| 537 | if (tr.userId != userId) return; |
| 538 | if (!taskPackageName.equals(packageName)) return; |
| 539 | |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 540 | mService.mStackSupervisor.removeTaskByIdLocked(tr.taskId, true, REMOVE_FROM_RECENTS, |
| 541 | "remove-package-task"); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | void cleanupDisabledPackageTasksLocked(String packageName, Set<String> filterByClasses, |
| 546 | int userId) { |
Winson Chung | 6308584 | 2017-10-13 17:18:58 -0700 | [diff] [blame] | 547 | for (int i = mTasks.size() - 1; i >= 0; --i) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 548 | final TaskRecord tr = mTasks.get(i); |
| 549 | if (userId != UserHandle.USER_ALL && tr.userId != userId) { |
| 550 | continue; |
| 551 | } |
| 552 | |
| 553 | ComponentName cn = tr.intent.getComponent(); |
| 554 | final boolean sameComponent = cn != null && cn.getPackageName().equals(packageName) |
| 555 | && (filterByClasses == null || filterByClasses.contains(cn.getClassName())); |
| 556 | if (sameComponent) { |
| 557 | mService.mStackSupervisor.removeTaskByIdLocked(tr.taskId, false, |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 558 | REMOVE_FROM_RECENTS, "disabled-package"); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 559 | } |
| 560 | } |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 563 | /** |
| 564 | * Update the recent tasks lists: make sure tasks should still be here (their |
| 565 | * applications / activities still exist), update their availability, fix-up ordering |
| 566 | * of affiliations. |
| 567 | */ |
| 568 | void cleanupLocked(int userId) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 569 | int recentsCount = mTasks.size(); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 570 | if (recentsCount == 0) { |
| 571 | // Happens when called from the packagemanager broadcast before boot, |
| 572 | // or just any empty list. |
| 573 | return; |
| 574 | } |
| 575 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 576 | // Clear the temp lists |
| 577 | mTmpAvailActCache.clear(); |
| 578 | mTmpAvailAppCache.clear(); |
| 579 | |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 580 | final IPackageManager pm = AppGlobals.getPackageManager(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 581 | for (int i = recentsCount - 1; i >= 0; i--) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 582 | final TaskRecord task = mTasks.get(i); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 583 | if (userId != UserHandle.USER_ALL && task.userId != userId) { |
| 584 | // Only look at tasks for the user ID of interest. |
| 585 | continue; |
| 586 | } |
| 587 | if (task.autoRemoveRecents && task.getTopActivity() == null) { |
| 588 | // This situation is broken, and we should just get rid of it now. |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 589 | mTasks.remove(i); |
| 590 | notifyTaskRemoved(task, !TRIMMED); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 591 | Slog.w(TAG, "Removing auto-remove without activity: " + task); |
| 592 | continue; |
| 593 | } |
| 594 | // Check whether this activity is currently available. |
| 595 | if (task.realActivity != null) { |
| 596 | ActivityInfo ai = mTmpAvailActCache.get(task.realActivity); |
| 597 | if (ai == null) { |
| 598 | try { |
| 599 | // At this first cut, we're only interested in |
| 600 | // activities that are fully runnable based on |
| 601 | // current system state. |
| 602 | ai = pm.getActivityInfo(task.realActivity, |
Philip P. Moltmann | cff8f0f | 2018-03-27 12:51:51 -0700 | [diff] [blame] | 603 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING |
| 604 | | ActivityManagerService.STOCK_PM_FLAGS, userId); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 605 | } catch (RemoteException e) { |
| 606 | // Will never happen. |
| 607 | continue; |
| 608 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 609 | if (ai == null) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 610 | ai = NO_ACTIVITY_INFO_TOKEN; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 611 | } |
| 612 | mTmpAvailActCache.put(task.realActivity, ai); |
| 613 | } |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 614 | if (ai == NO_ACTIVITY_INFO_TOKEN) { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 615 | // This could be either because the activity no longer exists, or the |
| 616 | // app is temporarily gone. For the former we want to remove the recents |
| 617 | // entry; for the latter we want to mark it as unavailable. |
| 618 | ApplicationInfo app = mTmpAvailAppCache |
| 619 | .get(task.realActivity.getPackageName()); |
| 620 | if (app == null) { |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 621 | try { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 622 | app = pm.getApplicationInfo(task.realActivity.getPackageName(), |
| 623 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 624 | } catch (RemoteException e) { |
| 625 | // Will never happen. |
| 626 | continue; |
| 627 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 628 | if (app == null) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 629 | app = NO_APPLICATION_INFO_TOKEN; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 630 | } |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 631 | mTmpAvailAppCache.put(task.realActivity.getPackageName(), app); |
| 632 | } |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 633 | if (app == NO_APPLICATION_INFO_TOKEN |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 634 | || (app.flags & ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 635 | // Doesn't exist any more! Good-bye. |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 636 | mTasks.remove(i); |
| 637 | notifyTaskRemoved(task, !TRIMMED); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 638 | Slog.w(TAG, "Removing no longer valid recent: " + task); |
| 639 | continue; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 640 | } else { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 641 | // Otherwise just not available for now. |
| 642 | if (DEBUG_RECENTS && task.isAvailable) Slog.d(TAG_RECENTS, |
| 643 | "Making recent unavailable: " + task); |
| 644 | task.isAvailable = false; |
| 645 | } |
| 646 | } else { |
| 647 | if (!ai.enabled || !ai.applicationInfo.enabled |
| 648 | || (ai.applicationInfo.flags |
| 649 | & ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 650 | if (DEBUG_RECENTS && task.isAvailable) Slog.d(TAG_RECENTS, |
| 651 | "Making recent unavailable: " + task |
| 652 | + " (enabled=" + ai.enabled + "/" |
| 653 | + ai.applicationInfo.enabled |
| 654 | + " flags=" |
| 655 | + Integer.toHexString(ai.applicationInfo.flags) |
| 656 | + ")"); |
| 657 | task.isAvailable = false; |
| 658 | } else { |
| 659 | if (DEBUG_RECENTS && !task.isAvailable) Slog.d(TAG_RECENTS, |
| 660 | "Making recent available: " + task); |
| 661 | task.isAvailable = true; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | // Verify the affiliate chain for each task. |
| 668 | int i = 0; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 669 | recentsCount = mTasks.size(); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 670 | while (i < recentsCount) { |
| 671 | i = processNextAffiliateChainLocked(i); |
| 672 | } |
| 673 | // recent tasks are now in sorted, affiliated order. |
| 674 | } |
| 675 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 676 | /** |
| 677 | * @return whether the given {@param task} can be added to the list without causing another |
| 678 | * task to be trimmed as a result of that add. |
| 679 | */ |
| 680 | private boolean canAddTaskWithoutTrim(TaskRecord task) { |
Winson Chung | 079221f | 2017-12-13 17:43:34 -0800 | [diff] [blame] | 681 | return findRemoveIndexForAddTask(task) == -1; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Returns the list of {@link ActivityManager.AppTask}s. |
| 686 | */ |
| 687 | ArrayList<IBinder> getAppTasksList(int callingUid, String callingPackage) { |
| 688 | final ArrayList<IBinder> list = new ArrayList<>(); |
| 689 | final int size = mTasks.size(); |
| 690 | for (int i = 0; i < size; i++) { |
| 691 | final TaskRecord tr = mTasks.get(i); |
| 692 | // Skip tasks that do not match the caller. We don't need to verify |
| 693 | // callingPackage, because we are also limiting to callingUid and know |
| 694 | // that will limit to the correct security sandbox. |
| 695 | if (tr.effectiveUid != callingUid) { |
| 696 | continue; |
| 697 | } |
| 698 | Intent intent = tr.getBaseIntent(); |
| 699 | if (intent == null || !callingPackage.equals(intent.getComponent().getPackageName())) { |
| 700 | continue; |
| 701 | } |
| 702 | ActivityManager.RecentTaskInfo taskInfo = createRecentTaskInfo(tr); |
| 703 | AppTaskImpl taskImpl = new AppTaskImpl(mService, taskInfo.persistentId, callingUid); |
| 704 | list.add(taskImpl.asBinder()); |
| 705 | } |
| 706 | return list; |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * @return the list of recent tasks for presentation. |
| 711 | */ |
| 712 | ParceledListSlice<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, int flags, |
| 713 | boolean getTasksAllowed, boolean getDetailedTasks, int userId, int callingUid) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 714 | final boolean withExcluded = (flags & RECENT_WITH_EXCLUDED) != 0; |
| 715 | |
| 716 | if (!mService.isUserRunning(userId, FLAG_AND_UNLOCKED)) { |
| 717 | Slog.i(TAG, "user " + userId + " is still locked. Cannot load recents"); |
| 718 | return ParceledListSlice.emptyList(); |
| 719 | } |
| 720 | loadUserRecentsLocked(userId); |
| 721 | |
Winson Chung | 5fa3975 | 2017-10-04 14:50:15 -0700 | [diff] [blame] | 722 | final Set<Integer> includedUsers = mUserController.getProfileIds(userId); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 723 | includedUsers.add(Integer.valueOf(userId)); |
| 724 | |
| 725 | final ArrayList<ActivityManager.RecentTaskInfo> res = new ArrayList<>(); |
| 726 | final int size = mTasks.size(); |
Winson Chung | fb44d21 | 2017-10-04 11:39:10 -0700 | [diff] [blame] | 727 | int numVisibleTasks = 0; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 728 | for (int i = 0; i < size; i++) { |
| 729 | final TaskRecord tr = mTasks.get(i); |
Winson Chung | fb44d21 | 2017-10-04 11:39:10 -0700 | [diff] [blame] | 730 | |
| 731 | if (isVisibleRecentTask(tr)) { |
| 732 | numVisibleTasks++; |
| 733 | if (isInVisibleRange(tr, numVisibleTasks)) { |
| 734 | // Fall through |
| 735 | } else { |
| 736 | // Not in visible range |
| 737 | continue; |
| 738 | } |
| 739 | } else { |
| 740 | // Not visible |
| 741 | continue; |
| 742 | } |
| 743 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 744 | // Skip remaining tasks once we reach the requested size |
| 745 | if (res.size() >= maxNum) { |
| 746 | continue; |
| 747 | } |
| 748 | |
| 749 | // Only add calling user or related users recent tasks |
| 750 | if (!includedUsers.contains(Integer.valueOf(tr.userId))) { |
| 751 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Skipping, not user: " + tr); |
| 752 | continue; |
| 753 | } |
| 754 | |
| 755 | if (tr.realActivitySuspended) { |
| 756 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Skipping, activity suspended: " + tr); |
| 757 | continue; |
| 758 | } |
| 759 | |
| 760 | // Return the entry if desired by the caller. We always return |
| 761 | // the first entry, because callers always expect this to be the |
| 762 | // foreground app. We may filter others if the caller has |
| 763 | // not supplied RECENT_WITH_EXCLUDED and there is some reason |
| 764 | // we should exclude the entry. |
| 765 | |
| 766 | if (i == 0 |
| 767 | || withExcluded |
| 768 | || (tr.intent == null) |
| 769 | || ((tr.intent.getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) |
| 770 | == 0)) { |
| 771 | if (!getTasksAllowed) { |
| 772 | // If the caller doesn't have the GET_TASKS permission, then only |
| 773 | // allow them to see a small subset of tasks -- their own and home. |
| 774 | if (!tr.isActivityTypeHome() && tr.effectiveUid != callingUid) { |
| 775 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Skipping, not allowed: " + tr); |
| 776 | continue; |
| 777 | } |
| 778 | } |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 779 | if (tr.autoRemoveRecents && tr.getTopActivity() == null) { |
| 780 | // Don't include auto remove tasks that are finished or finishing. |
| 781 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 782 | "Skipping, auto-remove without activity: " + tr); |
| 783 | continue; |
| 784 | } |
| 785 | if ((flags & RECENT_IGNORE_UNAVAILABLE) != 0 && !tr.isAvailable) { |
| 786 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 787 | "Skipping, unavail real act: " + tr); |
| 788 | continue; |
| 789 | } |
| 790 | |
| 791 | if (!tr.mUserSetupComplete) { |
| 792 | // Don't include task launched while user is not done setting-up. |
| 793 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 794 | "Skipping, user setup not complete: " + tr); |
| 795 | continue; |
| 796 | } |
| 797 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 798 | final ActivityManager.RecentTaskInfo rti = createRecentTaskInfo(tr); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 799 | if (!getDetailedTasks) { |
| 800 | rti.baseIntent.replaceExtras((Bundle)null); |
| 801 | } |
| 802 | |
| 803 | res.add(rti); |
| 804 | } |
| 805 | } |
| 806 | return new ParceledListSlice<>(res); |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * @return the list of persistable task ids. |
| 811 | */ |
| 812 | void getPersistableTaskIds(ArraySet<Integer> persistentTaskIds) { |
| 813 | final int size = mTasks.size(); |
| 814 | for (int i = 0; i < size; i++) { |
| 815 | final TaskRecord task = mTasks.get(i); |
| 816 | if (TaskPersister.DEBUG) Slog.d(TAG, "LazyTaskWriter: task=" + task |
| 817 | + " persistable=" + task.isPersistable); |
| 818 | final ActivityStack stack = task.getStack(); |
| 819 | if ((task.isPersistable || task.inRecents) |
| 820 | && (stack == null || !stack.isHomeOrRecentsStack())) { |
| 821 | if (TaskPersister.DEBUG) Slog.d(TAG, "adding to persistentTaskIds task=" + task); |
| 822 | persistentTaskIds.add(task.taskId); |
| 823 | } else { |
| 824 | if (TaskPersister.DEBUG) Slog.d(TAG, "omitting from persistentTaskIds task=" |
| 825 | + task); |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 830 | @VisibleForTesting |
| 831 | ArrayList<TaskRecord> getRawTasks() { |
| 832 | return mTasks; |
| 833 | } |
| 834 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 835 | /** |
Vadim Tryshev | 593e956 | 2018-03-08 17:15:45 -0800 | [diff] [blame] | 836 | * @return ids of tasks that are presented in Recents UI. |
| 837 | */ |
| 838 | SparseBooleanArray getRecentTaskIds() { |
| 839 | final SparseBooleanArray res = new SparseBooleanArray(); |
| 840 | final int size = mTasks.size(); |
| 841 | int numVisibleTasks = 0; |
| 842 | for (int i = 0; i < size; i++) { |
| 843 | final TaskRecord tr = mTasks.get(i); |
| 844 | if (isVisibleRecentTask(tr)) { |
| 845 | numVisibleTasks++; |
| 846 | if (isInVisibleRange(tr, numVisibleTasks)) { |
| 847 | res.put(tr.taskId, true); |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | return res; |
| 852 | } |
| 853 | |
| 854 | /** |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 855 | * @return the task in the task list with the given {@param id} if one exists. |
| 856 | */ |
| 857 | TaskRecord getTask(int id) { |
| 858 | final int recentsCount = mTasks.size(); |
| 859 | for (int i = 0; i < recentsCount; i++) { |
| 860 | TaskRecord tr = mTasks.get(i); |
| 861 | if (tr.taskId == id) { |
| 862 | return tr; |
| 863 | } |
| 864 | } |
| 865 | return null; |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * Add a new task to the recent tasks list. |
| 870 | */ |
| 871 | void add(TaskRecord task) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 872 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "add: task=" + task); |
| 873 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 874 | final boolean isAffiliated = task.mAffiliatedTaskId != task.taskId |
| 875 | || task.mNextAffiliateTaskId != INVALID_TASK_ID |
| 876 | || task.mPrevAffiliateTaskId != INVALID_TASK_ID; |
| 877 | |
| 878 | int recentsCount = mTasks.size(); |
| 879 | // Quick case: never add voice sessions. |
| 880 | // TODO: VI what about if it's just an activity? |
| 881 | // Probably nothing to do here |
| 882 | if (task.voiceSession != null) { |
| 883 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 884 | "addRecent: not adding voice interaction " + task); |
| 885 | return; |
| 886 | } |
| 887 | // Another quick case: check if the top-most recent task is the same. |
| 888 | if (!isAffiliated && recentsCount > 0 && mTasks.get(0) == task) { |
| 889 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: already at top: " + task); |
| 890 | return; |
| 891 | } |
| 892 | // Another quick case: check if this is part of a set of affiliated |
| 893 | // tasks that are at the top. |
| 894 | if (isAffiliated && recentsCount > 0 && task.inRecents |
| 895 | && task.mAffiliatedTaskId == mTasks.get(0).mAffiliatedTaskId) { |
| 896 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: affiliated " + mTasks.get(0) |
| 897 | + " at top when adding " + task); |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | boolean needAffiliationFix = false; |
| 902 | |
| 903 | // Slightly less quick case: the task is already in recents, so all we need |
| 904 | // to do is move it. |
| 905 | if (task.inRecents) { |
| 906 | int taskIndex = mTasks.indexOf(task); |
| 907 | if (taskIndex >= 0) { |
| 908 | if (!isAffiliated || !MOVE_AFFILIATED_TASKS_TO_FRONT) { |
| 909 | // Simple case: this is not an affiliated task, so we just move it to the front. |
| 910 | mTasks.remove(taskIndex); |
| 911 | mTasks.add(0, task); |
| 912 | notifyTaskPersisterLocked(task, false); |
| 913 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: moving to top " + task |
| 914 | + " from " + taskIndex); |
| 915 | return; |
| 916 | } else { |
| 917 | // More complicated: need to keep all affiliated tasks together. |
| 918 | if (moveAffiliatedTasksToFront(task, taskIndex)) { |
| 919 | // All went well. |
| 920 | return; |
| 921 | } |
| 922 | |
| 923 | // Uh oh... something bad in the affiliation chain, try to rebuild |
| 924 | // everything and then go through our general path of adding a new task. |
| 925 | needAffiliationFix = true; |
| 926 | } |
| 927 | } else { |
| 928 | Slog.wtf(TAG, "Task with inRecent not in recents: " + task); |
| 929 | needAffiliationFix = true; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: trimming tasks for " + task); |
Winson Chung | 079221f | 2017-12-13 17:43:34 -0800 | [diff] [blame] | 934 | removeForAddTask(task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 935 | |
| 936 | task.inRecents = true; |
| 937 | if (!isAffiliated || needAffiliationFix) { |
| 938 | // If this is a simple non-affiliated task, or we had some failure trying to |
| 939 | // handle it as part of an affilated task, then just place it at the top. |
| 940 | mTasks.add(0, task); |
| 941 | notifyTaskAdded(task); |
| 942 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: adding " + task); |
| 943 | } else if (isAffiliated) { |
| 944 | // If this is a new affiliated task, then move all of the affiliated tasks |
| 945 | // to the front and insert this new one. |
| 946 | TaskRecord other = task.mNextAffiliate; |
| 947 | if (other == null) { |
| 948 | other = task.mPrevAffiliate; |
| 949 | } |
| 950 | if (other != null) { |
| 951 | int otherIndex = mTasks.indexOf(other); |
| 952 | if (otherIndex >= 0) { |
| 953 | // Insert new task at appropriate location. |
| 954 | int taskIndex; |
| 955 | if (other == task.mNextAffiliate) { |
| 956 | // We found the index of our next affiliation, which is who is |
| 957 | // before us in the list, so add after that point. |
| 958 | taskIndex = otherIndex+1; |
| 959 | } else { |
| 960 | // We found the index of our previous affiliation, which is who is |
| 961 | // after us in the list, so add at their position. |
| 962 | taskIndex = otherIndex; |
| 963 | } |
| 964 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 965 | "addRecent: new affiliated task added at " + taskIndex + ": " + task); |
| 966 | mTasks.add(taskIndex, task); |
| 967 | notifyTaskAdded(task); |
| 968 | |
| 969 | // Now move everything to the front. |
| 970 | if (moveAffiliatedTasksToFront(task, taskIndex)) { |
| 971 | // All went well. |
| 972 | return; |
| 973 | } |
| 974 | |
| 975 | // Uh oh... something bad in the affiliation chain, try to rebuild |
| 976 | // everything and then go through our general path of adding a new task. |
| 977 | needAffiliationFix = true; |
| 978 | } else { |
| 979 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 980 | "addRecent: couldn't find other affiliation " + other); |
| 981 | needAffiliationFix = true; |
| 982 | } |
| 983 | } else { |
| 984 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 985 | "addRecent: adding affiliated task without next/prev:" + task); |
| 986 | needAffiliationFix = true; |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | if (needAffiliationFix) { |
| 991 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: regrouping affiliations"); |
| 992 | cleanupLocked(task.userId); |
| 993 | } |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 994 | |
| 995 | // Trim the set of tasks to the active set |
| 996 | trimInactiveRecentTasks(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Add the task to the bottom if possible. |
| 1001 | */ |
| 1002 | boolean addToBottom(TaskRecord task) { |
| 1003 | if (!canAddTaskWithoutTrim(task)) { |
| 1004 | // Adding this task would cause the task to be removed (since it's appended at |
| 1005 | // the bottom and would be trimmed) so just return now |
| 1006 | return false; |
| 1007 | } |
| 1008 | |
| 1009 | add(task); |
| 1010 | return true; |
| 1011 | } |
| 1012 | |
| 1013 | /** |
| 1014 | * Remove a task from the recent tasks list. |
| 1015 | */ |
| 1016 | void remove(TaskRecord task) { |
| 1017 | mTasks.remove(task); |
| 1018 | notifyTaskRemoved(task, !TRIMMED); |
| 1019 | } |
| 1020 | |
| 1021 | /** |
| 1022 | * Trims the recents task list to the global max number of recents. |
| 1023 | */ |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1024 | private void trimInactiveRecentTasks() { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1025 | int recentsCount = mTasks.size(); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1026 | |
| 1027 | // Remove from the end of the list until we reach the max number of recents |
| 1028 | while (recentsCount > mGlobalMaxNumTasks) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1029 | final TaskRecord tr = mTasks.remove(recentsCount - 1); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1030 | notifyTaskRemoved(tr, TRIMMED); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1031 | recentsCount--; |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1032 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "Trimming over max-recents task=" + tr |
| 1033 | + " max=" + mGlobalMaxNumTasks); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1034 | } |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1035 | |
| 1036 | // Remove any tasks that belong to currently quiet profiles |
| 1037 | final int[] profileUserIds = mUserController.getCurrentProfileIds(); |
| 1038 | mTmpQuietProfileUserIds.clear(); |
| 1039 | for (int userId : profileUserIds) { |
| 1040 | final UserInfo userInfo = mUserController.getUserInfo(userId); |
Winson Chung | 129771a0 | 2017-10-18 13:49:42 -0700 | [diff] [blame] | 1041 | if (userInfo != null && userInfo.isManagedProfile() && userInfo.isQuietModeEnabled()) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1042 | mTmpQuietProfileUserIds.put(userId, true); |
| 1043 | } |
| 1044 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "User: " + userInfo |
| 1045 | + " quiet=" + mTmpQuietProfileUserIds.get(userId)); |
| 1046 | } |
| 1047 | |
| 1048 | // Remove any inactive tasks, calculate the latest set of visible tasks |
| 1049 | int numVisibleTasks = 0; |
| 1050 | for (int i = 0; i < mTasks.size();) { |
| 1051 | final TaskRecord task = mTasks.get(i); |
| 1052 | |
| 1053 | if (isActiveRecentTask(task, mTmpQuietProfileUserIds)) { |
| 1054 | if (!mHasVisibleRecentTasks) { |
| 1055 | // Keep all active tasks if visible recent tasks is not supported |
| 1056 | i++; |
| 1057 | continue; |
| 1058 | } |
| 1059 | |
| 1060 | if (!isVisibleRecentTask(task)) { |
| 1061 | // Keep all active-but-invisible tasks |
| 1062 | i++; |
| 1063 | continue; |
| 1064 | } else { |
| 1065 | numVisibleTasks++; |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 1066 | if (isInVisibleRange(task, numVisibleTasks) || !isTrimmable(task)) { |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1067 | // Keep visible tasks in range |
| 1068 | i++; |
| 1069 | continue; |
| 1070 | } else { |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 1071 | // Fall through to trim visible tasks that are no longer in range and |
| 1072 | // trimmable |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1073 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, |
| 1074 | "Trimming out-of-range visible task=" + task); |
| 1075 | } |
| 1076 | } |
| 1077 | } else { |
| 1078 | // Fall through to trim inactive tasks |
| 1079 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "Trimming inactive task=" + task); |
| 1080 | } |
| 1081 | |
| 1082 | // Task is no longer active, trim it from the list |
| 1083 | mTasks.remove(task); |
| 1084 | notifyTaskRemoved(task, TRIMMED); |
| 1085 | notifyTaskPersisterLocked(task, false /* flush */); |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | /** |
| 1090 | * @return whether the given task should be considered active. |
| 1091 | */ |
| 1092 | private boolean isActiveRecentTask(TaskRecord task, SparseBooleanArray quietProfileUserIds) { |
| 1093 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "isActiveRecentTask: task=" + task |
| 1094 | + " globalMax=" + mGlobalMaxNumTasks); |
| 1095 | |
| 1096 | if (quietProfileUserIds.get(task.userId)) { |
| 1097 | // Quiet profile user's tasks are never active |
| 1098 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "\tisQuietProfileTask=true"); |
| 1099 | return false; |
| 1100 | } |
| 1101 | |
| 1102 | if (task.mAffiliatedTaskId != INVALID_TASK_ID && task.mAffiliatedTaskId != task.taskId) { |
| 1103 | // Keep the task active if its affiliated task is also active |
| 1104 | final TaskRecord affiliatedTask = getTask(task.mAffiliatedTaskId); |
| 1105 | if (affiliatedTask != null) { |
| 1106 | if (!isActiveRecentTask(affiliatedTask, quietProfileUserIds)) { |
| 1107 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, |
| 1108 | "\taffiliatedWithTask=" + affiliatedTask + " is not active"); |
| 1109 | return false; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | // All other tasks are considered active |
| 1115 | return true; |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * @return whether the given active task should be presented to the user through SystemUI. |
| 1120 | */ |
| 1121 | private boolean isVisibleRecentTask(TaskRecord task) { |
| 1122 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "isVisibleRecentTask: task=" + task |
| 1123 | + " minVis=" + mMinNumVisibleTasks + " maxVis=" + mMaxNumVisibleTasks |
| 1124 | + " sessionDuration=" + mActiveTasksSessionDurationMs |
| 1125 | + " inactiveDuration=" + task.getInactiveDuration() |
| 1126 | + " activityType=" + task.getActivityType() |
Vadim Tryshev | eff42d3 | 2018-03-05 18:33:48 -0800 | [diff] [blame] | 1127 | + " windowingMode=" + task.getWindowingMode() |
| 1128 | + " intentFlags=" + task.getBaseIntent().getFlags()); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1129 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1130 | switch (task.getActivityType()) { |
| 1131 | case ACTIVITY_TYPE_HOME: |
| 1132 | case ACTIVITY_TYPE_RECENTS: |
Vadim Tryshev | eff42d3 | 2018-03-05 18:33:48 -0800 | [diff] [blame] | 1133 | // Ignore certain activity types completely |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1134 | return false; |
Vadim Tryshev | eff42d3 | 2018-03-05 18:33:48 -0800 | [diff] [blame] | 1135 | case ACTIVITY_TYPE_ASSISTANT: |
| 1136 | // Ignore assistant that chose to be excluded from Recents, even if it's a top |
| 1137 | // task. |
| 1138 | if ((task.getBaseIntent().getFlags() |
| 1139 | & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) |
| 1140 | == Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) { |
| 1141 | return false; |
| 1142 | } |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | // Ignore certain windowing modes |
| 1146 | switch (task.getWindowingMode()) { |
| 1147 | case WINDOWING_MODE_PINNED: |
| 1148 | return false; |
| 1149 | case WINDOWING_MODE_SPLIT_SCREEN_PRIMARY: |
| 1150 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "\ttop=" + task.getStack().topTask()); |
| 1151 | final ActivityStack stack = task.getStack(); |
| 1152 | if (stack != null && stack.topTask() == task) { |
| 1153 | // Only the non-top task of the primary split screen mode is visible |
| 1154 | return false; |
| 1155 | } |
| 1156 | } |
| 1157 | |
Benjamin Franz | 7dcbfb0 | 2018-01-16 15:16:16 +0000 | [diff] [blame] | 1158 | // If we're in lock task mode, ignore the root task |
| 1159 | if (task == mService.mLockTaskController.getRootTask()) { |
| 1160 | return false; |
| 1161 | } |
| 1162 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1163 | return true; |
| 1164 | } |
| 1165 | |
| 1166 | /** |
| 1167 | * @return whether the given visible task is within the policy range. |
| 1168 | */ |
| 1169 | private boolean isInVisibleRange(TaskRecord task, int numVisibleTasks) { |
| 1170 | // Keep the last most task even if it is excluded from recents |
| 1171 | final boolean isExcludeFromRecents = |
| 1172 | (task.getBaseIntent().getFlags() & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) |
| 1173 | == Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS; |
| 1174 | if (isExcludeFromRecents) { |
| 1175 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "\texcludeFromRecents=true"); |
| 1176 | return numVisibleTasks == 1; |
| 1177 | } |
| 1178 | |
| 1179 | if (mMinNumVisibleTasks >= 0 && numVisibleTasks <= mMinNumVisibleTasks) { |
| 1180 | // Always keep up to the min number of recent tasks, after that fall through to the |
| 1181 | // checks below |
| 1182 | return true; |
| 1183 | } |
| 1184 | |
| 1185 | if (mMaxNumVisibleTasks >= 0) { |
| 1186 | // Always keep up to the max number of recent tasks, but return false afterwards |
| 1187 | return numVisibleTasks <= mMaxNumVisibleTasks; |
| 1188 | } |
| 1189 | |
| 1190 | if (mActiveTasksSessionDurationMs > 0) { |
| 1191 | // Keep the task if the inactive time is within the session window, this check must come |
| 1192 | // after the checks for the min/max visible task range |
| 1193 | if (task.getInactiveDuration() <= mActiveTasksSessionDurationMs) { |
| 1194 | return true; |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | return false; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | /** |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 1202 | * @return whether the given task can be trimmed even if it is outside the visible range. |
| 1203 | */ |
| 1204 | protected boolean isTrimmable(TaskRecord task) { |
| 1205 | final ActivityStack stack = task.getStack(); |
| 1206 | final ActivityStack homeStack = mService.mStackSupervisor.mHomeStack; |
| 1207 | |
| 1208 | // No stack for task, just trim it |
| 1209 | if (stack == null) { |
| 1210 | return true; |
| 1211 | } |
| 1212 | |
| 1213 | // Ignore tasks from different displays |
| 1214 | if (stack.getDisplay() != homeStack.getDisplay()) { |
| 1215 | return false; |
| 1216 | } |
| 1217 | |
| 1218 | // Trim tasks that are in stacks that are behind the home stack |
| 1219 | final ActivityDisplay display = stack.getDisplay(); |
| 1220 | return display.getIndexOf(stack) < display.getIndexOf(homeStack); |
| 1221 | } |
| 1222 | |
| 1223 | /** |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1224 | * If needed, remove oldest existing entries in recents that are for the same kind |
| 1225 | * of task as the given one. |
| 1226 | */ |
Winson Chung | 079221f | 2017-12-13 17:43:34 -0800 | [diff] [blame] | 1227 | private void removeForAddTask(TaskRecord task) { |
| 1228 | final int removeIndex = findRemoveIndexForAddTask(task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1229 | if (removeIndex == -1) { |
| 1230 | // Nothing to trim |
| 1231 | return; |
| 1232 | } |
| 1233 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1234 | // There is a similar task that will be removed for the addition of {@param task}, but it |
| 1235 | // can be the same task, and if so, the task will be re-added in add(), so skip the |
| 1236 | // callbacks here. |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1237 | final TaskRecord removedTask = mTasks.remove(removeIndex); |
| 1238 | if (removedTask != task) { |
Winson Chung | 079221f | 2017-12-13 17:43:34 -0800 | [diff] [blame] | 1239 | notifyTaskRemoved(removedTask, !TRIMMED); |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1240 | if (DEBUG_RECENTS_TRIM_TASKS) Slog.d(TAG, "Trimming task=" + removedTask |
| 1241 | + " for addition of task=" + task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1242 | } |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1243 | notifyTaskPersisterLocked(removedTask, false /* flush */); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | /** |
| 1247 | * Find the task that would be removed if the given {@param task} is added to the recent tasks |
| 1248 | * list (if any). |
| 1249 | */ |
Winson Chung | 079221f | 2017-12-13 17:43:34 -0800 | [diff] [blame] | 1250 | private int findRemoveIndexForAddTask(TaskRecord task) { |
Winson Chung | 6c9dcad | 2018-03-13 16:57:55 -0700 | [diff] [blame] | 1251 | final int recentsCount = mTasks.size(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1252 | final Intent intent = task.intent; |
| 1253 | final boolean document = intent != null && intent.isDocument(); |
| 1254 | int maxRecents = task.maxRecents - 1; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1255 | for (int i = 0; i < recentsCount; i++) { |
| 1256 | final TaskRecord tr = mTasks.get(i); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1257 | if (task != tr) { |
Winson Chung | b766727 | 2018-04-06 10:18:39 -0700 | [diff] [blame] | 1258 | if (!task.hasCompatibleActivityType(tr) || task.userId != tr.userId) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1259 | continue; |
| 1260 | } |
| 1261 | final Intent trIntent = tr.intent; |
| 1262 | final boolean sameAffinity = |
| 1263 | task.affinity != null && task.affinity.equals(tr.affinity); |
| 1264 | final boolean sameIntent = intent != null && intent.filterEquals(trIntent); |
| 1265 | boolean multiTasksAllowed = false; |
| 1266 | final int flags = intent.getFlags(); |
| 1267 | if ((flags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_NEW_DOCUMENT)) != 0 |
| 1268 | && (flags & FLAG_ACTIVITY_MULTIPLE_TASK) != 0) { |
| 1269 | multiTasksAllowed = true; |
| 1270 | } |
| 1271 | final boolean trIsDocument = trIntent != null && trIntent.isDocument(); |
| 1272 | final boolean bothDocuments = document && trIsDocument; |
| 1273 | if (!sameAffinity && !sameIntent && !bothDocuments) { |
| 1274 | continue; |
| 1275 | } |
| 1276 | |
| 1277 | if (bothDocuments) { |
| 1278 | // Do these documents belong to the same activity? |
| 1279 | final boolean sameActivity = task.realActivity != null |
| 1280 | && tr.realActivity != null |
| 1281 | && task.realActivity.equals(tr.realActivity); |
| 1282 | if (!sameActivity) { |
| 1283 | // If the document is open in another app or is not the same document, we |
| 1284 | // don't need to trim it. |
| 1285 | continue; |
| 1286 | } else if (maxRecents > 0) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1287 | --maxRecents; |
| 1288 | if (!sameIntent || multiTasksAllowed) { |
| 1289 | // We don't want to trim if we are not over the max allowed entries and |
| 1290 | // the tasks are not of the same intent filter, or multiple entries for |
| 1291 | // the task is allowed. |
| 1292 | continue; |
| 1293 | } |
| 1294 | } |
| 1295 | // Hit the maximum number of documents for this task. Fall through |
| 1296 | // and remove this document from recents. |
| 1297 | } else if (document || trIsDocument) { |
| 1298 | // Only one of these is a document. Not the droid we're looking for. |
| 1299 | continue; |
| 1300 | } |
| 1301 | } |
| 1302 | return i; |
| 1303 | } |
| 1304 | return -1; |
| 1305 | } |
| 1306 | |
| 1307 | // Extract the affiliates of the chain containing recent at index start. |
| 1308 | private int processNextAffiliateChainLocked(int start) { |
| 1309 | final TaskRecord startTask = mTasks.get(start); |
| 1310 | final int affiliateId = startTask.mAffiliatedTaskId; |
| 1311 | |
| 1312 | // Quick identification of isolated tasks. I.e. those not launched behind. |
| 1313 | if (startTask.taskId == affiliateId && startTask.mPrevAffiliate == null && |
| 1314 | startTask.mNextAffiliate == null) { |
| 1315 | // There is still a slim chance that there are other tasks that point to this task |
| 1316 | // and that the chain is so messed up that this task no longer points to them but |
| 1317 | // the gain of this optimization outweighs the risk. |
| 1318 | startTask.inRecents = true; |
| 1319 | return start + 1; |
| 1320 | } |
| 1321 | |
| 1322 | // Remove all tasks that are affiliated to affiliateId and put them in mTmpRecents. |
| 1323 | mTmpRecents.clear(); |
| 1324 | for (int i = mTasks.size() - 1; i >= start; --i) { |
| 1325 | final TaskRecord task = mTasks.get(i); |
| 1326 | if (task.mAffiliatedTaskId == affiliateId) { |
| 1327 | mTasks.remove(i); |
| 1328 | mTmpRecents.add(task); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | // Sort them all by taskId. That is the order they were create in and that order will |
| 1333 | // always be correct. |
| 1334 | Collections.sort(mTmpRecents, TASK_ID_COMPARATOR); |
| 1335 | |
| 1336 | // Go through and fix up the linked list. |
| 1337 | // The first one is the end of the chain and has no next. |
| 1338 | final TaskRecord first = mTmpRecents.get(0); |
| 1339 | first.inRecents = true; |
| 1340 | if (first.mNextAffiliate != null) { |
| 1341 | Slog.w(TAG, "Link error 1 first.next=" + first.mNextAffiliate); |
| 1342 | first.setNextAffiliate(null); |
| 1343 | notifyTaskPersisterLocked(first, false); |
| 1344 | } |
| 1345 | // Everything in the middle is doubly linked from next to prev. |
| 1346 | final int tmpSize = mTmpRecents.size(); |
| 1347 | for (int i = 0; i < tmpSize - 1; ++i) { |
| 1348 | final TaskRecord next = mTmpRecents.get(i); |
| 1349 | final TaskRecord prev = mTmpRecents.get(i + 1); |
| 1350 | if (next.mPrevAffiliate != prev) { |
| 1351 | Slog.w(TAG, "Link error 2 next=" + next + " prev=" + next.mPrevAffiliate + |
| 1352 | " setting prev=" + prev); |
| 1353 | next.setPrevAffiliate(prev); |
| 1354 | notifyTaskPersisterLocked(next, false); |
| 1355 | } |
| 1356 | if (prev.mNextAffiliate != next) { |
| 1357 | Slog.w(TAG, "Link error 3 prev=" + prev + " next=" + prev.mNextAffiliate + |
| 1358 | " setting next=" + next); |
| 1359 | prev.setNextAffiliate(next); |
| 1360 | notifyTaskPersisterLocked(prev, false); |
| 1361 | } |
| 1362 | prev.inRecents = true; |
| 1363 | } |
| 1364 | // The last one is the beginning of the list and has no prev. |
| 1365 | final TaskRecord last = mTmpRecents.get(tmpSize - 1); |
| 1366 | if (last.mPrevAffiliate != null) { |
| 1367 | Slog.w(TAG, "Link error 4 last.prev=" + last.mPrevAffiliate); |
| 1368 | last.setPrevAffiliate(null); |
| 1369 | notifyTaskPersisterLocked(last, false); |
| 1370 | } |
| 1371 | |
Winson Chung | d6aa3db | 2017-10-05 17:18:43 -0700 | [diff] [blame] | 1372 | // Insert the group back into mTmpTasks at start. |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1373 | mTasks.addAll(start, mTmpRecents); |
| 1374 | mTmpRecents.clear(); |
| 1375 | |
| 1376 | // Let the caller know where we left off. |
| 1377 | return start + tmpSize; |
| 1378 | } |
| 1379 | |
| 1380 | private boolean moveAffiliatedTasksToFront(TaskRecord task, int taskIndex) { |
| 1381 | int recentsCount = mTasks.size(); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1382 | TaskRecord top = task; |
| 1383 | int topIndex = taskIndex; |
| 1384 | while (top.mNextAffiliate != null && topIndex > 0) { |
| 1385 | top = top.mNextAffiliate; |
| 1386 | topIndex--; |
| 1387 | } |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 1388 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: adding affilliates starting at " |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1389 | + topIndex + " from intial " + taskIndex); |
| 1390 | // Find the end of the chain, doing a sanity check along the way. |
| 1391 | boolean sane = top.mAffiliatedTaskId == task.mAffiliatedTaskId; |
| 1392 | int endIndex = topIndex; |
| 1393 | TaskRecord prev = top; |
| 1394 | while (endIndex < recentsCount) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1395 | TaskRecord cur = mTasks.get(endIndex); |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 1396 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: looking at next chain @" |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1397 | + endIndex + " " + cur); |
| 1398 | if (cur == top) { |
| 1399 | // Verify start of the chain. |
| 1400 | if (cur.mNextAffiliate != null || cur.mNextAffiliateTaskId != INVALID_TASK_ID) { |
| 1401 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 1402 | + ": first task has next affiliate: " + prev); |
| 1403 | sane = false; |
| 1404 | break; |
| 1405 | } |
| 1406 | } else { |
| 1407 | // Verify middle of the chain's next points back to the one before. |
| 1408 | if (cur.mNextAffiliate != prev |
| 1409 | || cur.mNextAffiliateTaskId != prev.taskId) { |
| 1410 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 1411 | + ": middle task " + cur + " @" + endIndex |
| 1412 | + " has bad next affiliate " |
| 1413 | + cur.mNextAffiliate + " id " + cur.mNextAffiliateTaskId |
| 1414 | + ", expected " + prev); |
| 1415 | sane = false; |
| 1416 | break; |
| 1417 | } |
| 1418 | } |
| 1419 | if (cur.mPrevAffiliateTaskId == INVALID_TASK_ID) { |
| 1420 | // Chain ends here. |
| 1421 | if (cur.mPrevAffiliate != null) { |
| 1422 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 1423 | + ": last task " + cur + " has previous affiliate " |
| 1424 | + cur.mPrevAffiliate); |
| 1425 | sane = false; |
| 1426 | } |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 1427 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: end of chain @" + endIndex); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1428 | break; |
| 1429 | } else { |
| 1430 | // Verify middle of the chain's prev points to a valid item. |
| 1431 | if (cur.mPrevAffiliate == null) { |
| 1432 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 1433 | + ": task " + cur + " has previous affiliate " |
| 1434 | + cur.mPrevAffiliate + " but should be id " |
| 1435 | + cur.mPrevAffiliate); |
| 1436 | sane = false; |
| 1437 | break; |
| 1438 | } |
| 1439 | } |
| 1440 | if (cur.mAffiliatedTaskId != task.mAffiliatedTaskId) { |
| 1441 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 1442 | + ": task " + cur + " has affiliated id " |
| 1443 | + cur.mAffiliatedTaskId + " but should be " |
| 1444 | + task.mAffiliatedTaskId); |
| 1445 | sane = false; |
| 1446 | break; |
| 1447 | } |
| 1448 | prev = cur; |
| 1449 | endIndex++; |
| 1450 | if (endIndex >= recentsCount) { |
| 1451 | Slog.wtf(TAG, "Bad chain ran off index " + endIndex |
| 1452 | + ": last task " + prev); |
| 1453 | sane = false; |
| 1454 | break; |
| 1455 | } |
| 1456 | } |
| 1457 | if (sane) { |
| 1458 | if (endIndex < taskIndex) { |
| 1459 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 1460 | + ": did not extend to task " + task + " @" + taskIndex); |
| 1461 | sane = false; |
| 1462 | } |
| 1463 | } |
| 1464 | if (sane) { |
| 1465 | // All looks good, we can just move all of the affiliated tasks |
| 1466 | // to the top. |
| 1467 | for (int i=topIndex; i<=endIndex; i++) { |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 1468 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: moving affiliated " + task |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1469 | + " from " + i + " to " + (i-topIndex)); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1470 | TaskRecord cur = mTasks.remove(i); |
| 1471 | mTasks.add(i - topIndex, cur); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1472 | } |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 1473 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: done moving tasks " + topIndex |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1474 | + " to " + endIndex); |
| 1475 | return true; |
| 1476 | } |
| 1477 | |
| 1478 | // Whoops, couldn't do it. |
| 1479 | return false; |
| 1480 | } |
| 1481 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1482 | void dump(PrintWriter pw, boolean dumpAll, String dumpPackage) { |
| 1483 | pw.println("ACTIVITY MANAGER RECENT TASKS (dumpsys activity recents)"); |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 1484 | pw.println("mRecentsUid=" + mRecentsUid); |
| 1485 | pw.println("mRecentsComponent=" + mRecentsComponent); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1486 | if (mTasks.isEmpty()) { |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1487 | return; |
| 1488 | } |
| 1489 | |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 1490 | boolean printedAnything = false; |
| 1491 | boolean printedHeader = false; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1492 | final int size = mTasks.size(); |
| 1493 | for (int i = 0; i < size; i++) { |
| 1494 | final TaskRecord tr = mTasks.get(i); |
| 1495 | if (dumpPackage != null && (tr.realActivity == null || |
| 1496 | !dumpPackage.equals(tr.realActivity.getPackageName()))) { |
| 1497 | continue; |
| 1498 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1499 | |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 1500 | if (!printedHeader) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1501 | pw.println(" Recent tasks:"); |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 1502 | printedHeader = true; |
| 1503 | printedAnything = true; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1504 | } |
| 1505 | pw.print(" * Recent #"); pw.print(i); pw.print(": "); |
| 1506 | pw.println(tr); |
| 1507 | if (dumpAll) { |
| 1508 | tr.dump(pw, " "); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | |
Winson Chung | 3f0e59a | 2017-10-25 10:19:05 -0700 | [diff] [blame] | 1512 | if (!printedAnything) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1513 | pw.println(" (nothing)"); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | /** |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1518 | * Creates a new RecentTaskInfo from a TaskRecord. |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1519 | */ |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 1520 | ActivityManager.RecentTaskInfo createRecentTaskInfo(TaskRecord tr) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1521 | // Compose the recent task info |
| 1522 | ActivityManager.RecentTaskInfo rti = new ActivityManager.RecentTaskInfo(); |
| 1523 | rti.id = tr.getTopActivity() == null ? INVALID_TASK_ID : tr.taskId; |
| 1524 | rti.persistentId = tr.taskId; |
| 1525 | rti.baseIntent = new Intent(tr.getBaseIntent()); |
| 1526 | rti.origActivity = tr.origActivity; |
| 1527 | rti.realActivity = tr.realActivity; |
| 1528 | rti.description = tr.lastDescription; |
| 1529 | rti.stackId = tr.getStackId(); |
| 1530 | rti.userId = tr.userId; |
| 1531 | rti.taskDescription = new ActivityManager.TaskDescription(tr.lastTaskDescription); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1532 | rti.lastActiveTime = tr.lastActiveTime; |
| 1533 | rti.affiliatedTaskId = tr.mAffiliatedTaskId; |
| 1534 | rti.affiliatedTaskColor = tr.mAffiliatedTaskColor; |
| 1535 | rti.numActivities = 0; |
Bryce Lee | f3c6a47 | 2017-11-14 14:53:06 -0800 | [diff] [blame] | 1536 | if (!tr.matchParentBounds()) { |
| 1537 | rti.bounds = new Rect(tr.getOverrideBounds()); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1538 | } |
| 1539 | rti.supportsSplitScreenMultiWindow = tr.supportsSplitScreenWindowingMode(); |
| 1540 | rti.resizeMode = tr.mResizeMode; |
| 1541 | rti.configuration.setTo(tr.getConfiguration()); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1542 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 1543 | tr.getNumRunningActivities(mTmpReport); |
| 1544 | rti.numActivities = mTmpReport.numActivities; |
| 1545 | rti.baseActivity = (mTmpReport.base != null) ? mTmpReport.base.intent.getComponent() : null; |
| 1546 | rti.topActivity = (mTmpReport.top != null) ? mTmpReport.top.intent.getComponent() : null; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1547 | |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1548 | return rti; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1549 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 1550 | } |