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
17package com.android.managedprovisioning.task;
18
19import static com.android.internal.util.Preconditions.checkNotNull;
20
21import android.app.admin.DevicePolicyManager;
22import android.content.Context;
23import android.os.UserHandle;
24import android.os.UserManager;
25
26import com.android.internal.annotations.VisibleForTesting;
27import com.android.managedprovisioning.R;
28import com.android.managedprovisioning.common.SettingsFacade;
29import com.android.managedprovisioning.model.ProvisioningParams;
30
31public class ManagedProfileSettingsTask extends AbstractProvisioningTask {
32
33    @VisibleForTesting
34    static final boolean DEFAULT_CONTACT_REMOTE_SEARCH = true;
35
36    private final SettingsFacade mSettingsFacade;
37    private final CrossProfileIntentFiltersSetter mCrossProfileIntentFiltersSetter;
38
39    public ManagedProfileSettingsTask(
40            Context context,
41            ProvisioningParams params,
42            Callback callback) {
43        this(new SettingsFacade(), new CrossProfileIntentFiltersSetter(context), context, params,
44                callback);
45    }
46
47    @VisibleForTesting
48    ManagedProfileSettingsTask(
49            SettingsFacade settingsFacade,
50            CrossProfileIntentFiltersSetter crossProfileIntentFiltersSetter,
51            Context context,
52            ProvisioningParams params,
53            Callback callback) {
54        super(context, params, callback);
55        mSettingsFacade = checkNotNull(settingsFacade);
56        mCrossProfileIntentFiltersSetter = checkNotNull(crossProfileIntentFiltersSetter);
57    }
58
59    @Override
60    public void run(int userId) {
61        // Turn on managed profile contacts remote search.
62        mSettingsFacade.setProfileContactRemoteSearch(mContext, DEFAULT_CONTACT_REMOTE_SEARCH,
63                userId);
64
65        // Disable managed profile wallpaper access
66        UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
67        um.setUserRestriction(UserManager.DISALLOW_WALLPAPER, true, UserHandle.of(userId));
68
69        // Set the main color of managed provisioning from the provisioning params
70        if (mProvisioningParams.mainColor != null) {
71            DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
72                    Context.DEVICE_POLICY_SERVICE);
73            dpm.setOrganizationColorForUser(mProvisioningParams.mainColor, userId);
74        }
75
76        mCrossProfileIntentFiltersSetter.setFilters(UserHandle.myUserId(), userId);
77
78        // always mark managed profile setup as completed
79        mSettingsFacade.setUserSetupCompleted(mContext, userId);
80
81        success();
82    }
83
84    @Override
85    public int getStatusMsgId() {
86        return R.string.progress_finishing_touches;
87    }
88}
89