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