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.app.admin.DevicePolicyManager;
21 import android.content.Intent;
22 import android.inputmethodservice.InputMethodService;
23 import android.os.Bundle;
24 import android.os.UserManager;
25 import android.util.ArrayMap;
26 import android.view.accessibility.AccessibilityEvent;
27 import android.view.View;
28 import android.widget.AdapterView;
29 import android.widget.ArrayAdapter;
30 import android.widget.Button;
31 import android.widget.CompoundButton;
32 import android.widget.EditText;
33 import android.widget.Spinner;
34 import android.widget.Switch;
35 import android.widget.TextView;
36 
37 import com.android.cts.verifier.PassFailButtons;
38 import com.android.cts.verifier.R;
39 
40 import java.util.ArrayList;
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_PASSWORD_QUALITY = "check-password-quality";
62     public static final String TEST_CHECK_PERMITTED_ACCESSIBILITY_SERVICE =
63             "check-permitted-accessibility-service";
64     public static final String TEST_CHECK_PERMITTED_INPUT_METHOD = "check-permitted-input-method";
65 
66     public static final String EXTRA_SETTINGS_INTENT_ACTION =
67             "com.android.cts.verifier.managedprovisioning.extra.SETTINGS_INTENT_ACTION";
68     public static final String EXTRA_TITLE =
69             "com.android.cts.verifier.managedprovisioning.extra.TITLE";
70     // Identifies the test in the calling activity. We will set the result for this test.
71     // Type: String
72     public static final String EXTRA_TEST_ID =
73             "com.android.cts.verifier.managedprovisioning.extra.TEST_ID";
74 
75     private static final Map<String, PolicyTestItem> POLICY_TEST_ITEMS = new ArrayMap<>();
76     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))77         POLICY_TEST_ITEMS.put(TEST_CHECK_AUTO_TIME_REQUIRED, new PolicyTestItem(
78                 R.string.auto_time_required_set_step,
79                 R.string.set_auto_time_required_action,
80                 R.string.set_auto_time_required_widget_label,
81                 R.id.switch_widget,
82                 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))83         POLICY_TEST_ITEMS.put(TEST_CHECK_KEYGURAD_UNREDACTED_NOTIFICATION, new PolicyTestItem(
84                 R.string.disallow_keyguard_unredacted_notifications_set_step,
85                 R.string.disallow_keyguard_unredacted_notifications_action,
86                 R.string.disallow_keyguard_unredacted_notifications_widget_label,
87                 R.id.switch_widget,
88                 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))89         POLICY_TEST_ITEMS.put(TEST_CHECK_LOCK_SCREEN_INFO, new PolicyTestItem(
90                 R.string.lock_screen_info_set_step,
91                 R.string.set_lock_screen_info_action,
92                 R.string.set_lock_screen_info_widget_label,
93                 R.id.edit_text_widget,
94                 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))95         POLICY_TEST_ITEMS.put(TEST_CHECK_MAXIMUM_TIME_TO_LOCK, new PolicyTestItem(
96                 R.string.maximum_time_to_lock_set_step,
97                 R.string.set_maximum_time_to_lock_action,
98                 R.string.set_maximum_time_to_lock_widget_label,
99                 R.id.edit_text_widget,
100                 CommandReceiverActivity.COMMAND_SET_MAXIMUM_TO_LOCK));
POLICY_TEST_ITEMS.put(TEST_CHECK_PASSWORD_QUALITY, new PolicyTestItem( R.string.password_quality_set_step, R.string.set_password_quality_action, R.string.set_password_quality_widget_label, R.id.spinner_widget, CommandReceiverActivity.COMMAND_SET_PASSWORD_QUALITY))101         POLICY_TEST_ITEMS.put(TEST_CHECK_PASSWORD_QUALITY, new PolicyTestItem(
102                 R.string.password_quality_set_step,
103                 R.string.set_password_quality_action,
104                 R.string.set_password_quality_widget_label,
105                 R.id.spinner_widget,
106                 CommandReceiverActivity.COMMAND_SET_PASSWORD_QUALITY));
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     // IDs of settings for {@link DevicePolicyManager#setPasswordQuality(ComponentName, int)}.
122     private final int[] passwordQualityIds = new int[] {
123         DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED,
124         DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
125         DevicePolicyManager.PASSWORD_QUALITY_NUMERIC,
126         DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX,
127         DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC,
128         DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC,
129         DevicePolicyManager.PASSWORD_QUALITY_COMPLEX
130     };
131     // Strings to show for each password quality setting.
132     private final int[] passwordQualityLabelResIds = new int[] {
133         R.string.password_quality_unspecified,
134         R.string.password_quality_something,
135         R.string.password_quality_numeric,
136         R.string.password_quality_numeric_complex,
137         R.string.password_quality_alphabetic,
138         R.string.password_quality_alphanumeric,
139         R.string.password_quality_complex
140     };
141 
142     private String mSettingsIntentAction;
143     private String mTestId;
144     private String mTitle;
145     private String mTest;
146 
147     @Override
onCreate(Bundle savedInstanceState)148     public void onCreate(Bundle savedInstanceState) {
149         super.onCreate(savedInstanceState);
150         setContentView(R.layout.policy_transparency_test);
151         setPassFailButtonClickListeners();
152 
153         mTitle = getIntent().getStringExtra(EXTRA_TITLE);
154         mTestId = getIntent().getStringExtra(EXTRA_TEST_ID);
155         mSettingsIntentAction = getIntent().getStringExtra(EXTRA_SETTINGS_INTENT_ACTION);
156         mTest = getIntent().getStringExtra(EXTRA_TEST);
157 
158         setTitle(mTitle);
159         findViewById(R.id.open_settings_button).setOnClickListener(this);
160         updateTestInstructions();
161     }
162 
updateTestInstructions()163     private void updateTestInstructions() {
164         String setStep = null;
165         String userAction = null;
166         String widgetLabel = null;
167         int widgetId = 0;
168         if (TEST_CHECK_USER_RESTRICTION.equals(mTest)) {
169             setStep = getString(R.string.user_restriction_set_step, mTitle);
170             final String userRestriction = getIntent().getStringExtra(
171                     CommandReceiverActivity.EXTRA_USER_RESTRICTION);
172             userAction = UserRestrictions.getUserAction(this, userRestriction);
173             widgetLabel = mTitle;
174             widgetId = R.id.switch_widget;
175         } else {
176             final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest);
177             setStep = getString(testItem.setStep);
178             userAction = getString(testItem.userAction);
179             widgetLabel = getString(testItem.widgetLabel);
180             widgetId = testItem.widgetId;
181         }
182         ((TextView) findViewById(R.id.widget_label)).setText(widgetLabel);
183         ((TextView) findViewById(R.id.test_instructions)).setText(
184                 getString(R.string.policy_transparency_test_instructions, setStep, userAction));
185         updateWidget(widgetId);
186     }
187 
updateWidget(int widgetId)188     private void updateWidget(int widgetId) {
189         switch (widgetId) {
190             case R.id.switch_widget: {
191                 Switch switchWidget = (Switch) findViewById(R.id.switch_widget);
192                 switchWidget.setOnCheckedChangeListener(this);
193                 switchWidget.setVisibility(View.VISIBLE);
194             } break;
195             case R.id.edit_text_widget: {
196                 findViewById(R.id.edit_text_widget).setVisibility(View.VISIBLE);
197                 Button updateButton = (Button) findViewById(R.id.update_button);
198                 updateButton.setOnClickListener(this);
199                 updateButton.setVisibility(View.VISIBLE);
200             } break;
201             case R.id.spinner_widget: {
202                 if (TEST_CHECK_PASSWORD_QUALITY.equals(mTest)) {
203                     Spinner spinner = (Spinner) findViewById(R.id.spinner_widget);
204                     spinner.setVisibility(View.VISIBLE);
205                     spinner.setOnItemSelectedListener(this);
206                     final ArrayList<String> passwordQualityLabels = new ArrayList<String>();
207                     for (int resId : passwordQualityLabelResIds) {
208                         passwordQualityLabels.add(getString(resId));
209                     }
210                     ArrayAdapter<String> adapter = new ArrayAdapter(this,
211                             android.R.layout.simple_spinner_item, passwordQualityLabels);
212                     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
213                     spinner.setAdapter(adapter);
214                 }
215             } break;
216             default: {
217                 throw new IllegalArgumentException("Unknown widgetId: " + widgetId);
218             }
219         }
220     }
221 
222     @Override
onClick(View view)223     public void onClick(View view) {
224         if (view.getId() == R.id.open_settings_button) {
225             startActivity(new Intent(mSettingsIntentAction));
226         } else if (view.getId() == R.id.update_button) {
227             final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest);
228             final Intent intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND);
229             intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND, testItem.command);
230             final EditText editText = (EditText) findViewById(R.id.edit_text_widget);
231             intent.putExtra(CommandReceiverActivity.EXTRA_VALUE, editText.getText().toString());
232             startActivity(intent);
233         }
234     }
235 
236     @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)237     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
238         final Intent intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND);
239         if (TEST_CHECK_USER_RESTRICTION.equals(mTest)) {
240             final String userRestriction = getIntent().getStringExtra(
241                     CommandReceiverActivity.EXTRA_USER_RESTRICTION);
242             intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND,
243                     CommandReceiverActivity.COMMAND_SET_USER_RESTRICTION);
244             intent.putExtra(CommandReceiverActivity.EXTRA_USER_RESTRICTION, userRestriction);
245             intent.putExtra(CommandReceiverActivity.EXTRA_ENFORCED, isChecked);
246         } else {
247             final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest);
248             intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND, testItem.command);
249             intent.putExtra(CommandReceiverActivity.EXTRA_ENFORCED, isChecked);
250         }
251         startActivity(intent);
252     }
253 
254     @Override
onItemSelected(AdapterView<?> parent, View view, int pos, long id)255     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
256         final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest);
257         final Intent intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND);
258         intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND, testItem.command);
259         if (TEST_CHECK_PASSWORD_QUALITY.equals(mTest)) {
260             intent.putExtra(CommandReceiverActivity.EXTRA_VALUE, passwordQualityIds[(int) id]);
261         }
262         startActivity(intent);
263     }
264 
265     @Override
onNothingSelected(AdapterView<?> parent)266     public void onNothingSelected(AdapterView<?> parent) {
267         // Do nothing.
268     }
269 
270     @Override
getTestId()271     public String getTestId() {
272         return mTestId;
273     }
274 
275     public static class DummyAccessibilityService extends AccessibilityService {
276 
277         @Override
onAccessibilityEvent(AccessibilityEvent event)278         public void onAccessibilityEvent(AccessibilityEvent event) {
279             // Do nothing
280         }
281 
282         @Override
onInterrupt()283         public void onInterrupt() {
284             // Do nothing
285         }
286     }
287 
288     public static class DummyInputMethod extends InputMethodService {
289         @Override
onEvaluateFullscreenMode()290         public boolean onEvaluateFullscreenMode() {
291             return false;
292         }
293 
294         @Override
onEvaluateInputViewShown()295         public boolean onEvaluateInputViewShown() {
296             return false;
297         }
298     }
299 
300     private static class PolicyTestItem {
301         public final int setStep;
302         public final int userAction;
303         public final int widgetLabel;
304         public final int widgetId;
305         public final String command;
306 
PolicyTestItem(int setStep, int userAction, int widgetLabel, int widgetId, String command)307         public PolicyTestItem(int setStep, int userAction, int widgetLabel, int widgetId,
308                 String command) {
309             this.setStep = setStep;
310             this.userAction = userAction;
311             this.widgetLabel = widgetLabel;
312             this.widgetId = widgetId;
313             this.command = command;
314         }
315     }
316 }
317