Sunday, 31 March 2019

StartLockTask fails with Hardware Buttons

I have gone through the provisioning and ownership requirements to use SetLockTaskPackages() and StartLockTask(). In the emulators the StartLockTask() works as expected. The on-screen buttons are removed and message "Unpinning isn't allowed by your organization." is displayed on the screen.

This method seems to work good on supported devices for locking the user into a kiosk like environment. Although some devices, such as the Samsung Galaxy TabA 7, have hardware buttons. Even though the user is told "Unpinning isn't allowed by your organization." they can still use the hardware button combination to exit the application.

My solution for this was to detect the OnLockTaskModeExiting then notify my MainActivity to re-apply my policies and StartLockTask()

So I made EventPasser class. It's job is to raise an event from DeviceAdminReceiver to my MainActivity.

    public class EventPasser
    {
        public static event EventHandler ScreenUnlocked;

        public static void RaiseUnlockEvent()
        {
            EventHandler handler = ScreenUnlocked;
            if (null != handler) handler(null, EventArgs.Empty);
        }
    }

In my DeviceAdminReceiver I listen for OnLockTaskModeExiting and raise an event that is attached in my MainActivity.

    public class DeviceAdmin : DeviceAdminReceiver
    {
        public override void OnLockTaskModeExiting(Context context, Intent intent)
        {

            base.OnLockTaskModeExiting(context, intent);

            // Raise our Unlock Event
            EventPasser.RaiseUnlockEvent();

        }
     }

In my MainActivity on the OnCreate I hook up to the static event.

    EventPasser.ScreenUnlocked += EventPasser_ScreenUnlocked;

The event just re-applies my locking policies and StartLockTask().

    private void EventPasser_ScreenUnlocked(object sender, EventArgs e)
    {
        RegisterAndLock();
    }

This works very well asides from the small annoyance that you see the message "Application locked on screen".

I am a c# programmer, not quite yet an Android developer. I am very new to Android and I have solved this the way that I know how. I feel like I should be able to avoid this event passing solution I just don't know how.

Is there a more direct way to re-apply my StartLockTask() packages on OnLockTaskModeExiting? Or even better; is there a way to enforce the StartLockTask() to respect hardware buttons?



from StartLockTask fails with Hardware Buttons

No comments:

Post a Comment