Manage device applications
Now, you can use the Knox APIs to manage other device apps.
Show application status in the Log view
This method outputs all the enabled and disabled applications in a list.
private void showPackageState(boolean state) {
// Instantiate the EnterpriseDeviceManager class
EnterpriseDeviceManager enterpriseDeviceManager = EnterpriseDeviceManager.getInstance(this);
ApplicationPolicy appPolicy = enterpriseDeviceManager.getApplicationPolicy();
try {
// getApplicationStateList() gets the list of enabled or disabled applications based on state.
String[] packageList = appPolicy.getApplicationStateList(state);
if (packageList == null) {
// If an empty/null array returns, no enabled/disabled apps received
if (state) {
mUtils.log(getString(R.string.no_enabled_apps));
}
else {
mUtils.log(getString(R.string.no_disabled_apps));
}
} else {
// If not an empty array, display the contents whether its enabled or disabled apps
if (state) {
mUtils.log(getString(R.string.showing_enabled_apps));
} else {
mUtils.log(getString(R.string.showing_disabled_apps));
}
for (String packageName : packageList) {
mUtils.log(packageName);
}
mUtils.log(getString(R.string.finished_showing_app_list));
}
} catch(SecurityException e) {
mUtils.processException(e, TAG);
}
}
Enable all the disabled applications
private void enableAllDisabledPackages() {
// Instantiate the EnterpriseDeviceManager class
EnterpriseDeviceManager enterpriseDeviceManager = EnterpriseDeviceManager.getInstance(this);
// Get the ApplicationPolicy class where the setApplicationStateList method lives
ApplicationPolicy appPolicy = enterpriseDeviceManager.getApplicationPolicy();
try {
// Get an array of all the disabled packages
String [] disabledPackages = appPolicy.getApplicationStateList(false);
// Enable packages in the array received in previous line,
// get an array of successfully enabled packages
String [] enabledPackages = appPolicy.setApplicationStateList(disabledPackages, true);
if (enabledPackages == null) {
mUtils.log(getString(R.string.failed_enable_all_apps));
} else {
for (String enabledPackage : enabledPackages) {
mUtils.log(getString(R.string.enabled_package, enabledPackage));
}
}
} catch (SecurityException e) {
mUtils.processException(e, TAG);
}
}
Prompt the user for a package name to enable or disable
private void promptUserForPackageToEnableOrDisable(final boolean state) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Set the dialog's title to enable or disable depending on state
if (state) {
builder.setTitle(getString(R.string.enable_given_app));
} else {
builder.setTitle(getString(R.string.disable_given_app));
}
View viewInflated = LayoutInflater.from(this).inflate(R.layout.prompt_user_package_name,
findViewById(R.id.prompt_user_package_name), false);
final EditText packageNameTxt = viewInflated.findViewById(R.id.packageNameTxt);
builder.setView(viewInflated);
builder.setPositiveButton(getString(R.string.option_confirm), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Get the user provided package name
String packageName = packageNameTxt.getText().toString();
// Enable or disable the given package name depending on state
if (state) {
enablePackage(packageName);
} else {
disablePackage(packageName);
}
dialog.dismiss();
}
});
builder.setNegativeButton(getString(R.string.option_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
Enable given application
private void enablePackage(String packageName) {
// Instantiate the EnterpriseDeviceManager class
EnterpriseDeviceManager enterpriseDeviceManager = EnterpriseDeviceManager.getInstance(this);
// Get the ApplicationPolicy class where the setEnableApplication method lives
ApplicationPolicy appPolicy = enterpriseDeviceManager.getApplicationPolicy();
try {
// Enable the given app package name
boolean appEnabled = appPolicy.setEnableApplication(packageName);
mUtils.log(getResources().getString(R.string.enabled_app_result, appEnabled));
} catch (SecurityException e) {
mUtils.processException(e, TAG);
}
}
Disable given application
private void disablePackage(String packageName) {
// Instantiate the EnterpriseDeviceManager class
EnterpriseDeviceManager enterpriseDeviceManager =
EnterpriseDeviceManager.getInstance(this.getApplicationContext());
// Get the ApplicationPolicy class where the setDisableApplication method lives
ApplicationPolicy appPolicy = enterpriseDeviceManager.getApplicationPolicy();
try {
// Disable the given app package name
boolean appDisabled = appPolicy.setDisableApplication(packageName);
mUtils.log(getResources().getString(R.string.disabled_app_result, appDisabled));
} catch (SecurityException e) {
mUtils.processException(e, TAG);
}
}
Prompt the user for a package name to add or remove from the battery optimization whitelist
private void promptUserPackageForBatteryWhitelist() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.add_or_remove_app_battery_optimization_whitelist));
View viewInflated = LayoutInflater.from(this).inflate(R.layout.prompt_user_package_name,
findViewById(R.id.prompt_user_package_name), false);
final EditText packageNameTxt = viewInflated.findViewById(R.id.packageNameTxt);
builder.setView(viewInflated);
builder.setPositiveButton(getString(R.string.option_confirm), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Get the user provided package name
String packageName = packageNameTxt.getText().toString();
toggleBatteryOptimizationWhitelist(packageName);
dialog.dismiss();
}
});
builder.setNegativeButton(getString(R.string.option_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
Add or remove an application from the battery optimization whitelist
private void toggleBatteryOptimizationWhitelist(String packageName) {
EnterpriseDeviceManager enterpriseDeviceManager =
EnterpriseDeviceManager.getInstance(this.getApplicationContext());
ApplicationPolicy appPolicy = enterpriseDeviceManager.getApplicationPolicy();
int result;
String signature = null;
try {
signature = getPackageSignature(packageName);
} catch (PackageManager.NameNotFoundException e) {
mUtils.processException(e, TAG);
}
AppIdentity appIdentity = new AppIdentity(packageName, signature);
boolean isInWhitelist;
try {
List<String> packageNames = appPolicy.getPackagesFromBatteryOptimizationWhiteList();
isInWhitelist = packageNames.contains(packageName);
if(isInWhitelist) {
result = appPolicy.removePackageFromBatteryOptimizationWhiteList(appIdentity);
} else {
result = appPolicy.addPackageToBatteryOptimizationWhiteList(appIdentity);
}
if(result != ApplicationPolicy.ERROR_NONE) {
mUtils.log(getResources().getString(R.string.add_or_remove_app_battery_optimization_whitelist_fail,
Integer.toString(result), packageName, isInWhitelist));
} else {
mUtils.log(getResources().getString(R.string.add_or_remove_app_battery_optimization_whitelist_success,
packageName, !isInWhitelist));
}
} catch (SecurityException e) {
mUtils.processException(e, TAG);
}
}
Prompt the user for a package name to add or remove from the force stop blacklist
private void promptUserPackageForForceStopBlacklist() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.add_or_remove_app_force_stop_blacklist));
View viewInflated = LayoutInflater.from(this).inflate(R.layout.prompt_user_package_name,
findViewById(R.id.prompt_user_package_name), false);
final EditText packageNameTxt = viewInflated.findViewById(R.id.packageNameTxt);
builder.setView(viewInflated);
builder.setPositiveButton(getString(R.string.option_confirm), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Get the user provided package name
String packageName = packageNameTxt.getText().toString();
toggleForceStopBlacklist(packageName);
dialog.dismiss();
}
});
builder.setNegativeButton(getString(R.string.option_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
Add or remove an application from the force stop blacklist
private void toggleForceStopBlacklist(String packageName) {
EnterpriseDeviceManager enterpriseDeviceManager =
EnterpriseDeviceManager.getInstance(this.getApplicationContext());
ApplicationPolicy appPolicy = enterpriseDeviceManager.getApplicationPolicy();
List<String> list =new ArrayList<>();
list.add(packageName);
boolean result;
boolean isInBlacklist;
try {
List<String> packageNames = appPolicy.getPackagesFromForceStopBlackList();
isInBlacklist = packageNames.contains(packageName);
if(isInBlacklist) {
result = appPolicy.removePackagesFromForceStopBlackList(list);
} else {
result = appPolicy.addPackagesToForceStopBlackList(list);
}
if(!result) {
mUtils.log(getResources().getString(R.string.add_or_remove_app_force_stop_blacklist_fail,
packageName, isInBlacklist));
} else {
mUtils.log(getResources().getString(R.string.add_or_remove_app_force_stop_blacklist_success,
packageName, !isInBlacklist));
}
} catch (SecurityException e) {
mUtils.processException(e, TAG);
}
}
Get a package’s signature
private String getPackageSignature(String packageName) throws PackageManager.NameNotFoundException {
Signature[] signatures;
Context context = this;
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNING_CERTIFICATES);
signatures = packageInfo.signingInfo.getSigningCertificateHistory();
} else {
packageInfo = packageManager.getPackageInfo(packageName, GET_SIGNATURES);
signatures = packageInfo.signatures;
}
return signatures[0].toCharsString();
}
Tutorial progress
You’ve completed 6/7 steps!
NextOn this page
Is this page helpful?