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