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