Merge "Add a method to obtain task info with intent extra" into sc-dev

This commit is contained in:
Garfield Tan
2021-04-22 20:22:50 +00:00
committed by Android (Google) Code Review
8 changed files with 93 additions and 34 deletions

View File

@@ -400,8 +400,18 @@ 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 List of running tasks that can be filtered by visibility in recents and keep intent
* extra.
* @hide
*/
public List<ActivityManager.RunningTaskInfo> getTasks(
int maxNum, boolean filterOnlyVisibleRecents, boolean keepIntentExtra) {
try { try {
return getService().getTasks(maxNum, filterOnlyVisibleRecents); return getService().getTasks(maxNum, filterOnlyVisibleRecents, keepIntentExtra);
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }

View File

@@ -149,7 +149,8 @@ interface IActivityTaskManager {
void setFocusedTask(int taskId); void setFocusedTask(int taskId);
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);
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

@@ -5768,7 +5768,7 @@ public class ActivityManagerService extends IActivityManager.Stub
@Override @Override
public List<RunningTaskInfo> getTasks(int maxNum) { public List<RunningTaskInfo> getTasks(int maxNum) {
return mActivityTaskManager.getTasks(maxNum, false /* filterForVisibleRecents */); return mActivityTaskManager.getTasks(maxNum);
} }
@Override @Override

View File

@@ -2104,8 +2104,16 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
} }
} }
List<ActivityManager.RunningTaskInfo> getTasks(int maxNum) { /**
return getTasks(maxNum, false /* filterForVisibleRecents */); * Gets info of running tasks up to the given number.
*
* @param maxNum the maximum number of task info returned by this method. If the total number of
* running tasks is larger than it then there is no guarantee which task will be
* left out.
* @return a list of {@link ActivityManager.RunningTaskInfo} with up to {@code maxNum} items
*/
public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum) {
return getTasks(maxNum, false /* filterForVisibleRecents */, false /* keepIntentExtra */);
} }
/** /**
@@ -2114,10 +2122,14 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
*/ */
@Override @Override
public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum, public List<ActivityManager.RunningTaskInfo> getTasks(int maxNum,
boolean filterOnlyVisibleRecents) { boolean filterOnlyVisibleRecents, boolean keepIntentExtra) {
final int callingUid = Binder.getCallingUid(); final int callingUid = Binder.getCallingUid();
final int callingPid = Binder.getCallingPid(); final int callingPid = Binder.getCallingPid();
int flags = filterOnlyVisibleRecents ? RunningTasks.FLAG_FILTER_ONLY_VISIBLE_RECENTS : 0;
flags |= (keepIntentExtra ? RunningTasks.FLAG_KEEP_INTENT_EXTRA : 0);
final boolean crossUser = isCrossUserAllowed(callingPid, callingUid); final boolean crossUser = isCrossUserAllowed(callingPid, callingUid);
flags |= (crossUser ? RunningTasks.FLAG_CROSS_USERS : 0);
final int[] profileIds = getUserManager().getProfileIds( final int[] profileIds = getUserManager().getProfileIds(
UserHandle.getUserId(callingUid), true); UserHandle.getUserId(callingUid), true);
ArraySet<Integer> callingProfileIds = new ArraySet<>(); ArraySet<Integer> callingProfileIds = new ArraySet<>();
@@ -2130,8 +2142,9 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
if (DEBUG_ALL) Slog.v(TAG, "getTasks: max=" + maxNum); if (DEBUG_ALL) Slog.v(TAG, "getTasks: max=" + maxNum);
final boolean allowed = isGetTasksAllowed("getTasks", callingPid, callingUid); final boolean allowed = isGetTasksAllowed("getTasks", callingPid, callingUid);
mRootWindowContainer.getRunningTasks(maxNum, list, filterOnlyVisibleRecents, callingUid, flags |= (allowed ? RunningTasks.FLAG_ALLOWED : 0);
allowed, crossUser, callingProfileIds); mRootWindowContainer.getRunningTasks(
maxNum, list, flags, callingUid, callingProfileIds);
} }
return list; return list;

View File

