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 | |
| 17 | package com.android.server.am; |
| 18 | |
| 19 | import static com.android.server.am.ActivityStackSupervisor.REMOVE_FROM_RECENTS; |
| 20 | |
| 21 | import android.app.ActivityManager; |
| 22 | import android.app.IAppTask; |
| 23 | import android.app.IApplicationThread; |
| 24 | import android.content.Intent; |
| 25 | import android.os.Binder; |
| 26 | import android.os.Bundle; |
| 27 | import android.os.IBinder; |
| 28 | import android.os.UserHandle; |
| 29 | |
| 30 | /** |
| 31 | * An implementation of IAppTask, that allows an app to manage its own tasks via |
| 32 | * {@link android.app.ActivityManager.AppTask}. We keep track of the callingUid to ensure that |
| 33 | * only the process that calls getAppTasks() can call the AppTask methods. |
| 34 | */ |
| 35 | class AppTaskImpl extends IAppTask.Stub { |
| 36 | private ActivityManagerService mService; |
| 37 | |
| 38 | private int mTaskId; |
| 39 | private int mCallingUid; |
| 40 | |
| 41 | public AppTaskImpl(ActivityManagerService service, int taskId, int callingUid) { |
| 42 | mService = service; |
| 43 | mTaskId = taskId; |
| 44 | mCallingUid = callingUid; |
| 45 | } |
| 46 | |
| 47 | private void checkCaller() { |
| 48 | if (mCallingUid != Binder.getCallingUid()) { |
| 49 | throw new SecurityException("Caller " + mCallingUid |
| 50 | + " does not match caller of getAppTasks(): " + Binder.getCallingUid()); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void finishAndRemoveTask() { |
| 56 | checkCaller(); |
| 57 | |
| 58 | synchronized (mService) { |
| 59 | long origId = Binder.clearCallingIdentity(); |
| 60 | try { |
| 61 | // We remove the task from recents to preserve backwards |
| 62 | if (!mService.mStackSupervisor.removeTaskByIdLocked(mTaskId, false, |
| 63 | REMOVE_FROM_RECENTS)) { |
| 64 | throw new IllegalArgumentException("Unable to find task ID " + mTaskId); |
| 65 | } |
| 66 | } finally { |
| 67 | Binder.restoreCallingIdentity(origId); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public ActivityManager.RecentTaskInfo getTaskInfo() { |
| 74 | checkCaller(); |
| 75 | |
| 76 | synchronized (mService) { |
| 77 | long origId = Binder.clearCallingIdentity(); |
| 78 | try { |
| 79 | TaskRecord tr = mService.mStackSupervisor.anyTaskForIdLocked(mTaskId); |
| 80 | if (tr == null) { |
| 81 | throw new IllegalArgumentException("Unable to find task ID " + mTaskId); |
| 82 | } |
| 83 | return RecentTasks.createRecentTaskInfo(tr); |
| 84 | } finally { |
| 85 | Binder.restoreCallingIdentity(origId); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void moveToFront() { |
| 92 | checkCaller(); |
| 93 | // Will bring task to front if it already has a root activity. |
| 94 | final long origId = Binder.clearCallingIdentity(); |
| 95 | try { |
| 96 | synchronized (this) { |
| 97 | mService.mStackSupervisor.startActivityFromRecentsInner(mTaskId, null); |
| 98 | } |
| 99 | } finally { |
| 100 | Binder.restoreCallingIdentity(origId); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public int startActivity(IBinder whoThread, String callingPackage, |
| 106 | Intent intent, String resolvedType, Bundle bOptions) { |
| 107 | checkCaller(); |
| 108 | |
| 109 | int callingUser = UserHandle.getCallingUserId(); |
| 110 | TaskRecord tr; |
| 111 | IApplicationThread appThread; |
| 112 | synchronized (mService) { |
| 113 | tr = mService.mStackSupervisor.anyTaskForIdLocked(mTaskId); |
| 114 | if (tr == null) { |
| 115 | throw new IllegalArgumentException("Unable to find task ID " + mTaskId); |
| 116 | } |
| 117 | appThread = IApplicationThread.Stub.asInterface(whoThread); |
| 118 | if (appThread == null) { |
| 119 | throw new IllegalArgumentException("Bad app thread " + appThread); |
| 120 | } |
| 121 | } |
| 122 | return mService.mActivityStarter.startActivityMayWait(appThread, -1, callingPackage, |
| 123 | intent, resolvedType, null, null, null, null, 0, 0, null, null, |
| 124 | null, bOptions, false, callingUser, tr, "AppTaskImpl"); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void setExcludeFromRecents(boolean exclude) { |
| 129 | checkCaller(); |
| 130 | |
| 131 | synchronized (mService) { |
| 132 | long origId = Binder.clearCallingIdentity(); |
| 133 | try { |
| 134 | TaskRecord tr = mService.mStackSupervisor.anyTaskForIdLocked(mTaskId); |
| 135 | if (tr == null) { |
| 136 | throw new IllegalArgumentException("Unable to find task ID " + mTaskId); |
| 137 | } |
| 138 | Intent intent = tr.getBaseIntent(); |
| 139 | if (exclude) { |
| 140 | intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 141 | } else { |
| 142 | intent.setFlags(intent.getFlags() |
| 143 | & ~Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 144 | } |
| 145 | } finally { |
| 146 | Binder.restoreCallingIdentity(origId); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |