blob: 74f770679cc92d9418af2d285518f4658a095009 [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;
Robin Lee1d996bd2017-01-31 23:01:59 +000031import com.android.internal.annotations.VisibleForTesting;
Winson Chung2cf6ad82017-11-09 17:36:59 -080032import com.android.systemui.shared.system.ActivityManagerWrapper;
Winson Chung67f5c8b2018-09-24 12:09:19 -070033import com.android.systemui.shared.system.TaskStackChangeListener;
Robin Leec41f6ec2017-01-10 17:02:34 +000034
35public class WorkLockActivityController {
Winson Chungaa357452017-10-31 11:35:30 -070036 private static final String TAG = WorkLockActivityController.class.getSimpleName();
37
Robin Leec41f6ec2017-01-10 17:02:34 +000038 private final Context mContext;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070039 private final IActivityTaskManager mIatm;
Robin Leec41f6ec2017-01-10 17:02:34 +000040
41 public WorkLockActivityController(Context context) {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070042 this(context, ActivityManagerWrapper.getInstance(), ActivityTaskManager.getService());
Robin Lee1d996bd2017-01-31 23:01:59 +000043 }
Robin Lee3c82d3d2017-01-19 17:09:28 +000044
Robin Lee1d996bd2017-01-31 23:01:59 +000045 @VisibleForTesting
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070046 WorkLockActivityController(
47 Context context, ActivityManagerWrapper am, IActivityTaskManager iAtm) {
Robin Lee1d996bd2017-01-31 23:01:59 +000048 mContext = context;
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070049 mIatm = iAtm;
Robin Lee1d996bd2017-01-31 23:01:59 +000050
Winson Chung2cf6ad82017-11-09 17:36:59 -080051 am.registerTaskStackListener(mLockListener);
Robin Leec41f6ec2017-01-10 17:02:34 +000052 }
53
54 private void startWorkChallengeInTask(int taskId, int userId) {
Winson Chungaa357452017-10-31 11:35:30 -070055 ActivityManager.TaskDescription taskDescription = null;
56 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070057 taskDescription = mIatm.getTaskDescription(taskId);
Winson Chungaa357452017-10-31 11:35:30 -070058 } catch (RemoteException e) {
59 Log.w(TAG, "Failed to get description for task=" + taskId);
60 }
Robin Leec41f6ec2017-01-10 17:02:34 +000061 Intent intent = new Intent(KeyguardManager.ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER)
62 .setComponent(new ComponentName(mContext, WorkLockActivity.class))
63 .putExtra(Intent.EXTRA_USER_ID, userId)
Winson Chungaa357452017-10-31 11:35:30 -070064 .putExtra(WorkLockActivity.EXTRA_TASK_DESCRIPTION, taskDescription)
Robin Lee588a3332017-01-18 18:46:42 +000065 .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
66 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Robin Leec41f6ec2017-01-10 17:02:34 +000067
68 final ActivityOptions options = ActivityOptions.makeBasic();
69 options.setLaunchTaskId(taskId);
Winson Chungcbcadc92017-01-12 15:54:12 -080070 options.setTaskOverlay(true, false /* canResume */);
Robin Lee1d996bd2017-01-31 23:01:59 +000071
72 final int result = startActivityAsUser(intent, options.toBundle(), UserHandle.USER_CURRENT);
Bryce Lee7f936862017-05-09 15:33:18 -070073 if (ActivityManager.isStartResultSuccessful(result)) {
Robin Lee1d996bd2017-01-31 23:01:59 +000074 // OK
75 } else {
76 // Starting the activity inside the task failed. We can't be sure why, so to be
77 // safe just remove the whole task if it still exists.
Winson Chungaa357452017-10-31 11:35:30 -070078 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070079 mIatm.removeTask(taskId);
Winson Chungaa357452017-10-31 11:35:30 -070080 } catch (RemoteException e) {
81 Log.w(TAG, "Failed to get description for task=" + taskId);
82 }
Robin Lee1d996bd2017-01-31 23:01:59 +000083 }
84 }
85
86 /**
87 * Version of {@link Context#startActivityAsUser} which keeps the success code from
88 * IActivityManager, so we can read back whether ActivityManager thinks it started properly.
89 */
90 private int startActivityAsUser(Intent intent, Bundle options, int userId) {
91 try {
Wale Ogunwale04d9cb52018-04-30 13:55:07 -070092 return mIatm.startActivityAsUser(
Robin Lee1d996bd2017-01-31 23:01:59 +000093 mContext.getIApplicationThread() /*caller*/,
94 mContext.getBasePackageName() /*callingPackage*/,
95 intent /*intent*/,
96 intent.resolveTypeIfNeeded(mContext.getContentResolver()) /*resolvedType*/,
97 null /*resultTo*/,
98 null /*resultWho*/,
99 0 /*requestCode*/,
100 Intent.FLAG_ACTIVITY_NEW_TASK /*flags*/,
101 null /*profilerInfo*/,
102 options /*options*/,
103 userId /*user*/);
104 } catch (RemoteException e) {
105 return ActivityManager.START_CANCELED;
106 } catch (Exception e) {
107 return ActivityManager.START_CANCELED;
108 }
Robin Leec41f6ec2017-01-10 17:02:34 +0000109 }
110
Winson Chung67f5c8b2018-09-24 12:09:19 -0700111 private final TaskStackChangeListener mLockListener = new TaskStackChangeListener() {
Robin Leec41f6ec2017-01-10 17:02:34 +0000112 @Override
113 public void onTaskProfileLocked(int taskId, int userId) {
114 startWorkChallengeInTask(taskId, userId);
115 }
116 };
117}