Merge "Fixes COMMAND_SET_MAXIMUM_PASSWORD_ATTEMPTS for automotive" into sc-v2-dev
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/CommandReceiverActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/CommandReceiverActivity.java
index 05230ea..e0f4342 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/CommandReceiverActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/CommandReceiverActivity.java
@@ -474,32 +474,25 @@
                     mDpm.uninstallCaCert(mAdmin, TEST_CA.getBytes());
                 } break;
                 case COMMAND_SET_MAXIMUM_PASSWORD_ATTEMPTS: {
-                    if (!mDpm.isDeviceOwnerApp(getPackageName())) {
-                        return;
-                    }
-                    mDpm.setMaximumFailedPasswordsForWipe(mAdmin, 100);
+                    if (!isDeviceOwner()) return;
+                    int max = 100;
+                    Log.d(TAG, "Setting maximum password attempts to " + max + " using" + mDpm);
+                    mDpm.setMaximumFailedPasswordsForWipe(mAdmin, max);
                 } break;
                 case COMMAND_CLEAR_MAXIMUM_PASSWORD_ATTEMPTS: {
-                    if (!mDpm.isDeviceOwnerApp(getPackageName())) {
-                        return;
-                    }
+                    if (!isDeviceOwner()) return;
+                    Log.d(TAG, "Clearing maximum password attempts using" + mDpm);
                     mDpm.setMaximumFailedPasswordsForWipe(mAdmin, 0);
                 } break;
                 case COMMAND_SET_DEFAULT_IME: {
-                    if (!mDpm.isDeviceOwnerApp(getPackageName())
-                            && !UserManager.isHeadlessSystemUserMode()) {
-                        return;
-                    }
+                    if (!isDeviceOwner()) return;
                     Log.d(TAG, "Setting " + Settings.Secure.DEFAULT_INPUT_METHOD + " using "
                             + mDpm);
                     mDpm.setSecureSetting(mAdmin, Settings.Secure.DEFAULT_INPUT_METHOD,
                             getPackageName());
                 } break;
                 case COMMAND_CLEAR_DEFAULT_IME: {
-                    if (!mDpm.isDeviceOwnerApp(getPackageName())
-                            && !UserManager.isHeadlessSystemUserMode()) {
-                        return;
-                    }
+                    if (!isDeviceOwner()) return;
                     Log.d(TAG, "Clearing " + Settings.Secure.DEFAULT_INPUT_METHOD + " using "
                             + mDpm);
                     mDpm.setSecureSetting(mAdmin, Settings.Secure.DEFAULT_INPUT_METHOD, null);
@@ -577,6 +570,19 @@
         }
     }
 
+    /**
+     * Checks if {@code CtsVerifier} is the device owner.
+     */
+    private boolean isDeviceOwner() {
+        // Cannot use mDpm as it would be the DPM of the current user on headless system user mode,
+        // which would return false
+        DevicePolicyManager dpm = TestAppSystemServiceFactory.getDevicePolicyManager(this,
+                DeviceAdminTestReceiver.class, /* forDeviceOwner= */ true);
+        boolean isIt = dpm.isDeviceOwnerApp(getPackageName());
+        Log.v(TAG, "is " + getPackageName() + " DO, using " + dpm + "? " + isIt);
+        return isIt;
+    }
+
     private void installHelperPackage() throws Exception {
         LogAndSelfUnregisterBroadcastReceiver.register(this, ACTION_INSTALL_COMPLETE);
         final PackageInstaller packageInstaller = getPackageManager().getPackageInstaller();
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/EnterprisePrivacyTestListActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/EnterprisePrivacyTestListActivity.java
index 577a6c8..623f85b 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/EnterprisePrivacyTestListActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/EnterprisePrivacyTestListActivity.java
@@ -239,10 +239,10 @@
                             new ButtonInfo(R.string.enterprise_privacy_open_settings,
                                     new Intent(Settings.ACTION_ENTERPRISE_PRIVACY_SETTINGS)),
                             new ButtonInfo(R.string.enterprise_privacy_set_limit,
-                                    buildCommandIntent(CommandReceiverActivity
+                                    buildCommandIntentForCurrentUser(CommandReceiverActivity
                                             .COMMAND_SET_MAXIMUM_PASSWORD_ATTEMPTS)),
                             new ButtonInfo(R.string.enterprise_privacy_finish,
-                                    buildCommandIntent(CommandReceiverActivity
+                                    buildCommandIntentForCurrentUser(CommandReceiverActivity
                                             .COMMAND_CLEAR_MAXIMUM_PASSWORD_ATTEMPTS))}));
         }
         adapter.add(createInteractiveTestItem(this, ENTERPRISE_PRIVACY_QUICK_SETTINGS,