Merge "Allow getTasks to filter tasks by displayId." into tm-qpr-dev

This commit is contained in:
Yuncheol Heo
2022-08-04 20:54:51 +00:00
committed by Android (Google) Code Review
9 changed files with 109 additions and 27 deletions

View File

@@ -16,6 +16,8 @@
package android.app; package android.app;
import static android.view.Display.INVALID_DISPLAY;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.annotation.RequiresPermission; import android.annotation.RequiresPermission;
@@ -369,7 +371,8 @@ public class ActivityTaskManager {
* @hide * @hide
*/ */
public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum) { public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum) {
return getTasks(maxNum, false /* filterForVisibleRecents */); return getTasks(maxNum, false /* filterForVisibleRecents */, false /* keepIntentExtra */,
INVALID_DISPLAY);
} }
/** /**
@@ -378,7 +381,8 @@ public class ActivityTaskManager {
*/ */
public List<ActivityManager.RunningTaskInfo> getTasks( public List<ActivityManager.RunningTaskInfo> getTasks(
int maxNum, boolean filterOnlyVisibleRecents) { int maxNum, boolean filterOnlyVisibleRecents) {
return getTasks(maxNum, filterOnlyVisibleRecents, false /* keepIntentExtra */); return getTasks(maxNum, filterOnlyVisibleRecents, false /* keepIntentExtra */,
INVALID_DISPLAY);
} }
/** /**
@@ -388,8 +392,20 @@ public class ActivityTaskManager {
*/ */
public List<ActivityManager.RunningTaskInfo> getTasks( public List<ActivityManager.RunningTaskInfo> getTasks(
int maxNum, boolean filterOnlyVisibleRecents, boolean keepIntentExtra) { int maxNum, boolean filterOnlyVisibleRecents, boolean keepIntentExtra) {
return getTasks(maxNum, filterOnlyVisibleRecents, keepIntentExtra, INVALID_DISPLAY);
}
/**
* @return List of running tasks that can be filtered by visibility and displayId in recents
* and keep intent extra.
* @param displayId the target display id, or {@link INVALID_DISPLAY} not to filter by displayId
* @hide
*/
public List<ActivityManager.RunningTaskInfo> getTasks(
int maxNum, boolean filterOnlyVisibleRecents, boolean keepIntentExtra, int displayId) {
try { try {
return getService().getTasks(maxNum, filterOnlyVisibleRecents, keepIntentExtra); return getService().getTasks(maxNum, filterOnlyVisibleRecents, keepIntentExtra,
displayId);
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }

View File

@@ -158,7 +158,7 @@ interface IActivityTaskManager {
boolean removeTask(int taskId); boolean removeTask(int taskId);
void removeAllVisibleRecentTasks(); void removeAllVisibleRecentTasks();
List<ActivityManager.RunningTaskInfo> getTasks(int maxNum, boolean filterOnlyVisibleRecents, List<ActivityManager.RunningTaskInfo> getTasks(int maxNum, boolean filterOnlyVisibleRecents,
boolean keepIntentExtra); boolean keepIntentExtra, int displayId);
void moveTaskToFront(in IApplicationThread app, in String callingPackage, int task, void moveTaskToFront(in IApplicationThread app, in String callingPackage, int task,
int flags, in Bundle options); int flags, in Bundle options);
ParceledListSlice<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, int flags, ParceledListSlice<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum, int flags,

View File

@@ -16,6 +16,8 @@
package com.android.server.app; package com.android.server.app;
import static android.view.Display.INVALID_DISPLAY;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.app.ActivityManager.RunningTaskInfo; import android.app.ActivityManager.RunningTaskInfo;
@@ -93,7 +95,8 @@ final class GameTaskInfoProvider {
runningTaskInfos = mActivityTaskManager.getTasks( runningTaskInfos = mActivityTaskManager.getTasks(
/* maxNum= */ Integer.MAX_VALUE, /* maxNum= */ Integer.MAX_VALUE,
/* filterOnlyVisibleRecents= */ false, /* filterOnlyVisibleRecents= */ false,
/* keepIntentExtra= */ false); /* keepIntentExtra= */ false,
INVALID_DISPLAY);
} catch (RemoteException ex) { } catch (RemoteException ex) {
Slog.w(TAG, "Failed to fetch running tasks"); Slog.w(TAG, "Failed to fetch running tasks");
return null; return null;

View File

@@ -2311,16 +2311,25 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
* @return a list of {@link ActivityManager.RunningTaskInfo} with up to {@code maxNum} items * @return a list of {@link ActivityManager.RunningTaskInfo} with up to {@code maxNum} items
*/ */
public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum) { public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum) {
return getTasks(maxNum, false /* filterForVisibleRecents */, false /* keepIntentExtra */); return getTasks(maxNum, false /* filterForVisibleRecents */, false /* keepIntentExtra */,
INVALID_DISPLAY);
} }
/** /**
* @param filterOnlyVisibleRecents whether to filter the tasks based on whether they would ever * @param filterOnlyVisibleRecents whether to filter the tasks based on whether they would ever
* be visible in the recent task list in systemui * be visible in the recent task list in systemui
*/ */
@Override
public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum, public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum,
boolean filterOnlyVisibleRecents, boolean keepIntentExtra) { boolean filterOnlyVisibleRecents, boolean keepIntentExtra) {
return getTasks(maxNum, filterOnlyVisibleRecents, keepIntentExtra, INVALID_DISPLAY);
}
/**
* @param displayId the target display id, or {@link INVALID_DISPLAY} not to filter by displayId
*/
@Override
public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum,
boolean filterOnlyVisibleRecents, boolean keepIntentExtra, int displayId) {
final int callingUid = Binder.getCallingUid(); final int callingUid = Binder.getCallingUid();
final int callingPid = Binder.getCallingPid(); final int callingPid = Binder.getCallingPid();
@@ -2342,7 +2351,7 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
final boolean allowed = isGetTasksAllowed("getTasks", callingPid, callingUid); final boolean allowed = isGetTasksAllowed("getTasks", callingPid, callingUid);
flags |= (allowed ? RunningTasks.FLAG_ALLOWED : 0); flags |= (allowed ? RunningTasks.FLAG_ALLOWED : 0);
mRootWindowContainer.getRunningTasks( mRootWindowContainer.getRunningTasks(
maxNum, list, flags, callingUid, callingProfileIds); maxNum, list, flags, callingUid, callingProfileIds, displayId);
} }
return list; return list;

