Added getUserCreationTime to query user/profile creation time

Added public api to query creation time of the user or of a managed profile
associated with the calling user.

Bug: 20049349
Change-Id: I7f9263fe434233e6f7d4f165c974cab64ca7107c
diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java
index a8c5527..e79a2061 100644
--- a/services/core/java/com/android/server/pm/UserManagerService.java
+++ b/services/core/java/com/android/server/pm/UserManagerService.java
@@ -327,16 +327,20 @@
     public UserInfo getProfileParent(int userHandle) {
         checkManageUsersPermission("get the profile parent");
         synchronized (mPackagesLock) {
-            UserInfo profile = getUserInfoLocked(userHandle);
-            if (profile == null) {
-                return null;
-            }
-            int parentUserId = profile.profileGroupId;
-            if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) {
-                return null;
-            } else {
-                return getUserInfoLocked(parentUserId);
-            }
+            return getProfileParentLocked(userHandle);
+        }
+    }
+
+    private UserInfo getProfileParentLocked(int userHandle) {
+        UserInfo profile = getUserInfoLocked(userHandle);
+        if (profile == null) {
+            return null;
+        }
+        int parentUserId = profile.profileGroupId;
+        if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) {
+            return null;
+        } else {
+            return getUserInfoLocked(parentUserId);
         }
     }
 
@@ -1867,6 +1871,27 @@
         }
     }
 
+    @Override
+    public long getUserCreationTime(int userHandle) {
+        int callingUserId = UserHandle.getCallingUserId();
+        UserInfo userInfo = null;
+        synchronized (mPackagesLock) {
+            if (callingUserId == userHandle) {
+                userInfo = getUserInfoLocked(userHandle);
+            } else {
+                UserInfo parent = getProfileParentLocked(userHandle);
+                if (parent != null && parent.id == callingUserId) {
+                    userInfo = getUserInfoLocked(userHandle);
+                }
+            }
+        }
+        if (userInfo == null) {
+            throw new SecurityException("userHandle can only be the calling user or a managed "
+                    + "profile associated with this user");
+        }
+        return userInfo.creationTime;
+    }
+
     /**
      * Caches the list of user ids in an array, adjusting the array size when necessary.
      */