Merge "Mark clearDeviceOwnerApp/clearProfileOwner as deprecated"
diff --git a/api/current.txt b/api/current.txt
index 63295e0..2552fa9 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -6200,9 +6200,9 @@
     method public void addUserRestriction(android.content.ComponentName, java.lang.String);
     method public boolean bindDeviceAdminServiceAsUser(android.content.ComponentName, android.content.Intent, android.content.ServiceConnection, int, android.os.UserHandle);
     method public void clearCrossProfileIntentFilters(android.content.ComponentName);
-    method public void clearDeviceOwnerApp(java.lang.String);
+    method public deprecated void clearDeviceOwnerApp(java.lang.String);
     method public void clearPackagePersistentPreferredActivities(android.content.ComponentName, java.lang.String);
-    method public void clearProfileOwner(android.content.ComponentName);
+    method public deprecated void clearProfileOwner(android.content.ComponentName);
     method public boolean clearResetPasswordToken(android.content.ComponentName);
     method public void clearUserRestriction(android.content.ComponentName, java.lang.String);
     method public android.content.Intent createAdminSupportIntent(java.lang.String);
diff --git a/api/system-current.txt b/api/system-current.txt
index baaf82b..2b2e448 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -6405,9 +6405,9 @@
     method public void addUserRestriction(android.content.ComponentName, java.lang.String);
     method public boolean bindDeviceAdminServiceAsUser(android.content.ComponentName, android.content.Intent, android.content.ServiceConnection, int, android.os.UserHandle);
     method public void clearCrossProfileIntentFilters(android.content.ComponentName);
-    method public void clearDeviceOwnerApp(java.lang.String);
+    method public deprecated void clearDeviceOwnerApp(java.lang.String);
     method public void clearPackagePersistentPreferredActivities(android.content.ComponentName, java.lang.String);
-    method public void clearProfileOwner(android.content.ComponentName);
+    method public deprecated void clearProfileOwner(android.content.ComponentName);
     method public boolean clearResetPasswordToken(android.content.ComponentName);
     method public void clearUserRestriction(android.content.ComponentName, java.lang.String);
     method public android.content.Intent createAdminSupportIntent(java.lang.String);
diff --git a/api/test-current.txt b/api/test-current.txt
index b1be93b..248d265 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -6217,9 +6217,9 @@
     method public void addUserRestriction(android.content.ComponentName, java.lang.String);
     method public boolean bindDeviceAdminServiceAsUser(android.content.ComponentName, android.content.Intent, android.content.ServiceConnection, int, android.os.UserHandle);
     method public void clearCrossProfileIntentFilters(android.content.ComponentName);
-    method public void clearDeviceOwnerApp(java.lang.String);
+    method public deprecated void clearDeviceOwnerApp(java.lang.String);
     method public void clearPackagePersistentPreferredActivities(android.content.ComponentName, java.lang.String);
-    method public void clearProfileOwner(android.content.ComponentName);
+    method public deprecated void clearProfileOwner(android.content.ComponentName);
     method public boolean clearResetPasswordToken(android.content.ComponentName);
     method public void clearUserRestriction(android.content.ComponentName, java.lang.String);
     method public android.content.Intent createAdminSupportIntent(java.lang.String);
diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java
index 979702a..2ace0a2 100644
--- a/core/java/android/app/admin/DevicePolicyManager.java
+++ b/core/java/android/app/admin/DevicePolicyManager.java
@@ -4545,12 +4545,22 @@
     /**
      * Clears the current device owner. The caller must be the device owner. This function should be
      * used cautiously as once it is called it cannot be undone. The device owner can only be set as
-     * a part of device setup before setup completes.
+     * a part of device setup, before it completes.
+     * <p>
+     * While some policies previously set by the device owner will be cleared by this method, it is
+     * a best-effort process and some other policies will still remain in place after the device
+     * owner is cleared.
      *
      * @param packageName The package name of the device owner.
      * @throws SecurityException if the caller is not in {@code packageName} or {@code packageName}
      *             does not own the current device owner component.
+     *
+     * @deprecated This method is expected to be used for testing purposes only. The device owner
+     * will lose control of the device and its data after calling it. In order to protect any
+     * sensitive data that remains on the device, it is advised that the device owner factory resets
+     * the device instead of calling this method. See {@link #wipeData(int)}.
      */
+    @Deprecated
     public void clearDeviceOwnerApp(String packageName) {
         throwIfParentInstance("clearDeviceOwnerApp");
         if (mService != null) {
@@ -4672,15 +4682,23 @@
     }
 
     /**
-     * Clears the active profile owner and removes all user restrictions. The caller must be from
-     * the same package as the active profile owner for this user, otherwise a SecurityException
-     * will be thrown.
+     * Clears the active profile owner. The caller must be the profile owner of this user, otherwise
+     * a SecurityException will be thrown. This method is not available to managed profile owners.
      * <p>
-     * This doesn't work for managed profile owners.
+     * While some policies previously set by the profile owner will be cleared by this method, it is
+     * a best-effort process and some other policies will still remain in place after the profile
+     * owner is cleared.
      *
      * @param admin The component to remove as the profile owner.
-     * @throws SecurityException if {@code admin} is not an active profile owner.
+     * @throws SecurityException if {@code admin} is not an active profile owner, or the method is
+     * being called from a managed profile.
+     *
+     * @deprecated This method is expected to be used for testing purposes only. The profile owner
+     * will lose control of the user and its data after calling it. In order to protect any
+     * sensitive data that remains on this user, it is advised that the profile owner deletes it
+     * instead of calling this method. See {@link #wipeData(int)}.
      */
+    @Deprecated
     public void clearProfileOwner(@NonNull ComponentName admin) {
         throwIfParentInstance("clearProfileOwner");
         if (mService != null) {
diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
index b76cadc..e496666 100644
--- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
+++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
@@ -6647,14 +6647,16 @@
         if (!mHasFeature) {
             return;
         }
-        final UserHandle callingUser = mInjector.binderGetCallingUserHandle();
-        final int userId = callingUser.getIdentifier();
+        Preconditions.checkNotNull(who, "ComponentName is null");
+
+        final int userId = mInjector.userHandleGetCallingUserId();
         enforceNotManagedProfile(userId, "clear profile owner");
         enforceUserUnlocked(userId);
-        // Check if this is the profile owner who is calling
-        final ActiveAdmin admin =
-                getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER);
         synchronized (this) {
+            // Check if this is the profile owner who is calling
+            final ActiveAdmin admin =
+                    getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER);
+
             final long ident = mInjector.binderClearCallingIdentity();
             try {
                 clearProfileOwnerLocked(admin, userId);