1 /*
2  * Copyright (C) 2020 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.managedprovisioning.common;
18 
19 import static android.app.admin.DevicePolicyManager.ACTION_GET_PROVISIONING_MODE;
20 
21 import android.app.Activity;
22 import android.app.admin.DevicePolicyManager;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.os.Bundle;
26 
27 import com.android.managedprovisioning.model.ProvisioningParams;
28 
29 import com.google.android.setupcompat.util.WizardManagerHelper;
30 
31 /**
32  * A utility class for checking for the existence of and starting DPC's activity with intent filter
33  * with action {@link DevicePolicyManager#ACTION_GET_PROVISIONING_MODE}.
34  */
35 public class GetProvisioningModeUtils {
36 
37     /**
38      * Returns {@code true} if the DPC has an intent filter with action {@link
39      * DevicePolicyManager#ACTION_GET_PROVISIONING_MODE}.
40      */
isGetProvisioningModeActivityResolvable( Context context, ProvisioningParams params)41     public boolean isGetProvisioningModeActivityResolvable(
42             Context context, ProvisioningParams params) {
43         return getGetProvisioningModeIntent(params)
44                 .resolveActivity(context.getPackageManager()) != null;
45     }
46 
47     /**
48      * Starts the DPC activity with action @link DevicePolicyManager#ACTION_GET_PROVISIONING_MODE},
49      * if one exists.
50      *
51      * @param parentActivity the activity which starts the DPC screen
52      * @param params the {@link ProvisioningParams} used for provisioning
53      * @param additionalExtras a bundle of additional extras to be included for the DPC to read
54      * @param requestCode the request code to be used when started by {@code parentActivity}
55      * @return {@code true} if the activity was successfully started
56      */
startGetProvisioningModeActivityIfResolved( Activity parentActivity, ProvisioningParams params, Bundle additionalExtras, int requestCode, TransitionHelper transitionHelper)57     public boolean startGetProvisioningModeActivityIfResolved(
58             Activity parentActivity, ProvisioningParams params, Bundle additionalExtras,
59             int requestCode, TransitionHelper transitionHelper) {
60         if (!isGetProvisioningModeActivityResolvable(parentActivity, params)) {
61             return false;
62         }
63         Intent getProvisioningModeIntent = getGetProvisioningModeIntent(params);
64         getProvisioningModeIntent.putExtras(additionalExtras);
65         if (parentActivity.getIntent() != null) {
66             WizardManagerHelper.copyWizardManagerExtras(
67                     parentActivity.getIntent(), getProvisioningModeIntent);
68         }
69         transitionHelper.startActivityForResultWithTransition(
70                 parentActivity, getProvisioningModeIntent, requestCode);
71         return true;
72     }
73 
getGetProvisioningModeIntent(ProvisioningParams params)74     private Intent getGetProvisioningModeIntent(ProvisioningParams params) {
75         final Intent intentGetMode = new Intent(ACTION_GET_PROVISIONING_MODE);
76         intentGetMode.setPackage(params.inferDeviceAdminPackageName());
77         return intentGetMode;
78     }
79 }
80