blob: 16a75645f9ae519c11af7fdf2d41047656945071 [file] [log] [blame]
Winson Chung1dbc8112017-09-28 18:05:31 -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 Chung1dbc8112017-09-28 18:05:31 -070018
Wale Ogunwale59507092018-10-29 09:00:30 -070019import static com.android.server.wm.ActivityStackSupervisor.REMOVE_FROM_RECENTS;
Louis Chang149d5c82019-12-30 09:47:39 +080020import static com.android.server.wm.RootWindowContainer.MATCH_TASK_IN_STACKS_OR_RECENT_TASKS;
Winson Chung1dbc8112017-09-28 18:05:31 -070021
22import android.app.ActivityManager;
23import android.app.IAppTask;
24import android.app.IApplicationThread;
25import android.content.Intent;
26import android.os.Binder;
27import android.os.Bundle;
28import android.os.IBinder;
29import android.os.UserHandle;
30
31/**
32 * An implementation of IAppTask, that allows an app to manage its own tasks via
33 * {@link android.app.ActivityManager.AppTask}. We keep track of the callingUid to ensure that
34 * only the process that calls getAppTasks() can call the AppTask methods.
35 */
36class AppTaskImpl extends IAppTask.Stub {
Ricky Waiaca8a772019-04-04 16:01:06 +010037 private static final String TAG = "AppTaskImpl";
Wale Ogunwale16e505a2018-05-07 15:00:49 -070038 private ActivityTaskManagerService mService;
Winson Chung1dbc8112017-09-28 18:05:31 -070039
40 private int mTaskId;
41 private int mCallingUid;
42
Wale Ogunwale16e505a2018-05-07 15:00:49 -070043 public AppTaskImpl(ActivityTaskManagerService service, int taskId, int callingUid) {
Winson Chung1dbc8112017-09-28 18:05:31 -070044 mService = service;
45 mTaskId = taskId;
46 mCallingUid = callingUid;
47 }
48
49 private void checkCaller() {
50 if (mCallingUid != Binder.getCallingUid()) {
51 throw new SecurityException("Caller " + mCallingUid
52 + " does not match caller of getAppTasks(): " + Binder.getCallingUid());
53 }
54 }
55
56 @Override
57 public void finishAndRemoveTask() {
58 checkCaller();
59
Wale Ogunwale16e505a2018-05-07 15:00:49 -070060 synchronized (mService.mGlobalLock) {
Winson Chung1dbc8112017-09-28 18:05:31 -070061 long origId = Binder.clearCallingIdentity();
62 try {
63 // We remove the task from recents to preserve backwards
Wale Ogunwale85fb19a2019-12-05 10:41:05 +090064 if (!mService.mStackSupervisor.removeTaskById(mTaskId, false,
Winson Chung0ec2a352017-10-26 11:38:30 -070065 REMOVE_FROM_RECENTS, "finish-and-remove-task")) {
Winson Chung1dbc8112017-09-28 18:05:31 -070066 throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
67 }
68 } finally {
69 Binder.restoreCallingIdentity(origId);
70 }
71 }
72 }
73
74 @Override
75 public ActivityManager.RecentTaskInfo getTaskInfo() {
76 checkCaller();
77
Wale Ogunwale16e505a2018-05-07 15:00:49 -070078 synchronized (mService.mGlobalLock) {
Winson Chung1dbc8112017-09-28 18:05:31 -070079 long origId = Binder.clearCallingIdentity();
80 try {
Louis Chang149d5c82019-12-30 09:47:39 +080081 Task task = mService.mRootWindowContainer.anyTaskForId(mTaskId,
Winson Chung5d339f02017-10-23 11:10:58 -070082 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Louis Changcdec0802019-11-11 11:45:07 +080083 if (task == null) {
Winson Chung1dbc8112017-09-28 18:05:31 -070084 throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
85 }
Louis Changcdec0802019-11-11 11:45:07 +080086 return mService.getRecentTasks().createRecentTaskInfo(task);
Winson Chung1dbc8112017-09-28 18:05:31 -070087 } finally {
88 Binder.restoreCallingIdentity(origId);
89 }
90 }
91 }
92
93 @Override
Ricky Waiaca8a772019-04-04 16:01:06 +010094 public void moveToFront(IApplicationThread appThread, String callingPackage) {
Winson Chung1dbc8112017-09-28 18:05:31 -070095 checkCaller();
96 // Will bring task to front if it already has a root activity.
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +010097 final int callingPid = Binder.getCallingPid();
98 final int callingUid = Binder.getCallingUid();
Andrii Kulianf9e5c9f2019-07-16 11:24:45 -070099 mService.assertPackageMatchesCallingUid(callingPackage);
Winson Chung1dbc8112017-09-28 18:05:31 -0700100 final long origId = Binder.clearCallingIdentity();
101 try {
Wale Ogunwale9c189e12018-07-11 15:22:01 -0700102 synchronized (mService.mGlobalLock) {
Ricky Wai83946492019-04-11 17:26:57 +0100103 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, -1, -1,
104 "Move to front")) {
105 return;
106 }
Ricky Waiaca8a772019-04-04 16:01:06 +0100107 WindowProcessController callerApp = null;
108 if (appThread != null) {
109 callerApp = mService.getProcessController(appThread);
110 }
111 final ActivityStarter starter = mService.getActivityStartController().obtainStarter(
112 null /* intent */, "moveToFront");
113 if (starter.shouldAbortBackgroundActivityStart(callingUid, callingPid,
114 callingPackage, -1, -1, callerApp, null, false, null)) {
Alan Stokes9e245762019-05-21 14:54:28 +0100115 if (!mService.isBackgroundActivityStartsEnabled()) {
Ricky Waiaca8a772019-04-04 16:01:06 +0100116 return;
117 }
118 }
119 mService.mStackSupervisor.startActivityFromRecents(callingPid,
120 callingUid, mTaskId, null);
Winson Chung1dbc8112017-09-28 18:05:31 -0700121 }
122 } finally {
123 Binder.restoreCallingIdentity(origId);
124 }
125 }
126
127 @Override
Philip P. Moltmanne7421e92020-02-10 16:14:12 +0000128 public int startActivity(IBinder whoThread, String callingPackage,
Winson Chung1dbc8112017-09-28 18:05:31 -0700129 Intent intent, String resolvedType, Bundle bOptions) {
130 checkCaller();
Andrii Kulianf9e5c9f2019-07-16 11:24:45 -0700131 mService.assertPackageMatchesCallingUid(callingPackage);
Winson Chung1dbc8112017-09-28 18:05:31 -0700132
133 int callingUser = UserHandle.getCallingUserId();
Louis Changcdec0802019-11-11 11:45:07 +0800134 Task task;
Winson Chung1dbc8112017-09-28 18:05:31 -0700135 IApplicationThread appThread;
Wale Ogunwale16e505a2018-05-07 15:00:49 -0700136 synchronized (mService.mGlobalLock) {
Louis Chang149d5c82019-12-30 09:47:39 +0800137 task = mService.mRootWindowContainer.anyTaskForId(mTaskId,
Winson Chung5d339f02017-10-23 11:10:58 -0700138 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Louis Changcdec0802019-11-11 11:45:07 +0800139 if (task == null) {
Winson Chung1dbc8112017-09-28 18:05:31 -0700140 throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
141 }
142 appThread = IApplicationThread.Stub.asInterface(whoThread);
143 if (appThread == null) {
144 throw new IllegalArgumentException("Bad app thread " + appThread);
145 }
146 }
Bryce Lee4c9a5972017-12-01 22:14:24 -0800147
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700148 return mService.getActivityStartController().obtainStarter(intent, "AppTaskImpl")
Bryce Lee4c9a5972017-12-01 22:14:24 -0800149 .setCaller(appThread)
150 .setCallingPackage(callingPackage)
151 .setResolvedType(resolvedType)
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100152 .setActivityOptions(bOptions)
Louis Chang54fbb052019-10-16 17:10:17 +0800153 .setUserId(callingUser)
Louis Changcdec0802019-11-11 11:45:07 +0800154 .setInTask(task)
Bryce Lee4c9a5972017-12-01 22:14:24 -0800155 .execute();
Winson Chung1dbc8112017-09-28 18:05:31 -0700156 }
157
158 @Override
159 public void setExcludeFromRecents(boolean exclude) {
160 checkCaller();
161
Wale Ogunwale16e505a2018-05-07 15:00:49 -0700162 synchronized (mService.mGlobalLock) {
Winson Chung1dbc8112017-09-28 18:05:31 -0700163 long origId = Binder.clearCallingIdentity();
164 try {
Louis Chang149d5c82019-12-30 09:47:39 +0800165 Task task = mService.mRootWindowContainer.anyTaskForId(mTaskId,
Winson Chung5d339f02017-10-23 11:10:58 -0700166 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Louis Changcdec0802019-11-11 11:45:07 +0800167 if (task == null) {
Winson Chung1dbc8112017-09-28 18:05:31 -0700168 throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
169 }
Louis Changcdec0802019-11-11 11:45:07 +0800170 Intent intent = task.getBaseIntent();
Winson Chung1dbc8112017-09-28 18:05:31 -0700171 if (exclude) {
172 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
173 } else {
174 intent.setFlags(intent.getFlags()
175 & ~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
176 }
177 } finally {
178 Binder.restoreCallingIdentity(origId);
179 }
180 }
181 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700182}