Android Tutorial Android "Kiosk mode" tutorial

Edit: A better kiosk implementation is available: https://www.b4x.com/android/forum/threads/device-owner-tasklock-kiosk-apps-2017.81765/#post-518018

Kiosk mode applications are applications that lock the device and do not allow the user to run any other application other than the kiosk application.

Android doesn't allow true kiosk mode without building a custom ROM.
However using the following methods you can build an application that will prevent "regular" users from playing with anything other than your application.

The application is made of two modules. The main activity and a service.
The service is configured to start at boot.
When the service is started it checks if the activity is running or not. If it is not running it uses a timer to start the main activity.

When the activity is paused it schedules the service to start in one second:
B4X:
Sub Activity_Pause (UserClosed As Boolean)
   If kiosk Then StartServiceAt(KioskService, DateTime.Now + 1 * DateTime.TicksPerSecond, False)  
End Sub
If the user presses on the home screen, the home screen will appear for several seconds. However your application will return to the front after a few seconds and the user will not be able to interact with any other applications or change the settings.

The service is set to be a foreground service. This prevents Android from killing our service.
Press on the Stop button to deactivate kiosk mode.
 

Attachments

  • Kiosk.zip
    6.7 KB · Views: 5,498
Last edited:

mkh42

Member
Licensed User
Longtime User
Erel thank you for the hint.

Unfortunately activity_pause is not called if Task manager, Notification bar or Shutdown dialog come to front.

If it would be possible to get info about the actual window with focus from a service process, then the service process could send the intend broadcast.

i will look if i find a methode.
 

mkh42

Member
Licensed User
Longtime User
Here i upload a modded version of erel's Kiosk app

To not type everthing again i cut copy paste the comment's
i made at the top of the main module

Comments are welcome


' this is a modification of erel's kiosk mode app.

' IMPORTANT: for the first start you should choose 'choose by default for this action
' after pressing home button

' I modified the service function in such a way that one can allow an other app
' to be started
' In this example I allow to start google maps because it was an app i have
' default on my nexus AND my galaxy TAB 2. Certainly the allowed app should
' be chosen carefully to not destroy the lock.

' the service timer is always running
' the service timer broadcasts "android.intent.action.CLOSE_SYSTEM_DIALOGS"
' (could be that this implementation works only for android < 4.1)
' to close immediately system windows popping up like from Task manager ' '
' Notification panel and power of dialog

' I add a ReStarter1 activity to prevent that the Homescreen is showing for
' several seconds on some occasions

'known issues :
' - sometimes I can produce an Exception. I think that this
' exception has today with the restarter1 Activity and some race condition but
' I'm not sure until now I will take a further look on that.
' - Works not for all android versions (see above).

' - until now onlytested on a rooted device. I would assume that

' - no care was taken to sleepmodus and powersaving. Timer runs and tries to restart forever

' - reaction on trying to show the task manager differs on my nexus 7 and galaxy tab 2
' galaxy simply hide the taskmager windows again , nexus leaves the allowed application
' like pressing the back button but lock is ok for both



'This is only some kind of prototype. I'm happy for any suggestions.
'I post it here because Kiosk mode interest seems to be high and there are also
'several requests to launch other apps. Perhaps you can be inspired by this
'solution

' roughly tested on Nexus 7 (4.2) and galaxy tab 2 (4.0.3)
 

Attachments

  • Kiosk.zip
    4.4 KB · Views: 537

mkh42

Member
Licensed User
Longtime User
on thing i recognized. If one manually clear the Taskmanager recent app list the Kiosk app works very well on my two tablets (nexus 7 and galaxy tab 2).

The behavior is then very similar to the behavior Surlock (a professional kiosk app) shows.

With some trying it is still possible to produce setting changes on the very short upcoming notification bar but this is also possible in Surelock. (this test i made mainly on galaxy tab 2 )

So still room for improvement.
 

Rusty

Well-Known Member
Licensed User
Longtime User
Erel,
on your post #64 you showed a method to cancel the manifest setting that associates my app with the HOME button.
I have attempted to use this, but it does not change the HOME button association.
B4X:
Dim r As Reflector
    r.Target = Activity.GetStartingIntent
    r.RunMethod2("hasCategory", "android.intent.category.HOME", "java.lang.String")
this is called from my shutdown routine which follows with:
B4X:
Activity.Finish
        ExitApplication
