Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 18 | |
| 19 | import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED; |
| 20 | import static android.app.ActivityManager.LOCK_TASK_MODE_NONE; |
| 21 | import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED; |
Wale Ogunwale | 0568aed | 2017-09-08 13:29:37 -0700 | [diff] [blame] | 22 | import static android.app.WindowConfiguration.WINDOWING_MODE_UNDEFINED; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 23 | import static android.content.Context.DEVICE_POLICY_SERVICE; |
| 24 | import static android.content.Context.STATUS_BAR_SERVICE; |
Charles He | 858f132 | 2017-11-27 17:11:04 -0800 | [diff] [blame] | 25 | import static android.content.Intent.ACTION_CALL_EMERGENCY; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 26 | import static android.os.UserHandle.USER_ALL; |
Amith Yamasani | 7cbbf222 | 2017-08-30 14:22:37 -0700 | [diff] [blame] | 27 | import static android.os.UserHandle.USER_CURRENT; |
Charles He | 858f132 | 2017-11-27 17:11:04 -0800 | [diff] [blame] | 28 | import static android.telecom.TelecomManager.EMERGENCY_DIALER_COMPONENT; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 29 | import static android.view.Display.DEFAULT_DISPLAY; |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 30 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 31 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_LOCKTASK; |
| 32 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_LOCKTASK; |
| 33 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM; |
| 34 | import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 35 | import static com.android.server.wm.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK; |
| 36 | import static com.android.server.wm.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE; |
| 37 | import static com.android.server.wm.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV; |
| 38 | import static com.android.server.wm.TaskRecord.LOCK_TASK_AUTH_PINNABLE; |
| 39 | import static com.android.server.wm.TaskRecord.LOCK_TASK_AUTH_WHITELISTED; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 40 | |
| 41 | import android.annotation.NonNull; |
| 42 | import android.annotation.Nullable; |
| 43 | import android.app.Activity; |
| 44 | import android.app.ActivityManager; |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 45 | import android.app.StatusBarManager; |
| 46 | import android.app.admin.DevicePolicyManager; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 47 | import android.app.admin.IDevicePolicyManager; |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 48 | import android.content.ComponentName; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 49 | import android.content.Context; |
Charles He | 858f132 | 2017-11-27 17:11:04 -0800 | [diff] [blame] | 50 | import android.content.Intent; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 51 | import android.os.Binder; |
| 52 | import android.os.Debug; |
| 53 | import android.os.Handler; |
| 54 | import android.os.IBinder; |
| 55 | import android.os.RemoteException; |
| 56 | import android.os.ServiceManager; |
| 57 | import android.provider.Settings; |
Charles He | 858f132 | 2017-11-27 17:11:04 -0800 | [diff] [blame] | 58 | import android.telecom.TelecomManager; |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 59 | import android.util.Pair; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 60 | import android.util.Slog; |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 61 | import android.util.SparseArray; |
Charles He | 858f132 | 2017-11-27 17:11:04 -0800 | [diff] [blame] | 62 | import android.util.SparseIntArray; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 63 | |
| 64 | import com.android.internal.annotations.VisibleForTesting; |
Tony Mak | 2bf3691 | 2018-03-21 15:45:52 +0000 | [diff] [blame] | 65 | import com.android.internal.policy.IKeyguardDismissCallback; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 66 | import com.android.internal.statusbar.IStatusBarService; |
| 67 | import com.android.internal.widget.LockPatternUtils; |
| 68 | import com.android.server.LocalServices; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 69 | import com.android.server.am.ActivityManagerService; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 70 | import com.android.server.statusbar.StatusBarManagerInternal; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 71 | |
| 72 | import java.io.PrintWriter; |
| 73 | import java.util.ArrayList; |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 74 | import java.util.Arrays; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Helper class that deals with all things related to task locking. This includes the screen pinning |
| 78 | * mode that can be launched via System UI as well as the fully locked mode that can be achieved |
| 79 | * on fully managed devices. |
| 80 | * |
Wale Ogunwale | 27c48ae | 2018-10-25 19:01:01 -0700 | [diff] [blame] | 81 | * Note: All methods in this class should only be called with the ActivityTaskManagerService lock |
| 82 | * held. |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 83 | * |
| 84 | * @see Activity#startLockTask() |
| 85 | * @see Activity#stopLockTask() |
| 86 | */ |
| 87 | public class LockTaskController { |
Wale Ogunwale | 9887561 | 2018-10-12 07:53:02 -0700 | [diff] [blame] | 88 | private static final String TAG = TAG_WITH_CLASS_NAME ? "LockTaskController" : TAG_ATM; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 89 | private static final String TAG_LOCKTASK = TAG + POSTFIX_LOCKTASK; |
| 90 | |
| 91 | @VisibleForTesting |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 92 | static final int STATUS_BAR_MASK_LOCKED = StatusBarManager.DISABLE_MASK |
| 93 | & (~StatusBarManager.DISABLE_EXPAND) |
| 94 | & (~StatusBarManager.DISABLE_NOTIFICATION_TICKER) |
| 95 | & (~StatusBarManager.DISABLE_SYSTEM_INFO) |
| 96 | & (~StatusBarManager.DISABLE_BACK); |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 97 | @VisibleForTesting |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 98 | static final int STATUS_BAR_MASK_PINNED = StatusBarManager.DISABLE_MASK |
| 99 | & (~StatusBarManager.DISABLE_BACK) |
| 100 | & (~StatusBarManager.DISABLE_HOME) |
| 101 | & (~StatusBarManager.DISABLE_RECENT); |
| 102 | |
| 103 | private static final SparseArray<Pair<Integer, Integer>> STATUS_BAR_FLAG_MAP_LOCKED; |
| 104 | static { |
| 105 | STATUS_BAR_FLAG_MAP_LOCKED = new SparseArray<>(); |
| 106 | |
| 107 | STATUS_BAR_FLAG_MAP_LOCKED.append(DevicePolicyManager.LOCK_TASK_FEATURE_SYSTEM_INFO, |
| 108 | new Pair<>(StatusBarManager.DISABLE_CLOCK, StatusBarManager.DISABLE2_SYSTEM_ICONS)); |
| 109 | |
| 110 | STATUS_BAR_FLAG_MAP_LOCKED.append(DevicePolicyManager.LOCK_TASK_FEATURE_NOTIFICATIONS, |
| 111 | new Pair<>(StatusBarManager.DISABLE_NOTIFICATION_ICONS |
| 112 | | StatusBarManager.DISABLE_NOTIFICATION_ALERTS, |
| 113 | StatusBarManager.DISABLE2_NOTIFICATION_SHADE)); |
| 114 | |
| 115 | STATUS_BAR_FLAG_MAP_LOCKED.append(DevicePolicyManager.LOCK_TASK_FEATURE_HOME, |
| 116 | new Pair<>(StatusBarManager.DISABLE_HOME, StatusBarManager.DISABLE2_NONE)); |
| 117 | |
Benjamin Franz | caffa77 | 2018-02-05 16:36:10 +0000 | [diff] [blame] | 118 | STATUS_BAR_FLAG_MAP_LOCKED.append(DevicePolicyManager.LOCK_TASK_FEATURE_OVERVIEW, |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 119 | new Pair<>(StatusBarManager.DISABLE_RECENT, StatusBarManager.DISABLE2_NONE)); |
| 120 | |
| 121 | STATUS_BAR_FLAG_MAP_LOCKED.append(DevicePolicyManager.LOCK_TASK_FEATURE_GLOBAL_ACTIONS, |
| 122 | new Pair<>(StatusBarManager.DISABLE_NONE, |
| 123 | StatusBarManager.DISABLE2_GLOBAL_ACTIONS)); |
| 124 | } |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 125 | |
| 126 | /** Tag used for disabling of keyguard */ |
| 127 | private static final String LOCK_TASK_TAG = "Lock-to-App"; |
| 128 | |
| 129 | private final IBinder mToken = new Binder(); |
| 130 | private final ActivityStackSupervisor mSupervisor; |
| 131 | private final Context mContext; |
| 132 | |
| 133 | // The following system services cannot be final, because they do not exist when this class |
| 134 | // is instantiated during device boot |
| 135 | @VisibleForTesting |
| 136 | IStatusBarService mStatusBarService; |
| 137 | @VisibleForTesting |
| 138 | IDevicePolicyManager mDevicePolicyManager; |
| 139 | @VisibleForTesting |
| 140 | WindowManagerService mWindowManager; |
| 141 | @VisibleForTesting |
| 142 | LockPatternUtils mLockPatternUtils; |
Charles He | 858f132 | 2017-11-27 17:11:04 -0800 | [diff] [blame] | 143 | @VisibleForTesting |
| 144 | TelecomManager mTelecomManager; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 145 | |
| 146 | /** |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 147 | * The chain of tasks in LockTask mode, in the order of when they first entered LockTask mode. |
| 148 | * |
| 149 | * The first task in the list, which started the current LockTask session, is called the root |
| 150 | * task. It coincides with the Home task in a typical multi-app kiosk deployment. When there are |
| 151 | * more than one locked tasks, the root task can't be finished. Nor can it be moved to the back |
| 152 | * of the stack by {@link ActivityStack#moveTaskToBackLocked(int)}; |
| 153 | * |
| 154 | * Calling {@link Activity#stopLockTask()} on the root task will finish all tasks but itself in |
| 155 | * this list, and the device will exit LockTask mode. |
| 156 | * |
| 157 | * The list is empty if LockTask is inactive. |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 158 | */ |
| 159 | private final ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>(); |
| 160 | |
| 161 | /** |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 162 | * Packages that are allowed to be launched into the lock task mode for each user. |
| 163 | */ |
| 164 | private final SparseArray<String[]> mLockTaskPackages = new SparseArray<>(); |
| 165 | |
| 166 | /** |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 167 | * Features that are allowed by DPC to show during LockTask mode. |
| 168 | */ |
Charles He | 858f132 | 2017-11-27 17:11:04 -0800 | [diff] [blame] | 169 | private final SparseIntArray mLockTaskFeatures = new SparseIntArray(); |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 170 | |
| 171 | /** |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 172 | * Store the current lock task mode. Possible values: |
| 173 | * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED}, |
| 174 | * {@link ActivityManager#LOCK_TASK_MODE_PINNED} |
| 175 | */ |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 176 | private int mLockTaskModeState = LOCK_TASK_MODE_NONE; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * This is ActivityStackSupervisor's Handler. |
| 180 | */ |
| 181 | private final Handler mHandler; |
| 182 | |
| 183 | LockTaskController(Context context, ActivityStackSupervisor supervisor, |
| 184 | Handler handler) { |
| 185 | mContext = context; |
| 186 | mSupervisor = supervisor; |
| 187 | mHandler = handler; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Set the window manager instance used in this class. This is necessary, because the window |
| 192 | * manager does not exist during instantiation of this class. |
| 193 | */ |
| 194 | void setWindowManager(WindowManagerService windowManager) { |
| 195 | mWindowManager = windowManager; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * @return the current lock task state. This can be any of |
| 200 | * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED}, |
| 201 | * {@link ActivityManager#LOCK_TASK_MODE_PINNED}. |
| 202 | */ |
| 203 | int getLockTaskModeState() { |
| 204 | return mLockTaskModeState; |
| 205 | } |
| 206 | |
| 207 | /** |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 208 | * @return whether the given task is locked at the moment. Locked tasks cannot be moved to the |
| 209 | * back of the stack. |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 210 | */ |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 211 | @VisibleForTesting |
| 212 | boolean isTaskLocked(TaskRecord task) { |
| 213 | return mLockTaskModeTasks.contains(task); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @return {@code true} whether this task first started the current LockTask session. |
| 218 | */ |
| 219 | private boolean isRootTask(TaskRecord task) { |
| 220 | return mLockTaskModeTasks.indexOf(task) == 0; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * @return whether the given activity is blocked from finishing, because it is the only activity |
| 225 | * of the last locked task and finishing it would mean that lock task mode is ended illegally. |
| 226 | */ |
| 227 | boolean activityBlockedFromFinish(ActivityRecord activity) { |
Wale Ogunwale | 8b19de9 | 2018-11-29 19:58:26 -0800 | [diff] [blame] | 228 | final TaskRecord task = activity.getTaskRecord(); |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 229 | if (activity == task.getRootActivity() |
| 230 | && activity == task.getTopActivity() |
| 231 | && task.mLockTaskAuth != LOCK_TASK_AUTH_LAUNCHABLE_PRIV |
| 232 | && isRootTask(task)) { |
| 233 | Slog.i(TAG, "Not finishing task in lock task mode"); |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 234 | showLockTaskToast(); |
| 235 | return true; |
| 236 | } |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | /** |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 241 | * @return whether the given task can be moved to the back of the stack with |
| 242 | * {@link ActivityStack#moveTaskToBackLocked(int)} |
| 243 | * @see #mLockTaskModeTasks |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 244 | */ |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 245 | boolean canMoveTaskToBack(TaskRecord task) { |
| 246 | if (isRootTask(task)) { |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 247 | showLockTaskToast(); |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 248 | return false; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 249 | } |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 250 | return true; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
Charles He | d62f965 | 2017-11-01 10:05:51 +0000 | [diff] [blame] | 254 | * @return whether the requested task is allowed to be locked (either whitelisted, or declares |
| 255 | * lockTaskMode="always" in the manifest). |
| 256 | */ |
| 257 | boolean isTaskWhitelisted(TaskRecord task) { |
| 258 | switch(task.mLockTaskAuth) { |
| 259 | case LOCK_TASK_AUTH_WHITELISTED: |
| 260 | case LOCK_TASK_AUTH_LAUNCHABLE: |
| 261 | case LOCK_TASK_AUTH_LAUNCHABLE_PRIV: |
| 262 | return true; |
| 263 | case LOCK_TASK_AUTH_PINNABLE: |
| 264 | case LOCK_TASK_AUTH_DONT_LOCK: |
| 265 | default: |
| 266 | return false; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * @return whether the requested task is disallowed to be launched. |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 272 | */ |
| 273 | boolean isLockTaskModeViolation(TaskRecord task) { |
| 274 | return isLockTaskModeViolation(task, false); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * @param isNewClearTask whether the task would be cleared as part of the operation. |
Charles He | d62f965 | 2017-11-01 10:05:51 +0000 | [diff] [blame] | 279 | * @return whether the requested task is disallowed to be launched. |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 280 | */ |
| 281 | boolean isLockTaskModeViolation(TaskRecord task, boolean isNewClearTask) { |
| 282 | if (isLockTaskModeViolationInternal(task, isNewClearTask)) { |
| 283 | showLockTaskToast(); |
| 284 | return true; |
| 285 | } |
| 286 | return false; |
| 287 | } |
| 288 | |
Benjamin Franz | 7dcbfb0 | 2018-01-16 15:16:16 +0000 | [diff] [blame] | 289 | /** |
| 290 | * @return the root task of the lock task. |
| 291 | */ |
| 292 | TaskRecord getRootTask() { |
| 293 | if (mLockTaskModeTasks.isEmpty()) { |
| 294 | return null; |
| 295 | } |
| 296 | return mLockTaskModeTasks.get(0); |
| 297 | } |
| 298 | |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 299 | private boolean isLockTaskModeViolationInternal(TaskRecord task, boolean isNewClearTask) { |
| 300 | // TODO: Double check what's going on here. If the task is already in lock task mode, it's |
| 301 | // likely whitelisted, so will return false below. |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 302 | if (isTaskLocked(task) && !isNewClearTask) { |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 303 | // If the task is already at the top and won't be cleared, then allow the operation |
| 304 | return false; |
| 305 | } |
Charles He | d62f965 | 2017-11-01 10:05:51 +0000 | [diff] [blame] | 306 | |
| 307 | // Allow recents activity if enabled by policy |
| 308 | if (task.isActivityTypeRecents() && isRecentsAllowed(task.userId)) { |
| 309 | return false; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 310 | } |
Charles He | d62f965 | 2017-11-01 10:05:51 +0000 | [diff] [blame] | 311 | |
Charles He | 858f132 | 2017-11-27 17:11:04 -0800 | [diff] [blame] | 312 | // Allow emergency calling when the device is protected by a locked keyguard |
| 313 | if (isKeyguardAllowed(task.userId) && isEmergencyCallTask(task)) { |
| 314 | return false; |
| 315 | } |
| 316 | |
Charles He | d62f965 | 2017-11-01 10:05:51 +0000 | [diff] [blame] | 317 | return !(isTaskWhitelisted(task) || mLockTaskModeTasks.isEmpty()); |
| 318 | } |
| 319 | |
| 320 | private boolean isRecentsAllowed(int userId) { |
| 321 | return (getLockTaskFeaturesForUser(userId) |
Benjamin Franz | caffa77 | 2018-02-05 16:36:10 +0000 | [diff] [blame] | 322 | & DevicePolicyManager.LOCK_TASK_FEATURE_OVERVIEW) != 0; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Charles He | 858f132 | 2017-11-27 17:11:04 -0800 | [diff] [blame] | 325 | private boolean isKeyguardAllowed(int userId) { |
| 326 | return (getLockTaskFeaturesForUser(userId) |
| 327 | & DevicePolicyManager.LOCK_TASK_FEATURE_KEYGUARD) != 0; |
| 328 | } |
| 329 | |
| 330 | private boolean isEmergencyCallTask(TaskRecord task) { |
| 331 | final Intent intent = task.intent; |
| 332 | if (intent == null) { |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | // 1. The emergency keypad activity launched on top of the keyguard |
| 337 | if (EMERGENCY_DIALER_COMPONENT.equals(intent.getComponent())) { |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | // 2. The intent sent by the keypad, which is handled by Telephony |
| 342 | if (ACTION_CALL_EMERGENCY.equals(intent.getAction())) { |
| 343 | return true; |
| 344 | } |
| 345 | |
| 346 | // 3. Telephony then starts the default package for making the call |
| 347 | final TelecomManager tm = getTelecomManager(); |
| 348 | final String dialerPackage = tm != null ? tm.getSystemDialerPackage() : null; |
| 349 | if (dialerPackage != null && dialerPackage.equals(intent.getComponent().getPackageName())) { |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | return false; |
| 354 | } |
| 355 | |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 356 | /** |
| 357 | * Stop the current lock task mode. |
| 358 | * |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 359 | * This is called by {@link ActivityManagerService} and performs various checks before actually |
| 360 | * finishing the locked task. |
| 361 | * |
| 362 | * @param task the task that requested the end of lock task mode ({@code null} for quitting app |
| 363 | * pinning mode) |
| 364 | * @param isSystemCaller indicates whether this request comes from the system via |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 365 | * {@link ActivityTaskManagerService#stopSystemLockTaskMode()}. If |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 366 | * {@code true}, it means the user intends to stop pinned mode through UI; |
| 367 | * otherwise, it's called by an app and we need to stop locked or pinned |
| 368 | * mode, subject to checks. |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 369 | * @param callingUid the caller that requested the end of lock task mode. |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 370 | * @throws IllegalArgumentException if the calling task is invalid (e.g., {@code null} or not in |
| 371 | * foreground) |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 372 | * @throws SecurityException if the caller is not authorized to stop the lock task mode, i.e. if |
| 373 | * they differ from the one that launched lock task mode. |
| 374 | */ |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 375 | void stopLockTaskMode(@Nullable TaskRecord task, boolean isSystemCaller, int callingUid) { |
| 376 | if (mLockTaskModeState == LOCK_TASK_MODE_NONE) { |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 377 | return; |
| 378 | } |
| 379 | |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 380 | if (isSystemCaller) { |
| 381 | if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) { |
| 382 | clearLockedTasks("stopAppPinning"); |
| 383 | } else { |
| 384 | Slog.e(TAG_LOCKTASK, "Attempted to stop LockTask with isSystemCaller=true"); |
| 385 | showLockTaskToast(); |
| 386 | } |
| 387 | |
| 388 | } else { |
| 389 | // Ensure calling activity is not null |
| 390 | if (task == null) { |
| 391 | throw new IllegalArgumentException("can't stop LockTask for null task"); |
| 392 | } |
| 393 | |
| 394 | // Ensure the same caller for startLockTaskMode and stopLockTaskMode. |
| 395 | // It is possible lockTaskMode was started by the system process because |
| 396 | // android:lockTaskMode is set to a locking value in the application manifest |
| 397 | // instead of the app calling startLockTaskMode. In this case |
| 398 | // {@link TaskRecord.mLockTaskUid} will be 0, so we compare the callingUid to the |
| 399 | // {@link TaskRecord.effectiveUid} instead. Also caller with |
| 400 | // {@link MANAGE_ACTIVITY_STACKS} can stop any lock task. |
| 401 | if (callingUid != task.mLockTaskUid |
| 402 | && (task.mLockTaskUid != 0 || callingUid != task.effectiveUid)) { |
| 403 | throw new SecurityException("Invalid uid, expected " + task.mLockTaskUid |
| 404 | + " callingUid=" + callingUid + " effectiveUid=" + task.effectiveUid); |
| 405 | } |
| 406 | |
| 407 | // We don't care if it's pinned or locked mode; this will stop it anyways. |
| 408 | clearLockedTask(task); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Clear all locked tasks and request the end of LockTask mode. |
| 414 | * |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 415 | * This method is called by UserController when starting a new foreground user, and, |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 416 | * unlike {@link #stopLockTaskMode(TaskRecord, boolean, int)}, it doesn't perform the checks. |
| 417 | */ |
| 418 | void clearLockedTasks(String reason) { |
| 419 | if (DEBUG_LOCKTASK) Slog.i(TAG_LOCKTASK, "clearLockedTasks: " + reason); |
| 420 | if (!mLockTaskModeTasks.isEmpty()) { |
| 421 | clearLockedTask(mLockTaskModeTasks.get(0)); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Clear one locked task from LockTask mode. |
| 427 | * |
| 428 | * If the requested task is the root task (see {@link #mLockTaskModeTasks}), then all locked |
| 429 | * tasks are cleared. Otherwise, only the requested task is cleared. LockTask mode is stopped |
| 430 | * when the last locked task is cleared. |
| 431 | * |
| 432 | * @param task the task to be cleared from LockTask mode. |
| 433 | */ |
| 434 | void clearLockedTask(final TaskRecord task) { |
| 435 | if (task == null || mLockTaskModeTasks.isEmpty()) return; |
| 436 | |
| 437 | if (task == mLockTaskModeTasks.get(0)) { |
| 438 | // We're removing the root task while there are other locked tasks. Therefore we should |
| 439 | // clear all locked tasks in reverse order. |
| 440 | for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx > 0; --taskNdx) { |
| 441 | clearLockedTask(mLockTaskModeTasks.get(taskNdx)); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | removeLockedTask(task); |
| 446 | if (mLockTaskModeTasks.isEmpty()) { |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 447 | return; |
| 448 | } |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 449 | task.performClearTaskLocked(); |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 450 | mSupervisor.mRootActivityContainer.resumeFocusedStacksTopActivities(); |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Remove the given task from the locked task list. If this was the last task in the list, |
| 455 | * lock task mode is stopped. |
| 456 | */ |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 457 | private void removeLockedTask(final TaskRecord task) { |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 458 | if (!mLockTaskModeTasks.remove(task)) { |
| 459 | return; |
| 460 | } |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 461 | if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: removed " + task); |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 462 | if (mLockTaskModeTasks.isEmpty()) { |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 463 | if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "removeLockedTask: task=" + task + |
| 464 | " last task, reverting locktask mode. Callers=" + Debug.getCallers(3)); |
| 465 | mHandler.post(() -> performStopLockTask(task.userId)); |
| 466 | } |
| 467 | } |
| 468 | |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 469 | // This method should only be called on the handler thread |
| 470 | private void performStopLockTask(int userId) { |
| 471 | // When lock task ends, we enable the status bars. |
| 472 | try { |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 473 | setStatusBarState(LOCK_TASK_MODE_NONE, userId); |
| 474 | setKeyguardState(LOCK_TASK_MODE_NONE, userId); |
| 475 | if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) { |
| 476 | lockKeyguardIfNeeded(); |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 477 | } |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 478 | if (getDevicePolicyManager() != null) { |
| 479 | getDevicePolicyManager().notifyLockTaskModeChanged(false, null, userId); |
| 480 | } |
Charles He | bfe82d1 | 2017-10-20 11:59:44 +0100 | [diff] [blame] | 481 | if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) { |
Matthew Ng | 9c3bce5 | 2018-02-01 22:00:31 +0000 | [diff] [blame] | 482 | getStatusBarService().showPinningEnterExitToast(false /* entering */); |
Charles He | bfe82d1 | 2017-10-20 11:59:44 +0100 | [diff] [blame] | 483 | } |
Benjamin Franz | 3662b15 | 2018-01-16 17:23:44 +0000 | [diff] [blame] | 484 | mWindowManager.onLockTaskStateChanged(LOCK_TASK_MODE_NONE); |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 485 | } catch (RemoteException ex) { |
| 486 | throw new RuntimeException(ex); |
| 487 | } finally { |
| 488 | mLockTaskModeState = LOCK_TASK_MODE_NONE; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /** |
Charles He | bfe82d1 | 2017-10-20 11:59:44 +0100 | [diff] [blame] | 493 | * Show the lock task violation toast. Currently we only show toast for screen pinning mode, and |
| 494 | * no-op if the device is in locked mode. |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 495 | */ |
| 496 | void showLockTaskToast() { |
Charles He | bfe82d1 | 2017-10-20 11:59:44 +0100 | [diff] [blame] | 497 | if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) { |
Matthew Ng | 9c3bce5 | 2018-02-01 22:00:31 +0000 | [diff] [blame] | 498 | try { |
| 499 | getStatusBarService().showPinningEscapeToast(); |
| 500 | } catch (RemoteException e) { |
| 501 | Slog.e(TAG, "Failed to send pinning escape toast", e); |
| 502 | } |
Charles He | bfe82d1 | 2017-10-20 11:59:44 +0100 | [diff] [blame] | 503 | } |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | // Starting lock task |
| 507 | |
| 508 | /** |
| 509 | * Method to start lock task mode on a given task. |
| 510 | * |
| 511 | * @param task the task that should be locked. |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 512 | * @param isSystemCaller indicates whether this request was initiated by the system via |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 513 | * {@link ActivityTaskManagerService#startSystemLockTaskMode(int)}. If |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 514 | * {@code true}, this intends to start pinned mode; otherwise, we look |
| 515 | * at the calling task's mLockTaskAuth to decide which mode to start. |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 516 | * @param callingUid the caller that requested the launch of lock task mode. |
| 517 | */ |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 518 | void startLockTaskMode(@NonNull TaskRecord task, boolean isSystemCaller, int callingUid) { |
| 519 | if (!isSystemCaller) { |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 520 | task.mLockTaskUid = callingUid; |
| 521 | if (task.mLockTaskAuth == LOCK_TASK_AUTH_PINNABLE) { |
| 522 | // startLockTask() called by app, but app is not part of lock task whitelist. Show |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 523 | // app pinning request. We will come back here with isSystemCaller true. |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 524 | if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "Mode default, asking user"); |
| 525 | StatusBarManagerInternal statusBarManager = LocalServices.getService( |
| 526 | StatusBarManagerInternal.class); |
| 527 | if (statusBarManager != null) { |
| 528 | statusBarManager.showScreenPinningRequest(task.taskId); |
| 529 | } |
| 530 | return; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | // System can only initiate screen pinning, not full lock task mode |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 535 | if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, |
| 536 | isSystemCaller ? "Locking pinned" : "Locking fully"); |
| 537 | setLockTaskMode(task, isSystemCaller ? LOCK_TASK_MODE_PINNED : LOCK_TASK_MODE_LOCKED, |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 538 | "startLockTask", true); |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Start lock task mode on the given task. |
| 543 | * @param lockTaskModeState whether fully locked or pinned mode. |
| 544 | * @param andResume whether the task should be brought to foreground as part of the operation. |
| 545 | */ |
| 546 | private void setLockTaskMode(@NonNull TaskRecord task, int lockTaskModeState, |
| 547 | String reason, boolean andResume) { |
| 548 | // Should have already been checked, but do it again. |
| 549 | if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) { |
| 550 | if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, |
| 551 | "setLockTaskMode: Can't lock due to auth"); |
| 552 | return; |
| 553 | } |
| 554 | if (isLockTaskModeViolation(task)) { |
| 555 | Slog.e(TAG_LOCKTASK, "setLockTaskMode: Attempt to start an unauthorized lock task."); |
| 556 | return; |
| 557 | } |
| 558 | |
Bryce Lee | 1a990e5 | 2018-04-23 10:54:11 -0700 | [diff] [blame] | 559 | final Intent taskIntent = task.intent; |
| 560 | if (mLockTaskModeTasks.isEmpty() && taskIntent != null) { |
Charles He | d62f965 | 2017-11-01 10:05:51 +0000 | [diff] [blame] | 561 | mSupervisor.mRecentTasks.onLockTaskModeStateChanged(lockTaskModeState, task.userId); |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 562 | // Start lock task on the handler thread |
| 563 | mHandler.post(() -> performStartLockTask( |
Bryce Lee | 1a990e5 | 2018-04-23 10:54:11 -0700 | [diff] [blame] | 564 | taskIntent.getComponent().getPackageName(), |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 565 | task.userId, |
| 566 | lockTaskModeState)); |
| 567 | } |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 568 | if (DEBUG_LOCKTASK) Slog.w(TAG_LOCKTASK, "setLockTaskMode: Locking to " + task + |
| 569 | " Callers=" + Debug.getCallers(4)); |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 570 | |
| 571 | if (!mLockTaskModeTasks.contains(task)) { |
| 572 | mLockTaskModeTasks.add(task); |
| 573 | } |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 574 | |
| 575 | if (task.mLockTaskUid == -1) { |
| 576 | task.mLockTaskUid = task.effectiveUid; |
| 577 | } |
| 578 | |
| 579 | if (andResume) { |
Wale Ogunwale | 66e1685 | 2017-10-19 13:35:52 -0700 | [diff] [blame] | 580 | mSupervisor.findTaskToMoveToFront(task, 0, null, reason, |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 581 | lockTaskModeState != LOCK_TASK_MODE_NONE); |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 582 | mSupervisor.mRootActivityContainer.resumeFocusedStacksTopActivities(); |
lumark | 588a3e8 | 2018-07-20 18:53:54 +0800 | [diff] [blame] | 583 | final ActivityStack stack = task.getStack(); |
| 584 | if (stack != null) { |
| 585 | stack.getDisplay().getWindowContainerController().executeAppTransition(); |
| 586 | } |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 587 | } else if (lockTaskModeState != LOCK_TASK_MODE_NONE) { |
Wale Ogunwale | 0568aed | 2017-09-08 13:29:37 -0700 | [diff] [blame] | 588 | mSupervisor.handleNonResizableTaskIfNeeded(task, WINDOWING_MODE_UNDEFINED, |
Wale Ogunwale | a0f5b5e | 2017-10-11 09:37:23 -0700 | [diff] [blame] | 589 | DEFAULT_DISPLAY, task.getStack(), true /* forceNonResizable */); |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
| 593 | // This method should only be called on the handler thread |
| 594 | private void performStartLockTask(String packageName, int userId, int lockTaskModeState) { |
| 595 | // When lock task starts, we disable the status bars. |
| 596 | try { |
Charles He | bfe82d1 | 2017-10-20 11:59:44 +0100 | [diff] [blame] | 597 | if (lockTaskModeState == LOCK_TASK_MODE_PINNED) { |
Matthew Ng | 9c3bce5 | 2018-02-01 22:00:31 +0000 | [diff] [blame] | 598 | getStatusBarService().showPinningEnterExitToast(true /* entering */); |
Charles He | bfe82d1 | 2017-10-20 11:59:44 +0100 | [diff] [blame] | 599 | } |
Benjamin Franz | 3662b15 | 2018-01-16 17:23:44 +0000 | [diff] [blame] | 600 | mWindowManager.onLockTaskStateChanged(lockTaskModeState); |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 601 | mLockTaskModeState = lockTaskModeState; |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 602 | setStatusBarState(lockTaskModeState, userId); |
| 603 | setKeyguardState(lockTaskModeState, userId); |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 604 | if (getDevicePolicyManager() != null) { |
| 605 | getDevicePolicyManager().notifyLockTaskModeChanged(true, packageName, userId); |
| 606 | } |
| 607 | } catch (RemoteException ex) { |
| 608 | throw new RuntimeException(ex); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | /** |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 613 | * Update packages that are allowed to be launched in lock task mode. |
| 614 | * @param userId Which user this whitelist is associated with |
| 615 | * @param packages The whitelist of packages allowed in lock task mode |
| 616 | * @see #mLockTaskPackages |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 617 | */ |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 618 | void updateLockTaskPackages(int userId, String[] packages) { |
| 619 | mLockTaskPackages.put(userId, packages); |
| 620 | |
| 621 | boolean taskChanged = false; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 622 | for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) { |
| 623 | final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx); |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 624 | final boolean wasWhitelisted = lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE |
| 625 | || lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 626 | lockedTask.setLockTaskAuth(); |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 627 | final boolean isWhitelisted = lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE |
| 628 | || lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_WHITELISTED; |
| 629 | |
| 630 | if (mLockTaskModeState != LOCK_TASK_MODE_LOCKED |
| 631 | || lockedTask.userId != userId |
| 632 | || !wasWhitelisted || isWhitelisted) { |
| 633 | continue; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 634 | } |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 635 | |
| 636 | // Terminate locked tasks that have recently lost whitelist authorization. |
| 637 | if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, "onLockTaskPackagesUpdated: removing " + |
| 638 | lockedTask + " mLockTaskAuth()=" + lockedTask.lockTaskAuthToString()); |
| 639 | removeLockedTask(lockedTask); |
| 640 | lockedTask.performClearTaskLocked(); |
| 641 | taskChanged = true; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 642 | } |
| 643 | |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 644 | for (int displayNdx = mSupervisor.mRootActivityContainer.getChildCount() - 1; |
| 645 | displayNdx >= 0; --displayNdx) { |
| 646 | mSupervisor.mRootActivityContainer.getChildAt(displayNdx).onLockTaskPackagesUpdated(); |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 647 | } |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 648 | |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 649 | final ActivityRecord r = mSupervisor.mRootActivityContainer.topRunningActivity(); |
Wale Ogunwale | 8b19de9 | 2018-11-29 19:58:26 -0800 | [diff] [blame] | 650 | final TaskRecord task = (r != null) ? r.getTaskRecord() : null; |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 651 | if (mLockTaskModeTasks.isEmpty() && task!= null |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 652 | && task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) { |
| 653 | // This task must have just been authorized. |
| 654 | if (DEBUG_LOCKTASK) Slog.d(TAG_LOCKTASK, |
| 655 | "onLockTaskPackagesUpdated: starting new locktask task=" + task); |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 656 | setLockTaskMode(task, LOCK_TASK_MODE_LOCKED, "package updated", false); |
| 657 | taskChanged = true; |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 658 | } |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 659 | |
| 660 | if (taskChanged) { |
Wale Ogunwale | d32da47 | 2018-11-16 07:19:28 -0800 | [diff] [blame] | 661 | mSupervisor.mRootActivityContainer.resumeFocusedStacksTopActivities(); |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 665 | boolean isPackageWhitelisted(int userId, String pkg) { |
| 666 | if (pkg == null) { |
| 667 | return false; |
| 668 | } |
| 669 | String[] whitelist; |
| 670 | whitelist = mLockTaskPackages.get(userId); |
| 671 | if (whitelist == null) { |
| 672 | return false; |
| 673 | } |
| 674 | for (String whitelistedPkg : whitelist) { |
| 675 | if (pkg.equals(whitelistedPkg)) { |
| 676 | return true; |
| 677 | } |
| 678 | } |
| 679 | return false; |
| 680 | } |
| 681 | |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 682 | /** |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 683 | * Update the UI features that are enabled for LockTask mode. |
| 684 | * @param userId Which user these feature flags are associated with |
| 685 | * @param flags Bitfield of feature flags |
| 686 | * @see DevicePolicyManager#setLockTaskFeatures(ComponentName, int) |
| 687 | */ |
| 688 | void updateLockTaskFeatures(int userId, int flags) { |
| 689 | int oldFlags = getLockTaskFeaturesForUser(userId); |
| 690 | if (flags == oldFlags) { |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | mLockTaskFeatures.put(userId, flags); |
Charles He | ff9b4dff | 2017-09-22 10:18:37 +0100 | [diff] [blame] | 695 | if (!mLockTaskModeTasks.isEmpty() && userId == mLockTaskModeTasks.get(0).userId) { |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 696 | mHandler.post(() -> { |
| 697 | if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) { |
| 698 | setStatusBarState(mLockTaskModeState, userId); |
| 699 | setKeyguardState(mLockTaskModeState, userId); |
| 700 | } |
| 701 | }); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Helper method for configuring the status bar disabled state. |
| 707 | * Should only be called on the handler thread to avoid race. |
| 708 | */ |
| 709 | private void setStatusBarState(int lockTaskModeState, int userId) { |
| 710 | IStatusBarService statusBar = getStatusBarService(); |
| 711 | if (statusBar == null) { |
| 712 | Slog.e(TAG, "Can't find StatusBarService"); |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | // Default state, when lockTaskModeState == LOCK_TASK_MODE_NONE |
| 717 | int flags1 = StatusBarManager.DISABLE_NONE; |
| 718 | int flags2 = StatusBarManager.DISABLE2_NONE; |
| 719 | |
| 720 | if (lockTaskModeState == LOCK_TASK_MODE_PINNED) { |
| 721 | flags1 = STATUS_BAR_MASK_PINNED; |
| 722 | |
| 723 | } else if (lockTaskModeState == LOCK_TASK_MODE_LOCKED) { |
| 724 | int lockTaskFeatures = getLockTaskFeaturesForUser(userId); |
| 725 | Pair<Integer, Integer> statusBarFlags = getStatusBarDisableFlags(lockTaskFeatures); |
| 726 | flags1 = statusBarFlags.first; |
| 727 | flags2 = statusBarFlags.second; |
| 728 | } |
| 729 | |
| 730 | try { |
| 731 | statusBar.disable(flags1, mToken, mContext.getPackageName()); |
| 732 | statusBar.disable2(flags2, mToken, mContext.getPackageName()); |
| 733 | } catch (RemoteException e) { |
| 734 | Slog.e(TAG, "Failed to set status bar flags", e); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Helper method for configuring the keyguard disabled state. |
| 740 | * Should only be called on the handler thread to avoid race. |
| 741 | */ |
| 742 | private void setKeyguardState(int lockTaskModeState, int userId) { |
| 743 | if (lockTaskModeState == LOCK_TASK_MODE_NONE) { |
| 744 | mWindowManager.reenableKeyguard(mToken); |
| 745 | |
| 746 | } else if (lockTaskModeState == LOCK_TASK_MODE_LOCKED) { |
Charles He | 858f132 | 2017-11-27 17:11:04 -0800 | [diff] [blame] | 747 | if (isKeyguardAllowed(userId)) { |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 748 | mWindowManager.reenableKeyguard(mToken); |
Charles He | 858f132 | 2017-11-27 17:11:04 -0800 | [diff] [blame] | 749 | } else { |
Tony Mak | 2bf3691 | 2018-03-21 15:45:52 +0000 | [diff] [blame] | 750 | // If keyguard is not secure and it is locked, dismiss the keyguard before |
| 751 | // disabling it, which avoids the platform to think the keyguard is still on. |
| 752 | if (mWindowManager.isKeyguardLocked() && !mWindowManager.isKeyguardSecure()) { |
| 753 | mWindowManager.dismissKeyguard(new IKeyguardDismissCallback.Stub() { |
| 754 | @Override |
| 755 | public void onDismissError() throws RemoteException { |
| 756 | Slog.i(TAG, "setKeyguardState: failed to dismiss keyguard"); |
| 757 | } |
| 758 | |
| 759 | @Override |
| 760 | public void onDismissSucceeded() throws RemoteException { |
| 761 | mHandler.post( |
| 762 | () -> mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG)); |
| 763 | } |
| 764 | |
| 765 | @Override |
| 766 | public void onDismissCancelled() throws RemoteException { |
| 767 | Slog.i(TAG, "setKeyguardState: dismiss cancelled"); |
| 768 | } |
| 769 | }, null); |
| 770 | } else { |
| 771 | mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG); |
| 772 | } |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | } else { // lockTaskModeState == LOCK_TASK_MODE_PINNED |
| 776 | mWindowManager.disableKeyguard(mToken, LOCK_TASK_TAG); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Helper method for locking the device immediately. This may be necessary when the device |
| 782 | * leaves the pinned mode. |
| 783 | */ |
| 784 | private void lockKeyguardIfNeeded() { |
| 785 | try { |
| 786 | boolean shouldLockKeyguard = Settings.Secure.getIntForUser( |
| 787 | mContext.getContentResolver(), |
| 788 | Settings.Secure.LOCK_TO_APP_EXIT_LOCKED, |
| 789 | USER_CURRENT) != 0; |
| 790 | if (shouldLockKeyguard) { |
| 791 | mWindowManager.lockNow(null); |
Lucas Dupin | c80c67e | 2017-12-04 14:29:10 -0800 | [diff] [blame] | 792 | mWindowManager.dismissKeyguard(null /* callback */, null /* message */); |
Charles He | e078db7 | 2017-10-19 18:03:20 +0100 | [diff] [blame] | 793 | getLockPatternUtils().requireCredentialEntry(USER_ALL); |
| 794 | } |
| 795 | } catch (Settings.SettingNotFoundException e) { |
| 796 | // No setting, don't lock. |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * Translates from LockTask feature flags to StatusBarManager disable and disable2 flags. |
| 802 | * @param lockTaskFlags Bitfield of flags as per |
| 803 | * {@link DevicePolicyManager#setLockTaskFeatures(ComponentName, int)} |
| 804 | * @return A {@link Pair} of {@link StatusBarManager#disable(int)} and |
| 805 | * {@link StatusBarManager#disable2(int)} flags |
| 806 | */ |
| 807 | @VisibleForTesting |
| 808 | Pair<Integer, Integer> getStatusBarDisableFlags(int lockTaskFlags) { |
| 809 | // Everything is disabled by default |
| 810 | int flags1 = StatusBarManager.DISABLE_MASK; |
| 811 | int flags2 = StatusBarManager.DISABLE2_MASK; |
| 812 | for (int i = STATUS_BAR_FLAG_MAP_LOCKED.size() - 1; i >= 0; i--) { |
| 813 | Pair<Integer, Integer> statusBarFlags = STATUS_BAR_FLAG_MAP_LOCKED.valueAt(i); |
| 814 | if ((STATUS_BAR_FLAG_MAP_LOCKED.keyAt(i) & lockTaskFlags) != 0) { |
| 815 | flags1 &= ~statusBarFlags.first; |
| 816 | flags2 &= ~statusBarFlags.second; |
| 817 | } |
| 818 | } |
| 819 | // Some flags are not used for LockTask purposes, so we mask them |
| 820 | flags1 &= STATUS_BAR_MASK_LOCKED; |
| 821 | return new Pair<>(flags1, flags2); |
| 822 | } |
| 823 | |
| 824 | /** |
| 825 | * Gets the cached value of LockTask feature flags for a specific user. |
| 826 | */ |
| 827 | private int getLockTaskFeaturesForUser(int userId) { |
| 828 | return mLockTaskFeatures.get(userId, DevicePolicyManager.LOCK_TASK_FEATURE_NONE); |
| 829 | } |
| 830 | |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 831 | // Should only be called on the handler thread |
| 832 | @Nullable |
| 833 | private IStatusBarService getStatusBarService() { |
| 834 | if (mStatusBarService == null) { |
| 835 | mStatusBarService = IStatusBarService.Stub.asInterface( |
| 836 | ServiceManager.checkService(STATUS_BAR_SERVICE)); |
| 837 | if (mStatusBarService == null) { |
| 838 | Slog.w("StatusBarManager", "warning: no STATUS_BAR_SERVICE"); |
| 839 | } |
| 840 | } |
| 841 | return mStatusBarService; |
| 842 | } |
| 843 | |
| 844 | // Should only be called on the handler thread |
| 845 | @Nullable |
| 846 | private IDevicePolicyManager getDevicePolicyManager() { |
| 847 | if (mDevicePolicyManager == null) { |
| 848 | mDevicePolicyManager = IDevicePolicyManager.Stub.asInterface( |
| 849 | ServiceManager.checkService(DEVICE_POLICY_SERVICE)); |
| 850 | if (mDevicePolicyManager == null) { |
| 851 | Slog.w(TAG, "warning: no DEVICE_POLICY_SERVICE"); |
| 852 | } |
| 853 | } |
| 854 | return mDevicePolicyManager; |
| 855 | } |
| 856 | |
| 857 | @NonNull |
| 858 | private LockPatternUtils getLockPatternUtils() { |
| 859 | if (mLockPatternUtils == null) { |
| 860 | // We don't preserve the LPU object to save memory |
| 861 | return new LockPatternUtils(mContext); |
| 862 | } |
| 863 | return mLockPatternUtils; |
| 864 | } |
| 865 | |
Charles He | 858f132 | 2017-11-27 17:11:04 -0800 | [diff] [blame] | 866 | @Nullable |
| 867 | private TelecomManager getTelecomManager() { |
| 868 | if (mTelecomManager == null) { |
| 869 | // We don't preserve the TelecomManager object to save memory |
| 870 | return mContext.getSystemService(TelecomManager.class); |
| 871 | } |
| 872 | return mTelecomManager; |
| 873 | } |
| 874 | |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 875 | public void dump(PrintWriter pw, String prefix) { |
Charles He | 520b283 | 2017-09-02 15:27:16 +0100 | [diff] [blame] | 876 | pw.println(prefix + "LockTaskController"); |
| 877 | prefix = prefix + " "; |
| 878 | pw.println(prefix + "mLockTaskModeState=" + lockTaskModeToString()); |
| 879 | pw.println(prefix + "mLockTaskModeTasks="); |
| 880 | for (int i = 0; i < mLockTaskModeTasks.size(); ++i) { |
| 881 | pw.println(prefix + " #" + i + " " + mLockTaskModeTasks.get(i)); |
| 882 | } |
| 883 | pw.println(prefix + "mLockTaskPackages (userId:packages)="); |
| 884 | for (int i = 0; i < mLockTaskPackages.size(); ++i) { |
| 885 | pw.println(prefix + " u" + mLockTaskPackages.keyAt(i) |
| 886 | + ":" + Arrays.toString(mLockTaskPackages.valueAt(i))); |
| 887 | } |
Benjamin Franz | a83859f | 2017-07-03 16:34:14 +0100 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | private String lockTaskModeToString() { |
| 891 | switch (mLockTaskModeState) { |
| 892 | case LOCK_TASK_MODE_LOCKED: |
| 893 | return "LOCKED"; |
| 894 | case LOCK_TASK_MODE_PINNED: |
| 895 | return "PINNED"; |
| 896 | case LOCK_TASK_MODE_NONE: |
| 897 | return "NONE"; |
| 898 | default: return "unknown=" + mLockTaskModeState; |
| 899 | } |
| 900 | } |
| 901 | } |