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.content.Intent;
20 import android.content.pm.PackageManager;
21 import android.database.DataSetObserver;
22 import android.os.Bundle;
23 import android.provider.Settings;
24 import android.util.Pair;
25 import android.view.View;
26 
27 import com.android.cts.verifier.ArrayTestListAdapter;
28 import com.android.cts.verifier.PassFailButtons;
29 import com.android.cts.verifier.R;
30 import com.android.cts.verifier.TestListAdapter.TestListItem;
31 
32 import java.util.Arrays;
33 import java.util.List;
34 
35 /**
36  * Test class to verify transparency for policies enforced by device/profile owner.
37  */
38 public class PolicyTransparencyTestListActivity extends PassFailButtons.TestListActivity
39         implements View.OnClickListener {
40     public static final String ACTION_CHECK_POLICY_TRANSPARENCY =
41             "com.android.cts.verifier.managedprovisioning.action.CHECK_POLICY_TRANSPARENCY";
42 
43     public static final String EXTRA_MODE =
44             "com.android.cts.verifier.managedprovisioning.extra.mode";
45 
46     public static final int MODE_DEVICE_OWNER = 1;
47     public static final int MODE_MANAGED_PROFILE = 2;
48     public static final int MODE_COMP = 3;
49     public static final int MODE_MANAGED_USER = 4;
50 
51     /**
52      * Pairs of:
53      * <ul>
54      *   <li>An intent to start {@link PolicyTransparencyTestActivity}
55      *   <li>a label to show the user.
56      * </ul>
57      * These contain all the policies except for the user restriction ones.
58      */
59     private static final Pair<Intent, Integer>[] POLICIES;
60     static {
61         final String[] policyTests = new String[] {
62             PolicyTransparencyTestActivity.TEST_CHECK_AUTO_TIME_REQUIRED,
63             PolicyTransparencyTestActivity.TEST_CHECK_KEYGURAD_UNREDACTED_NOTIFICATION,
64             PolicyTransparencyTestActivity.TEST_CHECK_LOCK_SCREEN_INFO,
65             PolicyTransparencyTestActivity.TEST_CHECK_MAXIMUM_TIME_TO_LOCK,
66             PolicyTransparencyTestActivity.TEST_CHECK_PASSWORD_QUALITY,
67             PolicyTransparencyTestActivity.TEST_CHECK_PERMITTED_ACCESSIBILITY_SERVICE,
68             PolicyTransparencyTestActivity.TEST_CHECK_PERMITTED_INPUT_METHOD
69         };
70         final String[] settingsIntentActions = new String[] {
71             Settings.ACTION_DATE_SETTINGS,
72             Settings.ACTION_SETTINGS,
73             Settings.ACTION_DISPLAY_SETTINGS,
74             Settings.ACTION_DISPLAY_SETTINGS,
75             Settings.ACTION_SETTINGS,
76             Settings.ACTION_ACCESSIBILITY_SETTINGS,
77             Settings.ACTION_SETTINGS
78         };
79         final int[] policyLabels = new int[] {
80             R.string.set_auto_time_required,
81             R.string.disallow_keyguard_unredacted_notifications,
82             R.string.set_lock_screen_info,
83             R.string.set_maximum_time_to_lock,
84             R.string.set_password_quality,
85             R.string.set_permitted_accessibility_services,
86             R.string.set_permitted_input_methods
87         };
88         if (policyTests.length != settingsIntentActions.length ||
89                 policyTests.length != policyLabels.length) {
90             throw new AssertionError("Number of items in policyTests, "
91                     + " settingsIntentActions and policyLabels do not match");
92         }
93         POLICIES = new Pair[policyTests.length];
94         for (int i = 0; i < policyTests.length; ++i) {
95             final Intent intent =
96                     new Intent(PolicyTransparencyTestActivity.ACTION_SHOW_POLICY_TRANSPARENCY_TEST)
97                             .putExtra(PolicyTransparencyTestActivity.EXTRA_TEST, policyTests[i])
98                             .putExtra(PolicyTransparencyTestActivity.EXTRA_SETTINGS_INTENT_ACTION,
99                                     settingsIntentActions[i]);
100             POLICIES[i] = Pair.create(intent, policyLabels[i]);
101         }
102     }
103 
104     private static final List<String> ALSO_VALID_FOR_MANAGED_PROFILE = Arrays.asList(
105             PolicyTransparencyTestActivity.TEST_CHECK_PERMITTED_ACCESSIBILITY_SERVICE,
106             PolicyTransparencyTestActivity.TEST_CHECK_PERMITTED_INPUT_METHOD);
107     private static final List<String> ALSO_VALID_FOR_MANAGED_USER = Arrays.asList(
108             PolicyTransparencyTestActivity.TEST_CHECK_PERMITTED_ACCESSIBILITY_SERVICE,
109             PolicyTransparencyTestActivity.TEST_CHECK_PERMITTED_INPUT_METHOD);
110 
111     private int mMode;
112 
113     @Override
onCreate(Bundle savedInstanceState)114     protected void onCreate(Bundle savedInstanceState) {
115         super.onCreate(savedInstanceState);
116         setContentView(R.layout.policy_transparency_test_list);
117         setInfoResources(R.string.device_profile_owner_policy_transparency_test,
118                 R.string.device_profile_owner_policy_transparency_test_info, 0);
119         setPassFailButtonClickListeners();
120         setSupportMsgButtonClickListeners();
121 
122         if (!getIntent().hasExtra(EXTRA_MODE)) {
123             throw new RuntimeException("PolicyTransparencyTestListActivity started without extra "
124                     + EXTRA_MODE);
125         }
126         mMode = getIntent().getIntExtra(EXTRA_MODE, MODE_DEVICE_OWNER);
127         if (mMode != MODE_DEVICE_OWNER && mMode != MODE_MANAGED_PROFILE && mMode != MODE_COMP
128                 && mMode != MODE_MANAGED_USER) {
129             throw new RuntimeException("Unknown mode " + mMode);
130         }
131 
132         final ArrayTestListAdapter adapter = new ArrayTestListAdapter(this);
133         addTestsToAdapter(adapter);
134         adapter.registerDataSetObserver(new DataSetObserver() {
135             @Override
136             public void onChanged() {
137                 updatePassButton();
138             }
139         });
140 
141         setTestListAdapter(adapter);
142     }
143 
addTestsToAdapter(final ArrayTestListAdapter adapter)144     private void addTestsToAdapter(final ArrayTestListAdapter adapter) {
145         for (String restriction :
146                 UserRestrictions.getUserRestrictionsForPolicyTransparency(mMode)) {
147             final Intent intent = UserRestrictions.getUserRestrictionTestIntent(this, restriction);
148             if (!UserRestrictions.isRestrictionValid(this, restriction)) {
149                 continue;
150             }
151             final String title = UserRestrictions.getRestrictionLabel(this, restriction);
152             String testId = getTestId(title);
153             intent.putExtra(PolicyTransparencyTestActivity.EXTRA_TEST_ID, testId);
154             adapter.add(TestListItem.newTest(title, testId, intent, null));
155         }
156         if (mMode == MODE_COMP) {
157             // no other policies for COMP
158             return;
159         }
160         for (Pair<Intent, Integer> policy : POLICIES) {
161             final Intent intent = policy.first;
162             String test = intent.getStringExtra(PolicyTransparencyTestActivity.EXTRA_TEST);
163             if (!isPolicyValid(test)) {
164                 continue;
165             }
166             if (mMode == MODE_MANAGED_PROFILE && !ALSO_VALID_FOR_MANAGED_PROFILE.contains(test)) {
167                 continue;
168             }
169             if (mMode == MODE_MANAGED_USER && !ALSO_VALID_FOR_MANAGED_USER.contains(test)) {
170                 continue;
171             }
172             final String title = getString(policy.second);
173             String testId = getTestId(title);
174             intent.putExtra(PolicyTransparencyTestActivity.EXTRA_TITLE, title);
175             intent.putExtra(PolicyTransparencyTestActivity.EXTRA_TEST_ID, testId);
176             adapter.add(TestListItem.newTest(title, testId, intent, null));
177         }
178     }
179 
getTestId(String title)180     private String getTestId(String title) {
181         if (mMode == MODE_DEVICE_OWNER) {
182             return "DO_" + title;
183         } else if (mMode == MODE_MANAGED_PROFILE) {
184             return "MP_" + title;
185         } else if (mMode == MODE_COMP){
186             return "COMP_" + title;
187         } else if (mMode == MODE_MANAGED_USER) {
188             return "MU_" + title;
189         }
190         throw new RuntimeException("Unknown mode " + mMode);
191     }
192 
isPolicyValid(String test)193     private boolean isPolicyValid(String test) {
194         final PackageManager pm = getPackageManager();
195         switch (test) {
196             case PolicyTransparencyTestActivity.TEST_CHECK_PERMITTED_INPUT_METHOD:
197                 return pm.hasSystemFeature(PackageManager.FEATURE_INPUT_METHODS);
198             case PolicyTransparencyTestActivity.TEST_CHECK_PERMITTED_ACCESSIBILITY_SERVICE:
199                 return pm.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT);
200             case PolicyTransparencyTestActivity.TEST_CHECK_LOCK_SCREEN_INFO:
201                 return !pm.hasSystemFeature(PackageManager.FEATURE_WATCH);
202             default:
203                 return true;
204         }
205     }
206 
setSupportMsgButtonClickListeners()207     private void setSupportMsgButtonClickListeners() {
208         findViewById(R.id.short_msg_button).setOnClickListener(this);
209         findViewById(R.id.long_msg_button).setOnClickListener(this);
210     }
211 
212     @Override
onClick(View view)213     public void onClick(View view) {
214         if (view.getId() == R.id.short_msg_button) {
215             final Intent intent = new Intent(SetSupportMessageActivity.ACTION_SET_SUPPORT_MSG);
216             intent.putExtra(SetSupportMessageActivity.EXTRA_SUPPORT_MSG_TYPE,
217                     SetSupportMessageActivity.TYPE_SHORT_MSG);
218             startActivity(intent);
219         } else if (view.getId() == R.id.long_msg_button) {
220             final Intent intent = new Intent(SetSupportMessageActivity.ACTION_SET_SUPPORT_MSG);
221             intent.putExtra(SetSupportMessageActivity.EXTRA_SUPPORT_MSG_TYPE,
222                     SetSupportMessageActivity.TYPE_LONG_MSG);
223             startActivity(intent);
224         }
225     }
226 
227     @Override
getTestId()228     public String getTestId() {
229         return getIntent().getStringExtra(PolicyTransparencyTestActivity.EXTRA_TEST_ID);
230     }
231 
232     @Override
finish()233     public void finish() {
234         super.finish();
235         final Intent intent = new Intent(CommandReceiverActivity.ACTION_EXECUTE_COMMAND);
236         intent.putExtra(CommandReceiverActivity.EXTRA_COMMAND,
237                 CommandReceiverActivity.COMMAND_CLEAR_POLICIES);
238         intent.putExtra(PolicyTransparencyTestListActivity.EXTRA_MODE, mMode);
239         startActivity(intent);
240     }
241 }
242