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.cts.verifier.managedprovisioning;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.PackageManager;
22 import android.os.UserManager;
23 import android.provider.Settings;
24 import android.util.ArrayMap;
25 
26 import com.android.cts.verifier.R;
27 
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 
32 public class UserRestrictions {
33     private static final String[] RESTRICTION_IDS_FOR_POLICY_TRANSPARENCY = new String[] {
34         UserManager.DISALLOW_ADD_USER,
35         UserManager.DISALLOW_ADJUST_VOLUME,
36         UserManager.DISALLOW_APPS_CONTROL,
37         UserManager.DISALLOW_CONFIG_CELL_BROADCASTS,
38         UserManager.DISALLOW_CONFIG_CREDENTIALS,
39         UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS,
40         UserManager.DISALLOW_CONFIG_TETHERING,
41         UserManager.DISALLOW_CONFIG_WIFI,
42         UserManager.DISALLOW_DEBUGGING_FEATURES,
43         UserManager.DISALLOW_FACTORY_RESET,
44         UserManager.DISALLOW_FUN,
45         UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
46         UserManager.DISALLOW_MODIFY_ACCOUNTS,
47         UserManager.DISALLOW_NETWORK_RESET,
48         UserManager.DISALLOW_OUTGOING_BEAM,
49         UserManager.DISALLOW_REMOVE_MANAGED_PROFILE,
50         UserManager.DISALLOW_SHARE_LOCATION,
51         UserManager.DISALLOW_UNINSTALL_APPS,
52         UserManager.DISALLOW_UNIFIED_PASSWORD,
53         UserManager.DISALLOW_CONFIG_DATE_TIME,
54         UserManager.DISALLOW_CONFIG_LOCATION,
55         UserManager.DISALLOW_AIRPLANE_MODE,
56         UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT,
57         UserManager.DISALLOW_CONFIG_BRIGHTNESS,
58     };
59 
60     private static final ArrayMap<String, UserRestrictionItem> USER_RESTRICTION_ITEMS;
61     static {
62         final int[] restrictionLabels = new int[] {
63             R.string.disallow_add_user,
64             R.string.disallow_adjust_volume,
65             R.string.disallow_apps_control,
66             R.string.disallow_config_cell_broadcasts,
67             R.string.disallow_config_credentials,
68             R.string.disallow_config_mobile_networks,
69             R.string.disallow_config_tethering,
70             R.string.disallow_config_wifi,
71             R.string.disallow_debugging_features,
72             R.string.disallow_factory_reset,
73             R.string.disallow_fun,
74             R.string.disallow_install_unknown_sources,
75             R.string.disallow_modify_accounts,
76             R.string.disallow_network_reset,
77             R.string.disallow_outgoing_beam,
78             R.string.disallow_remove_managed_profile,
79             R.string.disallow_share_location,
80             R.string.disallow_uninstall_apps,
81             R.string.disallow_unified_challenge,
82             R.string.disallow_config_date_time,
83             R.string.disallow_config_location,
84             R.string.disallow_airplane_mode,
85             R.string.disallow_config_screen_timeout,
86             R.string.disallow_config_brightness
87         };
88 
89         final int[] restrictionActions = new int[] {
90             R.string.disallow_add_user_action,
91             R.string.disallow_adjust_volume_action,
92             R.string.disallow_apps_control_action,
93             R.string.disallow_config_cell_broadcasts_action,
94             R.string.disallow_config_credentials_action,
95             R.string.disallow_config_mobile_networks_action,
96             R.string.disallow_config_tethering_action,
97             R.string.disallow_config_wifi_action,
98             R.string.disallow_debugging_features_action,
99             R.string.disallow_factory_reset_action,
100             R.string.disallow_fun_action,
101             R.string.disallow_install_unknown_sources_action,
102             R.string.disallow_modify_accounts_action,
103             R.string.disallow_network_reset_action,
104             R.string.disallow_outgoing_beam_action,
105             R.string.disallow_remove_managed_profile_action,
106             R.string.disallow_share_location_action,
107             R.string.disallow_uninstall_apps_action,
108             R.string.disallow_unified_challenge_action,
109             R.string.disallow_config_date_time_action,
110             R.string.disallow_config_location_action,
111             R.string.disallow_airplane_mode_action,
112             R.string.disallow_config_screen_timeout_action,
113             R.string.disallow_config_brightness_action
114         };
115 
116         final String[] settingsIntentActions = new String[] {
117             Settings.ACTION_SETTINGS,
118             Settings.ACTION_SOUND_SETTINGS,
119             Settings.ACTION_APPLICATION_SETTINGS,
120             Settings.ACTION_SETTINGS,
121             Settings.ACTION_SECURITY_SETTINGS,
122             Settings.ACTION_WIRELESS_SETTINGS,
123             Settings.ACTION_WIRELESS_SETTINGS,
124             Settings.ACTION_WIFI_SETTINGS,
125             Settings.ACTION_DEVICE_INFO_SETTINGS,
126             Settings.ACTION_SETTINGS,
127             Settings.ACTION_DEVICE_INFO_SETTINGS,
128             Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,
129             Settings.ACTION_SYNC_SETTINGS,
130             Settings.ACTION_SETTINGS,
131             Settings.ACTION_NFC_SETTINGS,
132             Settings.ACTION_SETTINGS,
133             Settings.ACTION_LOCATION_SOURCE_SETTINGS,
134             Settings.ACTION_APPLICATION_SETTINGS,
135             Settings.ACTION_SECURITY_SETTINGS,
136             Settings.ACTION_DATE_SETTINGS,
137             Settings.ACTION_LOCATION_SOURCE_SETTINGS,
138             Settings.ACTION_AIRPLANE_MODE_SETTINGS,
139             Settings.ACTION_DISPLAY_SETTINGS,
140             Settings.ACTION_DISPLAY_SETTINGS,
141         };
142 
143         if (RESTRICTION_IDS_FOR_POLICY_TRANSPARENCY.length != restrictionLabels.length
144                 || RESTRICTION_IDS_FOR_POLICY_TRANSPARENCY.length != restrictionActions.length
145                 || RESTRICTION_IDS_FOR_POLICY_TRANSPARENCY.length != settingsIntentActions.length) {
146             throw new AssertionError("Number of items in restrictionIds, restrictionLabels, "
147                     + "restrictionActions, and settingsIntentActions do not match");
148         }
149         USER_RESTRICTION_ITEMS = new ArrayMap<>(RESTRICTION_IDS_FOR_POLICY_TRANSPARENCY.length);
150         for (int i = 0; i < RESTRICTION_IDS_FOR_POLICY_TRANSPARENCY.length; ++i) {
USER_RESTRICTION_ITEMS.put(RESTRICTION_IDS_FOR_POLICY_TRANSPARENCY[i], new UserRestrictionItem( restrictionLabels[i], restrictionActions[i], settingsIntentActions[i]))151             USER_RESTRICTION_ITEMS.put(RESTRICTION_IDS_FOR_POLICY_TRANSPARENCY[i],
152                     new UserRestrictionItem(
153                             restrictionLabels[i],
154                             restrictionActions[i],
155                             settingsIntentActions[i]));
156         }
157     }
158 
159     private static final List<String> ALSO_VALID_FOR_MANAGED_PROFILE_POLICY_TRANSPARENCY =
160             Arrays.asList(
161                     UserManager.DISALLOW_APPS_CONTROL,
162                     UserManager.DISALLOW_UNINSTALL_APPS,
163                     UserManager.DISALLOW_MODIFY_ACCOUNTS, UserManager.DISALLOW_SHARE_LOCATION,
164                     UserManager.DISALLOW_UNIFIED_PASSWORD,
165                     UserManager.DISALLOW_CONFIG_LOCATION);
166     private static final List<String> ALSO_VALID_FOR_MANAGED_USER_POLICY_TRANSPARENCY =
167             Arrays.asList(
168                     UserManager.DISALLOW_ADJUST_VOLUME,
169                     UserManager.DISALLOW_APPS_CONTROL,
170                     UserManager.DISALLOW_CONFIG_WIFI,
171                     UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
172                     UserManager.DISALLOW_MODIFY_ACCOUNTS,
173                     UserManager.DISALLOW_OUTGOING_BEAM,
174                     UserManager.DISALLOW_SHARE_LOCATION,
175                     UserManager.DISALLOW_UNINSTALL_APPS,
176                     UserManager.DISALLOW_CONFIG_DATE_TIME,
177                     UserManager.DISALLOW_CONFIG_LOCATION,
178                     UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT,
179                     UserManager.DISALLOW_CONFIG_BRIGHTNESS);
180 
getRestrictionLabel(Context context, String restriction)181     public static String getRestrictionLabel(Context context, String restriction) {
182         final UserRestrictionItem item = findRestrictionItem(restriction);
183         return context.getString(item.label);
184     }
185 
getUserAction(Context context, String restriction)186     public static String getUserAction(Context context, String restriction) {
187         final UserRestrictionItem item = findRestrictionItem(restriction);
188         return context.getString(item.userAction);
189     }
190 
findRestrictionItem(String restriction)191     private static UserRestrictionItem findRestrictionItem(String restriction) {
192         final UserRestrictionItem item = USER_RESTRICTION_ITEMS.get(restriction);
193         if (item == null) {
194             throw new IllegalArgumentException("Unknown restriction: " + restriction);
195         }
196         return item;
197     }
198 
getUserRestrictionsForPolicyTransparency(int mode)199     public static List<String> getUserRestrictionsForPolicyTransparency(int mode) {
200         if (mode == PolicyTransparencyTestListActivity.MODE_DEVICE_OWNER) {
201             ArrayList<String> result = new ArrayList<String>();
202             // They are all valid except for DISALLOW_REMOVE_MANAGED_PROFILE
203             for (String st : RESTRICTION_IDS_FOR_POLICY_TRANSPARENCY) {
204                 if (!st.equals(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE)
205                         && !st.equals(UserManager.DISALLOW_UNIFIED_PASSWORD)) {
206                     result.add(st);
207                 }
208             }
209             return result;
210         } else if (mode == PolicyTransparencyTestListActivity.MODE_COMP) {
211             return Arrays.asList(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE);
212         } else if (mode == PolicyTransparencyTestListActivity.MODE_MANAGED_PROFILE) {
213             return ALSO_VALID_FOR_MANAGED_PROFILE_POLICY_TRANSPARENCY;
214         } else if (mode == PolicyTransparencyTestListActivity.MODE_MANAGED_USER) {
215             return ALSO_VALID_FOR_MANAGED_USER_POLICY_TRANSPARENCY;
216         }
217         throw new RuntimeException("Invalid mode " + mode);
218     }
219 
getUserRestrictionTestIntent(Context context, String restriction)220     public static Intent getUserRestrictionTestIntent(Context context, String restriction) {
221         final UserRestrictionItem item = USER_RESTRICTION_ITEMS.get(restriction);
222         return new Intent(PolicyTransparencyTestActivity.ACTION_SHOW_POLICY_TRANSPARENCY_TEST)
223                 .putExtra(PolicyTransparencyTestActivity.EXTRA_TEST,
224                         PolicyTransparencyTestActivity.TEST_CHECK_USER_RESTRICTION)
225                 .putExtra(CommandReceiverActivity.EXTRA_USER_RESTRICTION, restriction)
226                 .putExtra(PolicyTransparencyTestActivity.EXTRA_TITLE, context.getString(item.label))
227                 .putExtra(PolicyTransparencyTestActivity.EXTRA_SETTINGS_INTENT_ACTION,
228                         item.intentAction);
229     }
230 
isRestrictionValid(Context context, String restriction)231     public static boolean isRestrictionValid(Context context, String restriction) {
232         final PackageManager pm = context.getPackageManager();
233         switch (restriction) {
234             case UserManager.DISALLOW_ADD_USER:
235                 return UserManager.supportsMultipleUsers();
236             case UserManager.DISALLOW_ADJUST_VOLUME:
237                 return pm.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT);
238             case UserManager.DISALLOW_CONFIG_CELL_BROADCASTS:
239                 // Get com.android.internal.R.bool.config_cellBroadcastAppLinks
240                 final int resId = context.getResources().getIdentifier(
241                         "config_cellBroadcastAppLinks", "bool", "android");
242                 boolean isCellBroadcastAppLinkEnabled = context.getResources().getBoolean(resId);
243                 try {
244                     if (isCellBroadcastAppLinkEnabled) {
245                         if (pm.getApplicationEnabledSetting("com.android.cellbroadcastreceiver")
246                                 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
247                             isCellBroadcastAppLinkEnabled = false;  // CMAS app disabled
248                         }
249                     }
250                 } catch (IllegalArgumentException ignored) {
251                     isCellBroadcastAppLinkEnabled = false;  // CMAS app not installed
252                 }
253                 return isCellBroadcastAppLinkEnabled;
254             case UserManager.DISALLOW_FUN:
255                 // Easter egg is not available on watch
256                 return !pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
257             case UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS:
258                 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
259             case UserManager.DISALLOW_CONFIG_WIFI:
260                 return pm.hasSystemFeature(PackageManager.FEATURE_WIFI);
261             case UserManager.DISALLOW_NETWORK_RESET:
262                 // This test should not run on watch
263                 return !pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
264             case UserManager.DISALLOW_OUTGOING_BEAM:
265                 return pm.hasSystemFeature(PackageManager.FEATURE_NFC)
266                         && pm.hasSystemFeature(PackageManager.FEATURE_NFC_BEAM);
267             case UserManager.DISALLOW_SHARE_LOCATION:
268                 return pm.hasSystemFeature(PackageManager.FEATURE_LOCATION);
269             case UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES:
270                 return !pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
271             case UserManager.DISALLOW_CONFIG_CREDENTIALS:
272                 return !pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
273             default:
274                 return true;
275         }
276     }
277 
278     private static class UserRestrictionItem {
279         final int label;
280         final int userAction;
281         final String intentAction;
UserRestrictionItem(int label, int userAction, String intentAction)282         public UserRestrictionItem(int label, int userAction, String intentAction) {
283             this.label = label;
284             this.userAction = userAction;
285             this.intentAction = intentAction;
286         }
287     }
288 }
289