Any suggestions?
Can this association be done in the program instead of via the manifest?
Thanks,
Rusty
 

Rusty

Well-Known Member
Licensed User
Longtime User
Sorry, Erel, post #63 you referred to:
You cannot remove the home key association. See this post for a possible workaround: http://www.b4x.com/forum/128770-post58.html
This link leads to the above code excerpts.

Given that you can't remove the manifest settings, and given that the Kiosk service can keep my app always on "top":
  • can the kiosk settings persist when started from MAIN and then from MAIN other activities are started
In other words, I start Kiosk service from main, then in main I startactivity("somethingelse"), will the Kiosk service then immediately restart main? Is there a way to keep the kiosk service functioning when a subordinate activity is run and then returned to the main activity once the subordinate activity is complete?
Is it possible to restart a different activity?
i.e.
If IsPaused(Main.CurrentActivity) Then
StartActivity(Main.CurrentActivity)
limit....
Where CurrentActivity is an object that defines the activity that Kiosk should resume.

Thanks for your excellent and continued support :)

Rusty
 
Last edited:

Rusty

Well-Known Member
Licensed User
Longtime User
Yes,
I've created a scenario that works, but may not be best practice.
In the Activity_Resume of EACH ACTIVITY, I store the Activity' name in a Main.Lastactivity string variable.
In KioskService,

Service_Start
B4X:
Select Case Main.LastActivity.tolowercase.trim
        Case "activity1"
            If IsPaused(Activity1) Then Timer1.Interval = 1000
        Case "facilitySelect"
            If IsPaused(Activity1) Then Timer1.Interval = 1000
        ...
        End Select
Timer1_Tick
B4X:
Select Case Main.LastActivity.tolowercase.trim
        Case "activity1"
            If IsPaused(Activity1) Then 
                StartActivity(Activity1)  
                LimitNumberOfTicks = LimitNumberOfTicks + 1  
             End If
        Case "activity2"
            If IsPaused(Activity2) Then 
                StartActivity(Activity2)  
                LimitNumberOfTicks = LimitNumberOfTicks + 1  
             End If
        Case ...
End Select
               If LimitNumberOfTicks >= 10 Then 
                    Timer1.Interval = 3000 
                End If
I tried creating an Object variable in the common of Main and then store the Activity in the object variable, but the compiler recognizes that I'm trying to store an Activity in this variable and dis-allows it :). So I resorted to string name of each activity. Seems to work pretty well.
Any improvement comments would be appreciated.
Regards,
Rusty
 

luke2012

Well-Known Member
Licensed User
Longtime User
Very good post!

What about STOCK ROMs ?
Kiosk mode isn't compatible?
 

peacemaker

Expert
Licensed User
Longtime User
Any final full code sample ZIP to test ?
 

IanMc

Well-Known Member
Licensed User
Longtime User
bad idea...... bad idea

am I the only one who can foresee a massive lawsuit coming about from taking over devices ?

Run my app and never run any other apps again! Yippeee!

Warning! You are now in Kiosk mode!

Pay X amount of dollars into this account and we'll release you.

Oh and thank you for using XYZ services, have a nice day!
 

IanMc

Well-Known Member
Licensed User
Longtime User
T'is a good point though.

Unless you've just discovered a new way to extort money.

But hey, notorious can be just as effective as famous

Apparently the app that took over more than 100,000 android devices was programmed in a new language called Basic4android ....

Both Google and others have started releasing patches to address the issue.
 
Last edited:

joneden

Active Member
Licensed User
Longtime User
Not really, using your logic MS and the other programming language developers should be held accountable for people writing viruses etc.

My comment stands
 

IanMc

Well-Known Member
Licensed User
Longtime User
You 'could' though release an innocent looking app on the Google Play Store, after it has been run 50 times (just enough to get past testing)

Kiosk mode, with a skull & crossbones and a message saying

'Muhahahaha! pay money into this account to unlock your device'

You wouldn't do that of course because you are honorable.

But you could further develop your app to inform the victim that for each day delayed in paying, one item of personal info will be deleted &/Or put on Facebook

However, luckily we are all too nice to even consider doing anything like that and I'm sure it'd never happen.

However2 the idea of asking people when they first load up an app 'this app will have access to your contacts, the internet, your phone, your panty drawer, do you allow?' is at best laughable

