Calling method in DPMS after resolving userId

ActivityManagerService was calling
hasDeviceOwnerOrProfileOwner in DevicePolicyManagerService.LocalService
before resolving negative userIds to a real user. Moved the call to a point
when userId is already resolved.

bug: b/27538249
Change-Id: I760412978b3260523e9abcfe59d8e100ec4af12f
diff --git a/core/java/android/app/admin/DevicePolicyManagerInternal.java b/core/java/android/app/admin/DevicePolicyManagerInternal.java
index 8cdfee5..61b40d4 100644
--- a/core/java/android/app/admin/DevicePolicyManagerInternal.java
+++ b/core/java/android/app/admin/DevicePolicyManagerInternal.java
@@ -16,6 +16,8 @@
 
 package android.app.admin;
 
+import android.os.UserHandle;
+
 import java.util.List;
 
 /**
@@ -71,8 +73,8 @@
     public abstract boolean isActiveAdminWithPolicy(int uid, int reqPolicy);
 
     /**
-     * Checks if a given package has a device or a profile owner for the given user
-     *
+     * Checks if a given package has a device or a profile owner for the given user.
+     * </br><em>Does <b>not</b> support negative userIds like {@link UserHandle#USER_ALL}</em>
      * @param packageName The package to check
      * @param userId the userId to check for.
      * @return true if package has a device or profile owner, false otherwise.
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index f07c1d0..7f30ae1 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -5270,6 +5270,10 @@
     public boolean clearApplicationUserData(final String packageName,
             final IPackageDataObserver observer, int userId) {
         enforceNotIsolatedCaller("clearApplicationUserData");
+        int uid = Binder.getCallingUid();
+        int pid = Binder.getCallingPid();
+        userId = mUserController.handleIncomingUser(pid, uid, userId, false,
+                ALLOW_FULL_ONLY, "clearApplicationUserData", null);
 
         final DevicePolicyManagerInternal dpmi = LocalServices
                 .getService(DevicePolicyManagerInternal.class);
@@ -5277,10 +5281,6 @@
             throw new SecurityException("Cannot clear data for a device owner or a profile owner");
         }
 
-        int uid = Binder.getCallingUid();
-        int pid = Binder.getCallingPid();
-        userId = mUserController.handleIncomingUser(pid, uid, userId, false,
-                ALLOW_FULL_ONLY, "clearApplicationUserData", null);
         long callingId = Binder.clearCallingIdentity();
         try {
             IPackageManager pm = AppGlobals.getPackageManager();