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.os.UserManager; 22 import android.provider.Settings; 23 import android.util.ArrayMap; 24 25 import java.util.ArrayList; 26 27 import com.android.cts.verifier.R; 28 29 public class UserRestrictions { 30 private static final String[] RESTRICTION_IDS = new String[] { 31 UserManager.DISALLOW_ADD_USER, 32 UserManager.DISALLOW_ADJUST_VOLUME, 33 UserManager.DISALLOW_APPS_CONTROL, 34 UserManager.DISALLOW_CONFIG_CELL_BROADCASTS, 35 UserManager.DISALLOW_CONFIG_CREDENTIALS, 36 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, 37 UserManager.DISALLOW_CONFIG_TETHERING, 38 UserManager.DISALLOW_CONFIG_WIFI, 39 UserManager.DISALLOW_DEBUGGING_FEATURES, 40 UserManager.DISALLOW_FACTORY_RESET, 41 UserManager.DISALLOW_FUN, 42 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, 43 UserManager.DISALLOW_MODIFY_ACCOUNTS, 44 UserManager.DISALLOW_NETWORK_RESET, 45 UserManager.DISALLOW_OUTGOING_BEAM, 46 UserManager.DISALLOW_REMOVE_USER, 47 UserManager.DISALLOW_SHARE_LOCATION, 48 UserManager.DISALLOW_UNINSTALL_APPS 49 }; 50 51 private static final ArrayMap<String, UserRestrictionItem> USER_RESTRICTION_ITEMS; 52 static { 53 final int[] restrictionLabels = new int[] { 54 R.string.disallow_add_user, 55 R.string.disallow_adjust_volume, 56 R.string.disallow_apps_control, 57 R.string.disallow_config_cell_broadcasts, 58 R.string.disallow_config_credentials, 59 R.string.disallow_config_mobile_networks, 60 R.string.disallow_config_tethering, 61 R.string.disallow_config_wifi, 62 R.string.disallow_debugging_features, 63 R.string.disallow_factory_reset, 64 R.string.disallow_fun, 65 R.string.disallow_install_unknown_sources, 66 R.string.disallow_modify_accounts, 67 R.string.disallow_network_reset, 68 R.string.disallow_outgoing_beam, 69 R.string.disallow_remove_user, 70 R.string.disallow_share_location, 71 R.string.disallow_uninstall_apps 72 }; 73 74 final int[] restrictionActions = new int[] { 75 R.string.disallow_add_user_action, 76 R.string.disallow_adjust_volume_action, 77 R.string.disallow_apps_control_action, 78 R.string.disallow_config_cell_broadcasts_action, 79 R.string.disallow_config_credentials_action, 80 R.string.disallow_config_mobile_networks_action, 81 R.string.disallow_config_tethering_action, 82 R.string.disallow_config_wifi_action, 83 R.string.disallow_debugging_features_action, 84 R.string.disallow_factory_reset_action, 85 R.string.disallow_fun_action, 86 R.string.disallow_install_unknown_sources_action, 87 R.string.disallow_modify_accounts_action, 88 R.string.disallow_network_reset_action, 89 R.string.disallow_outgoing_beam_action, 90 R.string.disallow_remove_user_action, 91 R.string.disallow_share_location_action, 92 R.string.disallow_uninstall_apps_action 93 }; 94 95 final String[] settingsIntentActions = new String[] { 96 Settings.ACTION_SETTINGS, 97 Settings.ACTION_SOUND_SETTINGS, 98 Settings.ACTION_APPLICATION_SETTINGS, 99 Settings.ACTION_SOUND_SETTINGS, 100 Settings.ACTION_SECURITY_SETTINGS, 101 Settings.ACTION_WIRELESS_SETTINGS, 102 Settings.ACTION_WIRELESS_SETTINGS, 103 Settings.ACTION_WIFI_SETTINGS, 104 Settings.ACTION_DEVICE_INFO_SETTINGS, 105 Settings.ACTION_PRIVACY_SETTINGS, 106 Settings.ACTION_DEVICE_INFO_SETTINGS, 107 Settings.ACTION_SECURITY_SETTINGS, 108 Settings.ACTION_SYNC_SETTINGS, 109 Settings.ACTION_PRIVACY_SETTINGS, 110 Settings.ACTION_WIRELESS_SETTINGS, 111 Settings.ACTION_SETTINGS, 112 Settings.ACTION_LOCATION_SOURCE_SETTINGS, 113 Settings.ACTION_APPLICATION_SETTINGS, 114 }; 115 116 if (RESTRICTION_IDS.length != restrictionLabels.length 117 || RESTRICTION_IDS.length != restrictionActions.length 118 || RESTRICTION_IDS.length != settingsIntentActions.length) { 119 throw new AssertionError("Number of items in restrictionIds, restrictionLabels, " 120 + "restrictionActions, and settingsIntentActions do not match"); 121 } 122 USER_RESTRICTION_ITEMS = new ArrayMap<>(RESTRICTION_IDS.length); 123 for (int i = 0; i < RESTRICTION_IDS.length; ++i) { USER_RESTRICTION_ITEMS.put(RESTRICTION_IDS[i], new UserRestrictionItem( restrictionLabels[i], restrictionActions[i], settingsIntentActions[i]))124 USER_RESTRICTION_ITEMS.put(RESTRICTION_IDS[i], new UserRestrictionItem( 125 restrictionLabels[i], 126 restrictionActions[i], 127 settingsIntentActions[i])); 128 } 129 } 130 131 private static final ArrayList<String> ALSO_VALID_FOR_PO = 132 new ArrayList<String>(); 133 static { 134 ALSO_VALID_FOR_PO.add(UserManager.DISALLOW_APPS_CONTROL); 135 ALSO_VALID_FOR_PO.add(UserManager.DISALLOW_UNINSTALL_APPS); 136 ALSO_VALID_FOR_PO.add(UserManager.DISALLOW_MODIFY_ACCOUNTS); 137 ALSO_VALID_FOR_PO.add(UserManager.DISALLOW_SHARE_LOCATION); 138 } 139 getRestrictionLabel(Context context, String restriction)140 public static String getRestrictionLabel(Context context, String restriction) { 141 final UserRestrictionItem item = findRestrictionItem(restriction); 142 return context.getString(item.label); 143 } 144 getUserAction(Context context, String restriction)145 public static String getUserAction(Context context, String restriction) { 146 final UserRestrictionItem item = findRestrictionItem(restriction); 147 return context.getString(item.userAction); 148 } 149 findRestrictionItem(String restriction)150 private static UserRestrictionItem findRestrictionItem(String restriction) { 151 final UserRestrictionItem item = USER_RESTRICTION_ITEMS.get(restriction); 152 if (item == null) { 153 throw new IllegalArgumentException("Unknown restriction: " + restriction); 154 } 155 return item; 156 } 157 isValidForPO(String restriction)158 public static boolean isValidForPO(String restriction) { 159 return ALSO_VALID_FOR_PO.contains(restriction); 160 } 161 getUserRestrictions()162 public static String[] getUserRestrictions() { 163 return RESTRICTION_IDS; 164 } 165 getUserRestrictionTestIntent(Context context, String restriction)166 public static Intent getUserRestrictionTestIntent(Context context, String restriction) { 167 final UserRestrictionItem item = USER_RESTRICTION_ITEMS.get(restriction); 168 return new Intent(PolicyTransparencyTestActivity.ACTION_SHOW_POLICY_TRANSPARENCY_TEST) 169 .putExtra(PolicyTransparencyTestActivity.EXTRA_TEST, 170 PolicyTransparencyTestActivity.TEST_CHECK_USER_RESTRICTION) 171 .putExtra(CommandReceiverActivity.EXTRA_USER_RESTRICTION, restriction) 172 .putExtra(PolicyTransparencyTestActivity.EXTRA_TITLE, context.getString(item.label)) 173 .putExtra(PolicyTransparencyTestActivity.EXTRA_SETTINGS_INTENT_ACTION, 174 item.intentAction); 175 } 176 177 private static class UserRestrictionItem { 178 final int label; 179 final int userAction; 180 final String intentAction; UserRestrictionItem(int label, int userAction, String intentAction)181 public UserRestrictionItem(int label, int userAction, String intentAction) { 182 this.label = label; 183 this.userAction = userAction; 184 this.intentAction = intentAction; 185 } 186 } 187 }