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