Reputation: 141
I am setting device ownership for my app using dpm command (dpm set-device-admin) and in my MainActivity.java I have placed the code below :
DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
// get this app package name
ComponentName mDPM = new ComponentName(this, MyAdmin.class);
if (myDevicePolicyManager.isDeviceOwnerApp(this.getPackageName())) {
// get this app package name
String[] packages = {this.getPackageName()};
// mDPM is the admin package, and allow the specified packages to lock task
myDevicePolicyManager.setLockTaskPackages(mDPM, packages);
startLockTask();
} else {
Toast.makeText(getApplicationContext(),"Unable to auto pin - not device owner", Toast.LENGTH_LONG).show();
}
Whenever my app starts it auto pins itself, making it unable for users to leave. Without setting device ownership I am able to leave 'pinned' state by long pressing back button and providing the pin.
When I set the ownership this actions is overrided - all I see is 'App is pinned : unpinning isn't allowed on this device'. Is there any way to enable unpinning by providing pin as without device ownership? I still need it to make sure it autopins without any prompts.
Upvotes: 0
Views: 444
Reputation: 141
Possible answer:
Override single/long key press, request pin and unpin app on success
MainActivity.java
@Override
public void onBackPressed() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
Intent intent = km.createConfirmDeviceCredentialIntent(null, null);
if (intent != null) {
startActivityForResult(intent, REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS);
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_CONFIRM_DEVICE_CREDENTIALS) {
// unpin the app
if (resultCode == RESULT_OK) {
stopLockTask();
} else {
// nope
Toast.makeText(this, "Pin is not correct", Toast.LENGTH_SHORT).show();
}
}
}
Upvotes: 1