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