ProfileOwner 译为配置文件所有者,在Android5.0系统推出。ProfileOwner涵盖了所有DeviceAdmin用户的管理能力,并且额外添加了很多管理权限。Android系统只能设置一个Profile Owner程序,并且该程序在设置为ProfileOwner后不能取消,应用不能卸载,唯一可以取消的途径是恢复出厂设置。
要使一个应用成为ProfileOwner,首先这个程序必须是一个DeviceAdmin,按照DeviceAdmin的标准流程配置一个程序,回顾往期文章Android Device Administration 应用的能力。
将配置好的程序设置为ProfileOwner之前,不必刻意去激活DeviceAdmin,系统在设置ProfileOwner的过程中会自动先激活DeviceAdmin,这也是ProfileOwner拥有DeviceAdmin所有能力的原因。
第三方应用没有权限设置ProfileOwner应用,属于system进程的应用可以设置。
// 获取设备管理服务
mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
// 需要激活的DeviceAdminReceiver组件
mComponentName = new ComponentName(this, DPMTestReceiver.class);
isProfileOwnerApp = mDevicePolicyManager.isProfileOwnerApp(mComponentName.getPackageName());
Log.d(TAG, "isProfileOwnerApp: " + isProfileOwnerApp);
private boolean addCrossProfileWidgetProvider(ComponentName admin, String packageName) {
boolean res = false;
if(isProfileOwnerApp) {
res = mDevicePolicyManager.addCrossProfileWidgetProvider(admin, packageName);
}
return res;
}
private boolean removeCrossProfileWidgetProvider(ComponentName admin, String packageName) {
boolean res = false;
if(isProfileOwnerApp) {
res = mDevicePolicyManager.removeCrossProfileWidgetProvider(admin, packageName);
}
return res;
}
private List<String> getCrossProfileWidgetProviders(ComponentName admin) {
List<String> res = null;
if(isProfileOwnerApp) {
res = mDevicePolicyManager.getCrossProfileWidgetProviders(admin);
}
return res;
}
private boolean setPermittedCrossProfileNotificationListeners(ComponentName admin, List<String> packageList) {
boolean res = false;
if (null == packageList) return res;
if(isProfileOwnerApp) {
res = mDevicePolicyManager.setPermittedCrossProfileNotificationListeners(admin, packageList);
}
Log.d(TAG, "ProfileOwner setPermittedCrossProfileNotificationListeners result: " + res);
return res;
}
private List<String> getPermittedCrossProfileNotificationListeners(ComponentName admin) {
List<String> res = null;
if(isProfileOwnerApp) {
res = mDevicePolicyManager.getPermittedCrossProfileNotificationListeners(admin);
}
return res;
}
private void setScreenCaptureDisabled(ComponentName admin, boolean disabled) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setScreenCaptureDisabled(admin, disabled);
}
}
private boolean getScreenCaptureDisabled(ComponentName admin) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getScreenCaptureDisabled(admin);
}
return res;
}
private void setOrganizationName(ComponentName admin, CharSequence title) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setOrganizationName(admin, title);
}
}
private CharSequence getOrganizationName(ComponentName admin) {
CharSequence res = null;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getOrganizationName(admin);
}
return res;
}
private boolean setPermissionGrantState(ComponentName admin, String packageName,
String permission, int grantState) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.setPermissionGrantState(admin, packageName, permission, grantState);
}
return res;
}
private int getPermissionGrantState(ComponentName admin, String packageName,
String permission) {
int res = 0;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getPermissionGrantState(admin, packageName, permission);
}
return res;
}
private void setPermissionPolicy(ComponentName admin, int policy) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setPermissionPolicy(admin, policy);
}
}
private int getPermissionPolicy(ComponentName admin) {
int res = 0;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getPermissionPolicy(admin);
}
return res;
}
private void setUserIcon(ComponentName admin, Bitmap icon) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setUserIcon(admin, icon);
}
}
private void setUninstallBlocked(ComponentName admin, String packageName,
boolean uninstallBlocked) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setUninstallBlocked(admin, packageName, uninstallBlocked);
}
}
private boolean isUninstallBlocked(ComponentName admin, String packageName) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.isUninstallBlocked(admin, packageName);
}
return res;
}
private void setMasterVolumeMuted(ComponentName admin, boolean on) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setMasterVolumeMuted(admin, on);
}
}
private boolean isMasterVolumeMuted(ComponentName admin) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.isMasterVolumeMuted(admin);
}
return res;
}
private void setRestrictionsProvider(ComponentName admin, ComponentName provider) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setRestrictionsProvider(admin, provider);
}
}
private void setSecureSetting(ComponentName admin, String setting, String value) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setSecureSetting(admin, setting, value);
}
}
private void setLockTaskPackages(ComponentName admin, String[] packages) {
if (packages == null) return;
if(isProfileOwnerApp) {
mDevicePolicyManager.setLockTaskPackages(admin, packages);
}
}
private String[] getLockTaskPackages(ComponentName admin) {
String[] res = null;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getLockTaskPackages(admin);
}
return res;
}
private boolean isLockTaskPermitted(String packageName) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.isLockTaskPermitted(packageName);
}
return res;
}
private void setAccountManagementDisabled(ComponentName admin, String accountType,
boolean disabled) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setAccountManagementDisabled(admin, accountType, disabled);
}
}
private String[] getAccountTypesWithManagementDisabled() {
String[] res = null;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getAccountTypesWithManagementDisabled();
}
return res;
}
private void enableSystemApp(ComponentName admin, String packageName) {
if(isProfileOwnerApp) {
mDevicePolicyManager.enableSystemApp(admin, packageName);
}
}
private boolean setApplicationHidden(ComponentName admin, String packageName, boolean hidden) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.setApplicationHidden(admin, packageName, hidden);
}
return res;
}
private boolean isApplicationHidden(ComponentName admin, String packageName) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.isApplicationHidden(admin, packageName);
}
return res;
}
private void addUserRestriction(ComponentName admin, String key) {
if(isProfileOwnerApp) {
mDevicePolicyManager.addUserRestriction(admin, key);
}
}
private void clearUserRestriction(ComponentName admin, String key) {
if(isProfileOwnerApp) {
mDevicePolicyManager.clearUserRestriction(admin, key);
}
}
private Bundle getUserRestrictions(ComponentName admin) {
Bundle res = null;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getUserRestrictions(admin);
}
return res;
}
private boolean setPermittedInputMethods(ComponentName admin, List<String> packageNames) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.setPermittedInputMethods(admin, packageNames);
}
return res;
}
private List<String> getPermittedInputMethods(ComponentName admin) {
List<String> res = null;
if(isProfileOwnerApp) {
res = mDevicePolicyManager.getPermittedInputMethods(admin);
}
return res;
}
private boolean setPermittedAccessibilityServices(ComponentName admin, List<String> packageNames) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.setPermittedAccessibilityServices(admin, packageNames);
}
return res;
}
private List<String> getPermittedAccessibilityServices(ComponentName admin) {
List<String> res = null;
if(isProfileOwnerApp) {
res = mDevicePolicyManager.getPermittedAccessibilityServices(admin);
}
return res;
}
private void setBluetoothContactSharingDisabled(ComponentName admin, boolean disabled) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setBluetoothContactSharingDisabled(admin, disabled);
}
}
private boolean getBluetoothContactSharingDisabled(ComponentName admin) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getBluetoothContactSharingDisabled(admin);
}
return res;
}
private void setCrossProfileContactsSearchDisabled(ComponentName admin, boolean disabled) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setCrossProfileContactsSearchDisabled(admin, disabled);
}
}
private boolean getCrossProfileContactsSearchDisabled(ComponentName admin) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getCrossProfileContactsSearchDisabled(admin);
}
return res;
}
private void setCrossProfileCallerIdDisabled(ComponentName admin, boolean disabled) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setCrossProfileCallerIdDisabled(admin, disabled);
}
}
private boolean getCrossProfileCallerIdDisabled(ComponentName admin) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getCrossProfileCallerIdDisabled(admin);
}
return res;
}
private void setApplicationRestrictions(ComponentName admin, String packageName,
Bundle settings) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setApplicationRestrictions(admin, packageName, settings);
}
}
private Bundle getApplicationRestrictions(ComponentName admin, String packageName) {
Bundle res = null;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getApplicationRestrictions(admin, packageName);
}
return res;
}
private String[] setPackagesSuspended(ComponentName admin, String[] packageNames, boolean suspended) {
String[] res = null;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.setPackagesSuspended(admin, packageNames, suspended);
}
return res;
}
private boolean isPackageSuspended(ComponentName admin, String packageName) {
boolean res = false;
if (isProfileOwnerApp) {
try {
res = mDevicePolicyManager.isPackageSuspended(admin, packageName);
} catch (NameNotFoundException e) {
Log.w(TAG, "Error getting appName for package: " + packageName, e);
}
}
return res;
}
private void setAlwaysOnVpnPackage(ComponentName admin, String vpnPackage,
boolean lockdownEnabled) {
if(isProfileOwnerApp) {
try {
mDevicePolicyManager.setAlwaysOnVpnPackage(admin, vpnPackage, lockdownEnabled);
} catch (NameNotFoundException | UnsupportedOperationException e) {
Log.w(TAG, "Error getting appName for package: " + vpnPackage, e);
}
}
}
private String getAlwaysOnVpnPackage(ComponentName admin) {
String res = null;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getAlwaysOnVpnPackage(admin);
}
return res;
}
private void setDelegatedScopes(ComponentName admin, String delegatePackage,
List<String> scopes) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setDelegatedScopes(admin, delegatePackage, scopes);
}
}
private List<String> getDelegatedScopes(ComponentName admin, String delegatedPackage) {
List<String> res = null;
if(isProfileOwnerApp) {
res = mDevicePolicyManager.getDelegatedScopes(admin, delegatedPackage);
}
return res;
}
private boolean installKeyPair(ComponentName admin, PrivateKey privKey, Certificate cert, String alias) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.installKeyPair(admin, privKey, cert, alias);
}
return res;
}
private boolean removeKeyPair(ComponentName admin, String alias) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.removeKeyPair(admin, alias);
}
return res;
}
private boolean hasCaCertInstalled(ComponentName admin, byte[] certBuffer) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.hasCaCertInstalled(admin, certBuffer);
}
return res;
}
private void uninstallAllUserCaCerts(ComponentName admin) {
if(isProfileOwnerApp) {
mDevicePolicyManager.uninstallAllUserCaCerts(admin);
}
}
private List<byte[]> getInstalledCaCerts(ComponentName admin) {
List<byte[]> res = null;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getInstalledCaCerts(admin);
}
return res;
}
private void uninstallCaCert(ComponentName admin, byte[] certBuffer) {
if(isProfileOwnerApp) {
mDevicePolicyManager.uninstallCaCert(admin, certBuffer);
}
}
private boolean installCaCert(ComponentName admin, byte[] certBuffer) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.installCaCert(admin, certBuffer);
}
return res;
}
private void setRequiredStrongAuthTimeout(ComponentName admin, long timeoutMs) {
if(isProfileOwnerApp) {
mDevicePolicyManager.setRequiredStrongAuthTimeout(admin, timeoutMs);
}
}
private long getRequiredStrongAuthTimeout(ComponentName admin) {
long res = 0;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.getRequiredStrongAuthTimeout(admin);
}
return res;
}
private boolean setResetPasswordToken(ComponentName admin, byte[] token) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.setResetPasswordToken(admin, token);
}
return res;
}
private boolean clearResetPasswordToken(ComponentName admin) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.clearResetPasswordToken(admin);
}
return res;
}
private boolean isResetPasswordTokenActive(ComponentName admin) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.isResetPasswordTokenActive(admin);
}
return res;
}
private boolean resetPasswordWithToken(ComponentName admin, String password,
byte[] token, int flags) {
boolean res = false;
if (isProfileOwnerApp) {
res = mDevicePolicyManager.resetPasswordWithToken(admin, password, token, flags);
}
return res;
}