blob: abcb563339aaf2f1c7cd60f4f307aa66579c99df [file] [log] [blame]
Winson Chunga91c2932014-11-07 15:02:38 -08001/*
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
17package com.android.systemui.recents.model;
18
19import android.app.ActivityManager;
20import android.content.Context;
Winson59e18722016-02-16 09:28:54 -080021import android.content.pm.ActivityInfo;
Winson8be16342016-02-09 11:53:27 -080022import android.content.pm.ApplicationInfo;
Rubin Xuefb844d2015-12-08 20:57:24 +000023import android.content.pm.UserInfo;
Winson Chunga91c2932014-11-07 15:02:38 -080024import android.content.res.Resources;
25import android.graphics.Bitmap;
26import android.graphics.drawable.Drawable;
27import android.os.UserHandle;
Rubin Xuefb844d2015-12-08 20:57:24 +000028import android.os.UserManager;
29import android.util.ArraySet;
Winson65c851e2016-01-20 12:43:35 -080030import android.util.SparseArray;
31import android.util.SparseIntArray;
Winsonc0d70582016-01-29 10:24:39 -080032
Winson250608a2015-11-24 15:00:31 -080033import com.android.systemui.Prefs;
Winson55003902016-01-12 12:00:37 -080034import com.android.systemui.R;
Winsone7f138c2015-10-22 16:15:21 -070035import com.android.systemui.recents.Recents;
Winson Chunga91c2932014-11-07 15:02:38 -080036import com.android.systemui.recents.RecentsConfiguration;
Winson6e6bd8772016-01-25 10:41:40 -080037import com.android.systemui.recents.RecentsDebugFlags;
Winson Chunga91c2932014-11-07 15:02:38 -080038import com.android.systemui.recents.misc.SystemServicesProxy;
39
40import java.util.ArrayList;
41import java.util.Collections;
Winson49df4202016-01-25 17:33:34 -080042import java.util.Formatter;
Winson Chunga91c2932014-11-07 15:02:38 -080043import java.util.List;
44
45
46/**
47 * This class stores the loading state as it goes through multiple stages of loading:
Winson Chung6ac8bd62015-01-07 16:38:35 -080048 * 1) preloadRawTasks() will load the raw set of recents tasks from the system
49 * 2) preloadPlan() will construct a new task stack with all metadata and only icons and
50 * thumbnails that are currently in the cache
51 * 3) executePlan() will actually load and fill in the icons and thumbnails according to the load
52 * options specified, such that we can transition into the Recents activity seamlessly
Winson Chunga91c2932014-11-07 15:02:38 -080053 */
54public class RecentsTaskLoadPlan {
Winson53ec42c2015-10-28 15:55:35 -070055
Winson250608a2015-11-24 15:00:31 -080056 private static int MIN_NUM_TASKS = 5;
Winson0c1a9b82015-12-03 10:59:07 -080057 private static int SESSION_BEGIN_TIME = 1000 /* ms/s */ * 60 /* s/min */ * 60 /* min/hr */ *
58 6 /* hrs */;
Winson Chunga91c2932014-11-07 15:02:38 -080059
60 /** The set of conditions to load tasks. */
61 public static class Options {
62 public int runningTaskId = -1;
63 public boolean loadIcons = true;
64 public boolean loadThumbnails = true;
Winson Chung90d51362014-11-13 14:30:26 -080065 public boolean onlyLoadForCache = false;
Winson Chung0eae5572014-12-11 11:04:19 -080066 public boolean onlyLoadPausedActivities = false;
Winson Chunga91c2932014-11-07 15:02:38 -080067 public int numVisibleTasks = 0;
68 public int numVisibleTaskThumbnails = 0;
69 }
70
71 Context mContext;
Winson Chunga91c2932014-11-07 15:02:38 -080072
73 List<ActivityManager.RecentTaskInfo> mRawTasks;
Winson147ecaf2015-09-16 16:49:55 -070074 TaskStack mStack;
Rubin Xuefb844d2015-12-08 20:57:24 +000075 ArraySet<Integer> mCurrentQuietProfiles = new ArraySet<Integer>();
Winson Chunga91c2932014-11-07 15:02:38 -080076
77 /** Package level ctor */
Winson53ec42c2015-10-28 15:55:35 -070078 RecentsTaskLoadPlan(Context context) {
Winson Chunga91c2932014-11-07 15:02:38 -080079 mContext = context;
Winson Chunga91c2932014-11-07 15:02:38 -080080 }
81
Rubin Xuefb844d2015-12-08 20:57:24 +000082 private void updateCurrentQuietProfilesCache(int currentUserId) {
83 mCurrentQuietProfiles.clear();
84
85 if (currentUserId == UserHandle.USER_CURRENT) {
86 currentUserId = ActivityManager.getCurrentUser();
87 }
88 UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
89 List<UserInfo> profiles = userManager.getProfiles(currentUserId);
90 if (profiles != null) {
91 for (int i = 0; i < profiles.size(); i++) {
92 UserInfo user = profiles.get(i);
93 if (user.isManagedProfile() && user.isQuietModeEnabled()) {
94 mCurrentQuietProfiles.add(user.id);
95 }
96 }
97 }
98 }
99
Winson Chunga91c2932014-11-07 15:02:38 -0800100 /**
Winson65c851e2016-01-20 12:43:35 -0800101 * An optimization to preload the raw list of tasks. The raw tasks are saved in least-recent
Winson53ec42c2015-10-28 15:55:35 -0700102 * to most-recent order.
Winson Chunga91c2932014-11-07 15:02:38 -0800103 */
104 public synchronized void preloadRawTasks(boolean isTopTaskHome) {
Rubin Xuefb844d2015-12-08 20:57:24 +0000105 int currentUserId = UserHandle.USER_CURRENT;
106 updateCurrentQuietProfilesCache(currentUserId);
Winsone7f138c2015-10-22 16:15:21 -0700107 SystemServicesProxy ssp = Recents.getSystemServices();
108 mRawTasks = ssp.getRecentTasks(ActivityManager.getMaxRecentTasksStatic(),
Rubin Xuefb844d2015-12-08 20:57:24 +0000109 currentUserId, isTopTaskHome, mCurrentQuietProfiles);
110
Winson53ec42c2015-10-28 15:55:35 -0700111 // Since the raw tasks are given in most-recent to least-recent order, we need to reverse it
Winson Chunga91c2932014-11-07 15:02:38 -0800112 Collections.reverse(mRawTasks);
Winson Chunga91c2932014-11-07 15:02:38 -0800113 }
114
115 /**
Winson65c851e2016-01-20 12:43:35 -0800116 * Preloads the list of recent tasks from the system. After this call, the TaskStack will
Winson Chunga91c2932014-11-07 15:02:38 -0800117 * have a list of all the recent tasks with their metadata, not including icons or
118 * thumbnails which were not cached and have to be loaded.
Winson53ec42c2015-10-28 15:55:35 -0700119 *
120 * The tasks will be ordered by:
121 * - least-recent to most-recent stack tasks
122 * - least-recent to most-recent freeform tasks
Winson Chunga91c2932014-11-07 15:02:38 -0800123 */
Winson65c851e2016-01-20 12:43:35 -0800124 public synchronized void preloadPlan(RecentsTaskLoader loader, int topTaskId,
125 boolean isTopTaskHome) {
Winson Chunga91c2932014-11-07 15:02:38 -0800126 Resources res = mContext.getResources();
Winson Chung509d0d02015-12-16 15:43:12 -0500127 ArrayList<Task> allTasks = new ArrayList<>();
Winson Chunga91c2932014-11-07 15:02:38 -0800128 if (mRawTasks == null) {
129 preloadRawTasks(isTopTaskHome);
130 }
Winson250608a2015-11-24 15:00:31 -0800131
Winson65c851e2016-01-20 12:43:35 -0800132 SparseArray<Task.TaskKey> affiliatedTasks = new SparseArray<>();
133 SparseIntArray affiliatedTaskCounts = new SparseIntArray();
Winson55003902016-01-12 12:00:37 -0800134 String dismissDescFormat = mContext.getString(
135 R.string.accessibility_recents_item_will_be_dismissed);
Winson250608a2015-11-24 15:00:31 -0800136 long lastStackActiveTime = Prefs.getLong(mContext,
137 Prefs.Key.OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME, 0);
Winson6e6bd8772016-01-25 10:41:40 -0800138 if (RecentsDebugFlags.Static.EnableMockTasks) {
139 lastStackActiveTime = 0;
140 }
Winson250608a2015-11-24 15:00:31 -0800141 long newLastStackActiveTime = -1;
Winsondf1020c2016-02-01 10:05:10 -0800142 long prevLastActiveTime = lastStackActiveTime;
Winson Chunga91c2932014-11-07 15:02:38 -0800143 int taskCount = mRawTasks.size();
144 for (int i = 0; i < taskCount; i++) {
145 ActivityManager.RecentTaskInfo t = mRawTasks.get(i);
146
Winsondf1020c2016-02-01 10:05:10 -0800147 /*
148 * Affiliated tasks are returned in a specific order from ActivityManager but without a
149 * lastActiveTime since it hasn't yet been started. However, we later sort the task list
150 * by lastActiveTime, which rearranges the tasks. For now, we need to workaround this
151 * by updating the lastActiveTime of this task to the lastActiveTime of the task it is
152 * affiliated with, in the same order that we encounter it in the original list (just
153 * its index in the task group for the task it is affiliated with).
154 *
155 * If the parent task is not available, then we will use the last active time of the
156 * previous task as a base point (since the task itself may not have an active time)
157 * for the entire affiliated group.
158 */
Winson65c851e2016-01-20 12:43:35 -0800159 if (t.persistentId != t.affiliatedTaskId) {
Winsondf1020c2016-02-01 10:05:10 -0800160 Task.TaskKey parentTask = affiliatedTasks.get(t.affiliatedTaskId);
161 long parentTaskLastActiveTime = parentTask != null
162 ? parentTask.lastActiveTime
163 : prevLastActiveTime;
164 t.lastActiveTime = parentTaskLastActiveTime +
Winson65c851e2016-01-20 12:43:35 -0800165 affiliatedTaskCounts.get(t.affiliatedTaskId, 0) + 1;
166 }
167
Winson Chunga91c2932014-11-07 15:02:38 -0800168 // Compose the task key
Winson Chungd16c5652015-01-26 16:11:07 -0800169 Task.TaskKey taskKey = new Task.TaskKey(t.persistentId, t.stackId, t.baseIntent,
170 t.userId, t.firstActiveTime, t.lastActiveTime);
Winson Chunga91c2932014-11-07 15:02:38 -0800171
Winson250608a2015-11-24 15:00:31 -0800172 // This task is only shown in the stack if it statisfies the historical time or min
173 // number of tasks constraints. Freeform tasks are also always shown.
Winson Chungead5c0f2015-12-14 11:18:57 -0500174 boolean isFreeformTask = SystemServicesProxy.isFreeformStack(t.stackId);
Winson Chung296278a2015-12-17 12:09:02 -0500175 boolean isStackTask = isFreeformTask || (!isHistoricalTask(t) ||
176 (t.lastActiveTime >= lastStackActiveTime && i >= (taskCount - MIN_NUM_TASKS)));
Winson65c851e2016-01-20 12:43:35 -0800177 boolean isLaunchTarget = taskKey.id == topTaskId;
Winson Chungead5c0f2015-12-14 11:18:57 -0500178 if (isStackTask && newLastStackActiveTime < 0) {
179 newLastStackActiveTime = t.lastActiveTime;
Winson250608a2015-11-24 15:00:31 -0800180 }
181
Winson Chung296278a2015-12-17 12:09:02 -0500182 // Load the title, icon, and color
Winson59e18722016-02-16 09:28:54 -0800183 ActivityInfo info = loader.getAndUpdateActivityInfo(taskKey);
Winson Chung296278a2015-12-17 12:09:02 -0500184 String title = loader.getAndUpdateActivityTitle(taskKey, t.taskDescription);
Winsonc5ef63f2016-01-21 14:39:23 -0800185 String contentDescription = loader.getAndUpdateContentDescription(taskKey, res);
Winson9da18852016-03-01 11:56:30 -0800186 String dismissDescription = String.format(dismissDescFormat, contentDescription);
Winson Chung296278a2015-12-17 12:09:02 -0500187 Drawable icon = isStackTask
188 ? loader.getAndUpdateActivityIcon(taskKey, t.taskDescription, res, false)
Winson250608a2015-11-24 15:00:31 -0800189 : null;
Winson Chung296278a2015-12-17 12:09:02 -0500190 Bitmap thumbnail = loader.getAndUpdateThumbnail(taskKey, false);
Winsone7f138c2015-10-22 16:15:21 -0700191 int activityColor = loader.getActivityPrimaryColor(t.taskDescription);
Winson Chung1af8eda2016-02-05 17:55:56 +0000192 int backgroundColor = loader.getActivityBackgroundColor(t.taskDescription);
Winson59e18722016-02-16 09:28:54 -0800193 boolean isSystemApp = (info != null) &&
194 ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
Winson Chunga91c2932014-11-07 15:02:38 -0800195
Winson Chunga91c2932014-11-07 15:02:38 -0800196 // Add the task to the stack
Winson Chung296278a2015-12-17 12:09:02 -0500197 Task task = new Task(taskKey, t.affiliatedTaskId, t.affiliatedTaskColor, icon,
Winson55003902016-01-12 12:00:37 -0800198 thumbnail, title, contentDescription, dismissDescription, activityColor,
Winson931845f2016-02-24 19:38:41 -0800199 backgroundColor, !isStackTask, isLaunchTarget, isSystemApp, t.isDockable,
200 t.bounds, t.taskDescription);
Winson Chunga91c2932014-11-07 15:02:38 -0800201
Winson Chung509d0d02015-12-16 15:43:12 -0500202 allTasks.add(task);
Winson65c851e2016-01-20 12:43:35 -0800203 affiliatedTaskCounts.put(taskKey.id, affiliatedTaskCounts.get(taskKey.id, 0) + 1);
204 affiliatedTasks.put(taskKey.id, taskKey);
Winsondf1020c2016-02-01 10:05:10 -0800205
206 prevLastActiveTime = t.lastActiveTime;
Winson Chungd16c5652015-01-26 16:11:07 -0800207 }
Winson Chungead5c0f2015-12-14 11:18:57 -0500208 if (newLastStackActiveTime != -1) {
Winson250608a2015-11-24 15:00:31 -0800209 Prefs.putLong(mContext, Prefs.Key.OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME,
210 newLastStackActiveTime);
211 }
Winson Chungd16c5652015-01-26 16:11:07 -0800212
213 // Initialize the stacks
Winson147ecaf2015-09-16 16:49:55 -0700214 mStack = new TaskStack();
Winson Chung06266772015-12-11 10:24:21 -0500215 mStack.setTasks(allTasks, false /* notifyStackChanges */);
Winson35f30502015-09-28 11:24:36 -0700216 mStack.createAffiliatedGroupings(mContext);
Winson Chunga91c2932014-11-07 15:02:38 -0800217 }
218
219 /**
220 * Called to apply the actual loading based on the specified conditions.
221 */
Winsone7f138c2015-10-22 16:15:21 -0700222 public synchronized void executePlan(Options opts, RecentsTaskLoader loader,
Winson Chung96d70412014-11-12 14:17:17 -0800223 TaskResourceLoadQueue loadQueue) {
Winson53ec42c2015-10-28 15:55:35 -0700224 RecentsConfiguration config = Recents.getConfiguration();
Winson Chunga91c2932014-11-07 15:02:38 -0800225 Resources res = mContext.getResources();
226
227 // Iterate through each of the tasks and load them according to the load conditions.
Winson250608a2015-11-24 15:00:31 -0800228 ArrayList<Task> tasks = mStack.getStackTasks();
Winson147ecaf2015-09-16 16:49:55 -0700229 int taskCount = tasks.size();
230 for (int i = 0; i < taskCount; i++) {
Winson147ecaf2015-09-16 16:49:55 -0700231 Task task = tasks.get(i);
232 Task.TaskKey taskKey = task.key;
Winson Chunga91c2932014-11-07 15:02:38 -0800233
Winson147ecaf2015-09-16 16:49:55 -0700234 boolean isRunningTask = (task.key.id == opts.runningTaskId);
235 boolean isVisibleTask = i >= (taskCount - opts.numVisibleTasks);
236 boolean isVisibleThumbnail = i >= (taskCount - opts.numVisibleTaskThumbnails);
237
238 // If requested, skip the running task
239 if (opts.onlyLoadPausedActivities && isRunningTask) {
240 continue;
241 }
242
243 if (opts.loadIcons && (isRunningTask || isVisibleTask)) {
Winson Chung296278a2015-12-17 12:09:02 -0500244 if (task.icon == null) {
245 task.icon = loader.getAndUpdateActivityIcon(taskKey, task.taskDescription,
246 res, true);
Winson Chunga91c2932014-11-07 15:02:38 -0800247 }
Winson147ecaf2015-09-16 16:49:55 -0700248 }
249 if (opts.loadThumbnails && (isRunningTask || isVisibleThumbnail)) {
250 if (task.thumbnail == null || isRunningTask) {
Winson53ec42c2015-10-28 15:55:35 -0700251 if (config.svelteLevel <= RecentsConfiguration.SVELTE_LIMIT_CACHE) {
Winson Chung296278a2015-12-17 12:09:02 -0500252 task.thumbnail = loader.getAndUpdateThumbnail(taskKey, true);
Winson53ec42c2015-10-28 15:55:35 -0700253 } else if (config.svelteLevel == RecentsConfiguration.SVELTE_DISABLE_CACHE) {
Winson147ecaf2015-09-16 16:49:55 -0700254 loadQueue.addTask(task);
Winson Chung96d70412014-11-12 14:17:17 -0800255 }
Winson Chunga91c2932014-11-07 15:02:38 -0800256 }
Winson147ecaf2015-09-16 16:49:55 -0700257 }
Winson Chunga91c2932014-11-07 15:02:38 -0800258 }
259 }
260
261 /**
Winson147ecaf2015-09-16 16:49:55 -0700262 * Returns the TaskStack from the preloaded list of recent tasks.
Winson Chunga91c2932014-11-07 15:02:38 -0800263 */
Winson147ecaf2015-09-16 16:49:55 -0700264 public TaskStack getTaskStack() {
265 return mStack;
Winson Chungd16c5652015-01-26 16:11:07 -0800266 }
267
268 /** Returns whether there are any tasks in any stacks. */
269 public boolean hasTasks() {
Winson147ecaf2015-09-16 16:49:55 -0700270 if (mStack != null) {
Winson4b057c62016-01-12 15:01:52 -0800271 return mStack.getTaskCount() > 0;
Winson Chungd16c5652015-01-26 16:11:07 -0800272 }
273 return false;
Winson Chunga91c2932014-11-07 15:02:38 -0800274 }
Winson250608a2015-11-24 15:00:31 -0800275
276 /**
277 * Returns whether this task is considered a task to be shown in the history.
278 */
279 private boolean isHistoricalTask(ActivityManager.RecentTaskInfo t) {
280 return t.lastActiveTime < (System.currentTimeMillis() - SESSION_BEGIN_TIME);
281 }
Winson Chunga91c2932014-11-07 15:02:38 -0800282}