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; |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 20 | import static com.android.server.wm.RootActivityContainer.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; |
Andrii Kulian | a8ccae4 | 2019-07-24 18:35:22 +0000 | [diff] [blame] | 30 | import android.util.Slog; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 31 | |
| 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 | */ |
| 37 | class AppTaskImpl extends IAppTask.Stub { |
Ricky Wai | aca8a77 | 2019-04-04 16:01:06 +0100 | [diff] [blame] | 38 | private static final String TAG = "AppTaskImpl"; |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 39 | private ActivityTaskManagerService mService; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 40 | |
| 41 | private int mTaskId; |
| 42 | private int mCallingUid; |
| 43 | |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 44 | public AppTaskImpl(ActivityTaskManagerService service, int taskId, int callingUid) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 45 | 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 Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 61 | synchronized (mService.mGlobalLock) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 62 | long origId = Binder.clearCallingIdentity(); |
| 63 | try { |
| 64 | // We remove the task from recents to preserve backwards |
Wale Ogunwale | 85fb19a | 2019-12-05 10:41:05 +0900 | [diff] [blame] | 65 | if (!mService.mStackSupervisor.removeTaskById(mTaskId, false, |
Winson Chung | 0ec2a35 | 2017-10-26 11:38:30 -0700 | [diff] [blame] | 66 | REMOVE_FROM_RECENTS, "finish-and-remove-task")) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 67 | 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 Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 79 | synchronized (mService.mGlobalLock) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 80 | long origId = Binder.clearCallingIdentity(); |
| 81 | try { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 82 | Task task = mService.mRootActivityContainer.anyTaskForId(mTaskId, |
Winson Chung | 5d339f0 | 2017-10-23 11:10:58 -0700 | [diff] [blame] | 83 | MATCH_TASK_IN_STACKS_OR_RECENT_TASKS); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 84 | if (task == null) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 85 | throw new IllegalArgumentException("Unable to find task ID " + mTaskId); |
| 86 | } |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 87 | return mService.getRecentTasks().createRecentTaskInfo(task); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 88 | } finally { |
| 89 | Binder.restoreCallingIdentity(origId); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Override |
Ricky Wai | aca8a77 | 2019-04-04 16:01:06 +0100 | [diff] [blame] | 95 | public void moveToFront(IApplicationThread appThread, String callingPackage) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 96 | checkCaller(); |
| 97 | // Will bring task to front if it already has a root activity. |
Jorim Jaggi | 4d8d32c | 2018-01-19 15:57:41 +0100 | [diff] [blame] | 98 | final int callingPid = Binder.getCallingPid(); |
| 99 | final int callingUid = Binder.getCallingUid(); |
Andrii Kulian | a8ccae4 | 2019-07-24 18:35:22 +0000 | [diff] [blame] | 100 | 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 Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 106 | final long origId = Binder.clearCallingIdentity(); |
| 107 | try { |
Wale Ogunwale | 9c189e1 | 2018-07-11 15:22:01 -0700 | [diff] [blame] | 108 | synchronized (mService.mGlobalLock) { |
Ricky Wai | 8394649 | 2019-04-11 17:26:57 +0100 | [diff] [blame] | 109 | if (!mService.checkAppSwitchAllowedLocked(callingPid, callingUid, -1, -1, |
| 110 | "Move to front")) { |
| 111 | return; |
| 112 | } |
Ricky Wai | aca8a77 | 2019-04-04 16:01:06 +0100 | [diff] [blame] | 113 | 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 Stokes | 9e24576 | 2019-05-21 14:54:28 +0100 | [diff] [blame] | 121 | if (!mService.isBackgroundActivityStartsEnabled()) { |
Ricky Wai | aca8a77 | 2019-04-04 16:01:06 +0100 | [diff] [blame] | 122 | return; |
| 123 | } |
| 124 | } |
| 125 | mService.mStackSupervisor.startActivityFromRecents(callingPid, |
| 126 | callingUid, mTaskId, null); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 127 | } |
| 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 Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 139 | Task task; |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 140 | IApplicationThread appThread; |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 141 | synchronized (mService.mGlobalLock) { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 142 | task = mService.mRootActivityContainer.anyTaskForId(mTaskId, |
Winson Chung | 5d339f0 | 2017-10-23 11:10:58 -0700 | [diff] [blame] | 143 | MATCH_TASK_IN_STACKS_OR_RECENT_TASKS); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 144 | if (task == null) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 145 | 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 Lee | 4c9a597 | 2017-12-01 22:14:24 -0800 | [diff] [blame] | 152 | |
Wale Ogunwale | 5fa8a8c | 2018-05-08 13:43:21 -0700 | [diff] [blame] | 153 | return mService.getActivityStartController().obtainStarter(intent, "AppTaskImpl") |
Bryce Lee | 4c9a597 | 2017-12-01 22:14:24 -0800 | [diff] [blame] | 154 | .setCaller(appThread) |
| 155 | .setCallingPackage(callingPackage) |
| 156 | .setResolvedType(resolvedType) |
Jorim Jaggi | 4d8d32c | 2018-01-19 15:57:41 +0100 | [diff] [blame] | 157 | .setActivityOptions(bOptions) |
Louis Chang | 54fbb05 | 2019-10-16 17:10:17 +0800 | [diff] [blame] | 158 | .setUserId(callingUser) |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 159 | .setInTask(task) |
Bryce Lee | 4c9a597 | 2017-12-01 22:14:24 -0800 | [diff] [blame] | 160 | .execute(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void setExcludeFromRecents(boolean exclude) { |
| 165 | checkCaller(); |
| 166 | |
Wale Ogunwale | 16e505a | 2018-05-07 15:00:49 -0700 | [diff] [blame] | 167 | synchronized (mService.mGlobalLock) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 168 | long origId = Binder.clearCallingIdentity(); |
| 169 | try { |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 170 | Task task = mService.mRootActivityContainer.anyTaskForId(mTaskId, |
Winson Chung | 5d339f0 | 2017-10-23 11:10:58 -0700 | [diff] [blame] | 171 | MATCH_TASK_IN_STACKS_OR_RECENT_TASKS); |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 172 | if (task == null) { |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 173 | throw new IllegalArgumentException("Unable to find task ID " + mTaskId); |
| 174 | } |
Louis Chang | cdec080 | 2019-11-11 11:45:07 +0800 | [diff] [blame] | 175 | Intent intent = task.getBaseIntent(); |
Winson Chung | 1dbc811 | 2017-09-28 18:05:31 -0700 | [diff] [blame] | 176 | 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 Ogunwale | ab5de37 | 2017-10-18 06:46:31 -0700 | [diff] [blame] | 187 | } |