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 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager.RunningTaskInfo; |
| 20 | import android.app.WindowConfiguration.ActivityType; |
| 21 | import android.app.WindowConfiguration.WindowingMode; |
Nicholas Sauer | d6b4452 | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 22 | import android.util.ArraySet; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Comparator; |
| 26 | import java.util.Iterator; |
| 27 | import java.util.List; |
| 28 | import java.util.TreeSet; |
| 29 | |
| 30 | /** |
| 31 | * Class for resolving the set of running tasks in the system. |
| 32 | */ |
| 33 | class RunningTasks { |
| 34 | |
| 35 | // Comparator to sort by last active time (descending) |
| 36 | private static final Comparator<TaskRecord> LAST_ACTIVE_TIME_COMPARATOR = |
| 37 | (o1, o2) -> Long.signum(o2.lastActiveTime - o1.lastActiveTime); |
| 38 | |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 39 | private final TreeSet<TaskRecord> mTmpSortedSet = new TreeSet<>(LAST_ACTIVE_TIME_COMPARATOR); |
| 40 | private final ArrayList<TaskRecord> mTmpStackTasks = new ArrayList<>(); |
| 41 | |
| 42 | void getTasks(int maxNum, List<RunningTaskInfo> list, @ActivityType int ignoreActivityType, |
Riddle Hsu | 86cb7de | 2018-08-13 23:29:58 +0800 | [diff] [blame] | 43 | @WindowingMode int ignoreWindowingMode, ArrayList<ActivityDisplay> activityDisplays, |
Nicholas Sauer | d6b4452 | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 44 | int callingUid, boolean allowed, boolean crossUser, ArraySet<Integer> profileIds) { |
Winson Chung | 3907285 | 2017-11-01 17:09:48 -0700 | [diff] [blame] | 45 | // Return early if there are no tasks to fetch |
| 46 | if (maxNum <= 0) { |
| 47 | return; |
| 48 | } |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 49 | |
| 50 | // Gather all of the tasks across all of the tasks, and add them to the sorted set |
| 51 | mTmpSortedSet.clear(); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 52 | final int numDisplays = activityDisplays.size(); |
| 53 | for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) { |
Riddle Hsu | 86cb7de | 2018-08-13 23:29:58 +0800 | [diff] [blame] | 54 | final ActivityDisplay display = activityDisplays.get(displayNdx); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 55 | for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) { |
| 56 | final ActivityStack stack = display.getChildAt(stackNdx); |
Winson Chung | d219960 | 2018-08-10 10:53:32 -0700 | [diff] [blame] | 57 | mTmpStackTasks.clear(); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 58 | stack.getRunningTasks(mTmpStackTasks, ignoreActivityType, ignoreWindowingMode, |
Nicholas Sauer | d6b4452 | 2019-09-10 20:23:41 -0700 | [diff] [blame] | 59 | callingUid, allowed, crossUser, profileIds); |
Winson Chung | d219960 | 2018-08-10 10:53:32 -0700 | [diff] [blame] | 60 | mTmpSortedSet.addAll(mTmpStackTasks); |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | // Take the first {@param maxNum} tasks and create running task infos for them |
| 65 | final Iterator<TaskRecord> iter = mTmpSortedSet.iterator(); |
| 66 | while (iter.hasNext()) { |
| 67 | if (maxNum == 0) { |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | final TaskRecord task = iter.next(); |
| 72 | list.add(createRunningTaskInfo(task)); |
| 73 | maxNum--; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Constructs a {@link RunningTaskInfo} from a given {@param task}. |
| 79 | */ |
| 80 | private RunningTaskInfo createRunningTaskInfo(TaskRecord task) { |
Winson Chung | abfdcce | 2018-07-02 17:23:33 -0700 | [diff] [blame] | 81 | final RunningTaskInfo rti = new RunningTaskInfo(); |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 82 | task.fillTaskInfo(rti); |
Winson Chung | abfdcce | 2018-07-02 17:23:33 -0700 | [diff] [blame] | 83 | // Fill in some deprecated values |
| 84 | rti.id = rti.taskId; |
| 85 | return rti; |
Winson Chung | 61c9e5a | 2017-10-11 10:39:32 -0700 | [diff] [blame] | 86 | } |
| 87 | } |