4

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?

clamchoda
  • 4,411
  • 2
  • 36
  • 74
  • 2
    On Samsung devices that support "Knox" (also called "My Knox"), you can control the physical device buttons via Knox Setting in global settings of the device. There are MDMs that somehow can remotely control those settings on Samsung devices so there is an device admin level API for them but I have personally never seen it as a publicly available API (maybe you have to a Samsung partner/licensor). If we are not using Samsung Knox tablets for kiosk devices, then we avoid physical hardware button-based devices (also some clients build locked covers that "hide" phy. buttons for some models). – SushiHangover Mar 29 '19 at 00:14
  • This is a really helpful comment. Thank you – clamchoda Mar 29 '19 at 15:13
  • How did you get device ownership? – JP4 Oct 06 '19 at 23:16
  • @JP4 Ownership requires ADB commands executed with the android device physically connected and factory reset. Look into `adb shell dpm set-device-owner` – clamchoda Oct 07 '19 at 14:56

0 Answers0