Home | History | Annotate | Download | only in task
      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.model.ProvisioningParams;
     29 import com.android.managedprovisioning.task.nonrequiredapps.NonRequiredAppsLogic;
     30 
     31 import java.util.Set;
     32 
     33 /**
     34  * Task to create a managed profile.
     35  */
     36 public class CreateManagedProfileTask extends AbstractProvisioningTask {
     37 
     38     private int mProfileUserId;
     39     private final NonRequiredAppsLogic mNonRequiredAppsLogic;
     40     private final UserManager mUserManager;
     41 
     42     public CreateManagedProfileTask(Context context, ProvisioningParams params, Callback callback) {
     43         this(
     44                 context,
     45                 params,
     46                 callback,
     47                 context.getSystemService(UserManager.class),
     48                 new NonRequiredAppsLogic(context, true, params));
     49     }
     50 
     51     @VisibleForTesting
     52     CreateManagedProfileTask(
     53             Context context,
     54             ProvisioningParams params,
     55             Callback callback,
     56             UserManager userManager,
     57             NonRequiredAppsLogic logic) {
     58         super(context, params, callback);
     59         mNonRequiredAppsLogic = checkNotNull(logic);
     60         mUserManager = checkNotNull(userManager);
     61     }
     62 
     63     @Override
     64     public void run(int userId) {
     65         startTaskTimer();
     66         final Set<String> nonRequiredApps = mNonRequiredAppsLogic.getSystemAppsToRemove(userId);
     67         UserInfo userInfo = mUserManager.createProfileForUserEvenWhenDisallowed(
     68                 mContext.getString(R.string.default_managed_profile_name),
     69                 UserInfo.FLAG_MANAGED_PROFILE | UserInfo.FLAG_DISABLED,
     70                 userId, nonRequiredApps.toArray(new String[nonRequiredApps.size()]));
     71         if (userInfo == null) {
     72             error(0);
     73             return;
     74         }
     75         mProfileUserId = userInfo.id;
     76         mNonRequiredAppsLogic.maybeTakeSystemAppsSnapshot(userInfo.id);
     77         stopTaskTimer();
     78         success();
     79     }
     80 
     81     @Override
     82     public int getStatusMsgId() {
     83         return R.string.progress_initialize;
     84     }
     85 
     86     @Override
     87     protected int getMetricsCategory() {
     88         return PROVISIONING_CREATE_PROFILE_TASK_MS;
     89     }
     90 
     91     public int getProfileUserId() {
     92         return mProfileUserId;
     93     }
     94 }
     95