• 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.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 optional for user to change on Settings.
82      */
83     private static final List<String> OPTIONAL_USER_RESTRICTION_ACTIONS = Arrays
84             .asList(UserManager.DISALLOW_CONFIG_CELL_BROADCASTS);
85 
86     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))87         POLICY_TEST_ITEMS.put(TEST_CHECK_AUTO_TIME_REQUIRED, new PolicyTestItem(
88                 R.string.auto_time_required_set_step,
89                 R.string.set_auto_time_required_action,
90                 R.string.set_auto_time_required_widget_label,
91                 R.id.switch_widget,
92                 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))93         POLICY_TEST_ITEMS.put(TEST_CHECK_KEYGURAD_UNREDACTED_NOTIFICATION, new PolicyTestItem(
94                 R.string.disallow_keyguard_unredacted_notifications_set_step,
95                 R.string.disallow_keyguard_unredacted_notifications_action,
96                 R.string.disallow_keyguard_unredacted_notifications_widget_label,
97                 R.id.switch_widget,
98                 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))99         POLICY_TEST_ITEMS.put(TEST_CHECK_LOCK_SCREEN_INFO, new PolicyTestItem(
100                 R.string.lock_screen_info_set_step,
101                 R.string.set_lock_screen_info_action,
102                 R.string.set_lock_screen_info_widget_label,
103                 R.id.edit_text_widget,
104                 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))105         POLICY_TEST_ITEMS.put(TEST_CHECK_MAXIMUM_TIME_TO_LOCK, new PolicyTestItem(
106                 R.string.maximum_time_to_lock_set_step,
107                 R.string.set_maximum_time_to_lock_action,
108                 R.string.set_maximum_time_to_lock_widget_label,
109                 R.id.edit_text_widget,
110                 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))111         POLICY_TEST_ITEMS.put(TEST_CHECK_PERMITTED_ACCESSIBILITY_SERVICE, new PolicyTestItem(
112                 R.string.permitted_accessibility_services_set_step,
113                 R.string.set_permitted_accessibility_services_action,
114                 R.string.set_permitted_accessibility_services_widget_label,
115                 R.id.switch_widget,
116                 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))117         POLICY_TEST_ITEMS.put(TEST_CHECK_PERMITTED_INPUT_METHOD, new PolicyTestItem(
118                 R.string.permitted_input_methods_set_step,
119                 R.string.set_permitted_input_methods_action,
120                 R.string.set_permitted_input_methods_widget_label,
121                 R.id.switch_widget,
122                 CommandReceiverActivity.COMMAND_ALLOW_ONLY_SYSTEM_INPUT_METHODS));
123     }
124 
125     private boolean mForceCurrentUserDpm;
126     private String mSettingsIntentAction;
127     private String mTestId;
128     private String mTitle;
129     private String mTest;
130 
131     @Override
onCreate(Bundle savedInstanceState)132     public void onCreate(Bundle savedInstanceState) {
133         super.onCreate(savedInstanceState);
134         setContentView(R.layout.policy_transparency_test);
135         setPassFailButtonClickListeners();
136 
137         mForceCurrentUserDpm =
138                 getIntent().getBooleanExtra(
139                         CommandReceiverActivity.EXTRA_USE_CURRENT_USER_DPM, false);
140         mTitle = getIntent().getStringExtra(EXTRA_TITLE);
141         mTestId = getIntent().getStringExtra(EXTRA_TEST_ID);
142         mSettingsIntentAction = getIntent().getStringExtra(EXTRA_SETTINGS_INTENT_ACTION);
143         mTest = getIntent().getStringExtra(EXTRA_TEST);
144 
145         setTitle(mTitle);
146         findViewById(R.id.open_settings_button).setOnClickListener(this);
147         updateTestInstructions();
148     }
149 
updateTestInstructions()150     private void updateTestInstructions() {
151         String setStep = null;
152         String userAction = null;
153         String widgetLabel = null;
154         int widgetId = 0;
155         String note = "";
156         if (TEST_CHECK_USER_RESTRICTION.equals(mTest)) {
157             setStep = getString(R.string.user_restriction_set_step, mTitle);
158             final String userRestriction = getIntent().getStringExtra(
159                     CommandReceiverActivity.EXTRA_USER_RESTRICTION);
160             userAction = UserRestrictions.getUserAction(this, userRestriction);
161             widgetLabel = mTitle;
162             widgetId = R.id.switch_widget;
163             if (OPTIONAL_USER_RESTRICTION_ACTIONS.contains(userRestriction)) {
164                 note = getString(R.string.optional_policy_transparency_test_note);
165             }
166         } else {
167             final PolicyTestItem testItem = POLICY_TEST_ITEMS.get(mTest);
168             setStep = getString(testItem.setStep);
169             userAction = getString(testItem.userAction);
170             widgetLabel = getString(testItem.widgetLabel);
171             widgetId = testItem.widgetId;
172         }
173         ((TextView) findViewById(R.id.widget_label)).setText(widgetLabel);
174         ((TextView) findViewById(R.id.test_instructions)).setText(
175                 getString(R.string.policy_transparency_test_instructions,
176                         setStep, userAction, note));
177         updateWidget(widgetId);
178     }
179 
updateWidget(int widgetId)180     private void updateWidget(int widgetId) {
181         if (widgetId == R.id.switch_widget) {
182             Switch switchWidget = (Switch) findViewById(R.id.switch_widget);
183             switchWidget.setOnCheckedChangeListener(this);
184             switchWidget.setVisibility(View.VISIBLE);
185         } else if (widgetId == R.id.edit_text_widget) {
186             findViewById(R.id.edit_text_widget).setVisibility(View.VISIBLE);
187             Button updateButton = (Button) findViewById(R.id.update_button);
188             updateButton.setOnClickListener(this);
189             updateButton.setVisibility(View.VISIBLE);
190         } else {
191             throw new IllegalArgumentException("Unknown widgetId: " + widgetId);
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         Log.d(TAG, "onItemSelected(): command=" + testItem.command);
242         startActivity(intent);
243     }
244 
245     @Override
onNothingSelected(AdapterView<?> parent)246     public void onNothingSelected(AdapterView<?> parent) {
247         // Do nothing.
248     }
249 
250     @Override
getTestId()251     public String getTestId() {
252         return mTestId;
253     }
254 
255     public static class TestAccessibilityService extends AccessibilityService {
256 
257         @Override
onAccessibilityEvent(AccessibilityEvent event)258         public void onAccessibilityEvent(AccessibilityEvent event) {
259             // Do nothing
260         }
261 
262         @Override
onInterrupt()263         public void onInterrupt() {
264             // Do nothing
265         }
266     }
267 
268     public static class TestInputMethod extends InputMethodService {
269         @Override
onEvaluateFullscreenMode()270         public boolean onEvaluateFullscreenMode() {
271             return false;
272         }
273 
274         @Override
onEvaluateInputViewShown()275         public boolean onEvaluateInputViewShown() {
276             return false;
277         }
278     }
279 
280     private static class PolicyTestItem {
281         public final int setStep;
282         public final int userAction;
283         public final int widgetLabel;
284         public final int widgetId;
285         public final String command;
286 
PolicyTestItem(int setStep, int userAction, int widgetLabel, int widgetId, String command)287         public PolicyTestItem(int setStep, int userAction, int widgetLabel, int widgetId,
288                 String command) {
289             this.setStep = setStep;
290             this.userAction = userAction;
291             this.widgetLabel = widgetLabel;
292             this.widgetId = widgetId;
293             this.command = command;
294         }
295     }
296 }
297