Provide api to set the managed profile name.

Instead of sending the profile name in the provisioning intent
the mdm should set the profile name after provisioning has completed.
This allows us to simplify the provisioning flow and the mdm can
change the name of the profile later on if required.

Change-Id: I821ef2300eae74e89872152ae1c89ac3ecbb82e7
diff --git a/api/current.txt b/api/current.txt
index e9a44e2..31d308c 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -5304,6 +5304,7 @@
     method public void setPasswordMinimumUpperCase(android.content.ComponentName, int);
     method public void setPasswordQuality(android.content.ComponentName, int);
     method public void setProfileEnabled(android.content.ComponentName);
+    method public void setProfileName(android.content.ComponentName, java.lang.String);
     method public void setRecommendedGlobalProxy(android.content.ComponentName, android.net.ProxyInfo);
     method public void setRestrictionsProvider(android.content.ComponentName, android.content.ComponentName);
     method public void setSecureSetting(android.content.ComponentName, java.lang.String, java.lang.String);
diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java
index 4351f9d..9ed298e 100644
--- a/core/java/android/app/admin/DevicePolicyManager.java
+++ b/core/java/android/app/admin/DevicePolicyManager.java
@@ -1922,6 +1922,26 @@
     }
 
     /**
+     * Sets the name of the Managed profile. In the device owner case it sets the name of the user
+     * which it is called from. Only the profile owner or device owner can call this. If this is
+     * never called by the profile or device owner, the name will be set to default values.
+     *
+     * @see #isProfileOwnerApp
+     * @see #isDeviceOwnerApp
+     *
+     * @param profileName The name of the profile.
+     */
+    public void setProfileName(ComponentName who, String profileName) {
+        if (mService != null) {
+            try {
+            mService.setProfileName(who, profileName);
+        } catch (RemoteException e) {
+            Log.w(TAG, "Failed talking with device policy service", e);
+        }
+    }
+}
+
+    /**
      * Used to determine if a particular package is registered as the Profile Owner for the
      * current user. A profile owner is a special device admin that has additional privileges
      * within the managed profile.
diff --git a/core/java/android/app/admin/IDevicePolicyManager.aidl b/core/java/android/app/admin/IDevicePolicyManager.aidl
index 5333ea6..8901e43 100644
--- a/core/java/android/app/admin/IDevicePolicyManager.aidl
+++ b/core/java/android/app/admin/IDevicePolicyManager.aidl
@@ -114,6 +114,7 @@
     String getProfileOwner(int userHandle);
     String getProfileOwnerName(int userHandle);
     void setProfileEnabled(in ComponentName who);
+    void setProfileName(in ComponentName who, String profileName);
 
     boolean installCaCert(in ComponentName admin, in byte[] certBuffer);
     void uninstallCaCert(in ComponentName admin, in String alias);
diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
index 893a891..9c12937 100644
--- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
+++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
@@ -3222,7 +3222,6 @@
             // Check if this is the profile owner who is calling
             getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER);
             int userId = UserHandle.getCallingUserId();
-            Slog.d(LOG_TAG, "Enabling the profile for: " + userId);
 
             long id = Binder.clearCallingIdentity();
             try {
@@ -3240,6 +3239,25 @@
     }
 
     @Override
+    public void setProfileName(ComponentName who, String profileName) {
+        int userId = UserHandle.getCallingUserId();
+
+        if (who == null) {
+            throw new NullPointerException("ComponentName is null");
+        }
+
+        // Check if this is the profile owner (includes device owner).
+        getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER);
+
+        long id = Binder.clearCallingIdentity();
+        try {
+            mUserManager.setUserName(userId, profileName);
+        } finally {
+            restoreCallingIdentity(id);
+        }
+    }
+
+    @Override
     public String getProfileOwner(int userHandle) {
         if (!mHasFeature) {
             return null;