blob: 3bf437d38bcc4fba148414cd0a7d0ae7a962e65c [file] [log] [blame]
Winson Chung61c9e5a2017-10-11 10:39:32 -07001/*
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 Ogunwale59507092018-10-29 09:00:30 -070017package com.android.server.wm;
Winson Chung61c9e5a2017-10-11 10:39:32 -070018
Winson Chung61c9e5a2017-10-11 10:39:32 -070019import android.app.ActivityManager.RunningTaskInfo;
20import android.app.WindowConfiguration.ActivityType;
21import android.app.WindowConfiguration.WindowingMode;
Winson Chung61c9e5a2017-10-11 10:39:32 -070022
23import java.util.ArrayList;
24import java.util.Comparator;
25import java.util.Iterator;
26import java.util.List;
27import java.util.TreeSet;
28
29/**
30 * Class for resolving the set of running tasks in the system.
31 */
32class RunningTasks {
33
34 // Comparator to sort by last active time (descending)
35 private static final Comparator<TaskRecord> LAST_ACTIVE_TIME_COMPARATOR =
36 (o1, o2) -> Long.signum(o2.lastActiveTime - o1.lastActiveTime);
37
Winson Chung61c9e5a2017-10-11 10:39:32 -070038 private final TreeSet<TaskRecord> mTmpSortedSet = new TreeSet<>(LAST_ACTIVE_TIME_COMPARATOR);
39 private final ArrayList<TaskRecord> mTmpStackTasks = new ArrayList<>();
40
41 void getTasks(int maxNum, List<RunningTaskInfo> list, @ActivityType int ignoreActivityType,
Riddle Hsu86cb7de2018-08-13 23:29:58 +080042 @WindowingMode int ignoreWindowingMode, ArrayList<ActivityDisplay> activityDisplays,
Winson Chung61c9e5a2017-10-11 10:39:32 -070043 int callingUid, boolean allowed) {
Winson Chung39072852017-11-01 17:09:48 -070044 // Return early if there are no tasks to fetch
45 if (maxNum <= 0) {
46 return;
47 }
Winson Chung61c9e5a2017-10-11 10:39:32 -070048
49 // Gather all of the tasks across all of the tasks, and add them to the sorted set
50 mTmpSortedSet.clear();
Winson Chung61c9e5a2017-10-11 10:39:32 -070051 final int numDisplays = activityDisplays.size();
52 for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
Riddle Hsu86cb7de2018-08-13 23:29:58 +080053 final ActivityDisplay display = activityDisplays.get(displayNdx);
Winson Chung61c9e5a2017-10-11 10:39:32 -070054 for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
55 final ActivityStack stack = display.getChildAt(stackNdx);
Winson Chungd2199602018-08-10 10:53:32 -070056 mTmpStackTasks.clear();
Winson Chung61c9e5a2017-10-11 10:39:32 -070057 stack.getRunningTasks(mTmpStackTasks, ignoreActivityType, ignoreWindowingMode,
58 callingUid, allowed);
Winson Chungd2199602018-08-10 10:53:32 -070059 mTmpSortedSet.addAll(mTmpStackTasks);
Winson Chung61c9e5a2017-10-11 10:39:32 -070060 }
61 }
62
63 // Take the first {@param maxNum} tasks and create running task infos for them
64 final Iterator<TaskRecord> iter = mTmpSortedSet.iterator();
65 while (iter.hasNext()) {
66 if (maxNum == 0) {
67 break;
68 }
69
70 final TaskRecord task = iter.next();
71 list.add(createRunningTaskInfo(task));
72 maxNum--;
73 }
74 }
75
76 /**
77 * Constructs a {@link RunningTaskInfo} from a given {@param task}.
78 */
79 private RunningTaskInfo createRunningTaskInfo(TaskRecord task) {
Winson Chungabfdcce2018-07-02 17:23:33 -070080 final RunningTaskInfo rti = new RunningTaskInfo();
Mark Renoufc808f062019-02-07 15:20:37 -050081 task.fillTaskInfo(rti);
Winson Chungabfdcce2018-07-02 17:23:33 -070082 // Fill in some deprecated values
83 rti.id = rti.taskId;
84 return rti;
Winson Chung61c9e5a2017-10-11 10:39:32 -070085 }
86}