• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (widgetId == R.id.switch_widget) {
178             Switch switchWidget = (Switch) findViewById(R.id.switch_widget);
179             switchWidget.setOnCheckedChangeListener(this);
180             switchWidget.setVisibility(View.VISIBLE);
181         } else if (widgetId == R.id.edit_text_widget) {
182             findViewById(R.id.edit_text_widget).setVisibility(View.VISIBLE);
183             Button updateButton = (Button) findViewById(R.id.update_button);
184             updateButton.setOnClickListener(this);
185             updateButton.setVisibility(View.VISIBLE);
186         } else {
187             throw new IllegalArgumentException("Unknown widgetId: " + widgetId);
188         }
189     }
190 
191     @Override
onClick(View view)192     public void onClick(View view) {
193         if (view.getId() == R.id.open_settings_button) {
194             try {
195                 startActivity(new Intent(mSettingsIntentAction));
196             } catch (ActivityNotFoundException e) {
197                 // If the given settings intent is not handled, use the main settings intent
198                 startActivity(new Intent(Settings.ACTION_SETTINGS));
199             }
200         } else if (view.getId() == R.id.update_button) {
201             final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest);
202             final Intent intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND);
203             intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND, testItem.command);
204             final EditText editText = (EditText) findViewById(R.id.edit_text_widget);
205             intent.putExtra(CommandReceiverActivity.EXTRA_VALUE, editText.getText().toString());
206             startActivity(intent);
207         }
208     }
209 
210     @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)211     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
212         final Intent intent;
213         if (TEST_CHECK_USER_RESTRICTION.equals(mTest)) {
214             final String userRestriction = getIntent().getStringExtra(
215                     CommandReceiverActivity.EXTRA_USER_RESTRICTION);
216             intent = mForceCurrentUserDpm
217                     ? CommandReceiverActivity.createSetCurrentUserRestrictionIntent(
218                             userRestriction, isChecked)
219                     : CommandReceiverActivity.createSetDeviceOwnerUserRestrictionIntent(
220                             userRestriction, isChecked);
221         } else {
222             intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND);
223             final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest);
224             intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND, testItem.command);
225             intent.putExtra(CommandReceiverActivity.EXTRA_ENFORCED, isChecked);
226             intent.putExtra(CommandReceiverActivity.EXTRA_USE_CURRENT_USER_DPM,
227                     mForceCurrentUserDpm);
228         }
229         startActivity(intent);
230     }
231 
232     @Override
onItemSelected(AdapterView<?> parent, View view, int pos, long id)233     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
234         final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest);
235         final Intent intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND);
236         intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND, testItem.command);
237         startActivity(intent);
238     }
239 
240     @Override
onNothingSelected(AdapterView<?> parent)241     public void onNothingSelected(AdapterView<?> parent) {
242         // Do nothing.
243     }
244 
245     @Override
getTestId()246     public String getTestId() {
247         return mTestId;
248     }
249 
250     public static class TestAccessibilityService extends AccessibilityService {
251 
252         @Override
onAccessibilityEvent(AccessibilityEvent event)253         public void onAccessibilityEvent(AccessibilityEvent event) {
254             // Do nothing
255         }
256 
257         @Override
onInterrupt()258         public void onInterrupt() {
259             // Do nothing
260         }
261     }
262 
263     public static class TestInputMethod extends InputMethodService {
264         @Override
onEvaluateFullscreenMode()265         public boolean onEvaluateFullscreenMode() {
266             return false;
267         }
268 
269         @Override
onEvaluateInputViewShown()270         public boolean onEvaluateInputViewShown() {
271             return false;
272         }
273     }
274 
275     private static class PolicyTestItem {
276         public final int setStep;
277         public final int userAction;
278         public final int widgetLabel;
279         public final int widgetId;
280         public final String command;
281 
PolicyTestItem(int setStep, int userAction, int widgetLabel, int widgetId, String command)282         public PolicyTestItem(int setStep, int userAction, int widgetLabel, int widgetId,
283                 String command) {
284             this.setStep = setStep;
285             this.userAction = userAction;
286             this.widgetLabel = widgetLabel;
287             this.widgetId = widgetId;
288             this.command = command;
289         }
290     }
291 }
292