1 /*
2  * Copyright 2015, 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 android.content.Context;
20 import android.content.pm.UserInfo;
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 
24 import com.android.internal.annotations.VisibleForTesting;
25 import com.android.managedprovisioning.common.ProvisionLogger;
26 import com.android.managedprovisioning.R;
27 import com.android.managedprovisioning.model.ProvisioningParams;
28 
29 /**
30  * Disables user addition for all users on the device.
31  */
32 public class DisallowAddUserTask extends AbstractProvisioningTask {
33     private final boolean mIsSplitSystemUser;
34     private final UserManager mUserManager;
35 
DisallowAddUserTask( Context context, ProvisioningParams params, Callback callback)36     public DisallowAddUserTask(
37             Context context,
38             ProvisioningParams params,
39             Callback callback) {
40         this(UserManager.isSplitSystemUser(), context, params, callback);
41     }
42 
43     @VisibleForTesting
DisallowAddUserTask(boolean splitSystemUser, Context context, ProvisioningParams params, Callback callback)44     DisallowAddUserTask(boolean splitSystemUser,
45             Context context,
46             ProvisioningParams params,
47             Callback callback) {
48         super(context, params, callback);
49         mIsSplitSystemUser = splitSystemUser;
50         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
51     }
52 
53     @Override
run(int userId)54     public void run(int userId) {
55 
56         if (mIsSplitSystemUser && (userId == UserHandle.USER_SYSTEM)) {
57             ProvisionLogger.logi("Not setting DISALLOW_ADD_USER as system device-owner detected.");
58             success();
59             return;
60         }
61 
62         for (UserInfo userInfo : mUserManager.getUsers()) {
63             UserHandle userHandle = userInfo.getUserHandle();
64             if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_ADD_USER, userHandle)) {
65                 mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_USER, true, userHandle);
66                 ProvisionLogger.logi("DISALLOW_ADD_USER restriction set on user: " + userInfo.id);
67             }
68         }
69         success();
70     }
71 
72     @Override
getStatusMsgId()73     public int getStatusMsgId() {
74         return R.string.progress_finishing_touches;
75     }
76 }
77