• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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.task;
18 
19 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.PROVISIONING_CREATE_PROFILE_TASK_MS;
20 import static com.android.internal.util.Preconditions.checkNotNull;
21 
22 import android.content.Context;
23 import android.content.pm.UserInfo;
24 import android.os.UserManager;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 import com.android.managedprovisioning.R;
28 import com.android.managedprovisioning.analytics.MetricsWriterFactory;
29 import com.android.managedprovisioning.analytics.ProvisioningAnalyticsTracker;
30 import com.android.managedprovisioning.common.ManagedProvisioningSharedPreferences;
31 import com.android.managedprovisioning.common.SettingsFacade;
32 import com.android.managedprovisioning.model.ProvisioningParams;
33 import com.android.managedprovisioning.task.nonrequiredapps.NonRequiredAppsLogic;
34 
35 import java.util.Set;
36 
37 /**
38  * Task to create a managed profile.
39  */
40 public class CreateManagedProfileTask extends AbstractProvisioningTask {
41 
42     private int mProfileUserId;
43     private final NonRequiredAppsLogic mNonRequiredAppsLogic;
44     private final UserManager mUserManager;
45 
CreateManagedProfileTask(Context context, ProvisioningParams params, Callback callback)46     public CreateManagedProfileTask(Context context, ProvisioningParams params, Callback callback) {
47         this(
48                 context,
49                 params,
50                 callback,
51                 context.getSystemService(UserManager.class),
52                 new NonRequiredAppsLogic(context, true, params),
53                 new ProvisioningAnalyticsTracker(
54                         MetricsWriterFactory.getMetricsWriter(context, new SettingsFacade()),
55                         new ManagedProvisioningSharedPreferences(context)));
56     }
57 
58     @VisibleForTesting
CreateManagedProfileTask( Context context, ProvisioningParams params, Callback callback, UserManager userManager, NonRequiredAppsLogic logic, ProvisioningAnalyticsTracker provisioningAnalyticsTracker)59     CreateManagedProfileTask(
60             Context context,
61             ProvisioningParams params,
62             Callback callback,
63             UserManager userManager,
64             NonRequiredAppsLogic logic,
65             ProvisioningAnalyticsTracker provisioningAnalyticsTracker) {
66         super(context, params, callback, provisioningAnalyticsTracker);
67         mNonRequiredAppsLogic = checkNotNull(logic);
68         mUserManager = checkNotNull(userManager);
69     }
70 
71     @Override
run(int userId)72     public void run(int userId) {
73         startTaskTimer();
74         final Set<String> nonRequiredApps = mNonRequiredAppsLogic.getSystemAppsToRemove(userId);
75         UserInfo userInfo = mUserManager.createProfileForUserEvenWhenDisallowed(
76                 mContext.getString(R.string.default_managed_profile_name),
77                 UserInfo.FLAG_MANAGED_PROFILE | UserInfo.FLAG_DISABLED,
78                 userId, nonRequiredApps.toArray(new String[nonRequiredApps.size()]));
79         if (userInfo == null) {
80             error(0);
81             return;
82         }
83         mProfileUserId = userInfo.id;
84         mNonRequiredAppsLogic.maybeTakeSystemAppsSnapshot(userInfo.id);
85         stopTaskTimer();
86         success();
87     }
88 
89     @Override
getStatusMsgId()90     public int getStatusMsgId() {
91         return R.string.progress_initialize;
92     }
93 
94     @Override
getMetricsCategory()95     protected int getMetricsCategory() {
96         return PROVISIONING_CREATE_PROFILE_TASK_MS;
97     }
98 
getProfileUserId()99     public int getProfileUserId() {
100         return mProfileUserId;
101     }
102 }
103