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 | |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 19 | import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_RECENTS; |
| 20 | import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_TASKS; |
| 21 | import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_RECENTS; |
| 22 | import static com.android.server.am.ActivityManagerDebugConfig.POSTFIX_TASKS; |
| 23 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; |
| 24 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; |
Wale Ogunwale | e23149f | 2015-03-06 15:39:44 -0800 | [diff] [blame] | 25 | import static com.android.server.am.TaskRecord.INVALID_TASK_ID; |
| 26 | |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 27 | import com.google.android.collect.Sets; |
| 28 | |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 29 | import android.app.ActivityManager; |
| 30 | import android.app.AppGlobals; |
| 31 | import android.content.ComponentName; |
| 32 | import android.content.Intent; |
| 33 | import android.content.pm.ActivityInfo; |
| 34 | import android.content.pm.ApplicationInfo; |
| 35 | import android.content.pm.IPackageManager; |
| 36 | import android.content.pm.PackageManager; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 37 | import android.graphics.Bitmap; |
| 38 | import android.os.Environment; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 39 | import android.os.RemoteException; |
| 40 | import android.os.UserHandle; |
| 41 | import android.util.Slog; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 42 | import android.util.SparseBooleanArray; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 43 | |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 44 | import java.io.File; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 45 | import java.util.ArrayList; |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 46 | import java.util.Arrays; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 47 | import java.util.Collections; |
| 48 | import java.util.Comparator; |
| 49 | import java.util.HashMap; |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 50 | import java.util.Set; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Class for managing the recent tasks list. |
| 54 | */ |
| 55 | class RecentTasks extends ArrayList<TaskRecord> { |
Wale Ogunwale | e23149f | 2015-03-06 15:39:44 -0800 | [diff] [blame] | 56 | private static final String TAG = TAG_WITH_CLASS_NAME ? "RecentTasks" : TAG_AM; |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 57 | private static final String TAG_RECENTS = TAG + POSTFIX_RECENTS; |
| 58 | private static final String TAG_TASKS = TAG + POSTFIX_TASKS; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 59 | |
| 60 | // Maximum number recent bitmaps to keep in memory. |
| 61 | private static final int MAX_RECENT_BITMAPS = 3; |
| 62 | |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Save recent tasks information across reboots. |
| 65 | */ |
| 66 | private final TaskPersister mTaskPersister; |
| 67 | private final SparseBooleanArray mUsersWithRecentsLoaded = new SparseBooleanArray(5); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 68 | |
| 69 | // Mainly to avoid object recreation on multiple calls. |
| 70 | private final ArrayList<TaskRecord> mTmpRecents = new ArrayList<TaskRecord>(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 71 | private final HashMap<ComponentName, ActivityInfo> mTmpAvailActCache = new HashMap<>(); |
| 72 | private final HashMap<String, ApplicationInfo> mTmpAvailAppCache = new HashMap<>(); |
| 73 | private final ActivityInfo mTmpActivityInfo = new ActivityInfo(); |
| 74 | private final ApplicationInfo mTmpAppInfo = new ApplicationInfo(); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 75 | |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 76 | RecentTasks(ActivityManagerService service, ActivityStackSupervisor mStackSupervisor) { |
| 77 | File systemDir = Environment.getDataSystemDirectory(); |
| 78 | mTaskPersister = new TaskPersister(systemDir, mStackSupervisor, service, this); |
| 79 | mStackSupervisor.setRecentTasks(this); |
| 80 | } |
| 81 | |
| 82 | /** |
Suprabh Shukla | f5bf0cb | 2016-01-19 17:42:03 -0800 | [diff] [blame] | 83 | * Loads the persistent recentTasks for {@code userId} into this list from persistent storage. |
| 84 | * Does nothing if they are already loaded. |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 85 | * |
| 86 | * @param userId the user Id |
| 87 | */ |
| 88 | void loadUserRecentsLocked(int userId) { |
| 89 | if (!mUsersWithRecentsLoaded.get(userId)) { |
| 90 | Slog.i(TAG, "Loading recents for user " + userId + " into memory."); |
| 91 | addAll(mTaskPersister.restoreTasksForUserLocked(userId)); |
| 92 | cleanupLocked(userId); |
| 93 | mUsersWithRecentsLoaded.put(userId, true); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void notifyTaskPersisterLocked(TaskRecord task, boolean flush) { |
| 98 | if (task != null && task.stack != null && task.stack.isHomeStack()) { |
| 99 | // Never persist the home stack. |
| 100 | return; |
| 101 | } |
| 102 | mTaskPersister.wakeup(task, flush); |
| 103 | } |
| 104 | |
| 105 | void onSystemReady() { |
| 106 | clear(); |
| 107 | loadUserRecentsLocked(UserHandle.USER_SYSTEM); |
| 108 | startPersisting(); |
| 109 | } |
| 110 | |
| 111 | void startPersisting() { |
| 112 | mTaskPersister.startPersisting(); |
| 113 | } |
| 114 | |
| 115 | Bitmap getTaskDescriptionIcon(String path) { |
| 116 | return mTaskPersister.getTaskDescriptionIcon(path); |
| 117 | } |
| 118 | |
| 119 | Bitmap getImageFromWriteQueue(String path) { |
| 120 | return mTaskPersister.getImageFromWriteQueue(path); |
| 121 | } |
| 122 | |
| 123 | void saveImage(Bitmap image, String path) { |
| 124 | mTaskPersister.saveImage(image, path); |
| 125 | } |
| 126 | |
| 127 | void flush() { |
| 128 | mTaskPersister.flush(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns all userIds for which recents from storage are loaded |
| 133 | * |
| 134 | * @return an array of userIds. |
| 135 | */ |
| 136 | int[] usersWithRecentsLoadedLocked() { |
| 137 | int[] usersWithRecentsLoaded = new int[mUsersWithRecentsLoaded.size()]; |
| 138 | int len = 0; |
| 139 | for (int i = 0; i < usersWithRecentsLoaded.length; i++) { |
| 140 | int userId = mUsersWithRecentsLoaded.keyAt(i); |
| 141 | if (mUsersWithRecentsLoaded.valueAt(i)) { |
| 142 | usersWithRecentsLoaded[len++] = userId; |
| 143 | } |
| 144 | } |
| 145 | if (len < usersWithRecentsLoaded.length) { |
| 146 | // should never happen. |
| 147 | return Arrays.copyOf(usersWithRecentsLoaded, len); |
| 148 | } |
| 149 | return usersWithRecentsLoaded; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Removes recent tasks for this user if they are loaded, does not do anything otherwise. |
| 154 | * |
| 155 | * @param userId the user id. |
| 156 | */ |
| 157 | void unloadUserRecentsLocked(int userId) { |
| 158 | if (mUsersWithRecentsLoaded.get(userId)) { |
| 159 | Slog.i(TAG, "Unloading recents for user " + userId + " from memory."); |
| 160 | mUsersWithRecentsLoaded.delete(userId); |
| 161 | removeTasksForUserLocked(userId); |
| 162 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | TaskRecord taskForIdLocked(int id) { |
| 166 | final int recentsCount = size(); |
| 167 | for (int i = 0; i < recentsCount; i++) { |
| 168 | TaskRecord tr = get(i); |
| 169 | if (tr.taskId == id) { |
| 170 | return tr; |
| 171 | } |
| 172 | } |
| 173 | return null; |
| 174 | } |
| 175 | |
| 176 | /** Remove recent tasks for a user. */ |
| 177 | void removeTasksForUserLocked(int userId) { |
| 178 | if(userId <= 0) { |
| 179 | Slog.i(TAG, "Can't remove recent task on user " + userId); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | for (int i = size() - 1; i >= 0; --i) { |
| 184 | TaskRecord tr = get(i); |
| 185 | if (tr.userId == userId) { |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 186 | if(DEBUG_TASKS) Slog.i(TAG_TASKS, |
| 187 | "remove RecentTask " + tr + " when finishing user" + userId); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 188 | remove(i); |
| 189 | tr.removedFromRecents(); |
| 190 | } |
| 191 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Andrei Stingaceanu | 4ccec53 | 2016-01-13 12:10:21 +0000 | [diff] [blame] | 194 | void onPackagesSuspendedChanged(String[] packages, boolean suspended, int userId) { |
| 195 | final Set<String> packageNames = Sets.newHashSet(packages); |
| 196 | for (int i = size() - 1; i >= 0; --i) { |
| 197 | final TaskRecord tr = get(i); |
| 198 | if (tr.realActivity != null |
| 199 | && packageNames.contains(tr.realActivity.getPackageName()) |
| 200 | && tr.userId == userId |
| 201 | && tr.realActivitySuspended != suspended) { |
| 202 | tr.realActivitySuspended = suspended; |
| 203 | notifyTaskPersisterLocked(tr, false); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | } |
| 208 | |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 209 | /** |
| 210 | * Update the recent tasks lists: make sure tasks should still be here (their |
| 211 | * applications / activities still exist), update their availability, fix-up ordering |
| 212 | * of affiliations. |
| 213 | */ |
| 214 | void cleanupLocked(int userId) { |
| 215 | int recentsCount = size(); |
| 216 | if (recentsCount == 0) { |
| 217 | // Happens when called from the packagemanager broadcast before boot, |
| 218 | // or just any empty list. |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | final IPackageManager pm = AppGlobals.getPackageManager(); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 223 | for (int i = recentsCount - 1; i >= 0; i--) { |
| 224 | final TaskRecord task = get(i); |
| 225 | if (userId != UserHandle.USER_ALL && task.userId != userId) { |
| 226 | // Only look at tasks for the user ID of interest. |
| 227 | continue; |
| 228 | } |
| 229 | if (task.autoRemoveRecents && task.getTopActivity() == null) { |
| 230 | // This situation is broken, and we should just get rid of it now. |
| 231 | remove(i); |
| 232 | task.removedFromRecents(); |
| 233 | Slog.w(TAG, "Removing auto-remove without activity: " + task); |
| 234 | continue; |
| 235 | } |
| 236 | // Check whether this activity is currently available. |
| 237 | if (task.realActivity != null) { |
| 238 | ActivityInfo ai = mTmpAvailActCache.get(task.realActivity); |
| 239 | if (ai == null) { |
| 240 | try { |
| 241 | // At this first cut, we're only interested in |
| 242 | // activities that are fully runnable based on |
| 243 | // current system state. |
| 244 | ai = pm.getActivityInfo(task.realActivity, |
| 245 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, userId); |
| 246 | } catch (RemoteException e) { |
| 247 | // Will never happen. |
| 248 | continue; |
| 249 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 250 | if (ai == null) { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 251 | ai = mTmpActivityInfo; |
| 252 | } |
| 253 | mTmpAvailActCache.put(task.realActivity, ai); |
| 254 | } |
| 255 | if (ai == mTmpActivityInfo) { |
| 256 | // This could be either because the activity no longer exists, or the |
| 257 | // app is temporarily gone. For the former we want to remove the recents |
| 258 | // entry; for the latter we want to mark it as unavailable. |
| 259 | ApplicationInfo app = mTmpAvailAppCache |
| 260 | .get(task.realActivity.getPackageName()); |
| 261 | if (app == null) { |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 262 | try { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 263 | app = pm.getApplicationInfo(task.realActivity.getPackageName(), |
| 264 | PackageManager.MATCH_UNINSTALLED_PACKAGES, userId); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 265 | } catch (RemoteException e) { |
| 266 | // Will never happen. |
| 267 | continue; |
| 268 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 269 | if (app == null) { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 270 | app = mTmpAppInfo; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 271 | } |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 272 | mTmpAvailAppCache.put(task.realActivity.getPackageName(), app); |
| 273 | } |
| 274 | if (app == mTmpAppInfo |
| 275 | || (app.flags & ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 276 | // Doesn't exist any more! Good-bye. |
| 277 | remove(i); |
| 278 | task.removedFromRecents(); |
| 279 | Slog.w(TAG, "Removing no longer valid recent: " + task); |
| 280 | continue; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 281 | } else { |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 282 | // Otherwise just not available for now. |
| 283 | if (DEBUG_RECENTS && task.isAvailable) Slog.d(TAG_RECENTS, |
| 284 | "Making recent unavailable: " + task); |
| 285 | task.isAvailable = false; |
| 286 | } |
| 287 | } else { |
| 288 | if (!ai.enabled || !ai.applicationInfo.enabled |
| 289 | || (ai.applicationInfo.flags |
| 290 | & ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 291 | if (DEBUG_RECENTS && task.isAvailable) Slog.d(TAG_RECENTS, |
| 292 | "Making recent unavailable: " + task |
| 293 | + " (enabled=" + ai.enabled + "/" |
| 294 | + ai.applicationInfo.enabled |
| 295 | + " flags=" |
| 296 | + Integer.toHexString(ai.applicationInfo.flags) |
| 297 | + ")"); |
| 298 | task.isAvailable = false; |
| 299 | } else { |
| 300 | if (DEBUG_RECENTS && !task.isAvailable) Slog.d(TAG_RECENTS, |
| 301 | "Making recent available: " + task); |
| 302 | task.isAvailable = true; |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // Verify the affiliate chain for each task. |
| 309 | int i = 0; |
| 310 | recentsCount = size(); |
| 311 | while (i < recentsCount) { |
| 312 | i = processNextAffiliateChainLocked(i); |
| 313 | } |
| 314 | // recent tasks are now in sorted, affiliated order. |
| 315 | } |
| 316 | |
| 317 | private final boolean moveAffiliatedTasksToFront(TaskRecord task, int taskIndex) { |
| 318 | int recentsCount = size(); |
| 319 | TaskRecord top = task; |
| 320 | int topIndex = taskIndex; |
| 321 | while (top.mNextAffiliate != null && topIndex > 0) { |
| 322 | top = top.mNextAffiliate; |
| 323 | topIndex--; |
| 324 | } |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 325 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: adding affilliates starting at " |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 326 | + topIndex + " from intial " + taskIndex); |
| 327 | // Find the end of the chain, doing a sanity check along the way. |
| 328 | boolean sane = top.mAffiliatedTaskId == task.mAffiliatedTaskId; |
| 329 | int endIndex = topIndex; |
| 330 | TaskRecord prev = top; |
| 331 | while (endIndex < recentsCount) { |
| 332 | TaskRecord cur = get(endIndex); |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 333 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: looking at next chain @" |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 334 | + endIndex + " " + cur); |
| 335 | if (cur == top) { |
| 336 | // Verify start of the chain. |
| 337 | if (cur.mNextAffiliate != null || cur.mNextAffiliateTaskId != INVALID_TASK_ID) { |
| 338 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 339 | + ": first task has next affiliate: " + prev); |
| 340 | sane = false; |
| 341 | break; |
| 342 | } |
| 343 | } else { |
| 344 | // Verify middle of the chain's next points back to the one before. |
| 345 | if (cur.mNextAffiliate != prev |
| 346 | || cur.mNextAffiliateTaskId != prev.taskId) { |
| 347 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 348 | + ": middle task " + cur + " @" + endIndex |
| 349 | + " has bad next affiliate " |
| 350 | + cur.mNextAffiliate + " id " + cur.mNextAffiliateTaskId |
| 351 | + ", expected " + prev); |
| 352 | sane = false; |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | if (cur.mPrevAffiliateTaskId == INVALID_TASK_ID) { |
| 357 | // Chain ends here. |
| 358 | if (cur.mPrevAffiliate != null) { |
| 359 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 360 | + ": last task " + cur + " has previous affiliate " |
| 361 | + cur.mPrevAffiliate); |
| 362 | sane = false; |
| 363 | } |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 364 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: end of chain @" + endIndex); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 365 | break; |
| 366 | } else { |
| 367 | // Verify middle of the chain's prev points to a valid item. |
| 368 | if (cur.mPrevAffiliate == null) { |
| 369 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 370 | + ": task " + cur + " has previous affiliate " |
| 371 | + cur.mPrevAffiliate + " but should be id " |
| 372 | + cur.mPrevAffiliate); |
| 373 | sane = false; |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | if (cur.mAffiliatedTaskId != task.mAffiliatedTaskId) { |
| 378 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 379 | + ": task " + cur + " has affiliated id " |
| 380 | + cur.mAffiliatedTaskId + " but should be " |
| 381 | + task.mAffiliatedTaskId); |
| 382 | sane = false; |
| 383 | break; |
| 384 | } |
| 385 | prev = cur; |
| 386 | endIndex++; |
| 387 | if (endIndex >= recentsCount) { |
| 388 | Slog.wtf(TAG, "Bad chain ran off index " + endIndex |
| 389 | + ": last task " + prev); |
| 390 | sane = false; |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | if (sane) { |
| 395 | if (endIndex < taskIndex) { |
| 396 | Slog.wtf(TAG, "Bad chain @" + endIndex |
| 397 | + ": did not extend to task " + task + " @" + taskIndex); |
| 398 | sane = false; |
| 399 | } |
| 400 | } |
| 401 | if (sane) { |
| 402 | // All looks good, we can just move all of the affiliated tasks |
| 403 | // to the top. |
| 404 | for (int i=topIndex; i<=endIndex; i++) { |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 405 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: moving affiliated " + task |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 406 | + " from " + i + " to " + (i-topIndex)); |
| 407 | TaskRecord cur = remove(i); |
| 408 | add(i - topIndex, cur); |
| 409 | } |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 410 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: done moving tasks " + topIndex |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 411 | + " to " + endIndex); |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | // Whoops, couldn't do it. |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | final void addLocked(TaskRecord task) { |
| 420 | final boolean isAffiliated = task.mAffiliatedTaskId != task.taskId |
| 421 | || task.mNextAffiliateTaskId != INVALID_TASK_ID |
| 422 | || task.mPrevAffiliateTaskId != INVALID_TASK_ID; |
| 423 | |
| 424 | int recentsCount = size(); |
| 425 | // Quick case: never add voice sessions. |
Amith Yamasani | 0af6fa7 | 2016-01-17 15:36:19 -0800 | [diff] [blame] | 426 | // TODO: VI what about if it's just an activity? |
| 427 | // Probably nothing to do here |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 428 | if (task.voiceSession != null) { |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 429 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 430 | "addRecent: not adding voice interaction " + task); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 431 | return; |
| 432 | } |
| 433 | // Another quick case: check if the top-most recent task is the same. |
| 434 | if (!isAffiliated && recentsCount > 0 && get(0) == task) { |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 435 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: already at top: " + task); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 436 | return; |
| 437 | } |
| 438 | // Another quick case: check if this is part of a set of affiliated |
| 439 | // tasks that are at the top. |
| 440 | if (isAffiliated && recentsCount > 0 && task.inRecents |
| 441 | && task.mAffiliatedTaskId == get(0).mAffiliatedTaskId) { |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 442 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: affiliated " + get(0) |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 443 | + " at top when adding " + task); |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | boolean needAffiliationFix = false; |
| 448 | |
| 449 | // Slightly less quick case: the task is already in recents, so all we need |
| 450 | // to do is move it. |
| 451 | if (task.inRecents) { |
| 452 | int taskIndex = indexOf(task); |
| 453 | if (taskIndex >= 0) { |
| 454 | if (!isAffiliated) { |
| 455 | // Simple case: this is not an affiliated task, so we just move it to the front. |
| 456 | remove(taskIndex); |
| 457 | add(0, task); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 458 | notifyTaskPersisterLocked(task, false); |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 459 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: moving to top " + task |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 460 | + " from " + taskIndex); |
| 461 | return; |
| 462 | } else { |
| 463 | // More complicated: need to keep all affiliated tasks together. |
| 464 | if (moveAffiliatedTasksToFront(task, taskIndex)) { |
| 465 | // All went well. |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | // Uh oh... something bad in the affiliation chain, try to rebuild |
| 470 | // everything and then go through our general path of adding a new task. |
| 471 | needAffiliationFix = true; |
| 472 | } |
| 473 | } else { |
| 474 | Slog.wtf(TAG, "Task with inRecent not in recents: " + task); |
| 475 | needAffiliationFix = true; |
| 476 | } |
| 477 | } |
| 478 | |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 479 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: trimming tasks for " + task); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 480 | trimForTaskLocked(task, true); |
| 481 | |
| 482 | recentsCount = size(); |
| 483 | final int maxRecents = ActivityManager.getMaxRecentTasksStatic(); |
| 484 | while (recentsCount >= maxRecents) { |
| 485 | final TaskRecord tr = remove(recentsCount - 1); |
| 486 | tr.removedFromRecents(); |
| 487 | recentsCount--; |
| 488 | } |
| 489 | task.inRecents = true; |
| 490 | if (!isAffiliated || needAffiliationFix) { |
| 491 | // If this is a simple non-affiliated task, or we had some failure trying to |
| 492 | // handle it as part of an affilated task, then just place it at the top. |
| 493 | add(0, task); |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 494 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: adding " + task); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 495 | } else if (isAffiliated) { |
| 496 | // If this is a new affiliated task, then move all of the affiliated tasks |
| 497 | // to the front and insert this new one. |
| 498 | TaskRecord other = task.mNextAffiliate; |
| 499 | if (other == null) { |
| 500 | other = task.mPrevAffiliate; |
| 501 | } |
| 502 | if (other != null) { |
| 503 | int otherIndex = indexOf(other); |
| 504 | if (otherIndex >= 0) { |
| 505 | // Insert new task at appropriate location. |
| 506 | int taskIndex; |
| 507 | if (other == task.mNextAffiliate) { |
| 508 | // We found the index of our next affiliation, which is who is |
| 509 | // before us in the list, so add after that point. |
| 510 | taskIndex = otherIndex+1; |
| 511 | } else { |
| 512 | // We found the index of our previous affiliation, which is who is |
| 513 | // after us in the list, so add at their position. |
| 514 | taskIndex = otherIndex; |
| 515 | } |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 516 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 517 | "addRecent: new affiliated task added at " + taskIndex + ": " + task); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 518 | add(taskIndex, task); |
| 519 | |
| 520 | // Now move everything to the front. |
| 521 | if (moveAffiliatedTasksToFront(task, taskIndex)) { |
| 522 | // All went well. |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | // Uh oh... something bad in the affiliation chain, try to rebuild |
| 527 | // everything and then go through our general path of adding a new task. |
| 528 | needAffiliationFix = true; |
| 529 | } else { |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 530 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
| 531 | "addRecent: couldn't find other affiliation " + other); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 532 | needAffiliationFix = true; |
| 533 | } |
| 534 | } else { |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 535 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 536 | "addRecent: adding affiliated task without next/prev:" + task); |
| 537 | needAffiliationFix = true; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | if (needAffiliationFix) { |
Wale Ogunwale | ee006da | 2015-03-30 14:49:25 -0700 | [diff] [blame] | 542 | if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "addRecent: regrouping affiliations"); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 543 | cleanupLocked(task.userId); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * If needed, remove oldest existing entries in recents that are for the same kind |
| 549 | * of task as the given one. |
| 550 | */ |
| 551 | int trimForTaskLocked(TaskRecord task, boolean doTrim) { |
| 552 | int recentsCount = size(); |
Wale Ogunwale | 2ab53cf | 2015-08-01 17:19:21 -0700 | [diff] [blame] | 553 | final Intent intent = task.intent; |
| 554 | final boolean document = intent != null && intent.isDocument(); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 555 | int maxRecents = task.maxRecents - 1; |
| 556 | for (int i = 0; i < recentsCount; i++) { |
| 557 | final TaskRecord tr = get(i); |
| 558 | if (task != tr) { |
| 559 | if (task.userId != tr.userId) { |
| 560 | continue; |
| 561 | } |
| 562 | if (i > MAX_RECENT_BITMAPS) { |
| 563 | tr.freeLastThumbnail(); |
| 564 | } |
Wale Ogunwale | 2ab53cf | 2015-08-01 17:19:21 -0700 | [diff] [blame] | 565 | final Intent trIntent = tr.intent; |
Wale Ogunwale | 7cbfcd8 | 2015-07-20 15:43:37 -0700 | [diff] [blame] | 566 | final boolean sameAffinity = |
| 567 | task.affinity != null && task.affinity.equals(tr.affinity); |
Wale Ogunwale | 2ab53cf | 2015-08-01 17:19:21 -0700 | [diff] [blame] | 568 | final boolean sameIntent = (intent != null && intent.filterEquals(trIntent)); |
| 569 | final boolean trIsDocument = trIntent != null && trIntent.isDocument(); |
Wale Ogunwale | 7cbfcd8 | 2015-07-20 15:43:37 -0700 | [diff] [blame] | 570 | final boolean bothDocuments = document && trIsDocument; |
Wale Ogunwale | 2ab53cf | 2015-08-01 17:19:21 -0700 | [diff] [blame] | 571 | if (!sameAffinity && !sameIntent && !bothDocuments) { |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 572 | continue; |
| 573 | } |
Wale Ogunwale | 7cbfcd8 | 2015-07-20 15:43:37 -0700 | [diff] [blame] | 574 | |
| 575 | if (bothDocuments) { |
| 576 | // Do these documents belong to the same activity? |
| 577 | final boolean sameActivity = task.realActivity != null |
| 578 | && tr.realActivity != null |
| 579 | && task.realActivity.equals(tr.realActivity); |
Robert Carr | 7cb57e1 | 2015-11-10 13:44:02 -0800 | [diff] [blame] | 580 | // If the document is open in another app or is not the same |
| 581 | // document, we don't need to trim it. |
| 582 | if (!sameActivity || !sameIntent) { |
Wale Ogunwale | 7cbfcd8 | 2015-07-20 15:43:37 -0700 | [diff] [blame] | 583 | continue; |
Robert Carr | 7cb57e1 | 2015-11-10 13:44:02 -0800 | [diff] [blame] | 584 | // Otherwise only trim if we are over our max recents for this task |
| 585 | } else if (maxRecents > 0 && !doTrim) { |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 586 | --maxRecents; |
| 587 | continue; |
| 588 | } |
| 589 | // Hit the maximum number of documents for this task. Fall through |
| 590 | // and remove this document from recents. |
| 591 | } else if (document || trIsDocument) { |
| 592 | // Only one of these is a document. Not the droid we're looking for. |
| 593 | continue; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | if (!doTrim) { |
| 598 | // If the caller is not actually asking for a trim, just tell them we reached |
| 599 | // a point where the trim would happen. |
| 600 | return i; |
| 601 | } |
| 602 | |
| 603 | // Either task and tr are the same or, their affinities match or their intents match |
| 604 | // and neither of them is a document, or they are documents using the same activity |
| 605 | // and their maxRecents has been reached. |
| 606 | tr.disposeThumbnail(); |
| 607 | remove(i); |
| 608 | if (task != tr) { |
| 609 | tr.removedFromRecents(); |
| 610 | } |
| 611 | i--; |
| 612 | recentsCount--; |
| 613 | if (task.intent == null) { |
| 614 | // If the new recent task we are adding is not fully |
| 615 | // specified, then replace it with the existing recent task. |
| 616 | task = tr; |
| 617 | } |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 618 | notifyTaskPersisterLocked(tr, false); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | return -1; |
| 622 | } |
| 623 | |
| 624 | // Sort by taskId |
| 625 | private static Comparator<TaskRecord> sTaskRecordComparator = new Comparator<TaskRecord>() { |
| 626 | @Override |
| 627 | public int compare(TaskRecord lhs, TaskRecord rhs) { |
| 628 | return rhs.taskId - lhs.taskId; |
| 629 | } |
| 630 | }; |
| 631 | |
| 632 | // Extract the affiliates of the chain containing recent at index start. |
| 633 | private int processNextAffiliateChainLocked(int start) { |
| 634 | final TaskRecord startTask = get(start); |
| 635 | final int affiliateId = startTask.mAffiliatedTaskId; |
| 636 | |
| 637 | // Quick identification of isolated tasks. I.e. those not launched behind. |
| 638 | if (startTask.taskId == affiliateId && startTask.mPrevAffiliate == null && |
| 639 | startTask.mNextAffiliate == null) { |
| 640 | // There is still a slim chance that there are other tasks that point to this task |
| 641 | // and that the chain is so messed up that this task no longer points to them but |
| 642 | // the gain of this optimization outweighs the risk. |
| 643 | startTask.inRecents = true; |
| 644 | return start + 1; |
| 645 | } |
| 646 | |
| 647 | // Remove all tasks that are affiliated to affiliateId and put them in mTmpRecents. |
| 648 | mTmpRecents.clear(); |
| 649 | for (int i = size() - 1; i >= start; --i) { |
| 650 | final TaskRecord task = get(i); |
| 651 | if (task.mAffiliatedTaskId == affiliateId) { |
| 652 | remove(i); |
| 653 | mTmpRecents.add(task); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | // Sort them all by taskId. That is the order they were create in and that order will |
| 658 | // always be correct. |
| 659 | Collections.sort(mTmpRecents, sTaskRecordComparator); |
| 660 | |
| 661 | // Go through and fix up the linked list. |
| 662 | // The first one is the end of the chain and has no next. |
| 663 | final TaskRecord first = mTmpRecents.get(0); |
| 664 | first.inRecents = true; |
| 665 | if (first.mNextAffiliate != null) { |
| 666 | Slog.w(TAG, "Link error 1 first.next=" + first.mNextAffiliate); |
| 667 | first.setNextAffiliate(null); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 668 | notifyTaskPersisterLocked(first, false); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 669 | } |
| 670 | // Everything in the middle is doubly linked from next to prev. |
| 671 | final int tmpSize = mTmpRecents.size(); |
| 672 | for (int i = 0; i < tmpSize - 1; ++i) { |
| 673 | final TaskRecord next = mTmpRecents.get(i); |
| 674 | final TaskRecord prev = mTmpRecents.get(i + 1); |
| 675 | if (next.mPrevAffiliate != prev) { |
| 676 | Slog.w(TAG, "Link error 2 next=" + next + " prev=" + next.mPrevAffiliate + |
| 677 | " setting prev=" + prev); |
| 678 | next.setPrevAffiliate(prev); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 679 | notifyTaskPersisterLocked(next, false); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 680 | } |
| 681 | if (prev.mNextAffiliate != next) { |
| 682 | Slog.w(TAG, "Link error 3 prev=" + prev + " next=" + prev.mNextAffiliate + |
| 683 | " setting next=" + next); |
| 684 | prev.setNextAffiliate(next); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 685 | notifyTaskPersisterLocked(prev, false); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 686 | } |
| 687 | prev.inRecents = true; |
| 688 | } |
| 689 | // The last one is the beginning of the list and has no prev. |
| 690 | final TaskRecord last = mTmpRecents.get(tmpSize - 1); |
| 691 | if (last.mPrevAffiliate != null) { |
| 692 | Slog.w(TAG, "Link error 4 last.prev=" + last.mPrevAffiliate); |
| 693 | last.setPrevAffiliate(null); |
Suprabh Shukla | 09a88f5 | 2015-12-02 14:36:31 -0800 | [diff] [blame] | 694 | notifyTaskPersisterLocked(last, false); |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | // Insert the group back into mRecentTasks at start. |
| 698 | addAll(start, mTmpRecents); |
| 699 | mTmpRecents.clear(); |
| 700 | |
| 701 | // Let the caller know where we left off. |
| 702 | return start + tmpSize; |
| 703 | } |
Wale Ogunwale | c82f2f5 | 2014-12-09 09:32:50 -0800 | [diff] [blame] | 704 | } |