@@ -3479,10 +3479,9 @@ class RootWindowContainer extends WindowContainer<DisplayContent>
@VisibleForTesting @VisibleForTesting
void getRunningTasks(int maxNum, List<ActivityManager.RunningTaskInfo> list, void getRunningTasks(int maxNum, List<ActivityManager.RunningTaskInfo> list,
boolean filterOnlyVisibleRecents, int callingUid, boolean allowed, boolean crossUser, int flags, int callingUid, ArraySet<Integer> profileIds) {
ArraySet<Integer> profileIds) { mTaskSupervisor.getRunningTasks().getTasks(maxNum, list, flags, this, callingUid,
mTaskSupervisor.getRunningTasks().getTasks(maxNum, list, filterOnlyVisibleRecents, this, profileIds);
callingUid, allowed, crossUser, profileIds);
} }
void startPowerModeLaunchIfNeeded(boolean forceSend, ActivityRecord targetActivity) { void startPowerModeLaunchIfNeeded(boolean forceSend, ActivityRecord targetActivity) {

View File

@@ -36,6 +36,11 @@ import java.util.TreeSet;
*/ */
class RunningTasks { class RunningTasks {
static final int FLAG_FILTER_ONLY_VISIBLE_RECENTS = 1;
static final int FLAG_ALLOWED = 1 << 1;
static final int FLAG_CROSS_USERS = 1 << 2;
static final int FLAG_KEEP_INTENT_EXTRA = 1 << 3;
// Comparator to sort by last active time (descending) // Comparator to sort by last active time (descending)
private static final Comparator<Task> LAST_ACTIVE_TIME_COMPARATOR = private static final Comparator<Task> LAST_ACTIVE_TIME_COMPARATOR =
(o1, o2) -> Long.signum(o2.lastActiveTime - o1.lastActiveTime); (o1, o2) -> Long.signum(o2.lastActiveTime - o1.lastActiveTime);
@@ -50,10 +55,10 @@ class RunningTasks {
private boolean mFilterOnlyVisibleRecents; private boolean mFilterOnlyVisibleRecents;
private Task mTopDisplayFocusRootTask; private Task mTopDisplayFocusRootTask;
private RecentTasks mRecentTasks; private RecentTasks mRecentTasks;
private boolean mKeepIntentExtra;
void getTasks(int maxNum, List<RunningTaskInfo> list, boolean filterOnlyVisibleRecents, void getTasks(int maxNum, List<RunningTaskInfo> list, int flags,
RootWindowContainer root, int callingUid, boolean allowed, boolean crossUser, RootWindowContainer root, int callingUid, ArraySet<Integer> profileIds) {
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;
@@ -63,12 +68,14 @@ class RunningTasks {
mTmpSortedSet.clear(); mTmpSortedSet.clear();
mCallingUid = callingUid; mCallingUid = callingUid;
mUserId = UserHandle.getUserId(callingUid); mUserId = UserHandle.getUserId(callingUid);
mCrossUser = crossUser; mCrossUser = (flags & FLAG_CROSS_USERS) == FLAG_CROSS_USERS;
mProfileIds = profileIds; mProfileIds = profileIds;
mAllowed = allowed; mAllowed = (flags & FLAG_ALLOWED) == FLAG_ALLOWED;
mFilterOnlyVisibleRecents = filterOnlyVisibleRecents; mFilterOnlyVisibleRecents =
(flags & FLAG_FILTER_ONLY_VISIBLE_RECENTS) == FLAG_FILTER_ONLY_VISIBLE_RECENTS;
mTopDisplayFocusRootTask = root.getTopDisplayFocusedRootTask(); mTopDisplayFocusRootTask = root.getTopDisplayFocusedRootTask();
mRecentTasks = root.mService.getRecentTasks(); mRecentTasks = root.mService.getRecentTasks();
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,
PooledLambda.__(Task.class)); PooledLambda.__(Task.class));
@@ -126,7 +133,8 @@ class RunningTasks {
/** Constructs a {@link RunningTaskInfo} from a given {@param task}. */ /** Constructs a {@link RunningTaskInfo} from a given {@param task}. */
private RunningTaskInfo createRunningTaskInfo(Task task) { private RunningTaskInfo createRunningTaskInfo(Task task) {
final RunningTaskInfo rti = task.getTaskInfo(); final RunningTaskInfo rti = new RunningTaskInfo();
task.fillTaskInfo(rti, !mKeepIntentExtra);
// Fill in some deprecated values // Fill in some deprecated values
rti.id = rti.taskId; rti.id = rti.taskId;
return rti; return rti;

View File

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

View File

@@ -16,9 +16,14 @@
package com.android.server.wm; package com.android.server.wm;
import static com.android.server.wm.RunningTasks.FLAG_ALLOWED;
import static com.android.server.wm.RunningTasks.FLAG_CROSS_USERS;
import static com.android.server.wm.RunningTasks.FLAG_KEEP_INTENT_EXTRA;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import android.app.ActivityManager.RunningTaskInfo; import android.app.ActivityManager.RunningTaskInfo;
@@ -83,8 +88,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, false /* filterOnlyVisibleRecents */, mRootWindowContainer, mRunningTasks.getTasks(5, tasks, FLAG_ALLOWED | FLAG_CROSS_USERS, mRootWindowContainer,
-1 /* callingUid */, true /* allowed */, true /*crossUser */, PROFILE_IDS); -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);
@@ -93,9 +98,8 @@ public class RunningTasksTest extends WindowTestsBase {
// Ensure that requesting more than the total number of tasks only returns the subset // Ensure that requesting more than the total number of tasks only returns the subset
// and does not crash // and does not crash
tasks.clear(); tasks.clear();
mRunningTasks.getTasks(100, tasks, false /* filterOnlyVisibleRecents */, mRunningTasks.getTasks(100, tasks, FLAG_ALLOWED | FLAG_CROSS_USERS,
mRootWindowContainer, -1 /* callingUid */, true /* allowed */, true /* crossUser */, mRootWindowContainer, -1 /* callingUid */, PROFILE_IDS);
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);
@@ -103,7 +107,7 @@ public class RunningTasksTest extends WindowTestsBase {
} }
@Test @Test
public void testTaskInfo_expectNoExtras() { public void testTaskInfo_expectNoExtrasByDefault() {
final DisplayContent display = new TestDisplayContent.Builder(mAtm, 1000, 2500).build(); final DisplayContent display = new TestDisplayContent.Builder(mAtm, 1000, 2500).build();
final int numTasks = 10; final int numTasks = 10;
for (int i = 0; i < numTasks; i++) { for (int i = 0; i < numTasks; i++) {
@@ -118,9 +122,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, false /* filterOnlyVisibleRecents */, mRunningTasks.getTasks(numFetchTasks, tasks, FLAG_ALLOWED | FLAG_CROSS_USERS,
mRootWindowContainer, -1 /* callingUid */, true /* allowed */, true /*crossUser */, mRootWindowContainer, -1 /* callingUid */, PROFILE_IDS);
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();
@@ -128,6 +131,32 @@ public class RunningTasksTest extends WindowTestsBase {
} }
} }
@Test
public void testTaskInfo_expectExtrasWithKeepExtraFlag() {
final DisplayContent display = new TestDisplayContent.Builder(mAtm, 1000, 2500).build();
final int numTasks = 10;
for (int i = 0; i < numTasks; i++) {
final Task stack = new TaskBuilder(mSupervisor)
.setDisplay(display)
.setOnTop(true)
.build();
final Bundle data = new Bundle();
data.putInt("key", 100);
createTask(stack, ".Task" + i, i, i, data);
}
final int numFetchTasks = 5;
final ArrayList<RunningTaskInfo> tasks = new ArrayList<>();
mRunningTasks.getTasks(numFetchTasks, tasks,
FLAG_ALLOWED | FLAG_CROSS_USERS | FLAG_KEEP_INTENT_EXTRA, mRootWindowContainer,
-1 /* callingUid */, PROFILE_IDS);
assertThat(tasks).hasSize(numFetchTasks);
for (int i = 0; i < tasks.size(); i++) {
final Bundle extras = tasks.get(i).baseIntent.getExtras();
assertNotNull(extras);
assertEquals(100, extras.getInt("key"));
}
}
/** /**
* Create a task with a single activity in it, with the given last active time. * Create a task with a single activity in it, with the given last active time.
@@ -145,6 +174,7 @@ public class RunningTasksTest extends WindowTestsBase {
.setComponent(new ComponentName(mContext.getPackageName(), ".TaskActivity")) .setComponent(new ComponentName(mContext.getPackageName(), ".TaskActivity"))
.setIntentExtras(extras) .setIntentExtras(extras)
.build(); .build();
task.intent = activity.intent;
return task; return task;
} }
} }