blob: 6d9584c50b9c5994a7998d506d876447030f0a92 [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;
Wale Ogunwaled32da472018-11-16 07:19:28 -080020import static com.android.server.wm.RootActivityContainer.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;
Andrii Kuliana8ccae42019-07-24 18:35:22 +000030import android.util.Slog;
Winson Chung1dbc8112017-09-28 18:05:31 -070031
32/**
33 * An implementation of IAppTask, that allows an app to manage its own tasks via
34 * {@link android.app.ActivityManager.AppTask}. We keep track of the callingUid to ensure that
35 * only the process that calls getAppTasks() can call the AppTask methods.
36 */
37class AppTaskImpl extends IAppTask.Stub {
Ricky Waiaca8a772019-04-04 16:01:06 +010038 private static final String TAG = "AppTaskImpl";
Wale Ogunwale16e505a2018-05-07 15:00:49 -070039 private ActivityTaskManagerService mService;
Winson Chung1dbc8112017-09-28 18:05:31 -070040
41 private int mTaskId;
42 private int mCallingUid;
43
Wale Ogunwale16e505a2018-05-07 15:00:49 -070044 public AppTaskImpl(ActivityTaskManagerService service, int taskId, int callingUid) {
Winson Chung1dbc8112017-09-28 18:05:31 -070045 mService = service;
46 mTaskId = taskId;
47 mCallingUid = callingUid;
48 }
49
50 private void checkCaller() {
51 if (mCallingUid != Binder.getCallingUid()) {
52 throw new SecurityException("Caller " + mCallingUid
53 + " does not match caller of getAppTasks(): " + Binder.getCallingUid());
54 }
55 }
56
57 @Override
58 public void finishAndRemoveTask() {
59 checkCaller();
60
Wale Ogunwale16e505a2018-05-07 15:00:49 -070061 synchronized (mService.mGlobalLock) {
Winson Chung1dbc8112017-09-28 18:05:31 -070062 long origId = Binder.clearCallingIdentity();
63 try {
64 // We remove the task from recents to preserve backwards
Wale Ogunwale85fb19a2019-12-05 10:41:05 +090065 if (!mService.mStackSupervisor.removeTaskById(mTaskId, false,
Winson Chung0ec2a352017-10-26 11:38:30 -070066 REMOVE_FROM_RECENTS, "finish-and-remove-task")) {
Winson Chung1dbc8112017-09-28 18:05:31 -070067 throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
68 }
69 } finally {
70 Binder.restoreCallingIdentity(origId);
71 }
72 }
73 }
74
75 @Override
76 public ActivityManager.RecentTaskInfo getTaskInfo() {
77 checkCaller();
78
Wale Ogunwale16e505a2018-05-07 15:00:49 -070079 synchronized (mService.mGlobalLock) {
Winson Chung1dbc8112017-09-28 18:05:31 -070080 long origId = Binder.clearCallingIdentity();
81 try {
Louis Changcdec0802019-11-11 11:45:07 +080082 Task task = mService.mRootActivityContainer.anyTaskForId(mTaskId,
Winson Chung5d339f02017-10-23 11:10:58 -070083 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Louis Changcdec0802019-11-11 11:45:07 +080084 if (task == null) {
Winson Chung1dbc8112017-09-28 18:05:31 -070085 throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
86 }
Louis Changcdec0802019-11-11 11:45:07 +080087 return mService.getRecentTasks().createRecentTaskInfo(task);
Winson Chung1dbc8112017-09-28 18:05:31 -070088 } finally {
89 Binder.restoreCallingIdentity(origId);
90 }
91 }
92 }
93
94 @Override
Ricky Waiaca8a772019-04-04 16:01:06 +010095 public void moveToFront(IApplicationThread appThread, String callingPackage) {
Winson Chung1dbc8112017-09-28 18:05:31 -070096 checkCaller();
97 // Will bring task to front if it already has a root activity.
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +010098 final int callingPid = Binder.getCallingPid();
99 final int callingUid = Binder.getCallingUid();
Andrii Kuliana8ccae42019-07-24 18:35:22 +0000100 if (!mService.isSameApp(callingUid, callingPackage)) {
101 String msg = "Permission Denial: moveToFront() from pid="
102 + Binder.getCallingPid() + " as package " + callingPackage;
103 Slog.w(TAG, msg);
104 throw new SecurityException(msg);
105 }
Winson Chung1dbc8112017-09-28 18:05:31 -0700106 final long origId = Binder.clearCallingIdentity();
107 try {
Wale Ogunwale9c189e12018-07-11 15:22:01 -0700108 synchronized (mService.mGlobalLock) {
Ricky Wai83946492019-04-11 17:26:57 +0100109 if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, -1, -1,
110 "Move to front")) {
111 return;
112 }
Ricky Waiaca8a772019-04-04 16:01:06 +0100113 WindowProcessController callerApp = null;
114 if (appThread != null) {
115 callerApp = mService.getProcessController(appThread);
116 }
117 final ActivityStarter starter = mService.getActivityStartController().obtainStarter(
118 null /* intent */, "moveToFront");
119 if (starter.shouldAbortBackgroundActivityStart(callingUid, callingPid,
120 callingPackage, -1, -1, callerApp, null, false, null)) {
Alan Stokes9e245762019-05-21 14:54:28 +0100121 if (!mService.isBackgroundActivityStartsEnabled()) {
Ricky Waiaca8a772019-04-04 16:01:06 +0100122 return;
123 }
124 }
125 mService.mStackSupervisor.startActivityFromRecents(callingPid,
126 callingUid, mTaskId, null);
Winson Chung1dbc8112017-09-28 18:05:31 -0700127 }
128 } finally {
129 Binder.restoreCallingIdentity(origId);
130 }
131 }
132
133 @Override
134 public int startActivity(IBinder whoThread, String callingPackage,
135 Intent intent, String resolvedType, Bundle bOptions) {
136 checkCaller();
137
138 int callingUser = UserHandle.getCallingUserId();
Louis Changcdec0802019-11-11 11:45:07 +0800139 Task task;
Winson Chung1dbc8112017-09-28 18:05:31 -0700140 IApplicationThread appThread;
Wale Ogunwale16e505a2018-05-07 15:00:49 -0700141 synchronized (mService.mGlobalLock) {
Louis Changcdec0802019-11-11 11:45:07 +0800142 task = mService.mRootActivityContainer.anyTaskForId(mTaskId,
Winson Chung5d339f02017-10-23 11:10:58 -0700143 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Louis Changcdec0802019-11-11 11:45:07 +0800144 if (task == null) {
Winson Chung1dbc8112017-09-28 18:05:31 -0700145 throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
146 }
147 appThread = IApplicationThread.Stub.asInterface(whoThread);
148 if (appThread == null) {
149 throw new IllegalArgumentException("Bad app thread " + appThread);
150 }
151 }
Bryce Lee4c9a5972017-12-01 22:14:24 -0800152
Wale Ogunwale5fa8a8c2018-05-08 13:43:21 -0700153 return mService.getActivityStartController().obtainStarter(intent, "AppTaskImpl")
Bryce Lee4c9a5972017-12-01 22:14:24 -0800154 .setCaller(appThread)
155 .setCallingPackage(callingPackage)
156 .setResolvedType(resolvedType)
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +0100157 .setActivityOptions(bOptions)
Louis Chang54fbb052019-10-16 17:10:17 +0800158 .setUserId(callingUser)
Louis Changcdec0802019-11-11 11:45:07 +0800159 .setInTask(task)
Bryce Lee4c9a5972017-12-01 22:14:24 -0800160 .execute();
Winson Chung1dbc8112017-09-28 18:05:31 -0700161 }
162
163 @Override
164 public void setExcludeFromRecents(boolean exclude) {
165 checkCaller();
166
Wale Ogunwale16e505a2018-05-07 15:00:49 -0700167 synchronized (mService.mGlobalLock) {
Winson Chung1dbc8112017-09-28 18:05:31 -0700168 long origId = Binder.clearCallingIdentity();
169 try {
Louis Changcdec0802019-11-11 11:45:07 +0800170 Task task = mService.mRootActivityContainer.anyTaskForId(mTaskId,
Winson Chung5d339f02017-10-23 11:10:58 -0700171 MATCH_TASK_IN_STACKS_OR_RECENT_TASKS);
Louis Changcdec0802019-11-11 11:45:07 +0800172 if (task == null) {
Winson Chung1dbc8112017-09-28 18:05:31 -0700173 throw new IllegalArgumentException("Unable to find task ID " + mTaskId);
174 }
Louis Changcdec0802019-11-11 11:45:07 +0800175 Intent intent = task.getBaseIntent();
Winson Chung1dbc8112017-09-28 18:05:31 -0700176 if (exclude) {
177 intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
178 } else {
179 intent.setFlags(intent.getFlags()
180 & ~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
181 }
182 } finally {
183 Binder.restoreCallingIdentity(origId);
184 }
185 }
186 }
Wale Ogunwaleab5de372017-10-18 06:46:31 -0700187}