Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.keyguard; |
| 18 | |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 20 | import android.app.ActivityOptions; |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 21 | import android.app.ActivityTaskManager; |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 22 | import android.app.IActivityTaskManager; |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 23 | import android.app.KeyguardManager; |
| 24 | import android.content.ComponentName; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.os.Bundle; |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 28 | import android.os.RemoteException; |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 29 | import android.os.UserHandle; |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 31 | |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 32 | import com.android.internal.annotations.VisibleForTesting; |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 33 | import com.android.systemui.shared.system.ActivityManagerWrapper; |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 34 | import com.android.systemui.shared.system.TaskStackChangeListener; |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 35 | |
| 36 | public class WorkLockActivityController { |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 37 | private static final String TAG = WorkLockActivityController.class.getSimpleName(); |
| 38 | |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 39 | private final Context mContext; |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 40 | private final IActivityTaskManager mIatm; |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 41 | |
| 42 | public WorkLockActivityController(Context context) { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 43 | this(context, ActivityManagerWrapper.getInstance(), ActivityTaskManager.getService()); |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 44 | } |
Robin Lee | 3c82d3d | 2017-01-19 17:09:28 +0000 | [diff] [blame] | 45 | |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 46 | @VisibleForTesting |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 47 | WorkLockActivityController( |
| 48 | Context context, ActivityManagerWrapper am, IActivityTaskManager iAtm) { |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 49 | mContext = context; |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 50 | mIatm = iAtm; |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 51 | |
Winson Chung | 2cf6ad8 | 2017-11-09 17:36:59 -0800 | [diff] [blame] | 52 | am.registerTaskStackListener(mLockListener); |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | private void startWorkChallengeInTask(int taskId, int userId) { |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 56 | ActivityManager.TaskDescription taskDescription = null; |
| 57 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 58 | taskDescription = mIatm.getTaskDescription(taskId); |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 59 | } catch (RemoteException e) { |
| 60 | Log.w(TAG, "Failed to get description for task=" + taskId); |
| 61 | } |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 62 | Intent intent = new Intent(KeyguardManager.ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER) |
| 63 | .setComponent(new ComponentName(mContext, WorkLockActivity.class)) |
| 64 | .putExtra(Intent.EXTRA_USER_ID, userId) |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 65 | .putExtra(WorkLockActivity.EXTRA_TASK_DESCRIPTION, taskDescription) |
Robin Lee | 588a333 | 2017-01-18 18:46:42 +0000 | [diff] [blame] | 66 | .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
| 67 | | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 68 | |
| 69 | final ActivityOptions options = ActivityOptions.makeBasic(); |
| 70 | options.setLaunchTaskId(taskId); |
Winson Chung | cbcadc9 | 2017-01-12 15:54:12 -0800 | [diff] [blame] | 71 | options.setTaskOverlay(true, false /* canResume */); |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 72 | |
| 73 | final int result = startActivityAsUser(intent, options.toBundle(), UserHandle.USER_CURRENT); |
Bryce Lee | 7f93686 | 2017-05-09 15:33:18 -0700 | [diff] [blame] | 74 | if (ActivityManager.isStartResultSuccessful(result)) { |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 75 | // OK |
| 76 | } else { |
| 77 | // Starting the activity inside the task failed. We can't be sure why, so to be |
| 78 | // safe just remove the whole task if it still exists. |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 79 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 80 | mIatm.removeTask(taskId); |
Winson Chung | aa35745 | 2017-10-31 11:35:30 -0700 | [diff] [blame] | 81 | } catch (RemoteException e) { |
| 82 | Log.w(TAG, "Failed to get description for task=" + taskId); |
| 83 | } |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Version of {@link Context#startActivityAsUser} which keeps the success code from |
| 89 | * IActivityManager, so we can read back whether ActivityManager thinks it started properly. |
| 90 | */ |
| 91 | private int startActivityAsUser(Intent intent, Bundle options, int userId) { |
| 92 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 93 | return mIatm.startActivityAsUser( |
Robin Lee | 1d996bd | 2017-01-31 23:01:59 +0000 | [diff] [blame] | 94 | mContext.getIApplicationThread() /*caller*/, |
| 95 | mContext.getBasePackageName() /*callingPackage*/, |
| 96 | intent /*intent*/, |
| 97 | intent.resolveTypeIfNeeded(mContext.getContentResolver()) /*resolvedType*/, |
| 98 | null /*resultTo*/, |
| 99 | null /*resultWho*/, |
| 100 | 0 /*requestCode*/, |
| 101 | Intent.FLAG_ACTIVITY_NEW_TASK /*flags*/, |
| 102 | null /*profilerInfo*/, |
| 103 | options /*options*/, |
| 104 | userId /*user*/); |
| 105 | } catch (RemoteException e) { |
| 106 | return ActivityManager.START_CANCELED; |
| 107 | } catch (Exception e) { |
| 108 | return ActivityManager.START_CANCELED; |
| 109 | } |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Winson Chung | 67f5c8b | 2018-09-24 12:09:19 -0700 | [diff] [blame] | 112 | private final TaskStackChangeListener mLockListener = new TaskStackChangeListener() { |
Robin Lee | c41f6ec | 2017-01-10 17:02:34 +0000 | [diff] [blame] | 113 | @Override |
| 114 | public void onTaskProfileLocked(int taskId, int userId) { |
| 115 | startWorkChallengeInTask(taskId, userId); |
| 116 | } |
| 117 | }; |
| 118 | } |