• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                 new SendDpcBroadcastServiceUtils());
57     }
58 
PreFinalizationController(Context context)59     public PreFinalizationController(Context context) {
60         this(
61                 context,
62                 new Utils(),
63                 new SettingsFacade(),
64                 new UserProvisioningStateHelper(context),
65                 new ProvisioningParamsUtils(),
66                 new SendDpcBroadcastServiceUtils());
67     }
68 
69     @VisibleForTesting
PreFinalizationController(Context context, Utils utils, SettingsFacade settingsFacade, UserProvisioningStateHelper helper, ProvisioningParamsUtils provisioningParamsUtils, SendDpcBroadcastServiceUtils sendDpcBroadcastServiceUtils)70     PreFinalizationController(Context context,
71             Utils utils,
72             SettingsFacade settingsFacade,
73             UserProvisioningStateHelper helper,
74             ProvisioningParamsUtils provisioningParamsUtils,
75             SendDpcBroadcastServiceUtils sendDpcBroadcastServiceUtils) {
76         mContext = checkNotNull(context);
77         mUtils = checkNotNull(utils);
78         mSettingsFacade = checkNotNull(settingsFacade);
79         mUserProvisioningStateHelper = checkNotNull(helper);
80         mProvisioningParamsUtils = provisioningParamsUtils;
81         mSendDpcBroadcastServiceUtils = sendDpcBroadcastServiceUtils;
82     }
83 
84     /**
85      * This method is invoked after a Device Owner or Profile Owner has been set up.
86      *
87      * <p>If provisioning happens as part of SUW, we rely on an instance of
88      * FinalizationControllerBase to be invoked later on. Otherwise, this method will finalize
89      * provisioning. If called after SUW, this method notifies the DPC about the completed
90      * provisioning; otherwise, it stores the provisioning params for later digestion.</p>
91      *
92      * <p>Note that fully managed device provisioning is only possible during SUW.
93      *
94      * @param params the provisioning params
95      */
deviceManagementEstablished(ProvisioningParams params)96     public final void deviceManagementEstablished(ProvisioningParams params) {
97         if (!mUserProvisioningStateHelper.isStateUnmanagedOrFinalized()) {
98             // In any other state than STATE_USER_SETUP_FINALIZED, STATE_USER_PROFILE_FINALIZED and
99             // STATE_USER_UNMANAGED, we've already run this method, so don't do anything.
100             ProvisionLogger.logw("deviceManagementEstablished called, but state is not finalized "
101                     + "or unmanaged");
102             return;
103         }
104 
105         mUserProvisioningStateHelper.markUserProvisioningStateInitiallyDone(params);
106         if (ACTION_PROVISION_MANAGED_PROFILE.equals(params.provisioningAction)) {
107             if (!params.returnBeforePolicyCompliance) {
108                 // If a managed profile was provisioned and the provisioning initiator has requested
109                 // managed profile provisioning and DPC setup to happen in one step, notify the
110                 // DPC straight away.
111                 mSendDpcBroadcastServiceUtils.startSendDpcBroadcastService(mContext, params);
112             }
113         }
114         if (params.returnBeforePolicyCompliance) {
115             // Store the information and wait for provisioningFinalized to be called
116             storeProvisioningParams(params);
117         }
118     }
119 
storeProvisioningParams(ProvisioningParams params)120     private void storeProvisioningParams(ProvisioningParams params) {
121         params.save(mProvisioningParamsUtils.getProvisioningParamsFile(mContext));
122     }
123 }
124