I am currently trying to create a COSU app. I want to lock the user into the app, and disable all functionality to leave, e.g. Home button, menu button, back button, toolbar, ect. I have followed the guide here: https://developer.android.com/work/cosu.html#cosu-solutions
The issue I am running into is that I am receiving a "no active admin" error. I do not know how to make me active. Is there an intent to call that will bring up this prompt? I followed the instructions directly from developers, but am missing some details not posted on the site.
Caused by: java.lang.SecurityException: No active admin ComponentInfo{android.example.stage/com.example.app.framework.utilities.kiosk.DeviceAdminReceiver} at android.os.Parcel.readException(Parcel.java:1693) at android.os.Parcel.readException(Parcel.java:1646) at android.app.admin.IDevicePolicyManager$Stub$Proxy.setUserRestriction(IDevicePolicyManager.java:7977) at android.app.admin.DevicePolicyManager.addUserRestriction(DevicePolicyManager.java:6296) at android.example.activity.RegistrationActivity.setUserRestriction(RegistrationActivity.java:320) at android.example.activity.RegistrationActivity.setDefaultCosuPolicies(RegistrationActivity.java:274) at android.example.activity.RegistrationActivity.onCreate(RegistrationActivity.java:110) at android.app.Activity.performCreate(Activity.java:6942) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Here is what my implementation looks like.
In OnCreate()
mAdminComponentName = DeviceAdminReceiver.getComponentName(this);
mDevicePolicyManager = (DevicePolicyManager) getSystemService(
Context.DEVICE_POLICY_SERVICE);
mPackageManager = getPackageManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setDefaultCosuPolicies(true);
}
In onStart()
// start lock task mode if it's not already active
ActivityManager am = (ActivityManager) getSystemService(
Context.ACTIVITY_SERVICE);
// ActivityManager.getLockTaskModeState api is not available in pre-M.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (!am.isInLockTaskMode()) {
startLockTask();
}
}
} else {
if (am.getLockTaskModeState() ==
ActivityManager.LOCK_TASK_MODE_NONE) {
startLockTask();
}
}
My helper methods
@TargetApi(Build.VERSION_CODES.M)
private void setDefaultCosuPolicies(boolean active) {
// set user restrictions
setUserRestriction(UserManager.DISALLOW_SAFE_BOOT, active);
setUserRestriction(UserManager.DISALLOW_FACTORY_RESET, active);
setUserRestriction(UserManager.DISALLOW_ADD_USER, active);
setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, active);
setUserRestriction(UserManager.DISALLOW_ADJUST_VOLUME, active);
// disable keyguard and status bar
mDevicePolicyManager.setKeyguardDisabled(mAdminComponentName, active);
mDevicePolicyManager.setStatusBarDisabled(mAdminComponentName, active);
// enable STAY_ON_WHILE_PLUGGED_IN
enableStayOnWhilePluggedIn(active);
// set System Update policy
if (active){
mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName,
SystemUpdatePolicy.createWindowedInstallPolicy(60,120));
} else {
mDevicePolicyManager.setSystemUpdatePolicy(mAdminComponentName, null);
}
// set this Activity as a lock task package
mDevicePolicyManager.setLockTaskPackages(mAdminComponentName,
active ? new String[]{getPackageName()} : new String[]{});
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
intentFilter.addCategory(Intent.CATEGORY_HOME);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
if (active) {
// set Cosu activity as home intent receiver so that it is started
// on reboot
mDevicePolicyManager.addPersistentPreferredActivity(
mAdminComponentName, intentFilter, new ComponentName(
getPackageName(), RegistrationActivity.class.getName()));
} else {
mDevicePolicyManager.clearPackagePersistentPreferredActivities(
mAdminComponentName, getPackageName());
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setUserRestriction(String restriction, boolean disallow) {
if (disallow) {
mDevicePolicyManager.addUserRestriction(mAdminComponentName,
restriction);
} else {
mDevicePolicyManager.clearUserRestriction(mAdminComponentName,
restriction);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void enableStayOnWhilePluggedIn(boolean enabled) {
if (enabled) {
mDevicePolicyManager.setGlobalSetting(
mAdminComponentName,
Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
Battery_PLUGGED_ANY);
} else {
mDevicePolicyManager.setGlobalSetting(
mAdminComponentName,
Settings.Global.STAY_ON_WHILE_PLUGGED_IN, DONT_STAY_ON);
}
}
My device_admin.xml
<?xml version="1.0" encoding="utf-8"?>
<device-admin>
<uses-policies>
<limit-password/>
<watch-login/>
<reset-password/>
<force-lock/>
<wipe-data/>
<expire-password/>
<encrypted-storage/>
<disable-camera/>
<disable-keyguard-features/>
</uses-policies>
</device-admin>
My receiver in AndroidManifest.xml
<receiver
android:name="com.example.app.framework.utilities.kiosk.DeviceAdminReceiver"
android:description="@string/app_name"
android:label="@string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin"/>
<intent-filter>
<action android:name="android.intent.action.DEVICE_ADMIN_ENABLED"/>
<action android:name="android.intent.action.PROFILE_PROVISIONING_COMPLETE"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
MyDeviceAdminReceiver.kt
:class MyDeviceAdminReceiver : DeviceAdminReceiver()
res/xml/device_admin.xml
:
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<!-- Put whatever policies you need below -->
<limit-password />
</uses-policies>
</device-admin>
AndroidManifest.xml
:
<receiver android:name=".MyDeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
Make sure to log out of all accounts in Settings > Accounts on device.
Run the dpm command to set device owner:
adb shell dpm set-device-owner com.example.myapp/.MyDeviceAdminReceiver
String[] ALLOWED_PACKAGES = {getPackageName()};
DevicePolicyManager dpm = (DevicePolicyManager) getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName adminName = new ComponentName(getApplicationContext(), MyDeviceAdminReceiver.class);
dpm.setLockTaskPackages(adminName, ALLOWED_PACKAGES);
val dpm = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
if (dpm.isLockTaskPermitted(packageName)) {
startLockTask()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With