1 /*
2  * Copyright (C) 2015 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 static com.android.cts.verifier.managedprovisioning.Utils.createInteractiveTestItem;
20 
21 import android.app.Activity;
22 import android.app.admin.DevicePolicyManager;
23 import android.content.Intent;
24 import android.content.pm.PackageManager;
25 import android.database.DataSetObserver;
26 import android.os.Bundle;
27 import android.os.UserManager;
28 import android.provider.Settings;
29 
30 import com.android.cts.verifier.ArrayTestListAdapter;
31 import com.android.cts.verifier.IntentDrivenTestActivity.ButtonInfo;
32 import com.android.cts.verifier.PassFailButtons;
33 import com.android.cts.verifier.R;
34 import com.android.cts.verifier.TestListAdapter.TestListItem;
35 import com.android.cts.verifier.TestResult;
36 
37 /**
38  * Activity that lists all positive managed user tests.
39  */
40 public class ManagedUserPositiveTestActivity extends PassFailButtons.TestListActivity {
41     private static final String TAG = "ManagedUserPositiveTestActivity";
42 
43     private static final String ACTION_CHECK_AFFILIATED_PROFILE_OWNER =
44             "com.android.cts.verifier.managedprovisioning.action.CHECK_AFFILIATED_PROFILE_OWNER";
45     static final String EXTRA_TEST_ID = "extra-test-id";
46 
47     private static final String CHECK_AFFILIATED_PROFILE_OWNER_TEST_ID =
48             "CHECK_AFFILIATED_PROFILE_OWNER";
49     private static final String DEVICE_ADMIN_SETTINGS_ID = "DEVICE_ADMIN_SETTINGS";
50     private static final String DISABLE_STATUS_BAR_TEST_ID = "DISABLE_STATUS_BAR";
51     private static final String DISABLE_KEYGUARD_TEST_ID = "DISABLE_KEYGUARD";
52     private static final String POLICY_TRANSPARENCY_TEST_ID = "POLICY_TRANSPARENCY";
53     private static final String DISALLOW_REMOVE_USER_TEST_ID = "DISALLOW_REMOVE_USER";
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58 
59         if (ACTION_CHECK_AFFILIATED_PROFILE_OWNER.equals(getIntent().getAction())) {
60             DevicePolicyManager dpm = getSystemService(DevicePolicyManager.class);
61             if (dpm.isProfileOwnerApp(getPackageName()) && dpm.isAffiliatedUser()) {
62                 TestResult.setPassedResult(this, getIntent().getStringExtra(EXTRA_TEST_ID),
63                         null, null);
64             } else {
65                 TestResult.setFailedResult(this, getIntent().getStringExtra(EXTRA_TEST_ID),
66                         getString(R.string.managed_user_incorrect_managed_user), null);
67             }
68             finish();
69             return;
70         }
71 
72         setContentView(R.layout.positive_managed_user);
73         setInfoResources(R.string.managed_user_positive_tests,
74                 R.string.managed_user_positive_tests_info, 0);
75         setPassFailButtonClickListeners();
76 
77         final ArrayTestListAdapter adapter = new ArrayTestListAdapter(this);
78         adapter.add(TestListItem.newCategory(this, R.string.managed_user_positive_category));
79 
80         addTestsToAdapter(adapter);
81 
82         adapter.registerDataSetObserver(new DataSetObserver() {
83             @Override
84             public void onChanged() {
85                 updatePassButton();
86             }
87         });
88 
89         setTestListAdapter(adapter);
90     }
91 
92     @Override
finish()93     public void finish() {
94         // If this activity was started for checking profile owner status, then no need to do any
95         // tear down.
96         if (!ACTION_CHECK_AFFILIATED_PROFILE_OWNER.equals(getIntent().getAction())) {
97             // Pass and fail buttons are known to call finish() when clicked,
98             // and this is when we want to remove the managed owner.
99             DevicePolicyManager dpm = getSystemService(DevicePolicyManager.class);
100             dpm.logoutUser(DeviceAdminTestReceiver.getReceiverComponentName());
101         }
102         super.finish();
103     }
104 
addTestsToAdapter(final ArrayTestListAdapter adapter)105     private void addTestsToAdapter(final ArrayTestListAdapter adapter) {
106         adapter.add(createTestItem(this, CHECK_AFFILIATED_PROFILE_OWNER_TEST_ID,
107                 R.string.managed_user_check_managed_user_test,
108                 new Intent(ACTION_CHECK_AFFILIATED_PROFILE_OWNER)
109                         .putExtra(EXTRA_TEST_ID, getIntent().getStringExtra(EXTRA_TEST_ID))));
110 
111         // device admin settings
112         adapter.add(createInteractiveTestItem(this, DEVICE_ADMIN_SETTINGS_ID,
113                 R.string.device_owner_device_admin_visible,
114                 R.string.device_owner_device_admin_visible_info,
115                 new ButtonInfo(
116                         R.string.device_owner_settings_go,
117                         new Intent(Settings.ACTION_SECURITY_SETTINGS))));
118 
119         // DISABLE_STATUS_BAR_TEST
120         if (isStatusBarEnabled()) {
121             adapter.add(createInteractiveTestItem(this, DISABLE_STATUS_BAR_TEST_ID,
122                     R.string.device_owner_disable_statusbar_test,
123                     R.string.device_owner_disable_statusbar_test_info,
124                     new ButtonInfo[]{
125                             new ButtonInfo(
126                                     R.string.device_owner_disable_statusbar_button,
127                                     createManagedUserIntentWithBooleanParameter(
128                                             CommandReceiverActivity.COMMAND_SET_STATUSBAR_DISABLED,
129                                             true)),
130                             new ButtonInfo(
131                                     R.string.device_owner_reenable_statusbar_button,
132                                     createManagedUserIntentWithBooleanParameter(
133                                             CommandReceiverActivity.COMMAND_SET_STATUSBAR_DISABLED,
134                                             false))}));
135         }
136 
137         // setKeyguardDisabled
138         adapter.add(createInteractiveTestItem(this, DISABLE_KEYGUARD_TEST_ID,
139                 R.string.device_owner_disable_keyguard_test,
140                 R.string.device_owner_disable_keyguard_test_info,
141                 new ButtonInfo[]{
142                         new ButtonInfo(
143                                 R.string.device_owner_disable_keyguard_button,
144                                 createManagedUserIntentWithBooleanParameter(
145                                         CommandReceiverActivity.COMMAND_SET_KEYGUARD_DISABLED,
146                                         true)),
147                         new ButtonInfo(
148                                 R.string.device_owner_reenable_keyguard_button,
149                                 createManagedUserIntentWithBooleanParameter(
150                                         CommandReceiverActivity.COMMAND_SET_KEYGUARD_DISABLED,
151                                         false))}));
152 
153         // DISALLOW_REMOVE_USER
154         adapter.add(createInteractiveTestItem(this, DISALLOW_REMOVE_USER_TEST_ID,
155                 R.string.disallow_remove_user,
156                 R.string.managed_user_disallow_remove_user_info,
157                 new ButtonInfo[]{
158                         new ButtonInfo(
159                                 R.string.device_owner_user_restriction_set,
160                                 CommandReceiverActivity.createSetCurrentUserRestrictionIntent(
161                                         UserManager.DISALLOW_REMOVE_USER, true)),
162                         new ButtonInfo(
163                                 R.string.device_owner_settings_go,
164                                 new Intent(Settings.ACTION_USER_SETTINGS))}));
165 
166         // Policy Transparency
167         final Intent policyTransparencyTestIntent = new Intent(this,
168                 PolicyTransparencyTestListActivity.class);
169         policyTransparencyTestIntent.putExtra(
170                 PolicyTransparencyTestListActivity.EXTRA_MODE,
171                 PolicyTransparencyTestListActivity.MODE_MANAGED_USER);
172         // So that PolicyTransparencyTestListActivity knows which test to update with the result:
173         policyTransparencyTestIntent.putExtra(
174                 PolicyTransparencyTestActivity.EXTRA_TEST_ID, POLICY_TRANSPARENCY_TEST_ID);
175         adapter.add(createTestItem(this, POLICY_TRANSPARENCY_TEST_ID,
176                 R.string.device_profile_owner_policy_transparency_test,
177                 policyTransparencyTestIntent));
178 
179     }
180 
181 
createTestItem(Activity activity, String id, int titleRes, Intent intent)182     static TestListItem createTestItem(Activity activity, String id, int titleRes,
183             Intent intent) {
184         intent.putExtra(EXTRA_TEST_ID, id);
185         return TestListItem.newTest(activity, titleRes, id, intent, null);
186     }
187 
createManagedUserIntentWithBooleanParameter(String command, boolean value)188     private Intent createManagedUserIntentWithBooleanParameter(String command, boolean value) {
189         return new Intent(this, CommandReceiverActivity.class)
190                 .putExtra(CommandReceiverActivity.EXTRA_COMMAND, command)
191                 .putExtra(CommandReceiverActivity.EXTRA_ENFORCED, value);
192     }
193 
isStatusBarEnabled()194     private boolean isStatusBarEnabled() {
195         // Watches don't support the status bar so this is an ok proxy, but this is not the most
196         // general test for that. TODO: add a test API to do a real check for status bar support.
197         return !getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH);
198     }
199 }
200