1 /*
2  * Copyright 2016, 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.provisioning;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 
22 import com.android.internal.annotations.VisibleForTesting;
23 import com.android.managedprovisioning.common.ProvisionLogger;
24 import com.android.managedprovisioning.common.Utils;
25 import com.android.managedprovisioning.model.ProvisioningParams;
26 
27 /**
28  * Factory class to create an {@link AbstractProvisioningController} from a set of
29  * {@link ProvisioningParams}.
30  */
31 @VisibleForTesting
32 public class ProvisioningControllerFactory {
33 
34     private final Utils mUtils = new Utils();
35 
36     /**
37      * This method constructs the controller used for the given type of provisioning.
38      */
39     @VisibleForTesting
createProvisioningController( Context context, ProvisioningParams params, ProvisioningControllerCallback callback)40     public AbstractProvisioningController createProvisioningController(
41             Context context,
42             ProvisioningParams params,
43             ProvisioningControllerCallback callback) {
44         if (mUtils.isDeviceOwnerAction(params.provisioningAction)) {
45             int deviceOwner = mUtils.isHeadlessSystemUserMode()
46                         ? UserHandle.USER_SYSTEM : UserHandle.myUserId();
47             ProvisionLogger.logi("Setting device owner on user: " + deviceOwner);
48             return DeviceOwnerProvisioningController.createInstance(
49                     context,
50                     params,
51                     deviceOwner,
52                     callback,
53                     mUtils);
54         } else if (mUtils.isFinancedDeviceAction(params.provisioningAction)) {
55             return FinancedDeviceProvisioningController.createInstance(
56                     context,
57                     params,
58                     UserHandle.myUserId(),
59                     callback);
60         } else {
61             return ProfileOwnerProvisioningController.createInstance(
62                     context,
63                     params,
64                     UserHandle.myUserId(),
65                     callback);
66         }
67     }
68 }
69