• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.task;
18 
19 import static java.util.Objects.requireNonNull;
20 
21 import android.annotation.UserIdInt;
22 import android.app.admin.DevicePolicyManager;
23 import android.app.admin.ManagedProfileProvisioningParams;
24 import android.app.admin.ProvisioningException;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.os.UserHandle;
28 import android.stats.devicepolicy.DevicePolicyEnums;
29 
30 import com.android.internal.annotations.VisibleForTesting;
31 import com.android.managedprovisioning.R;
32 import com.android.managedprovisioning.analytics.MetricsWriterFactory;
33 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
34 import com.android.managedprovisioning.common.IllegalProvisioningArgumentException;
35 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
36 import com.android.managedprovisioning.common.ProvisionLogger;
37 import com.android.managedprovisioning.common.SettingsFacade;
38 import com.android.managedprovisioning.common.Utils;
39 import com.android.managedprovisioning.model.ProvisioningParams;
40 
41 /**
42  * Task to create and provision a managed profile.
43  */
44 public class CreateAndProvisionManagedProfileTask extends AbstractProvisioningTask {
45     private final DevicePolicyManager mDpm;
46     private final Utils mUtils;
47     private int mProfileUserId;
48 
CreateAndProvisionManagedProfileTask( Context context, ProvisioningParams params, Callback callback)49     public CreateAndProvisionManagedProfileTask(
50             Context context,
51             ProvisioningParams params,
52             Callback callback) {
53         this(
54                 new Utils(),
55                 context,
56                 params,
57                 callback,
58                 new ProvisioningAnalyticsTracker(
59                         MetricsWriterFactory.getMetricsWriter(context, new SettingsFacade()),
60                         new ManagedProvisioningSharedPreferences(context)));
61     }
62 
63     @VisibleForTesting
CreateAndProvisionManagedProfileTask( Utils utils, Context context, ProvisioningParams params, Callback callback, ProvisioningAnalyticsTracker provisioningAnalyticsTracker)64     CreateAndProvisionManagedProfileTask(
65             Utils utils,
66             Context context,
67             ProvisioningParams params,
68             Callback callback,
69             ProvisioningAnalyticsTracker provisioningAnalyticsTracker) {
70         super(context, params, callback, provisioningAnalyticsTracker);
71         mDpm = requireNonNull(context.getSystemService(DevicePolicyManager.class));
72         mUtils = requireNonNull(utils);
73     }
74 
75     @Override
run(@serIdInt int userId)76     public void run(@UserIdInt int userId) {
77         startTaskTimer();
78         UserHandle profile;
79         ManagedProfileProvisioningParams params;
80 
81         try {
82             params = buildManagedProfileProvisioningParams(userId);
83         } catch (IllegalProvisioningArgumentException e) {
84             ProvisionLogger.loge("Failure provisioning managed profile, failed to "
85                     + "infer the device admin component name", e);
86             error(/* resultCode= */ 0);
87             return;
88         }
89 
90         try {
91             profile = mDpm.createAndProvisionManagedProfile(params);
92         } catch (ProvisioningException provisioningException) {
93             ProvisionLogger.loge("Failure provisioning managed profile.", provisioningException);
94             error(/* resultCode= */ 0, provisioningException.getMessage());
95             return;
96         } catch (Exception e) {
97             // Catching all Exceptions to allow Managed Provisioning to handle any failure
98             // during provisioning properly and perform any necessary cleanup.
99             ProvisionLogger.loge("Failure provisioning managed profile.", e);
100             error(/* resultCode= */ 0);
101             return;
102         }
103 
104         if (profile == null) {
105             ProvisionLogger.loge("Failure provisioning managed profile, "
106                     + "createAndProvisionManagedProfile returned null");
107             error(/* resultCode= */ 0);
108             return;
109         }
110         mProfileUserId = profile.getIdentifier();
111 
112         stopTaskTimer();
113         success();
114     }
115 
buildManagedProfileProvisioningParams( @serIdInt int userId)116     private ManagedProfileProvisioningParams buildManagedProfileProvisioningParams(
117             @UserIdInt int userId)
118             throws IllegalProvisioningArgumentException {
119         ComponentName adminComponent =
120                 mProvisioningParams.inferDeviceAdminComponentName(mUtils, mContext, userId);
121         return new ManagedProfileProvisioningParams.Builder(
122                 adminComponent, adminComponent.getPackageName())
123                 .setProfileName(mContext.getString(R.string.default_managed_profile_name))
124                 .setAccountToMigrate(mProvisioningParams.accountToMigrate)
125                 .setLeaveAllSystemAppsEnabled(
126                         mProvisioningParams.leaveAllSystemAppsEnabled)
127                 .setOrganizationOwnedProvisioning(
128                         mProvisioningParams.isOrganizationOwnedProvisioning)
129                 .setKeepingAccountOnMigration(mProvisioningParams.keepAccountMigrated)
130                 .setAdminExtras(mProvisioningParams.adminExtrasBundle)
131                 .build();
132     }
133 
getProfileUserId()134     public int getProfileUserId() {
135         return mProfileUserId;
136     }
137 
138     @Override
getMetricsCategory()139     protected int getMetricsCategory() {
140         return DevicePolicyEnums.PROVISIONING_PROVISION_MANAGED_PROFILE_TASK_MS;
141     }
142 }
143