diff options
Diffstat (limited to 'services/devicepolicy')
-rw-r--r-- | services/devicepolicy/java/com/android/server/devicepolicy/DeviceOwner.java | 66 | ||||
-rw-r--r-- | services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java | 64 |
2 files changed, 106 insertions, 24 deletions
diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DeviceOwner.java b/services/devicepolicy/java/com/android/server/devicepolicy/DeviceOwner.java index f1284d8..4b60c9f 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DeviceOwner.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DeviceOwner.java @@ -17,6 +17,7 @@ package com.android.server.devicepolicy; import android.app.AppGlobals; +import android.content.ComponentName; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; @@ -53,6 +54,7 @@ public class DeviceOwner { private static final String TAG_PROFILE_OWNER = "profile-owner"; private static final String ATTR_NAME = "name"; private static final String ATTR_PACKAGE = "package"; + private static final String ATTR_COMPONENT_NAME = "component"; private static final String ATTR_USERID = "userId"; private AtomicFile fileForWriting; @@ -100,6 +102,7 @@ public class DeviceOwner { } /** + * @deprecated Use a component name instead of package name * Creates an instance of the device owner object with the profile owner set. */ static DeviceOwner createWithProfileOwner(String packageName, String ownerName, int userId) { @@ -108,6 +111,15 @@ public class DeviceOwner { return owner; } + /** + * Creates an instance of the device owner object with the profile owner set. + */ + static DeviceOwner createWithProfileOwner(ComponentName admin, String ownerName, int userId) { + DeviceOwner owner = new DeviceOwner(); + owner.mProfileOwners.put(userId, new OwnerInfo(ownerName, admin)); + return owner; + } + String getDeviceOwnerPackageName() { return mDeviceOwner != null ? mDeviceOwner.packageName : null; } @@ -124,19 +136,36 @@ public class DeviceOwner { mDeviceOwner = null; } + /** + * @deprecated + */ void setProfileOwner(String packageName, String ownerName, int userId) { mProfileOwners.put(userId, new OwnerInfo(ownerName, packageName)); } + void setProfileOwner(ComponentName admin, String ownerName, int userId) { + mProfileOwners.put(userId, new OwnerInfo(ownerName, admin)); + } + void removeProfileOwner(int userId) { mProfileOwners.remove(userId); } + /** + * @deprecated Use getProfileOwnerComponent + * @param userId + * @return + */ String getProfileOwnerPackageName(int userId) { OwnerInfo profileOwner = mProfileOwners.get(userId); return profileOwner != null ? profileOwner.packageName : null; } + ComponentName getProfileOwnerComponent(int userId) { + OwnerInfo profileOwner = mProfileOwners.get(userId); + return profileOwner != null ? profileOwner.admin : null; + } + String getProfileOwnerName(int userId) { OwnerInfo profileOwner = mProfileOwners.get(userId); return profileOwner != null ? profileOwner.name : null; @@ -191,15 +220,23 @@ public class DeviceOwner { String tag = parser.getName(); if (tag.equals(TAG_DEVICE_OWNER)) { - mDeviceOwner = new OwnerInfo( - parser.getAttributeValue(null, ATTR_NAME), - parser.getAttributeValue(null, ATTR_PACKAGE)); + String name = parser.getAttributeValue(null, ATTR_NAME); + String packageName = parser.getAttributeValue(null, ATTR_PACKAGE); + mDeviceOwner = new OwnerInfo(name, packageName); } else if (tag.equals(TAG_PROFILE_OWNER)) { String profileOwnerPackageName = parser.getAttributeValue(null, ATTR_PACKAGE); String profileOwnerName = parser.getAttributeValue(null, ATTR_NAME); + String profileOwnerComponentStr = + parser.getAttributeValue(null, ATTR_COMPONENT_NAME); int userId = Integer.parseInt(parser.getAttributeValue(null, ATTR_USERID)); - mProfileOwners.put(userId, - new OwnerInfo(profileOwnerName, profileOwnerPackageName)); + OwnerInfo profileOwnerInfo; + if (profileOwnerComponentStr != null) { + profileOwnerInfo = new OwnerInfo(profileOwnerName, + ComponentName.unflattenFromString(profileOwnerComponentStr)); + } else { + profileOwnerInfo = new OwnerInfo(profileOwnerName, profileOwnerPackageName); + } + mProfileOwners.put(userId, profileOwnerInfo); } else { throw new XmlPullParserException( "Unexpected tag in device owner file: " + tag); @@ -230,9 +267,6 @@ public class DeviceOwner { if (mDeviceOwner != null) { out.startTag(null, TAG_DEVICE_OWNER); out.attribute(null, ATTR_PACKAGE, mDeviceOwner.packageName); - if (mDeviceOwner.packageName != null) { - out.attribute(null, ATTR_NAME, mDeviceOwner.packageName); - } out.endTag(null, TAG_DEVICE_OWNER); } @@ -240,9 +274,13 @@ public class DeviceOwner { if (mProfileOwners.size() > 0) { for (HashMap.Entry<Integer, OwnerInfo> owner : mProfileOwners.entrySet()) { out.startTag(null, TAG_PROFILE_OWNER); - out.attribute(null, ATTR_PACKAGE, owner.getValue().packageName); - out.attribute(null, ATTR_NAME, owner.getValue().name); + OwnerInfo ownerInfo = owner.getValue(); + out.attribute(null, ATTR_PACKAGE, ownerInfo.packageName); + out.attribute(null, ATTR_NAME, ownerInfo.name); out.attribute(null, ATTR_USERID, Integer.toString(owner.getKey())); + if (ownerInfo.admin != null) { + out.attribute(null, ATTR_COMPONENT_NAME, ownerInfo.admin.flattenToString()); + } out.endTag(null, TAG_PROFILE_OWNER); } } @@ -282,10 +320,18 @@ public class DeviceOwner { static class OwnerInfo { public String name; public String packageName; + public ComponentName admin; public OwnerInfo(String name, String packageName) { this.name = name; this.packageName = packageName; + this.admin = new ComponentName(packageName, ""); + } + + public OwnerInfo(String name, ComponentName admin) { + this.name = name; + this.admin = admin; + this.packageName = admin.getPackageName(); } } } diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index f49451e..15bea5a 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -905,7 +905,8 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { for (ActiveAdmin admin : candidates) { boolean ownsDevice = isDeviceOwner(admin.info.getPackageName()); boolean ownsProfile = (getProfileOwner(userHandle) != null - && getProfileOwner(userHandle).equals(admin.info.getPackageName())); + && getProfileOwner(userHandle).getPackageName() + .equals(admin.info.getPackageName())); if (reqPolicy == DeviceAdminInfo.USES_POLICY_DEVICE_OWNER) { if (ownsDevice) { @@ -3310,7 +3311,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { } @Override - public boolean setProfileOwner(String packageName, String ownerName, int userHandle) { + public boolean setProfileOwner(ComponentName who, String ownerName, int userHandle) { if (!mHasFeature) { return false; } @@ -3322,26 +3323,28 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { "Attempted to set profile owner for invalid userId: " + userHandle); } - if (packageName == null - || !DeviceOwner.isInstalledForUser(packageName, userHandle)) { - throw new IllegalArgumentException("Package name " + packageName + if (who == null + || !DeviceOwner.isInstalledForUser(who.getPackageName(), userHandle)) { + throw new IllegalArgumentException("Component " + who + " not installed for userId:" + userHandle); } synchronized (this) { - if (isUserSetupComplete(userHandle)) { + // Only SYSTEM_UID can override the userSetupComplete + if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID + && isUserSetupComplete(userHandle)) { throw new IllegalStateException( "Trying to set profile owner but user is already set-up."); } if (mDeviceOwner == null) { // Device owner state does not exist, create it. - mDeviceOwner = DeviceOwner.createWithProfileOwner(packageName, ownerName, + mDeviceOwner = DeviceOwner.createWithProfileOwner(who, ownerName, userHandle); mDeviceOwner.writeOwnerFile(); return true; } else { // Device owner already exists, update it. - mDeviceOwner.setProfileOwner(packageName, ownerName, userHandle); + mDeviceOwner.setProfileOwner(who, ownerName, userHandle); mDeviceOwner.writeOwnerFile(); return true; } @@ -3349,6 +3352,38 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { } @Override + public void clearProfileOwner(ComponentName who) { + if (!mHasFeature) { + return; + } + UserHandle callingUser = Binder.getCallingUserHandle(); + // Check if this is the profile owner who is calling + getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); + synchronized (this) { + long ident = Binder.clearCallingIdentity(); + try { + mUserManager.setUserRestrictions(new Bundle(), callingUser); + if (mDeviceOwner != null) { + mDeviceOwner.removeProfileOwner(callingUser.getIdentifier()); + mDeviceOwner.writeOwnerFile(); + } + } finally { + Binder.restoreCallingIdentity(ident); + } + } + } + + @Override + public boolean hasUserSetupCompleted() { + if (!mHasFeature) { + return true; + } + DevicePolicyData policy = getUserData(UserHandle.getCallingUserId()); + // If policy is null, return true, else check if the setup has completed. + return policy == null || policy.mUserSetupComplete; + } + + @Override public void setProfileEnabled(ComponentName who) { if (!mHasFeature) { return; @@ -3397,14 +3432,14 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { } @Override - public String getProfileOwner(int userHandle) { + public ComponentName getProfileOwner(int userHandle) { if (!mHasFeature) { return null; } synchronized (this) { if (mDeviceOwner != null) { - return mDeviceOwner.getProfileOwnerPackageName(userHandle); + return mDeviceOwner.getProfileOwnerComponent(userHandle); } } return null; @@ -3413,8 +3448,9 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { // Returns the active profile owner for this user or null if the current user has no // profile owner. private ActiveAdmin getProfileOwnerAdmin(int userHandle) { - String profileOwnerPackage = getProfileOwner(userHandle); - if (profileOwnerPackage == null) { + ComponentName profileOwner = + mDeviceOwner != null ? mDeviceOwner.getProfileOwnerComponent(userHandle) : null; + if (profileOwner == null) { return null; } @@ -3422,7 +3458,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { final int n = policy.mAdminList.size(); for (int i = 0; i < n; i++) { ActiveAdmin admin = policy.mAdminList.get(i); - if (profileOwnerPackage.equals(admin.info.getPackageName())) { + if (profileOwner.equals(admin.info)) { return admin; } } @@ -3800,7 +3836,7 @@ public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { } setActiveAdmin(profileOwnerComponent, true, user.getIdentifier(), adminExtras); - setProfileOwner(profileOwnerPkg, ownerName, user.getIdentifier()); + setProfileOwner(profileOwnerComponent, ownerName, user.getIdentifier()); return user; } finally { restoreCallingIdentity(id); |