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.app.admin.DevicePolicyManager;
20 import android.content.pm.UserInfo;
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 
24 import com.android.managedprovisioning.ProvisionLogger;
25 
26 /**
27  * Disables user addition for all users on the device.
28  */
29 public class DisallowAddUserTask {
30     private final UserManager mUserManager;
31     private final int mDeviceOwnerUserId;
32     private final boolean mIsSplitSystemUser;
33 
DisallowAddUserTask(UserManager userManager, int deviceOwnerUserId, boolean isSplitSystemUser)34     public DisallowAddUserTask(UserManager userManager, int deviceOwnerUserId,
35             boolean isSplitSystemUser) {
36         mUserManager = userManager;
37         mDeviceOwnerUserId = deviceOwnerUserId;
38         mIsSplitSystemUser = isSplitSystemUser;
39     }
40 
maybeDisallowAddUsers()41     public void maybeDisallowAddUsers() {
42         if (mIsSplitSystemUser && (mDeviceOwnerUserId == UserHandle.USER_SYSTEM)) {
43             ProvisionLogger.logi("Not setting DISALLOW_ADD_USER as system device-owner detected.");
44             return;
45         }
46         for (UserInfo userInfo : mUserManager.getUsers()) {
47             UserHandle userHandle = userInfo.getUserHandle();
48             if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_ADD_USER, userHandle)) {
49                 mUserManager.setUserRestriction(UserManager.DISALLOW_ADD_USER, true, userHandle);
50                 ProvisionLogger.logi("DISALLOW_ADD_USER restriction set on user: " + userInfo.id);
51             }
52         }
53     }
54 }
55