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.AlertDialog;
20 import android.app.admin.DevicePolicyManager;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.provider.Settings;
27 import android.view.View;
28 
29 import com.android.cts.verifier.ArrayTestListAdapter;
30 import com.android.cts.verifier.DialogTestListActivity;
31 import com.android.cts.verifier.R;
32 import com.android.cts.verifier.TestResult;
33 
34 /**
35  * This test verifies that if a work profile is locked with a separate password, Recents views for
36  * for applications in the work profile are redacted appropriately.
37  */
38 public class RecentsRedactionActivity extends DialogTestListActivity {
39     private static final String TAG = RecentsRedactionActivity.class.getSimpleName();
40 
41     public static final String ACTION_RECENTS =
42             "com.android.cts.verifier.managedprovisioning.RECENTS";
43 
44     private ComponentName mAdmin;
45     private DevicePolicyManager mDevicePolicyManager;
46 
47     private DialogTestListItem mVerifyRedacted;
48     private DialogTestListItem mVerifyNotRedacted;
49 
RecentsRedactionActivity()50     public RecentsRedactionActivity() {
51         super(R.layout.provisioning_byod,
52                 /* title */ R.string.provisioning_byod_recents,
53                 /* description */ R.string.provisioning_byod_recents_info,
54                 /* instructions */ R.string.provisioning_byod_recents_instructions);
55     }
56 
57     // Default listener will use setResult(), which won't work due to activity being in a new task.
58     private View.OnClickListener clickListener = target -> {
59         final int resultCode;
60         int id = target.getId();
61         if (id == R.id.pass_button) {
62             resultCode = TestResult.TEST_RESULT_PASSED;
63         } else if (id == R.id.fail_button) {
64             resultCode = TestResult.TEST_RESULT_FAILED;
65         } else {
66             throw new IllegalArgumentException("Unknown id: " + target.getId());
67         }
68         Intent resultIntent = TestResult.createResult(RecentsRedactionActivity.this, resultCode,
69                 getTestId(), getTestDetails(), getReportLog(), getHistoryCollection());
70 
71         new ByodFlowTestHelper(this).sendResultToPrimary(resultIntent);
72         finish();
73     };
74 
75     @Override
onCreate(Bundle savedInstanceState)76     protected void onCreate(Bundle savedInstanceState) {
77         super.onCreate(savedInstanceState);
78 
79         findViewById(R.id.pass_button).setOnClickListener(clickListener);
80         findViewById(R.id.fail_button).setOnClickListener(clickListener);
81 
82         mPrepareTestButton.setText(R.string.provisioning_byod_recents_lock_now);
83         mPrepareTestButton.setOnClickListener((View view) -> {
84             mDevicePolicyManager.lockNow();
85         });
86 
87         mAdmin = new ComponentName(this, DeviceAdminTestReceiver.class.getName());
88         mDevicePolicyManager = (DevicePolicyManager)
89                 getSystemService(Context.DEVICE_POLICY_SERVICE);
90     }
91 
92     @Override
setupTests(ArrayTestListAdapter adapter)93     protected void setupTests(ArrayTestListAdapter adapter) {
94         mVerifyRedacted = new DialogTestListItem(this,
95                 /* name */ R.string.provisioning_byod_recents_verify_redacted,
96                 /* testId */ "BYOD_recents_verify_redacted",
97                 /* instruction */ R.string.provisioning_byod_recents_verify_redacted_instruction,
98                 /* action */ new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD));
99 
100         mVerifyNotRedacted = new DialogTestListItem(this,
101                 /* name */ R.string.provisioning_byod_recents_verify_not_redacted,
102                 /* testId */ "BYOD_recents_verify_not_redacted",
103                 /* intruction */ R.string.provisioning_byod_recents_verify_not_redacted_instruction,
104                 /* action */ new Intent(Settings.ACTION_SECURITY_SETTINGS));
105 
106         adapter.add(mVerifyRedacted);
107         adapter.add(mVerifyNotRedacted);
108     }
109 
110     @Override
onBackPressed()111     public void onBackPressed() {
112         if (hasPassword()) {
113             requestRemovePassword();
114             return;
115         }
116         super.onBackPressed();
117     }
118 
hasPassword()119     private boolean hasPassword() {
120         try {
121             mDevicePolicyManager.setPasswordQuality(mAdmin,
122                     DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
123             return mDevicePolicyManager.isActivePasswordSufficient();
124         } finally {
125             mDevicePolicyManager.setPasswordQuality(mAdmin,
126                     DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
127         }
128     }
129 
requestRemovePassword()130     private void requestRemovePassword() {
131         new AlertDialog.Builder(this)
132                 .setIcon(android.R.drawable.ic_dialog_info)
133                 .setTitle(R.string.provisioning_byod_recents)
134                 .setMessage(R.string.provisioning_byod_recents_remove_password)
135                 .setPositiveButton(android.R.string.ok, (DialogInterface dialog, int which) -> {
136                     startActivity(new Intent(Settings.ACTION_SECURITY_SETTINGS));
137                 })
138                 .show();
139     }
140 }
141