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.accessibilityservice.AccessibilityService; 20 import android.content.ActivityNotFoundException; 21 import android.content.Intent; 22 import android.inputmethodservice.InputMethodService; 23 import android.os.Bundle; 24 import android.os.UserManager; 25 import android.provider.Settings; 26 import android.util.ArrayMap; 27 import android.view.View; 28 import android.view.accessibility.AccessibilityEvent; 29 import android.widget.AdapterView; 30 import android.widget.Button; 31 import android.widget.CompoundButton; 32 import android.widget.EditText; 33 import android.widget.Switch; 34 import android.widget.TextView; 35 36 import com.android.cts.verifier.PassFailButtons; 37 import com.android.cts.verifier.R; 38 39 import java.util.Arrays; 40 import java.util.List; 41 import java.util.Map; 42 43 public class PolicyTransparencyTestActivity extends PassFailButtons.Activity implements 44 View.OnClickListener, CompoundButton.OnCheckedChangeListener, 45 AdapterView.OnItemSelectedListener { 46 public static final String ACTION_SHOW_POLICY_TRANSPARENCY_TEST = 47 "com.android.cts.verifier.managedprovisioning.action.SHOW_POLICY_TRANSPARENCY_TEST"; 48 49 // Identifies a test to perform. Type String. The possible values are the ones underneath. 50 public static final String EXTRA_TEST = 51 "com.android.cts.verifier.managedprovisioning.extra.TEST"; 52 53 // In this case: should also contain an extra 54 // {@link CommandReceiverActivity.EXTRA_USER_RESTRICTION} 55 public static final String TEST_CHECK_USER_RESTRICTION = "check-user-restriction"; 56 public static final String TEST_CHECK_AUTO_TIME_REQUIRED = "check-auto-time-required"; 57 public static final String TEST_CHECK_KEYGURAD_UNREDACTED_NOTIFICATION = 58 "check-keyguard-unredacted-notification"; 59 public static final String TEST_CHECK_LOCK_SCREEN_INFO = "check-lock-screen-info"; 60 public static final String TEST_CHECK_MAXIMUM_TIME_TO_LOCK = "check-maximum-time-to-lock"; 61 public static final String TEST_CHECK_PERMITTED_ACCESSIBILITY_SERVICE = 62 "check-permitted-accessibility-service"; 63 public static final String TEST_CHECK_PERMITTED_INPUT_METHOD = "check-permitted-input-method"; 64 65 public static final String EXTRA_SETTINGS_INTENT_ACTION = 66 "com.android.cts.verifier.managedprovisioning.extra.SETTINGS_INTENT_ACTION"; 67 public static final String EXTRA_TITLE = 68 "com.android.cts.verifier.managedprovisioning.extra.TITLE"; 69 // Identifies the test in the calling activity. We will set the result for this test. 70 // Type: String 71 public static final String EXTRA_TEST_ID = 72 "com.android.cts.verifier.managedprovisioning.extra.TEST_ID"; 73 74 private static final Map<String, PolicyTestItem> POLICY_TEST_ITEMS = new ArrayMap<>(); 75 76 /** 77 * List of restrictions that might not have an optional for user to change on Settings. 78 */ 79 private static final List<String> OPTIONAL_USER_RESTRICTION_ACTIONS = Arrays 80 .asList(UserManager.DISALLOW_CONFIG_CELL_BROADCASTS); 81 82 static { POLICY_TEST_ITEMS.put(TEST_CHECK_AUTO_TIME_REQUIRED, new PolicyTestItem( R.string.auto_time_required_set_step, R.string.set_auto_time_required_action, R.string.set_auto_time_required_widget_label, R.id.switch_widget, CommandReceiverActivity.COMMAND_SET_AUTO_TIME_REQUIRED))83 POLICY_TEST_ITEMS.put(TEST_CHECK_AUTO_TIME_REQUIRED, new PolicyTestItem( 84 R.string.auto_time_required_set_step, 85 R.string.set_auto_time_required_action, 86 R.string.set_auto_time_required_widget_label, 87 R.id.switch_widget, 88 CommandReceiverActivity.COMMAND_SET_AUTO_TIME_REQUIRED)); POLICY_TEST_ITEMS.put(TEST_CHECK_KEYGURAD_UNREDACTED_NOTIFICATION, new PolicyTestItem( R.string.disallow_keyguard_unredacted_notifications_set_step, R.string.disallow_keyguard_unredacted_notifications_action, R.string.disallow_keyguard_unredacted_notifications_widget_label, R.id.switch_widget, CommandReceiverActivity.COMMAND_DISALLOW_KEYGUARD_UNREDACTED_NOTIFICATIONS))89 POLICY_TEST_ITEMS.put(TEST_CHECK_KEYGURAD_UNREDACTED_NOTIFICATION, new PolicyTestItem( 90 R.string.disallow_keyguard_unredacted_notifications_set_step, 91 R.string.disallow_keyguard_unredacted_notifications_action, 92 R.string.disallow_keyguard_unredacted_notifications_widget_label, 93 R.id.switch_widget, 94 CommandReceiverActivity.COMMAND_DISALLOW_KEYGUARD_UNREDACTED_NOTIFICATIONS)); POLICY_TEST_ITEMS.put(TEST_CHECK_LOCK_SCREEN_INFO, new PolicyTestItem( R.string.lock_screen_info_set_step, R.string.set_lock_screen_info_action, R.string.set_lock_screen_info_widget_label, R.id.edit_text_widget, CommandReceiverActivity.COMMAND_SET_LOCK_SCREEN_INFO))95 POLICY_TEST_ITEMS.put(TEST_CHECK_LOCK_SCREEN_INFO, new PolicyTestItem( 96 R.string.lock_screen_info_set_step, 97 R.string.set_lock_screen_info_action, 98 R.string.set_lock_screen_info_widget_label, 99 R.id.edit_text_widget, 100 CommandReceiverActivity.COMMAND_SET_LOCK_SCREEN_INFO)); POLICY_TEST_ITEMS.put(TEST_CHECK_MAXIMUM_TIME_TO_LOCK, new PolicyTestItem( R.string.maximum_time_to_lock_set_step, R.string.set_maximum_time_to_lock_action, R.string.set_maximum_time_to_lock_widget_label, R.id.edit_text_widget, CommandReceiverActivity.COMMAND_SET_MAXIMUM_TO_LOCK))101 POLICY_TEST_ITEMS.put(TEST_CHECK_MAXIMUM_TIME_TO_LOCK, new PolicyTestItem( 102 R.string.maximum_time_to_lock_set_step, 103 R.string.set_maximum_time_to_lock_action, 104 R.string.set_maximum_time_to_lock_widget_label, 105 R.id.edit_text_widget, 106 CommandReceiverActivity.COMMAND_SET_MAXIMUM_TO_LOCK)); POLICY_TEST_ITEMS.put(TEST_CHECK_PERMITTED_ACCESSIBILITY_SERVICE, new PolicyTestItem( R.string.permitted_accessibility_services_set_step, R.string.set_permitted_accessibility_services_action, R.string.set_permitted_accessibility_services_widget_label, R.id.switch_widget, CommandReceiverActivity.COMMAND_ALLOW_ONLY_SYSTEM_ACCESSIBILITY_SERVICES))107 POLICY_TEST_ITEMS.put(TEST_CHECK_PERMITTED_ACCESSIBILITY_SERVICE, new PolicyTestItem( 108 R.string.permitted_accessibility_services_set_step, 109 R.string.set_permitted_accessibility_services_action, 110 R.string.set_permitted_accessibility_services_widget_label, 111 R.id.switch_widget, 112 CommandReceiverActivity.COMMAND_ALLOW_ONLY_SYSTEM_ACCESSIBILITY_SERVICES)); POLICY_TEST_ITEMS.put(TEST_CHECK_PERMITTED_INPUT_METHOD, new PolicyTestItem( R.string.permitted_input_methods_set_step, R.string.set_permitted_input_methods_action, R.string.set_permitted_input_methods_widget_label, R.id.switch_widget, CommandReceiverActivity.COMMAND_ALLOW_ONLY_SYSTEM_INPUT_METHODS))113 POLICY_TEST_ITEMS.put(TEST_CHECK_PERMITTED_INPUT_METHOD, new PolicyTestItem( 114 R.string.permitted_input_methods_set_step, 115 R.string.set_permitted_input_methods_action, 116 R.string.set_permitted_input_methods_widget_label, 117 R.id.switch_widget, 118 CommandReceiverActivity.COMMAND_ALLOW_ONLY_SYSTEM_INPUT_METHODS)); 119 } 120 121 private boolean mForceCurrentUserDpm; 122 private String mSettingsIntentAction; 123 private String mTestId; 124 private String mTitle; 125 private String mTest; 126 127 @Override onCreate(Bundle savedInstanceState)128 public void onCreate(Bundle savedInstanceState) { 129 super.onCreate(savedInstanceState); 130 setContentView(R.layout.policy_transparency_test); 131 setPassFailButtonClickListeners(); 132 133 mForceCurrentUserDpm = 134 getIntent().getBooleanExtra( 135 CommandReceiverActivity.EXTRA_USE_CURRENT_USER_DPM, false); 136 mTitle = getIntent().getStringExtra(EXTRA_TITLE); 137 mTestId = getIntent().getStringExtra(EXTRA_TEST_ID); 138 mSettingsIntentAction = getIntent().getStringExtra(EXTRA_SETTINGS_INTENT_ACTION); 139 mTest = getIntent().getStringExtra(EXTRA_TEST); 140 141 setTitle(mTitle); 142 findViewById(R.id.open_settings_button).setOnClickListener(this); 143 updateTestInstructions(); 144 } 145 updateTestInstructions()146 private void updateTestInstructions() { 147 String setStep = null; 148 String userAction = null; 149 String widgetLabel = null; 150 int widgetId = 0; 151 String note = ""; 152 if (TEST_CHECK_USER_RESTRICTION.equals(mTest)) { 153 setStep = getString(R.string.user_restriction_set_step, mTitle); 154 final String userRestriction = getIntent().getStringExtra( 155 CommandReceiverActivity.EXTRA_USER_RESTRICTION); 156 userAction = UserRestrictions.getUserAction(this, userRestriction); 157 widgetLabel = mTitle; 158 widgetId = R.id.switch_widget; 159 if (OPTIONAL_USER_RESTRICTION_ACTIONS.contains(userRestriction)) { 160 note = getString(R.string.optional_policy_transparency_test_note); 161 } 162 } else { 163 final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest); 164 setStep = getString(testItem.setStep); 165 userAction = getString(testItem.userAction); 166 widgetLabel = getString(testItem.widgetLabel); 167 widgetId = testItem.widgetId; 168 } 169 ((TextView) findViewById(R.id.widget_label)).setText(widgetLabel); 170 ((TextView) findViewById(R.id.test_instructions)).setText( 171 getString(R.string.policy_transparency_test_instructions, 172 setStep, userAction, note)); 173 updateWidget(widgetId); 174 } 175 updateWidget(int widgetId)176 private void updateWidget(int widgetId) { 177 switch (widgetId) { 178 case R.id.switch_widget: { 179 Switch switchWidget = (Switch) findViewById(R.id.switch_widget); 180 switchWidget.setOnCheckedChangeListener(this); 181 switchWidget.setVisibility(View.VISIBLE); 182 } break; 183 case R.id.edit_text_widget: { 184 findViewById(R.id.edit_text_widget).setVisibility(View.VISIBLE); 185 Button updateButton = (Button) findViewById(R.id.update_button); 186 updateButton.setOnClickListener(this); 187 updateButton.setVisibility(View.VISIBLE); 188 } break; 189 default: { 190 throw new IllegalArgumentException("Unknown widgetId: " + widgetId); 191 } 192 } 193 } 194 195 @Override onClick(View view)196 public void onClick(View view) { 197 if (view.getId() == R.id.open_settings_button) { 198 try { 199 startActivity(new Intent(mSettingsIntentAction)); 200 } catch (ActivityNotFoundException e) { 201 // If the given settings intent is not handled, use the main settings intent 202 startActivity(new Intent(Settings.ACTION_SETTINGS)); 203 } 204 } else if (view.getId() == R.id.update_button) { 205 final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest); 206 final Intent intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND); 207 intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND, testItem.command); 208 final EditText editText = (EditText) findViewById(R.id.edit_text_widget); 209 intent.putExtra(CommandReceiverActivity.EXTRA_VALUE, editText.getText().toString()); 210 startActivity(intent); 211 } 212 } 213 214 @Override onCheckedChanged(CompoundButton buttonView, boolean isChecked)215 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 216 final Intent intent; 217 if (TEST_CHECK_USER_RESTRICTION.equals(mTest)) { 218 final String userRestriction = getIntent().getStringExtra( 219 CommandReceiverActivity.EXTRA_USER_RESTRICTION); 220 intent = mForceCurrentUserDpm 221 ? CommandReceiverActivity.createSetCurrentUserRestrictionIntent( 222 userRestriction, isChecked) 223 : CommandReceiverActivity.createSetDeviceOwnerUserRestrictionIntent( 224 userRestriction, isChecked); 225 } else { 226 intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND); 227 final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest); 228 intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND, testItem.command); 229 intent.putExtra(CommandReceiverActivity.EXTRA_ENFORCED, isChecked); 230 intent.putExtra(CommandReceiverActivity.EXTRA_USE_CURRENT_USER_DPM, 231 mForceCurrentUserDpm); 232 } 233 startActivity(intent); 234 } 235 236 @Override onItemSelected(AdapterView<?> parent, View view, int pos, long id)237 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 238 final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest); 239 final Intent intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND); 240 intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND, testItem.command); 241 startActivity(intent); 242 } 243 244 @Override onNothingSelected(AdapterView<?> parent)245 public void onNothingSelected(AdapterView<?> parent) { 246 // Do nothing. 247 } 248 249 @Override getTestId()250 public String getTestId() { 251 return mTestId; 252 } 253 254 public static class TestAccessibilityService extends AccessibilityService { 255 256 @Override onAccessibilityEvent(AccessibilityEvent event)257 public void onAccessibilityEvent(AccessibilityEvent event) { 258 // Do nothing 259 } 260 261 @Override onInterrupt()262 public void onInterrupt() { 263 // Do nothing 264 } 265 } 266 267 public static class TestInputMethod extends InputMethodService { 268 @Override onEvaluateFullscreenMode()269 public boolean onEvaluateFullscreenMode() { 270 return false; 271 } 272 273 @Override onEvaluateInputViewShown()274 public boolean onEvaluateInputViewShown() { 275 return false; 276 } 277 } 278 279 private static class PolicyTestItem { 280 public final int setStep; 281 public final int userAction; 282 public final int widgetLabel; 283 public final int widgetId; 284 public final String command; 285 PolicyTestItem(int setStep, int userAction, int widgetLabel, int widgetId, String command)286 public PolicyTestItem(int setStep, int userAction, int widgetLabel, int widgetId, 287 String command) { 288 this.setStep = setStep; 289 this.userAction = userAction; 290 this.widgetLabel = widgetLabel; 291 this.widgetId = widgetId; 292 this.command = command; 293 } 294 } 295 } 296