Ask user to set CtsVerifier as active admin for KeyguardDisabledFeatures test

Otherwise resetPassword would not work.

Bug:23415505
Change-Id: Id8d465f361ac03bebe19b7645f67a363c59e6682
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index c137c5a..7e9cc6a 100644
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -1453,11 +1453,15 @@
         This test exercises Keyguard Disabled Features. Follow instructions above.
     </string>
     <string name="provisioning_byod_keyguard_disabled_features_instruction">
-        Please press the \"Prepare test\" button to disable trust agents.\n
+        Please go to Settings &gt; Security &gt; Device administrators and set
+        \"CTS Verifier - AfW Admin\" as active admin.\n
+        After that please press the \"Prepare test\" button to disable trust agents.\n
         Then please press through the following verification steps.\n
-        Note: Device password will be set to \"testpassword\". After leaving the screen device password be cleared.
+        Note: Device password will be set to \"testpassword\". After leaving the screen device
+        password and active admin status will be cleared.
     </string>
     <string name="provisioning_byod_keyguard_disabled_features_prepare_button">Prepare test</string>
+    <string name="provisioning_byod_keyguard_disabled_features_not_admin">CtsVerifier is not active admin. Please follow instructions.</string>
     <string name="provisioning_byod_disable_trust_agents">Disable trust agents</string>
     <string name="provisioning_byod_disable_trust_agents_instruction">
         Please press the Go button to go to Settings > Security. Then go to Trusted agents and\n
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/KeyguardDisabledFeaturesActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/KeyguardDisabledFeaturesActivity.java
index 0fdc498..1a158f8 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/KeyguardDisabledFeaturesActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/KeyguardDisabledFeaturesActivity.java
@@ -18,6 +18,7 @@
 
 import android.app.admin.DevicePolicyManager;
 import android.content.ActivityNotFoundException;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.hardware.fingerprint.FingerprintManager;
@@ -25,6 +26,7 @@
 import android.provider.Settings;
 import android.view.View;
 import android.view.View.OnClickListener;
+import android.widget.Toast;
 
 import com.android.cts.verifier.ArrayTestListAdapter;
 import com.android.cts.verifier.DialogTestListActivity;
@@ -32,6 +34,8 @@
 
 public class KeyguardDisabledFeaturesActivity extends DialogTestListActivity {
 
+    private DevicePolicyManager mDpm;
+
     public KeyguardDisabledFeaturesActivity() {
         super(R.layout.provisioning_byod,
                 R.string.provisioning_byod_keyguard_disabled_features,
@@ -43,12 +47,20 @@
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
+        mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
+
         mPrepareTestButton.setText(
                 R.string.provisioning_byod_keyguard_disabled_features_prepare_button);
         mPrepareTestButton.setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View v) {
-                    resetPassword("testpassword");
+                    if (!mDpm.isAdminActive(DeviceAdminTestReceiver.getReceiverComponentName())) {
+                        Toast.makeText(KeyguardDisabledFeaturesActivity.this,
+                                R.string.provisioning_byod_keyguard_disabled_features_not_admin,
+                                Toast.LENGTH_SHORT).show();
+                        return;
+                    }
+                    mDpm.resetPassword("testpassword", 0);
                     setKeyguardDisabledFeatures(DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS |
                             DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT |
                             DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
@@ -60,7 +72,11 @@
     public void finish() {
         // Pass and fail buttons are known to call finish() when clicked, and this is when we want to
         // clear the password.
-        resetPassword(null);
+        final ComponentName adminComponent = DeviceAdminTestReceiver.getReceiverComponentName();
+        if (mDpm.isAdminActive(adminComponent)) {
+            mDpm.resetPassword(null, 0);
+            mDpm.removeActiveAdmin(adminComponent);
+        }
         super.finish();
     }
 
@@ -71,16 +87,6 @@
         startActivity(setKeyguardDisabledFeaturesIntent);
     }
 
-    /**
-     * Reset device password
-     * @param password password to reset to (may be null)
-     */
-    private void resetPassword(String password) {
-        DevicePolicyManager dpm = (DevicePolicyManager)
-                getSystemService(Context.DEVICE_POLICY_SERVICE);
-        dpm.resetPassword(password, 0);
-    }
-
     @Override
     protected void setupTests(ArrayTestListAdapter adapter) {
         adapter.add(new DialogTestListItem(this, R.string.provisioning_byod_disable_trust_agents,