Building a Kiosk Application in Android 5.0 (Lollipop)
Android L introduced “Screen Pinning” API. This allows an application to temporarily restrict a user from navigating away from your activity. When Screen Pinning in enabled, it restricts a user from gaining access to notification bar on top as well as the navigation bar at the bottom. Prior to Android L, developers had to hack around in order to build a kiosk type application. Those workarounds included making the application a “default launcher”, write code to forcefully close the notification bar back up so users could not gain access to them.
In this post we will go over the new API and build a simple Kiosk application that will show an informational product webpage. Users will not be able to navigate away from the activity or gain access to notification bar.
Getting Android L
At the time of this writing Android L is still in preview. You can download and install Android L for Nexus 5/7. You can follow the steps at http://developer.android.com/preview/index.html#download. If you are new to flashing these images, please follow the flashing instructions posted here: https://developers.google.com/android/nexus/images#instructions
After you have successfully flashed your device, you can follow along with this post and try out Screen Pinning.
Building the application
Lets begin by creating a simple android project. We set the compileSdkVersion to 21 in your build.gradle file. This builds using the Android 5.0 SDK. In the Android manifest set the permission to use Internet. This will allow our application to access the Internet to load the webpage.
In the layout file, add a WebView tag. We will load information about the latest Nexus 6 device on that webview component.
To take advantage of full screen we need to set Immersive mode. We do that by setting System UI flags in the DecorView.
On “onResume”, we will call the new method “startLockTask” which was introduced as part of Android L SDK. By calling that method, it will run the activity in Screen Pinning mode. Lets start the application. You should notice that we get a popup confirming Screen Pinning. Accept it to start Screen pinning.
In this mode, pressing home, back or recents button does not do anything. By default pressing and holding back and recents button will get you out of screen pinning mode.
Above mode is perfect for apps that needs to restrict users from accessing the notification or navigation bar. Business apps used to display presentation for sales consultants, or educational apps that may need to restrict students from navigating away from the activity are good examples. In the new SDK, google introduced the concept of “Device Owner”. It is a specialized type of device administrator that has additional abilities to create/remove secondary user profiles and to configure global settings. The Device owner app can run screen pinning without the user confirmation we saw above. When Device owner app runs screen pinning, it needs to call “stopLockTask()” to get out of the screen pinning mode. As of this writing, there is no simple way to make an app, “device owner” without root. According to the documentation, Device owner mode can be set via NFC but there is no specific instructions as to how it can be done. (http://developer.android.com/about/versions/android-5.0.html#DeviceOwner) To try out the capability of a device owner, you can root your device and set an app as a device owner. To do this, you would need to create a device_owner.xml file and place it in /data/system folder. You can do this by following the steps below on a rooted device: (You can finds steps to root Nexus 7 with Android L on the internet)
Create a device_owner.xml, copy it over to /data/system Example device_owner.xml
cp /sdcard/device_owner.xml /data/system
chown system:system device_owner.xml
(App with the package name described in device_owner.xml must be present on the device before you copy the xml to it)
Lets modify our application to request device administrator access. To do so we will need to create a receiver class that extends DeviceAdminReceiver class. We will define an empty implementation as we don’t need to do anything specific as an administrator, except for being an administrator.
Define the receiver in the AndroidManifest.xml file:
After that is defined, in our activity we can request to be an “Administrator” if it isn’t already.
In “provisionOwner()” method, we request to be device administrator. We then enable the application to call startLockTask. When our modified application starts screen pinning, the only way to get out of that mode is by calling “stopLockTask()”. Lets do that when the back button is pressed. (You can make it so the user needs to enter a pin code etc before screen pinning is disabled, for this demo we will call it when user presses the back button)
If you run the app now, app will automatically go into screen pinning mode. (Notice no confirmation dialog is shown) When you hit back would get you out of the screen pinning.
You can find the source of the sample at https://github.com/manijshrestha/LollipopKiosk