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
17package com.android.managedprovisioning.task;
18
19import android.content.Context;
20import android.content.pm.UserInfo;
21import android.os.UserHandle;
22import android.os.UserManager;
23
24import com.android.internal.annotations.VisibleForTesting;
25import com.android.managedprovisioning.common.ProvisionLogger;
26import com.android.managedprovisioning.R;
27import com.android.managedprovisioning.model.ProvisioningParams;
28
29/**
30 * Disables user addition for all users on the device.
31 */
32public class DisallowAddUserTask extends AbstractProvisioningTask {
33    private final boolean mIsSplitSystemUser;
34    private final UserManager mUserManager;
35
36    public DisallowAddUserTask(
37            Context context,
38            ProvisioningParams params,
39            Callback callback) {
40        this(UserManager.isSplitSystemUser(), context, params, callback);
41    }
42
43    @VisibleForTesting
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
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
73    public int getStatusMsgId() {
74        return R.string.progress_finishing_touches;
75    }
76}
77