Home | History | Annotate | Download | only in keyguard
      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.Activity;
     20 import android.app.ActivityManager;
     21 import android.app.ActivityOptions;
     22 import android.app.IActivityManager;
     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 
     31 import com.android.internal.annotations.VisibleForTesting;
     32 import com.android.systemui.recents.events.EventBus;
     33 import com.android.systemui.recents.misc.SystemServicesProxy;
     34 import com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener;
     35 
     36 public class WorkLockActivityController {
     37     private final Context mContext;
     38     private final SystemServicesProxy mSsp;
     39     private final IActivityManager mIam;
     40 
     41     public WorkLockActivityController(Context context) {
     42         this(context, SystemServicesProxy.getInstance(context), ActivityManager.getService());
     43     }
     44 
     45     @VisibleForTesting
     46     WorkLockActivityController(Context context, SystemServicesProxy ssp, IActivityManager am) {
     47         mContext = context;
     48         mSsp = ssp;
     49         mIam = am;
     50 
     51         mSsp.registerTaskStackListener(mLockListener);
     52     }
     53 
     54     private void startWorkChallengeInTask(int taskId, int userId) {
     55         Intent intent = new Intent(KeyguardManager.ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER)
     56                 .setComponent(new ComponentName(mContext, WorkLockActivity.class))
     57                 .putExtra(Intent.EXTRA_USER_ID, userId)
     58                 .putExtra(WorkLockActivity.EXTRA_TASK_DESCRIPTION, mSsp.getTaskDescription(taskId))
     59                 .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
     60                         | Intent.FLAG_ACTIVITY_CLEAR_TOP);
     61 
     62         final ActivityOptions options = ActivityOptions.makeBasic();
     63         options.setLaunchTaskId(taskId);
     64         options.setTaskOverlay(true, false /* canResume */);
     65 
     66         final int result = startActivityAsUser(intent, options.toBundle(), UserHandle.USER_CURRENT);
     67         if (ActivityManager.isStartResultSuccessful(result)) {
     68             // OK
     69         } else {
     70             // Starting the activity inside the task failed. We can't be sure why, so to be
     71             // safe just remove the whole task if it still exists.
     72             mSsp.removeTask(taskId);
     73         }
     74     }
     75 
     76     /**
     77      * Version of {@link Context#startActivityAsUser} which keeps the success code from
     78      * IActivityManager, so we can read back whether ActivityManager thinks it started properly.
     79      */
     80     private int startActivityAsUser(Intent intent, Bundle options, int userId) {
     81         try {
     82             return mIam.startActivityAsUser(
     83                     mContext.getIApplicationThread() /*caller*/,
     84                     mContext.getBasePackageName() /*callingPackage*/,
     85                     intent /*intent*/,
     86                     intent.resolveTypeIfNeeded(mContext.getContentResolver()) /*resolvedType*/,
     87                     null /*resultTo*/,
     88                     null /*resultWho*/,
     89                     0 /*requestCode*/,
     90                     Intent.FLAG_ACTIVITY_NEW_TASK /*flags*/,
     91                     null /*profilerInfo*/,
     92                     options /*options*/,
     93                     userId /*user*/);
     94         } catch (RemoteException e) {
     95             return ActivityManager.START_CANCELED;
     96         } catch (Exception e) {
     97             return ActivityManager.START_CANCELED;
     98         }
     99     }
    100 
    101     private final TaskStackListener mLockListener = new TaskStackListener() {
    102         @Override
    103         public void onTaskProfileLocked(int taskId, int userId) {
    104             startWorkChallengeInTask(taskId, userId);
    105         }
    106     };
    107 }
    108