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