Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 1 | /* |
| 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 Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 17 | package com.android.server.wm; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 18 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 19 | import static com.android.server.wm.ActivityStackSupervisor.REMOVE_FROM_RECENTS; |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 20 | import static com.android.server.wm.RootWindowContainer.MATCH_TASK_IN_STACKS_OR_RECENT_TASKS; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 21 | |
| 22 | import android.app.ActivityManager; |
| 23 | import android.app.IAppTask; |
| 24 | import android.app.IApplicationThread; |
| 25 | import android.content.Intent; |
| 26 | import android.os.Binder; |
| 27 | import android.os.Bundle; |
| 28 | import android.os.IBinder; |
| 29 | import 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 | */ |
| 36 | class AppTaskImpl extends IAppTask.Stub { |
Ricky Wai | aca8a77 | 2019-04-04 16:01:06 +0100 | [diff] [blame] | 37 | private static final String TAG = "AppTaskImpl"; |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 38 | private ActivityTaskManagerService mService; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 39 | |
| 40 | private int mTaskId; |
| 41 | private int mCallingUid; |
| 42 | |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 43 | public AppTaskImpl(ActivityTaskManagerService service, int taskId, int callingUid) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 44 | 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 Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 60 | synchronized (mService.mGlobalLock) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 61 | long origId = Binder.clearCallingIdentity(); |
| 62 | try { |
| 63 | // We remove the task from recents to preserve backwards |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 64 | if (!mService.mStackSupervisor.removeTaskById(mTaskId, false, |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 65 | REMOVE_FROM_RECENTS, "finish-and-remove-task")) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 66 | 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 Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 78 | synchronized (mService.mGlobalLock) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 79 | long origId = Binder.clearCallingIdentity(); |
| 80 | try { |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 81 | Task task = mService.mRootWindowContainer.anyTaskForId(mTaskId, |
Winson Chung | 5d339f0 | 2017-10-23 11:10:58 -0700 | [diff] [blame] | 82 | MATCH_TASK_IN_STACKS_OR_RECENT_TASKS); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 83 | if (task == null) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 84 | throw new IllegalArgumentException("Unable to find task ID " + mTaskId); |
| 85 | } |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 86 | return mService.getRecentTasks().createRecentTaskInfo(task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 87 | } finally { |
| 88 | Binder.restoreCallingIdentity(origId); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | @Override |
Ricky Wai | aca8a77 | 2019-04-04 16:01:06 +0100 | [diff] [blame] | 94 | public void moveToFront(IApplicationThread appThread, String callingPackage) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 95 | checkCaller(); |
| 96 | // Will bring task to front if it already has a root activity. |
Jorim Jaggi | 4d8d32c | 2018-01-19 15:57:41 +0100 | [diff] [blame] | 97 | final int callingPid = Binder.getCallingPid(); |
| 98 | final int callingUid = Binder.getCallingUid(); |
Andrii Kulian | f9e5c9f | 2019-07-16 11:24:45 -0700 | [diff] [blame] | 99 | mService.assertPackageMatchesCallingUid(callingPackage); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 100 | final long origId = Binder.clearCallingIdentity(); |
| 101 | try { |
Wale Ogunwale | 9c189e1 | 2018-07-11 15:22:01 -0700 | [diff] [blame] | 102 | synchronized (mService.mGlobalLock) { |
Ricky Wai | 8394649 | 2019-04-11 17:26:57 +0100 | [diff] [blame] | 103 | if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, -1, -1, |
| 104 | "Move to front")) { |
| 105 | return; |
| 106 | } |
Ricky Wai | aca8a77 | 2019-04-04 16:01:06 +0100 | [diff] [blame] | 107 | 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 Stokes | 9e24576 | 2019-05-21 14:54:28 +0100 | [diff] [blame] | 115 | if (!mService.isBackgroundActivityStartsEnabled()) { |
Ricky Wai | aca8a77 | 2019-04-04 16:01:06 +0100 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | } |
| 119 | mService.mStackSupervisor.startActivityFromRecents(callingPid, |
| 120 | callingUid, mTaskId, null); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 121 | } |
| 122 | } finally { |
| 123 | Binder.restoreCallingIdentity(origId); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @Override |
Philip P. Moltmann | e7421e9 | 2020-02-10 16:14:12 +0000 | [diff] [blame^] | 128 | public int startActivity(IBinder whoThread, String callingPackage, |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 129 | Intent intent, String resolvedType, Bundle bOptions) { |
| 130 | checkCaller(); |
Andrii Kulian | f9e5c9f | 2019-07-16 11:24:45 -0700 | [diff] [blame] | 131 | mService.assertPackageMatchesCallingUid(callingPackage); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 132 | |
| 133 | int callingUser = UserHandle.getCallingUserId(); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 134 | Task task; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 135 | IApplicationThread appThread; |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 136 | synchronized (mService.mGlobalLock) { |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 137 | task = mService.mRootWindowContainer.anyTaskForId(mTaskId, |
Winson Chung | 5d339f0 | 2017-10-23 11:10:58 -0700 | [diff] [blame] | 138 | MATCH_TASK_IN_STACKS_OR_RECENT_TASKS); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 139 | if (task == null) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 140 | 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 Lee | 4c9a597 | 2017-12-01 22:14:24 -0800 | [diff] [blame] | 147 | |
Wale Ogunwale | 5fa8a8c | 2018-05-08 13:43:21 -0700 | [diff] [blame] | 148 | return mService.getActivityStartController().obtainStarter(intent, "AppTaskImpl") |
Bryce Lee | 4c9a597 | 2017-12-01 22:14:24 -0800 | [diff] [blame] | 149 | .setCaller(appThread) |
| 150 | .setCallingPackage(callingPackage) |
| 151 | .setResolvedType(resolvedType) |
Jorim Jaggi | 4d8d32c | 2018-01-19 15:57:41 +0100 | [diff] [blame] | 152 | .setActivityOptions(bOptions) |
Louis Chang | 54fbb05 | 2019-10-16 17:10:17 +0800 | [diff] [blame] | 153 | .setUserId(callingUser) |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 154 | .setInTask(task) |
Bryce Lee | 4c9a597 | 2017-12-01 22:14:24 -0800 | [diff] [blame] | 155 | .execute(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void setExcludeFromRecents(boolean exclude) { |
| 160 | checkCaller(); |
| 161 | |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 162 | synchronized (mService.mGlobalLock) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 163 | long origId = Binder.clearCallingIdentity(); |
| 164 | try { |
Louis Chang | 149d5c8 | 2019-12-30 09:47:39 +0800 | [diff] [blame] | 165 | Task task = mService.mRootWindowContainer.anyTaskForId(mTaskId, |
Winson Chung | 5d339f0 | 2017-10-23 11:10:58 -0700 | [diff] [blame] | 166 | MATCH_TASK_IN_STACKS_OR_RECENT_TASKS); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 167 | if (task == null) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 168 | throw new IllegalArgumentException("Unable to find task ID " + mTaskId); |
| 169 | } |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 170 | Intent intent = task.getBaseIntent(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 171 | 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 Ogunwale | ab5de37 | 2017-10-18 06:46:31 -0700 | [diff] [blame] | 182 | } |