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 19 import android.app.ActivityManager; 20 import android.app.ActivityOptions; 21 import android.app.ActivityTaskManager; 22 import android.app.IActivityTaskManager; 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; 28 import android.os.RemoteException; 29 import android.os.UserHandle; 30 import android.util.Log; 31 32 import com.android.internal.annotations.VisibleForTesting; 33 import com.android.systemui.shared.system.TaskStackChangeListener; 34 import com.android.systemui.shared.system.TaskStackChangeListeners; 35 36 public class WorkLockActivityController { 37 private static final String TAG = WorkLockActivityController.class.getSimpleName(); 38 39 private final Context mContext; 40 private final IActivityTaskManager mIatm; 41 WorkLockActivityController(Context context)42 public WorkLockActivityController(Context context) { 43 this(context, TaskStackChangeListeners.getInstance(), ActivityTaskManager.getService()); 44 } 45 46 @VisibleForTesting WorkLockActivityController( Context context, TaskStackChangeListeners tscl, IActivityTaskManager iAtm)47 WorkLockActivityController( 48 Context context, TaskStackChangeListeners tscl, IActivityTaskManager iAtm) { 49 mContext = context; 50 mIatm = iAtm; 51 52 tscl.registerTaskStackListener(mLockListener); 53 } 54 startWorkChallengeInTask(int taskId, int userId)55 private void startWorkChallengeInTask(int taskId, int userId) { 56 ActivityManager.TaskDescription taskDescription = null; 57 try { 58 taskDescription = mIatm.getTaskDescription(taskId); 59 } catch (RemoteException e) { 60 Log.w(TAG, "Failed to get description for task=" + taskId); 61 } 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) 65 .putExtra(WorkLockActivity.EXTRA_TASK_DESCRIPTION, taskDescription) 66 .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT 67 | Intent.FLAG_ACTIVITY_CLEAR_TOP); 68 69 final ActivityOptions options = ActivityOptions.makeBasic(); 70 options.setLaunchTaskId(taskId); 71 options.setTaskOverlay(true, false /* canResume */); 72 73 final int result = startActivityAsUser(intent, options.toBundle(), UserHandle.USER_CURRENT); 74 if (ActivityManager.isStartResultSuccessful(result)) { 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. 79 try { 80 mIatm.removeTask(taskId); 81 } catch (RemoteException e) { 82 Log.w(TAG, "Failed to get description for task=" + taskId); 83 } 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 */ startActivityAsUser(Intent intent, Bundle options, int userId)91 private int startActivityAsUser(Intent intent, Bundle options, int userId) { 92 try { 93 return mIatm.startActivityAsUser( 94 mContext.getIApplicationThread() /*caller*/, 95 mContext.getBasePackageName() /*callingPackage*/, 96 mContext.getAttributionTag() /*callingAttributionTag*/, 97 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 } 111 } 112 113 private final TaskStackChangeListener mLockListener = new TaskStackChangeListener() { 114 @Override 115 public void onTaskProfileLocked(int taskId, int userId) { 116 startWorkChallengeInTask(taskId, userId); 117 } 118 }; 119 } 120