blob: ae380b72f5e039451945a9c4ef5f7ae67af98516 [file] [log] [blame]
Robin Leec41f6ec2017-01-10 17:02:34 +00001/*
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
17package com.android.systemui.keyguard;
18
Robin Lee1d996bd2017-01-31 23:01:59 +000019import android.app.ActivityManager;
Robin Leec41f6ec2017-01-10 17:02:34 +000020import android.app.ActivityOptions;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070021import android.app.ActivityTaskManager;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070022import android.app.IActivityTaskManager;
Robin Leec41f6ec2017-01-10 17:02:34 +000023import android.app.KeyguardManager;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.os.Bundle;
Robin Lee1d996bd2017-01-31 23:01:59 +000028import android.os.RemoteException;
Robin Leec41f6ec2017-01-10 17:02:34 +000029import android.os.UserHandle;
Winson Chungaa357452017-10-31 11:35:30 -070030import android.util.Log;
Gus Prevasab336792018-11-14 13:52:20 -050031
Robin Lee1d996bd2017-01-31 23:01:59 +000032import com.android.internal.annotations.VisibleForTesting;
Winson Chung2cf6ad82017-11-09 17:36:59 -080033import com.android.systemui.shared.system.ActivityManagerWrapper;
Winson Chung67f5c8b2018-09-24 12:09:19 -070034import com.android.systemui.shared.system.TaskStackChangeListener;
Robin Leec41f6ec2017-01-10 17:02:34 +000035
36public class WorkLockActivityController {
Winson Chungaa357452017-10-31 11:35:30 -070037 private static final String TAG = WorkLockActivityController.class.getSimpleName();
38
Robin Leec41f6ec2017-01-10 17:02:34 +000039 private final Context mContext;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070040 private final IActivityTaskManager mIatm;
Robin Leec41f6ec2017-01-10 17:02:34 +000041
42 public WorkLockActivityController(Context context) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070043 this(context, ActivityManagerWrapper.getInstance(), ActivityTaskManager.getService());
Robin Lee1d996bd2017-01-31 23:01:59 +000044 }
Robin Lee3c82d3d2017-01-19 17:09:28 +000045
Robin Lee1d996bd2017-01-31 23:01:59 +000046 @VisibleForTesting
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070047 WorkLockActivityController(
48 Context context, ActivityManagerWrapper am, IActivityTaskManager iAtm) {
Robin Lee1d996bd2017-01-31 23:01:59 +000049 mContext = context;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070050 mIatm = iAtm;
Robin Lee1d996bd2017-01-31 23:01:59 +000051
Winson Chung2cf6ad82017-11-09 17:36:59 -080052 am.registerTaskStackListener(mLockListener);
Robin Leec41f6ec2017-01-10 17:02:34 +000053 }
54
55 private void startWorkChallengeInTask(int taskId, int userId) {
Winson Chungaa357452017-10-31 11:35:30 -070056 ActivityManager.TaskDescription taskDescription = null;
57 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070058 taskDescription = mIatm.getTaskDescription(taskId);
Winson Chungaa357452017-10-31 11:35:30 -070059 } catch (RemoteException e) {
60 Log.w(TAG, "Failed to get description for task=" + taskId);
61 }
Robin Leec41f6ec2017-01-10 17:02:34 +000062 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 Chungaa357452017-10-31 11:35:30 -070065 .putExtra(WorkLockActivity.EXTRA_TASK_DESCRIPTION, taskDescription)
Robin Lee588a3332017-01-18 18:46:42 +000066 .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
67 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Robin Leec41f6ec2017-01-10 17:02:34 +000068
69 final ActivityOptions options = ActivityOptions.makeBasic();
70 options.setLaunchTaskId(taskId);
Winson Chungcbcadc92017-01-12 15:54:12 -080071 options.setTaskOverlay(true, false /* canResume */);
Robin Lee1d996bd2017-01-31 23:01:59 +000072
73 final int result = startActivityAsUser(intent, options.toBundle(), UserHandle.USER_CURRENT);
Bryce Lee7f936862017-05-09 15:33:18 -070074 if (ActivityManager.isStartResultSuccessful(result)) {
Robin Lee1d996bd2017-01-31 23:01:59 +000075 // 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 Chungaa357452017-10-31 11:35:30 -070079 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070080 mIatm.removeTask(taskId);
Winson Chungaa357452017-10-31 11:35:30 -070081 } catch (RemoteException e) {
82 Log.w(TAG, "Failed to get description for task=" + taskId);
83 }
Robin Lee1d996bd2017-01-31 23:01:59 +000084 }
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 Ogunwale04d9cb52018-04-30 13:55:07 -070093 return mIatm.startActivityAsUser(
Robin Lee1d996bd2017-01-31 23:01:59 +000094 mContext.getIApplicationThread() /*caller*/,
95 mContext.getBasePackageName() /*callingPackage*/,
Philip P. Moltmann9c5226f2020-01-10 08:53:43 -080096 mContext.getFeatureId() /*callingFeatureId*/,
Robin Lee1d996bd2017-01-31 23:01:59 +000097 intent /*intent*/,
98 intent.resolveTypeIfNeeded(mContext.getContentResolver()) /*resolvedType*/,
99 null /*resultTo*/,
100 null /*resultWho*/,
101 0 /*requestCode*/,
102 Intent.FLAG_ACTIVITY_NEW_TASK /*flags*/,
103 null /*profilerInfo*/,
104 options /*options*/,
105 userId /*user*/);
106 } catch (RemoteException e) {
107 return ActivityManager.START_CANCELED;
108 } catch (Exception e) {
109 return ActivityManager.START_CANCELED;
110 }
Robin Leec41f6ec2017-01-10 17:02:34 +0000111 }
112
Winson Chung67f5c8b2018-09-24 12:09:19 -0700113 private final TaskStackChangeListener mLockListener = new TaskStackChangeListener() {
Robin Leec41f6ec2017-01-10 17:02:34 +0000114 @Override
115 public void onTaskProfileLocked(int taskId, int userId) {
116 startWorkChallengeInTask(taskId, userId);
117 }
118 };
119}