sys user split: fix primary flag bug.

When the end user first login as Guest, and the device rebooted during
the session (Guest user not removed), then the first user created
afterwards would not be correctly marked as primary.  We need to
explicitly looks through all current users to be able to tell for
sure.

Bug: 19913735
Change-Id: Iab1160c17c8dfdc054fd5dd136435fe2711dfa0c
diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java
index c96373b..eb21098 100644
--- a/services/core/java/com/android/server/pm/UserManagerService.java
+++ b/services/core/java/com/android/server/pm/UserManagerService.java
@@ -20,6 +20,7 @@
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
 import android.app.IStopUserCallback;
+import android.app.admin.DevicePolicyManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -1262,12 +1263,15 @@
                         return null;
                     }
                     // In split system user mode, we assign the first human user the primary flag.
-                    // And if the only system user is not admin, we also assign the admin flag to
-                    // primary user.
+                    // And if there is no device owner, we also assign the admin flag to primary
+                    // user.
                     if (UserManager.isSplitSystemUser()
-                            && !isGuest && !isManagedProfile && mUsers.size() == 1) {
+                            && !isGuest && !isManagedProfile && getPrimaryUser() == null) {
                         flags |= UserInfo.FLAG_PRIMARY;
-                        if (!mUsers.get(UserHandle.USER_SYSTEM).isAdmin()) {
+                        DevicePolicyManager devicePolicyManager = (DevicePolicyManager)
+                                mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
+                        if (devicePolicyManager == null
+                                || devicePolicyManager.getDeviceOwner() == null) {
                             flags |= UserInfo.FLAG_ADMIN;
                         }
                     }