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.util.ArrayMap;
25 import android.view.accessibility.AccessibilityEvent;
26 import android.view.View;
27 import android.widget.AdapterView;
28 import android.widget.ArrayAdapter;
29 import android.widget.Button;
30 import android.widget.CompoundButton;
31 import android.widget.EditText;
32 import android.widget.Spinner;
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.ArrayList;
40 import java.util.Map;
41 
42 public class PolicyTransparencyTestActivity extends PassFailButtons.Activity implements
43         View.OnClickListener, CompoundButton.OnCheckedChangeListener,
44         AdapterView.OnItemSelectedListener {
45     public static final String ACTION_SHOW_POLICY_TRANSPARENCY_TEST =
46             "com.android.cts.verifier.managedprovisioning.action.SHOW_POLICY_TRANSPARENCY_TEST";
47 
48     // Identifies a test to perform. Type String. The possible values are the ones underneath.
49     public static final String EXTRA_TEST =
50             "com.android.cts.verifier.managedprovisioning.extra.TEST";
51 
52     // In this case: should also contain an extra
53     // {@link CommandReceiverActivity.EXTRA_USER_RESTRICTION}
54     public static final String TEST_CHECK_USER_RESTRICTION = "check-user-restriction";
55     public static final String TEST_CHECK_AUTO_TIME_REQUIRED = "check-auto-time-required";
56     public static final String TEST_CHECK_KEYGURAD_UNREDACTED_NOTIFICATION =
57             "check-keyguard-unredacted-notification";
58     public static final String TEST_CHECK_LOCK_SCREEN_INFO = "check-lock-screen-info";
59     public static final String TEST_CHECK_MAXIMUM_TIME_TO_LOCK = "check-maximum-time-to-lock";
60     public static final String TEST_CHECK_PASSWORD_QUALITY = "check-password-quality";
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     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))76         POLICY_TEST_ITEMS.put(TEST_CHECK_AUTO_TIME_REQUIRED, new PolicyTestItem(
77                 R.string.auto_time_required_set_step,
78                 R.string.set_auto_time_required_action,
79                 R.string.set_auto_time_required_widget_label,
80                 R.id.switch_widget,
81                 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))82         POLICY_TEST_ITEMS.put(TEST_CHECK_KEYGURAD_UNREDACTED_NOTIFICATION, new PolicyTestItem(
83                 R.string.disallow_keyguard_unredacted_notifications_set_step,
84                 R.string.disallow_keyguard_unredacted_notifications_action,
85                 R.string.disallow_keyguard_unredacted_notifications_widget_label,
86                 R.id.switch_widget,
87                 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))88         POLICY_TEST_ITEMS.put(TEST_CHECK_LOCK_SCREEN_INFO, new PolicyTestItem(
89                 R.string.lock_screen_info_set_step,
90                 R.string.set_lock_screen_info_action,
91                 R.string.set_lock_screen_info_widget_label,
92                 R.id.edit_text_widget,
93                 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))94         POLICY_TEST_ITEMS.put(TEST_CHECK_MAXIMUM_TIME_TO_LOCK, new PolicyTestItem(
95                 R.string.maximum_time_to_lock_set_step,
96                 R.string.set_maximum_time_to_lock_action,
97                 R.string.set_maximum_time_to_lock_widget_label,
98                 R.id.edit_text_widget,
99                 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))100         POLICY_TEST_ITEMS.put(TEST_CHECK_PASSWORD_QUALITY, new PolicyTestItem(
101                 R.string.password_quality_set_step,
102                 R.string.set_password_quality_action,
103                 R.string.set_password_quality_widget_label,
104                 R.id.spinner_widget,
105                 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))106         POLICY_TEST_ITEMS.put(TEST_CHECK_PERMITTED_ACCESSIBILITY_SERVICE, new PolicyTestItem(
107                 R.string.permitted_accessibility_services_set_step,
108                 R.string.set_permitted_accessibility_services_action,
109                 R.string.set_permitted_accessibility_services_widget_label,
110                 R.id.switch_widget,
111                 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))112         POLICY_TEST_ITEMS.put(TEST_CHECK_PERMITTED_INPUT_METHOD, new PolicyTestItem(
113                 R.string.permitted_input_methods_set_step,
114                 R.string.set_permitted_input_methods_action,
115                 R.string.set_permitted_input_methods_widget_label,
116                 R.id.switch_widget,
117                 CommandReceiverActivity.COMMAND_ALLOW_ONLY_SYSTEM_INPUT_METHODS));
118     }
119 
120     // IDs of settings for {@link DevicePolicyManager#setPasswordQuality(ComponentName, int)}.
121     private final int[] passwordQualityIds = new int[] {
122         DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED,
123         DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
124         DevicePolicyManager.PASSWORD_QUALITY_NUMERIC,
125         DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX,
126         DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC,
127         DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC,
128         DevicePolicyManager.PASSWORD_QUALITY_COMPLEX
129     };
130     // Strings to show for each password quality setting.
131     private final int[] passwordQualityLabelResIds = new int[] {
132         R.string.password_quality_unspecified,
133         R.string.password_quality_something,
134         R.string.password_quality_numeric,
135         R.string.password_quality_numeric_complex,
136         R.string.password_quality_alphabetic,
137         R.string.password_quality_alphanumeric,
138         R.string.password_quality_complex
139     };
140 
141     private String mSettingsIntentAction;
142     private String mTestId;
143     private String mTitle;
144     private String mTest;
145 
146     @Override
onCreate(Bundle savedInstanceState)147     public void onCreate(Bundle savedInstanceState) {
148         super.onCreate(savedInstanceState);
149         setContentView(R.layout.policy_transparency_test);
150         setPassFailButtonClickListeners();
151 
152         mTitle = getIntent().getStringExtra(EXTRA_TITLE);
153         mTestId = getIntent().getStringExtra(EXTRA_TEST_ID);
154         mSettingsIntentAction = getIntent().getStringExtra(EXTRA_SETTINGS_INTENT_ACTION);
155         mTest = getIntent().getStringExtra(EXTRA_TEST);
156 
157         setTitle(mTitle);
158         findViewById(R.id.open_settings_button).setOnClickListener(this);
159         updateTestInstructions();
160     }
161 
updateTestInstructions()162     private void updateTestInstructions() {
163         String setStep = null;
164         String userAction = null;
165         String widgetLabel = null;
166         int widgetId = 0;
167         if (TEST_CHECK_USER_RESTRICTION.equals(mTest)) {
168             setStep = getString(R.string.user_restriction_set_step, mTitle);
169             final String userRestriction = getIntent().getStringExtra(
170                     CommandReceiverActivity.EXTRA_USER_RESTRICTION);
171             userAction = UserRestrictions.getUserAction(this, userRestriction);
172             widgetLabel = mTitle;
173             widgetId = R.id.switch_widget;
174         } else {
175             final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest);
176             setStep = getString(testItem.setStep);
177             userAction = getString(testItem.userAction);
178             widgetLabel = getString(testItem.widgetLabel);
179             widgetId = testItem.widgetId;
180         }
181         ((TextView) findViewById(R.id.widget_label)).setText(widgetLabel);
182         ((TextView) findViewById(R.id.test_instructions)).setText(
183                 getString(R.string.policy_transparency_test_instructions, setStep, userAction));
184         updateWidget(widgetId);
185     }
186 
updateWidget(int widgetId)187     private void updateWidget(int widgetId) {
188         switch (widgetId) {
189             case R.id.switch_widget: {
190                 Switch switchWidget = (Switch) findViewById(R.id.switch_widget);
191                 switchWidget.setOnCheckedChangeListener(this);
192                 switchWidget.setVisibility(View.VISIBLE);
193             } break;
194             case R.id.edit_text_widget: {
195                 findViewById(R.id.edit_text_widget).setVisibility(View.VISIBLE);
196                 Button updateButton = (Button) findViewById(R.id.update_button);
197                 updateButton.setOnClickListener(this);
198                 updateButton.setVisibility(View.VISIBLE);
199             } break;
200             case R.id.spinner_widget: {
201                 if (TEST_CHECK_PASSWORD_QUALITY.equals(mTest)) {
202                     Spinner spinner = (Spinner) findViewById(R.id.spinner_widget);
203                     spinner.setVisibility(View.VISIBLE);
204                     spinner.setOnItemSelectedListener(this);
205                     final ArrayList<String> passwordQualityLabels = new ArrayList<String>();
206                     for (int resId : passwordQualityLabelResIds) {
207                         passwordQualityLabels.add(getString(resId));
208                     }
209                     ArrayAdapter<String> adapter = new ArrayAdapter(this,
210                             android.R.layout.simple_spinner_item, passwordQualityLabels);
211                     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
212                     spinner.setAdapter(adapter);
213                 }
214             } break;
215             default: {
216                 throw new IllegalArgumentException("Unknown widgetId: " + widgetId);
217             }
218         }
219     }
220 
221     @Override
onClick(View view)222     public void onClick(View view) {
223         if (view.getId() == R.id.open_settings_button) {
224             startActivity(new Intent(mSettingsIntentAction));
225         } else if (view.getId() == R.id.update_button) {
226             final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest);
227             final Intent intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND);
228             intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND, testItem.command);
229             final EditText editText = (EditText) findViewById(R.id.edit_text_widget);
230             intent.putExtra(CommandReceiverActivity.EXTRA_VALUE, editText.getText().toString());
231             startActivity(intent);
232         }
233     }
234 
235     @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)236     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
237         final Intent intent;
238         if (TEST_CHECK_USER_RESTRICTION.equals(mTest)) {
239             final String userRestriction = getIntent().getStringExtra(
240                     CommandReceiverActivity.EXTRA_USER_RESTRICTION);
241             intent = CommandReceiverActivity.createSetUserRestrictionIntent(
242                     userRestriction, isChecked);
243         } else {
244             intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND);
245             final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest);
246             intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND, testItem.command);
247             intent.putExtra(CommandReceiverActivity.EXTRA_ENFORCED, isChecked);
248         }
249         startActivity(intent);
250     }
251 
252     @Override
onItemSelected(AdapterView<?> parent, View view, int pos, long id)253     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
254         final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest);
255         final Intent intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND);
256         intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND, testItem.command);
257         if (TEST_CHECK_PASSWORD_QUALITY.equals(mTest)) {
258             intent.putExtra(CommandReceiverActivity.EXTRA_VALUE, passwordQualityIds[(int) id]);
259         }
260         startActivity(intent);
261     }
262 
263     @Override
onNothingSelected(AdapterView<?> parent)264     public void onNothingSelected(AdapterView<?> parent) {
265         // Do nothing.
266     }
267 
268     @Override
getTestId()269     public String getTestId() {
270         return mTestId;
271     }
272 
273     public static class DummyAccessibilityService extends AccessibilityService {
274 
275         @Override
onAccessibilityEvent(AccessibilityEvent event)276         public void onAccessibilityEvent(AccessibilityEvent event) {
277             // Do nothing
278         }
279 
280         @Override
onInterrupt()281         public void onInterrupt() {
282             // Do nothing
283         }
284     }
285 
286     public static class DummyInputMethod extends InputMethodService {
287         @Override
onEvaluateFullscreenMode()288         public boolean onEvaluateFullscreenMode() {
289             return false;
290         }
291 
292         @Override
onEvaluateInputViewShown()293         public boolean onEvaluateInputViewShown() {
294             return false;
295         }
296     }
297 
298     private static class PolicyTestItem {
299         public final int setStep;
300         public final int userAction;
301         public final int widgetLabel;
302         public final int widgetId;
303         public final String command;
304 
PolicyTestItem(int setStep, int userAction, int widgetLabel, int widgetId, String command)305         public PolicyTestItem(int setStep, int userAction, int widgetLabel, int widgetId,
306                 String command) {
307             this.setStep = setStep;
308             this.userAction = userAction;
309             this.widgetLabel = widgetLabel;
310             this.widgetId = widgetId;
311             this.command = command;
312         }
313     }
314 }
315