coz everyone just clicks on the 'Duh! OK' button
 
Last edited:

mkh42

Member
Licensed User
Longtime User
Hello

My Kiosk App now is grown to a state that I'm able to share my findings
here in the Forum in form of an extend KioskApp demo
Because I find a lot of useful information scattered in the forum from
kindly community members I want to share this code to make it simpler to
create a good kiosk app

The shared code is a stripped down version of my App I wrote for a small museum.
I hope I don't make to much errors during strip down. I certainly tested the
stripped down code but not as much as I did the original code.
My original app was landscape only but I do not find a big problem
in setting the orientation to unspecified. Hope I'm right.

Excuse that the logs are often in German and some subs to.

There are some ini files in the files directory so please take a look in the ini files.

kiosk.ini: forced settings link to allowed app ini file

appstart.ini applauncher ini file and also inifile for allowed apps in kiosk.ini
in this example I start com.android.calendar for other apps
I dump a file of apps to file.dirinternal
"Apps.txt" see WriteAppsDatei

main.ini configures main menue

German lesson for the ini files :
Lautstaerke = volume
Bild = Picture


IMPORTANT: Take a look in the manifest there are a lot of additions

I used a lot of libs to achieve this results .
Needed libs:

ActivityManager
Audio
CiniFile
Core
Dialogs
ICSControls
Jhwifi
OSLibrary
Phone
Reflection
Serial
ToggleWifidata


What are the extensions:
1) Proper management of default Home intent setting/deleting
see therefore the subs

ClearAllHomeAppDefaults
a FakeHome activity is activated and then deactivated to remove all Home
default settings see also PmsetComponentEnabledSettingFakeHome
for more information see
java - Clearing and setting the default home application (SOLVED) - Stack Overflow


CompareWithDefaultHomeActivity
check if app is already default for Home


ClearAppDefaults
clear app own defaults


2) because I was not able to delete recent task list I worked with
a couple of start activities Start0-Start7
That works very well for me no problems with accidentally activating the
original launcher and a long delay


3)already implement an example simple secret exit method with password
see ListView1_ItemLongClick I think you will got the idea

4)Closing system windows to prevent recent task list and notification bar
to pop up. Not totally perfect but for all important settings which
could eventually be changed I support forced settings

5)the timer support some kind of forced settings see kiosk.ini
also works with the transparent activity trick mentioned here in the forum
There also possible to have some dynamic settings for volume (0-100).
Use this code to create a tmp ini file and it should work

B4X:
Dim Settings As List
      Settings.Initialize
      Settings.Add("Lautstaerke="& Menuepunkt.Lautstaerke)
      Utils.WriteTmpSettings(File.DirInternal, Settings )

this file is delete during Main resume
see for example ListView1_ItemClick in AppLauncher


6)Power management. My form of power management is certainly kind of specific.
My app works as follows. Always on if unplugged for easy for the visitor.
A short press on power disables only very short time the screen and
the application returns to Main activity with a toast message for the
visitor. If plugged a short press on power disables screen to speed up
charging. Make a search in kioskservice for 'plugged' an you will get it.

7)as long as screen is off the timer interval is slow (in my app only possible when plugged)

8)during kiosk mode delete all screen-shots made see Timer5min

9)unlock screen see CheckLock and SetShowWhenLocked



what's IMHO not possible
prevent reboot from very long press on power
no workaround but Tab reboot to app again
for some good informed kids this could mean some trouble

prevent screenshots with power + volume keys
workaround with delete screenshots see Timer5min

Totally prevent settings change possible in notification bar
workaround with forced settings

Delete recent task list
workaround with start activities

disable gimmicks integrated in status bar from producer like miniapps without rooting
workaround choose a right tab
or root your device



Give it a try.
Post additional questions in this thread and I will try to answer them


IMPORTANT: Exit app with long press on listview 1, longpress on listview 2
password 'pass' see ListView1_ItemLongClick to change this



Thanks again to all people who posts there know how to this forum a
lot of solutions I in this app are from them

greetings
mkh42



versioncode 2 (vc2) (quick fixes which I overlooked during strip down sorry for that)
- fixed path error of ini file
- smaller filesize (reduced jpg size)
*
*
 

Attachments

  • KioskAppExtended_vc2.zip
    64.4 KB · Views: 748
Last edited:
Top