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
17package com.android.systemui.keyguard;
18
19import android.app.Activity;
20import android.app.ActivityManager;
21import android.app.ActivityOptions;
22import android.app.IActivityManager;
23import android.app.KeyguardManager;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.os.Bundle;
28import android.os.RemoteException;
29import android.os.UserHandle;
30
31import com.android.internal.annotations.VisibleForTesting;
32import com.android.systemui.recents.events.EventBus;
33import com.android.systemui.recents.misc.SystemServicesProxy;
34import com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener;
35
36public 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