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 android.app.admin.DevicePolicyManager;
20 import android.content.ActivityNotFoundException;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.PackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.hardware.fingerprint.FingerprintManager;
27 import android.os.Bundle;
28 import android.provider.Settings;
29 import android.view.View;
30 import android.view.View.OnClickListener;
31 import android.widget.Toast;
32 
33 import com.android.cts.verifier.ArrayTestListAdapter;
34 import com.android.cts.verifier.DialogTestListActivity;
35 import com.android.cts.verifier.R;
36 
37 import java.util.List;
38 
39 public class KeyguardDisabledFeaturesActivity extends DialogTestListActivity {
40 
41     protected DevicePolicyManager mDpm;
42 
KeyguardDisabledFeaturesActivity()43     public KeyguardDisabledFeaturesActivity() {
44         super(R.layout.provisioning_byod,
45                 R.string.provisioning_byod_keyguard_disabled_features,
46                 R.string.provisioning_byod_keyguard_disabled_features_info,
47                 R.string.provisioning_byod_keyguard_disabled_features_instruction);
48     }
49 
getKeyguardDisabledFeatures()50     protected int getKeyguardDisabledFeatures() {
51         return DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS
52                 | DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT
53                 | DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS;
54     }
55 
56     @Override
onCreate(Bundle savedInstanceState)57     protected void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59 
60         mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
61 
62         mPrepareTestButton.setText(
63                 R.string.provisioning_byod_keyguard_disabled_features_prepare_button);
64         mPrepareTestButton.setOnClickListener(new OnClickListener() {
65                 @Override
66                 public void onClick(View v) {
67                     if (!mDpm.isAdminActive(getAdminComponent())) {
68                         Toast.makeText(KeyguardDisabledFeaturesActivity.this,
69                                 R.string.provisioning_byod_keyguard_disabled_features_not_admin,
70                                 Toast.LENGTH_SHORT).show();
71                         return;
72                     }
73                     setKeyguardDisabledFeatures();
74                 }
75             });
76     }
77 
getAdminComponent()78     protected ComponentName getAdminComponent() {
79         return DeviceAdminTestReceiver.getReceiverComponentName();
80     }
81 
getTestIdPrefix()82     protected String getTestIdPrefix() {
83         return "BYOD_";
84     }
85 
86     @Override
finish()87     public void finish() {
88         // Pass and fail buttons are known to call finish() when clicked, and this is when we want to
89         // clear the password.
90         final ComponentName adminComponent = getAdminComponent();
91         if (mDpm.isAdminActive(adminComponent)) {
92             mDpm.removeActiveAdmin(adminComponent);
93         }
94         super.finish();
95     }
96 
setKeyguardDisabledFeatures()97     protected void setKeyguardDisabledFeatures() {
98         int flags = getKeyguardDisabledFeatures();
99         Intent setKeyguardDisabledFeaturesIntent = new Intent(
100                 ByodHelperActivity.ACTION_KEYGUARD_DISABLED_FEATURES)
101                         .putExtra(ByodHelperActivity.EXTRA_PARAMETER_1, flags);
102         startActivity(setKeyguardDisabledFeaturesIntent);
103     }
104 
setupDisableTrustAgentsTest(ArrayTestListAdapter adapter)105     protected void setupDisableTrustAgentsTest(ArrayTestListAdapter adapter) {
106         adapter.add(new DialogTestListItem(this, R.string.provisioning_byod_disable_trust_agents,
107                 getTestIdPrefix() + "DisableTrustAgentsTest",
108                 R.string.provisioning_byod_disable_trust_agents_instruction,
109                 new Intent(Settings.ACTION_SECURITY_SETTINGS)));
110     }
111 
setupDisableUnredactedWorkNotification(ArrayTestListAdapter adapter)112     protected void setupDisableUnredactedWorkNotification(ArrayTestListAdapter adapter) {
113         adapter.add(new DialogTestListItemWithIcon(this,
114                 R.string.provisioning_byod_disable_unredacted_notifications,
115                 getTestIdPrefix() + "DisableUnredactedNotifications",
116                 R.string.provisioning_byod_disable_unredacted_notifications_instruction,
117                 new Intent(ByodHelperActivity.ACTION_NOTIFICATION_ON_LOCKSCREEN),
118                 R.drawable.ic_corp_icon));
119     }
120 
setupFingerprintTests(ArrayTestListAdapter adapter)121     protected void setupFingerprintTests(ArrayTestListAdapter adapter) {
122         FingerprintManager fpm = (FingerprintManager) getSystemService(Context.FINGERPRINT_SERVICE);
123         if (fpm != null && fpm.isHardwareDetected()) {
124             adapter.add(new DialogTestListItem(this,
125                     R.string.provisioning_byod_fingerprint_disabled_in_settings,
126                     getTestIdPrefix() + "FingerprintDisabledInSettings",
127                     R.string.provisioning_byod_fingerprint_disabled_in_settings_instruction,
128                     new Intent(Settings.ACTION_SECURITY_SETTINGS)));
129             adapter.add(new DialogTestListItem(this, R.string.provisioning_byod_disable_fingerprint,
130                     getTestIdPrefix() + "DisableFingerprint",
131                     R.string.provisioning_byod_disable_fingerprint_instruction,
132                     ByodHelperActivity.createLockIntent()));
133         }
134     }
135 
136     @Override
setupTests(ArrayTestListAdapter adapter)137     protected void setupTests(ArrayTestListAdapter adapter) {
138         if (hasTrustAgents()) {
139             setupDisableTrustAgentsTest(adapter);
140         }
141         setupDisableUnredactedWorkNotification(adapter);
142         setupFingerprintTests(adapter);
143     }
144 
hasTrustAgents()145     private boolean hasTrustAgents() {
146         PackageManager packageManager = getPackageManager();
147         Intent intent = new Intent("android.service.trust.TrustAgentService");
148         List<ResolveInfo> resolveInfos = packageManager.queryIntentServices(intent, 0);
149         return resolveInfos.size() > 0;
150     }
151 
152     @Override
clearRemainingState(final DialogTestListItem test)153     protected void clearRemainingState(final DialogTestListItem test) {
154         super.clearRemainingState(test);
155         if (ByodHelperActivity.ACTION_NOTIFICATION_ON_LOCKSCREEN.equals(
156                 test.getManualTestIntent().getAction())) {
157             try {
158                 startActivity(new Intent(
159                         ByodHelperActivity.ACTION_CLEAR_NOTIFICATION));
160             } catch (ActivityNotFoundException e) {
161                 // User shouldn't run this test before work profile is set up.
162             }
163         }
164     }
165 }
166