View File

@@ -3345,9 +3345,16 @@ class RootWindowContainer extends WindowContainer<DisplayContent>
@VisibleForTesting @VisibleForTesting
void getRunningTasks(int maxNum, List<ActivityManager.RunningTaskInfo> list, void getRunningTasks(int maxNum, List<ActivityManager.RunningTaskInfo> list,
int flags, int callingUid, ArraySet<Integer> profileIds) { int flags, int callingUid, ArraySet<Integer> profileIds, int displayId) {
mTaskSupervisor.getRunningTasks().getTasks(maxNum, list, flags, this, callingUid, WindowContainer root = this;
profileIds); if (displayId != INVALID_DISPLAY) {
root = getDisplayContent(displayId);
if (root == null) {
return;
}
}
mTaskSupervisor.getRunningTasks().getTasks(maxNum, list, flags, mService.getRecentTasks(),
root, callingUid, profileIds);
} }
void startPowerModeLaunchIfNeeded(boolean forceSend, ActivityRecord targetActivity) { void startPowerModeLaunchIfNeeded(boolean forceSend, ActivityRecord targetActivity) {

View File

@@ -60,8 +60,8 @@ class RunningTasks {
private RecentTasks mRecentTasks; private RecentTasks mRecentTasks;
private boolean mKeepIntentExtra; private boolean mKeepIntentExtra;
void getTasks(int maxNum, List<RunningTaskInfo> list, int flags, void getTasks(int maxNum, List<RunningTaskInfo> list, int flags, RecentTasks recentTasks,
RootWindowContainer root, int callingUid, ArraySet<Integer> profileIds) { WindowContainer root, int callingUid, ArraySet<Integer> profileIds) {
// Return early if there are no tasks to fetch // Return early if there are no tasks to fetch
if (maxNum <= 0) { if (maxNum <= 0) {
return; return;
@@ -76,7 +76,7 @@ class RunningTasks {
mAllowed = (flags & FLAG_ALLOWED) == FLAG_ALLOWED; mAllowed = (flags & FLAG_ALLOWED) == FLAG_ALLOWED;
mFilterOnlyVisibleRecents = mFilterOnlyVisibleRecents =
(flags & FLAG_FILTER_ONLY_VISIBLE_RECENTS) == FLAG_FILTER_ONLY_VISIBLE_RECENTS; (flags & FLAG_FILTER_ONLY_VISIBLE_RECENTS) == FLAG_FILTER_ONLY_VISIBLE_RECENTS;
mRecentTasks = root.mService.getRecentTasks(); mRecentTasks = recentTasks;
mKeepIntentExtra = (flags & FLAG_KEEP_INTENT_EXTRA) == FLAG_KEEP_INTENT_EXTRA; mKeepIntentExtra = (flags & FLAG_KEEP_INTENT_EXTRA) == FLAG_KEEP_INTENT_EXTRA;
final PooledConsumer c = PooledLambda.obtainConsumer(RunningTasks::processTask, this, final PooledConsumer c = PooledLambda.obtainConsumer(RunningTasks::processTask, this,

View File

@@ -218,8 +218,8 @@ public final class GameServiceProviderInstanceImplTest {
}).when(mMockWindowManagerInternal).unregisterTaskSystemBarsListener(any()); }).when(mMockWindowManagerInternal).unregisterTaskSystemBarsListener(any());
mRunningTaskInfos = new ArrayList<>(); mRunningTaskInfos = new ArrayList<>();
when(mMockActivityTaskManager.getTasks(anyInt(), anyBoolean(), anyBoolean())).thenReturn( when(mMockActivityTaskManager.getTasks(anyInt(), anyBoolean(), anyBoolean(), anyInt()))
mRunningTaskInfos); .thenReturn(mRunningTaskInfos);
final UserHandle userHandle = new UserHandle(USER_ID); final UserHandle userHandle = new UserHandle(USER_ID);

View File

@@ -1531,10 +1531,10 @@ public class RecentTasksTest extends WindowTestsBase {
public boolean mLastAllowed; public boolean mLastAllowed;
@Override @Override
void getTasks(int maxNum, List<RunningTaskInfo> list, int flags, void getTasks(int maxNum, List<RunningTaskInfo> list, int flags, RecentTasks recentTasks,
RootWindowContainer root, int callingUid, ArraySet<Integer> profileIds) { WindowContainer root, int callingUid, ArraySet<Integer> profileIds) {
mLastAllowed = (flags & FLAG_ALLOWED) == FLAG_ALLOWED; mLastAllowed = (flags & FLAG_ALLOWED) == FLAG_ALLOWED;
super.getTasks(maxNum, list, flags, root, callingUid, profileIds); super.getTasks(maxNum, list, flags, recentTasks, root, callingUid, profileIds);
} }
} }
} }

View File

@@ -35,6 +35,8 @@ import android.util.ArraySet;
import androidx.test.filters.MediumTest; import androidx.test.filters.MediumTest;
import com.google.common.truth.Correspondence;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -52,6 +54,9 @@ import java.util.List;
public class RunningTasksTest extends WindowTestsBase { public class RunningTasksTest extends WindowTestsBase {
private static final ArraySet<Integer> PROFILE_IDS = new ArraySet<>(); private static final ArraySet<Integer> PROFILE_IDS = new ArraySet<>();
private static final Correspondence<RunningTaskInfo, Integer> TASKINFO_HAS_ID =
Correspondence.transforming((RunningTaskInfo t) -> t.taskId, "has id");
private RunningTasks mRunningTasks; private RunningTasks mRunningTasks;
@@ -91,8 +96,8 @@ public class RunningTasksTest extends WindowTestsBase {
// collected from all tasks across all the stacks // collected from all tasks across all the stacks
final int numFetchTasks = 5; final int numFetchTasks = 5;
ArrayList<RunningTaskInfo> tasks = new ArrayList<>(); ArrayList<RunningTaskInfo> tasks = new ArrayList<>();
mRunningTasks.getTasks(5, tasks, FLAG_ALLOWED | FLAG_CROSS_USERS, mRootWindowContainer, mRunningTasks.getTasks(5, tasks, FLAG_ALLOWED | FLAG_CROSS_USERS,
-1 /* callingUid */, PROFILE_IDS); mAtm.getRecentTasks(), mRootWindowContainer, -1 /* callingUid */, PROFILE_IDS);
assertThat(tasks).hasSize(numFetchTasks); assertThat(tasks).hasSize(numFetchTasks);
for (int i = 0; i < numFetchTasks; i++) { for (int i = 0; i < numFetchTasks; i++) {
assertEquals(numTasks - i - 1, tasks.get(i).id); assertEquals(numTasks - i - 1, tasks.get(i).id);
@@ -102,7 +107,7 @@ public class RunningTasksTest extends WindowTestsBase {
// and does not crash // and does not crash
tasks.clear(); tasks.clear();
mRunningTasks.getTasks(100, tasks, FLAG_ALLOWED | FLAG_CROSS_USERS, mRunningTasks.getTasks(100, tasks, FLAG_ALLOWED | FLAG_CROSS_USERS,
mRootWindowContainer, -1 /* callingUid */, PROFILE_IDS); mAtm.getRecentTasks(), mRootWindowContainer, -1 /* callingUid */, PROFILE_IDS);
assertThat(tasks).hasSize(numTasks); assertThat(tasks).hasSize(numTasks);
for (int i = 0; i < numTasks; i++) { for (int i = 0; i < numTasks; i++) {
assertEquals(numTasks - i - 1, tasks.get(i).id); assertEquals(numTasks - i - 1, tasks.get(i).id);
@@ -126,7 +131,7 @@ public class RunningTasksTest extends WindowTestsBase {
final int numFetchTasks = 5; final int numFetchTasks = 5;
final ArrayList<RunningTaskInfo> tasks = new ArrayList<>(); final ArrayList<RunningTaskInfo> tasks = new ArrayList<>();
mRunningTasks.getTasks(numFetchTasks, tasks, FLAG_ALLOWED | FLAG_CROSS_USERS, mRunningTasks.getTasks(numFetchTasks, tasks, FLAG_ALLOWED | FLAG_CROSS_USERS,
mRootWindowContainer, -1 /* callingUid */, PROFILE_IDS); mAtm.getRecentTasks(), mRootWindowContainer, -1 /* callingUid */, PROFILE_IDS);
assertThat(tasks).hasSize(numFetchTasks); assertThat(tasks).hasSize(numFetchTasks);
for (int i = 0; i < tasks.size(); i++) { for (int i = 0; i < tasks.size(); i++) {
final Bundle extras = tasks.get(i).baseIntent.getExtras(); final Bundle extras = tasks.get(i).baseIntent.getExtras();
@@ -151,8 +156,8 @@ public class RunningTasksTest extends WindowTestsBase {
final int numFetchTasks = 5; final int numFetchTasks = 5;
final ArrayList<RunningTaskInfo> tasks = new ArrayList<>(); final ArrayList<RunningTaskInfo> tasks = new ArrayList<>();
mRunningTasks.getTasks(numFetchTasks, tasks, mRunningTasks.getTasks(numFetchTasks, tasks,
FLAG_ALLOWED | FLAG_CROSS_USERS | FLAG_KEEP_INTENT_EXTRA, mRootWindowContainer, FLAG_ALLOWED | FLAG_CROSS_USERS | FLAG_KEEP_INTENT_EXTRA,
-1 /* callingUid */, PROFILE_IDS); mAtm.getRecentTasks(), mRootWindowContainer, -1 /* callingUid */, PROFILE_IDS);
assertThat(tasks).hasSize(numFetchTasks); assertThat(tasks).hasSize(numFetchTasks);
for (int i = 0; i < tasks.size(); i++) { for (int i = 0; i < tasks.size(); i++) {
final Bundle extras = tasks.get(i).baseIntent.getExtras(); final Bundle extras = tasks.get(i).baseIntent.getExtras();
@@ -184,8 +189,8 @@ public class RunningTasksTest extends WindowTestsBase {
final int numFetchTasks = 5; final int numFetchTasks = 5;
final ArrayList<RunningTaskInfo> fetchTasks = new ArrayList<>(); final ArrayList<RunningTaskInfo> fetchTasks = new ArrayList<>();
mRunningTasks.getTasks(numFetchTasks, fetchTasks, mRunningTasks.getTasks(numFetchTasks, fetchTasks,
FLAG_ALLOWED | FLAG_CROSS_USERS | FLAG_KEEP_INTENT_EXTRA, mRootWindowContainer, FLAG_ALLOWED | FLAG_CROSS_USERS | FLAG_KEEP_INTENT_EXTRA,
-1 /* callingUid */, PROFILE_IDS); mAtm.getRecentTasks(), mRootWindowContainer, -1 /* callingUid */, PROFILE_IDS);
assertThat(fetchTasks).hasSize(numFetchTasks); assertThat(fetchTasks).hasSize(numFetchTasks);
assertEquals(fetchTasks.get(0).id, focusedTask.mTaskId); assertEquals(fetchTasks.get(0).id, focusedTask.mTaskId);
assertEquals(fetchTasks.get(1).id, visibleTask.mTaskId); assertEquals(fetchTasks.get(1).id, visibleTask.mTaskId);
@@ -210,4 +215,46 @@ public class RunningTasksTest extends WindowTestsBase {
task.intent = activity.intent; task.intent = activity.intent;
return task; return task;
} }
@Test
public void testMultipleDisplays() {
final DisplayContent display0 = new TestDisplayContent.Builder(mAtm, 1000, 2500).build();
final DisplayContent display1 = new TestDisplayContent.Builder(mAtm, 1000, 2500).build();
final int numTasks = 10;
final ArrayList<Task> tasks = new ArrayList<>();
for (int i = 0; i < numTasks; i++) {
final Task stack = new TaskBuilder(mSupervisor)
.setDisplay(i % 2 == 0 ? display0 : display1)
.setOnTop(true)
.build();
final Task task = createTask(stack, ".Task" + i, i, i, null);
tasks.add(task);
}
final int numFetchTasks = numTasks;
final ArrayList<RunningTaskInfo> fetchTasks = new ArrayList<>();
mRunningTasks.getTasks(numFetchTasks, fetchTasks,
FLAG_ALLOWED | FLAG_CROSS_USERS,
mAtm.getRecentTasks(), display0, -1 /* callingUid */, PROFILE_IDS);
assertThat(fetchTasks).hasSize(numTasks / 2);
assertThat(fetchTasks).comparingElementsUsing(TASKINFO_HAS_ID)
.containsExactly(0, 2, 4, 6, 8);
fetchTasks.clear();
mRunningTasks.getTasks(numFetchTasks, fetchTasks,
FLAG_ALLOWED | FLAG_CROSS_USERS,
mAtm.getRecentTasks(), display1, -1 /* callingUid */, PROFILE_IDS);
assertThat(fetchTasks).hasSize(numTasks / 2);
assertThat(fetchTasks).comparingElementsUsing(TASKINFO_HAS_ID)
.containsExactly(1, 3, 5, 7, 9);
fetchTasks.clear();
mRunningTasks.getTasks(numFetchTasks, fetchTasks,
FLAG_ALLOWED | FLAG_CROSS_USERS,
mAtm.getRecentTasks(), mRootWindowContainer, -1 /* callingUid */, PROFILE_IDS);
assertThat(fetchTasks).hasSize(numTasks);
assertThat(fetchTasks).comparingElementsUsing(TASKINFO_HAS_ID)
.containsExactly(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
}
} }