1 /*
2  * Copyright 2019, 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.finalization;
18 
19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
20 
21 import static com.android.internal.util.Preconditions.checkNotNull;
22 
23 import android.content.Context;
24 
25 import com.android.internal.annotations.VisibleForTesting;
26 import com.android.managedprovisioning.common.ProvisionLogger;
27 import com.android.managedprovisioning.common.SettingsFacade;
28 import com.android.managedprovisioning.common.Utils;
29 import com.android.managedprovisioning.model.ProvisioningParams;
30 
31 /**
32  * This controller is invoked, via a call to
33  * {@link #deviceManagementEstablished(ProvisioningParams)}, immediately after a Device Owner or
34  * Profile Owner is provisioned.  If the user is creating a managed profile on a device post-Setup
35  * Wizard, then we immediately start the DPC app and set the provisioning state to finalized, so no
36  * further finalization calls are necessary.  On the other hand, if we are still inside of Setup
37  * Wizard, provisioning needs to be finalized after this using an instance of
38  * {@link FinalizationController}.
39  */
40 public final class PreFinalizationController {
41     private final Context mContext;
42     private final Utils mUtils;
43     private final SettingsFacade mSettingsFacade;
44     private final UserProvisioningStateHelper mUserProvisioningStateHelper;
45     private final ProvisioningParamsUtils mProvisioningParamsUtils;
46     private final SendDpcBroadcastServiceUtils mSendDpcBroadcastServiceUtils;
47 
PreFinalizationController(Context context, UserProvisioningStateHelper userProvisioningStateHelper)48     public PreFinalizationController(Context context,
49           UserProvisioningStateHelper userProvisioningStateHelper) {
50         this(
51                 context,
52                 new Utils(),
53                 new SettingsFacade(),
54                 userProvisioningStateHelper,
55                 new ProvisioningParamsUtils(
56                         ProvisioningParamsUtils.DEFAULT_PROVISIONING_PARAMS_FILE_PROVIDER),
57                 new SendDpcBroadcastServiceUtils());
58     }
59 
PreFinalizationController(Context context)60     public PreFinalizationController(Context context) {
61         this(
62                 context,
63                 new Utils(),
64                 new SettingsFacade(),
65                 new UserProvisioningStateHelper(context),
66                 new ProvisioningParamsUtils(
67                         ProvisioningParamsUtils.DEFAULT_PROVISIONING_PARAMS_FILE_PROVIDER),
68                 new SendDpcBroadcastServiceUtils());
69     }
70 
71     @VisibleForTesting
PreFinalizationController(Context context, Utils utils, SettingsFacade settingsFacade, UserProvisioningStateHelper helper, ProvisioningParamsUtils provisioningParamsUtils, SendDpcBroadcastServiceUtils sendDpcBroadcastServiceUtils)72     PreFinalizationController(Context context,
73             Utils utils,
74             SettingsFacade settingsFacade,
75             UserProvisioningStateHelper helper,
76             ProvisioningParamsUtils provisioningParamsUtils,
77             SendDpcBroadcastServiceUtils sendDpcBroadcastServiceUtils) {
78         mContext = checkNotNull(context);
79         mUtils = checkNotNull(utils);
80         mSettingsFacade = checkNotNull(settingsFacade);
81         mUserProvisioningStateHelper = checkNotNull(helper);
82         mProvisioningParamsUtils = provisioningParamsUtils;
83         mSendDpcBroadcastServiceUtils = sendDpcBroadcastServiceUtils;
84     }
85 
86     /**
87      * This method is invoked after a Device Owner or Profile Owner has been set up.
88      *
89      * <p>If provisioning happens as part of SUW, we rely on an instance of
90      * FinalizationControllerBase to be invoked later on. Otherwise, this method will finalize
91      * provisioning. If called after SUW, this method notifies the DPC about the completed
92      * provisioning; otherwise, it stores the provisioning params for later digestion.</p>
93      *
94      * <p>Note that fully managed device provisioning is only possible during SUW.
95      *
96      * @param params the provisioning params
97      */
deviceManagementEstablished(ProvisioningParams params)98     public final void deviceManagementEstablished(ProvisioningParams params) {
99         if (!mUserProvisioningStateHelper.isStateUnmanagedOrFinalized()) {
100             // In any other state than STATE_USER_SETUP_FINALIZED, STATE_USER_PROFILE_FINALIZED and
101             // STATE_USER_UNMANAGED, we've already run this method, so don't do anything.
102             ProvisionLogger.logw("deviceManagementEstablished called, but state is not finalized "
103                     + "or unmanaged");
104             return;
105         }
106 
107         mUserProvisioningStateHelper.markUserProvisioningStateInitiallyDone(params);
108         if (ACTION_PROVISION_MANAGED_PROFILE.equals(params.provisioningAction)) {
109             if (!params.returnBeforePolicyCompliance) {
110                 // If a managed profile was provisioned and the provisioning initiator has requested
111                 // managed profile provisioning and DPC setup to happen in one step, notify the
112                 // DPC straight away.
113                 mSendDpcBroadcastServiceUtils.startSendDpcBroadcastService(mContext, params);
114             }
115         }
116         if (params.returnBeforePolicyCompliance) {
117             // Store the information and wait for provisioningFinalized to be called
118             storeProvisioningParams(params);
119         }
120     }
121 
storeProvisioningParams(ProvisioningParams params)122     private void storeProvisioningParams(ProvisioningParams params) {
123         params.save(mProvisioningParamsUtils.getProvisioningParamsFile(mContext));
124     }
125 }
126