am 6e761c5d: Merge "Device policy: use owner label instead of name" into mnc-dev

* commit '6e761c5d6b8ed2126c6544ce32db569203c46e2a':
  Device policy: use owner label instead of name
This commit is contained in:
Robin Lee
2015-05-01 13:08:45 +00:00
committed by Android Git Automerger
2 changed files with 37 additions and 13 deletions

View File

@@ -2926,7 +2926,7 @@ public class DevicePolicyManager {
* the user has already been set up. * the user has already been set up.
*/ */
@SystemApi @SystemApi
public boolean setActiveProfileOwner(ComponentName admin, String ownerName) public boolean setActiveProfileOwner(ComponentName admin, @Deprecated String ownerName)
throws IllegalArgumentException { throws IllegalArgumentException {
if (mService != null) { if (mService != null) {
try { try {
@@ -2992,8 +2992,8 @@ public class DevicePolicyManager {
* @throws IllegalArgumentException if admin is null, the package isn't installed, or the * @throws IllegalArgumentException if admin is null, the package isn't installed, or the
* preconditions mentioned are not met. * preconditions mentioned are not met.
*/ */
public boolean setProfileOwner(ComponentName admin, String ownerName, int userHandle) public boolean setProfileOwner(ComponentName admin, @Deprecated String ownerName,
throws IllegalArgumentException { int userHandle) throws IllegalArgumentException {
if (admin == null) { if (admin == null) {
throw new NullPointerException("admin cannot be null"); throw new NullPointerException("admin cannot be null");
} }

View File

@@ -4065,11 +4065,12 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
} }
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USERS, null); mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USERS, null);
synchronized (this) { synchronized (this) {
if (mDeviceOwner != null) { if (mDeviceOwner == null || !mDeviceOwner.hasDeviceOwner()) {
return mDeviceOwner.getDeviceOwnerName(); return null;
} }
String deviceOwnerPackage = mDeviceOwner.getDeviceOwnerPackageName();
return getApplicationLabel(deviceOwnerPackage, UserHandle.USER_OWNER);
} }
return null;
} }
// Returns the active device owner or null if there is no device owner. // Returns the active device owner or null if there is no device owner.
@@ -4426,7 +4427,6 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
if (profileOwner == null) { if (profileOwner == null) {
return null; return null;
} }
DevicePolicyData policy = getUserData(userHandle); DevicePolicyData policy = getUserData(userHandle);
final int n = policy.mAdminList.size(); final int n = policy.mAdminList.size();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
@@ -4444,13 +4444,37 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
return null; return null;
} }
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USERS, null); mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USERS, null);
ComponentName profileOwner = getProfileOwner(userHandle);
synchronized (this) { if (profileOwner == null) {
if (mDeviceOwner != null) { return null;
return mDeviceOwner.getProfileOwnerName(userHandle); }
} return getApplicationLabel(profileOwner.getPackageName(), userHandle);
}
/**
* Canonical name for a given package.
*/
private String getApplicationLabel(String packageName, int userHandle) {
long token = Binder.clearCallingIdentity();
try {
final Context userContext;
try {
UserHandle handle = new UserHandle(userHandle);
userContext = mContext.createPackageContextAsUser(packageName, 0, handle);
} catch (PackageManager.NameNotFoundException nnfe) {
Log.w(LOG_TAG, packageName + " is not installed for user " + userHandle, nnfe);
return null;
}
ApplicationInfo appInfo = userContext.getApplicationInfo();
CharSequence result = null;
if (appInfo != null) {
PackageManager pm = userContext.getPackageManager();
result = pm.getApplicationLabel(appInfo);
}
return result != null ? result.toString() : null;
} finally {
Binder.restoreCallingIdentity(token);
} }
return null;
} }
/** /**