Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 17 | package com.android.server.wm; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 18 | |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED; |
| 20 | import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
| 21 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 22 | import android.app.ActivityManager.RunningTaskInfo; |
| 23 | import android.app.WindowConfiguration.ActivityType; |
| 24 | import android.app.WindowConfiguration.WindowingMode; |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 25 | import android.os.UserHandle; |
Nicholas Sauer | 3f9249f | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 26 | import android.util.ArraySet; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 27 | |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 28 | import com.android.internal.util.function.pooled.PooledConsumer; |
| 29 | import com.android.internal.util.function.pooled.PooledLambda; |
| 30 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 31 | import java.util.ArrayList; |
| 32 | import java.util.Comparator; |
| 33 | import java.util.Iterator; |
| 34 | import java.util.List; |
| 35 | import java.util.TreeSet; |
| 36 | |
| 37 | /** |
| 38 | * Class for resolving the set of running tasks in the system. |
| 39 | */ |
| 40 | class RunningTasks { |
| 41 | |
| 42 | // Comparator to sort by last active time (descending) |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 43 | private static final Comparator<Task> LAST_ACTIVE_TIME_COMPARATOR = |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 44 | (o1, o2) -> Long.signum(o2.lastActiveTime - o1.lastActiveTime); |
| 45 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 46 | private final TreeSet<Task> mTmpSortedSet = new TreeSet<>(LAST_ACTIVE_TIME_COMPARATOR); |
| 47 | private final ArrayList<Task> mTmpStackTasks = new ArrayList<>(); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 48 | |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 49 | private int mCallingUid; |
| 50 | private int mUserId; |
| 51 | private boolean mCrossUser; |
| 52 | private ArraySet<Integer> mProfileIds; |
| 53 | private boolean mAllowed; |
| 54 | private int mIgnoreActivityType; |
| 55 | private int mIgnoreWindowingMode; |
| 56 | private ActivityStack mTopDisplayFocusStack; |
| 57 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 58 | void getTasks(int maxNum, List<RunningTaskInfo> list, @ActivityType int ignoreActivityType, |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 59 | @WindowingMode int ignoreWindowingMode, RootActivityContainer root, |
Nicholas Sauer | 3f9249f | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 60 | int callingUid, boolean allowed, boolean crossUser, ArraySet<Integer> profileIds) { |
Winson Chung | 3907285 | 2017-11-01 17:09:48 -0700 | [diff] [blame] | 61 | // Return early if there are no tasks to fetch |
| 62 | if (maxNum <= 0) { |
| 63 | return; |
| 64 | } |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 65 | |
| 66 | // Gather all of the tasks across all of the tasks, and add them to the sorted set |
| 67 | mTmpSortedSet.clear(); |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 68 | mCallingUid = callingUid; |
| 69 | mUserId = UserHandle.getUserId(callingUid); |
| 70 | mCrossUser = crossUser; |
| 71 | mProfileIds = profileIds; |
| 72 | mAllowed = allowed; |
| 73 | mIgnoreActivityType = ignoreActivityType; |
| 74 | mIgnoreWindowingMode = ignoreWindowingMode; |
| 75 | mTopDisplayFocusStack = root.getTopDisplayFocusedStack(); |
| 76 | |
| 77 | final PooledConsumer c = PooledLambda.obtainConsumer(RunningTasks::processTask, this, |
| 78 | PooledLambda.__(Task.class)); |
| 79 | root.mRootWindowContainer.forAllTasks(c, false); |
| 80 | c.recycle(); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 81 | |
| 82 | // Take the first {@param maxNum} tasks and create running task infos for them |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 83 | final Iterator<Task> iter = mTmpSortedSet.iterator(); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 84 | while (iter.hasNext()) { |
| 85 | if (maxNum == 0) { |
| 86 | break; |
| 87 | } |
| 88 | |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 89 | final Task task = iter.next(); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 90 | list.add(createRunningTaskInfo(task)); |
| 91 | maxNum--; |
| 92 | } |
| 93 | } |
| 94 | |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 95 | private void processTask(Task task) { |
| 96 | if (task.getTopNonFinishingActivity() == null) { |
| 97 | // Skip if there are no activities in the task |
| 98 | return; |
| 99 | } |
| 100 | if (task.effectiveUid != mCallingUid) { |
| 101 | if (task.mUserId != mUserId && !mCrossUser && !mProfileIds.contains(task.mUserId)) { |
| 102 | // Skip if the caller does not have cross user permission or cannot access |
| 103 | // the task's profile |
| 104 | return; |
| 105 | } |
| 106 | if (!mAllowed && !task.isActivityTypeHome()) { |
| 107 | // Skip if the caller isn't allowed to fetch this task, except for the home |
| 108 | // task which we always return. |
| 109 | return; |
| 110 | } |
| 111 | } |
| 112 | if (mIgnoreActivityType != ACTIVITY_TYPE_UNDEFINED |
| 113 | && task.getActivityType() == mIgnoreActivityType) { |
| 114 | // Skip ignored activity type |
| 115 | return; |
| 116 | } |
| 117 | if (mIgnoreWindowingMode != WINDOWING_MODE_UNDEFINED |
| 118 | && task.getWindowingMode() == mIgnoreWindowingMode) { |
| 119 | // Skip ignored windowing mode |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | final ActivityStack stack = task.getStack(); |
| 124 | if (stack == mTopDisplayFocusStack && stack.getTopMostTask() == task) { |
| 125 | // For the focused stack top task, update the last stack active time so that it can be |
| 126 | // used to determine the order of the tasks (it may not be set for newly created tasks) |
| 127 | task.touchActiveTime(); |
| 128 | } |
| 129 | |
| 130 | mTmpSortedSet.add(task); |
| 131 | } |
| 132 | |
| 133 | /** Constructs a {@link RunningTaskInfo} from a given {@param task}. */ |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 134 | private RunningTaskInfo createRunningTaskInfo(Task task) { |
Winson Chung | abfdcce | 2018-07-02 17:23:33 -0700 | [diff] [blame] | 135 | final RunningTaskInfo rti = new RunningTaskInfo(); |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 136 | task.fillTaskInfo(rti); |
Winson Chung | abfdcce | 2018-07-02 17:23:33 -0700 | [diff] [blame] | 137 | // Fill in some deprecated values |
| 138 | rti.id = rti.taskId; |
| 139 | return rti; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 140 | } |
| 141 | } |