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