2

From past few days, I am trying to implement kiosk mode (Locked app) in NativeScript with Angular for Android App.

I have tried it directly but unable to handle all the buttons in Android like Home and Recent Apps buttons. I am able to handle Back and Volume up and down buttons.

The other way, I tried is creating my own Plugin using Android Native but I was unable to do so.

There are two options for Kiosk mode in Android Native. One is Screen Pinning (Programmatically) and the other is using COSU in Android (Which is not my requirement because I cannot set the app as Device Owner for 1000's of devices).

So, Can anyone share their knowledge of using Screen Pinning in Android and Creating the plugin for that in NativeScript and example Code in NativeScript.

Thanks In Advance!

Below is my Java code for Android Native to implement Screen Pinning

package org.nativescript.sdoddapaneni.kioskmodeplugin;

import android.app.ActivityManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    private Button pinAppBtn;
    private Button unpinAppBtn;
    private ActivityManager am;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        pinAppBtn = (Button) findViewById(R.id.pin_app);
        unpinAppBtn = (Button) findViewById(R.id.unpin_app);

        am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

        pinAppBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pin();
            }
        });
        unpinAppBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unpin();
            }
        });

    }

    public void unpin() {
        if (am.isInLockTaskMode()) {
            stopLockTask();
        } else {
            Toast.makeText(this, "Application already unpinned !", Toast.LENGTH_SHORT).show();
        }
    }

    public void pin() {
        startLockTask();
    }
}
Sanketh
  • 31
  • 3
  • Learn how to make a launcher app to intercept the home button. That's the first step. This is not a simple task to accomplish and you need to do a lot of research. – Richard Le Mesurier Oct 28 '18 at 16:20
  • I want to make the app locked programmatically with the click of a button without any changes in the device like rooting it or making the app has launcher or device owner. I want this to be done using NativeScript. I couldn't find useful examples or information regarding this in the internet. – Sanketh Oct 29 '18 at 00:16
  • I doubt NativeScript can do this, without a plugin that implements it as above. – Richard Le Mesurier Oct 29 '18 at 05:31
  • A plug-in in just a piece of portable / reusable module. You always have 100% access to native apis even within project. – Manoj Oct 29 '18 at 11:15
  • I think kiosk mode is something different and even though NativeScript has access to native APIs , I don't think there is a straight forward approach to do that. – Sanketh Oct 29 '18 at 14:38
  • @RichardLeMesurier Do you think a NativeScript plugin can override the onCreate() by passing required parameters like android.io.Bundle and getting the instance? – Sanketh Oct 29 '18 at 14:40
  • @Manoj Yes, there may well be a plugin that can do this. It will be written using Android native code. To implement such a plugin oneself, one would need to learn about how to make a launcher app (or implement pinning) - hence my advice to OP. In other words there is no simple "make it a kiosk app" method that I know of. – Richard Le Mesurier Oct 29 '18 at 14:56
  • @Sanketh Yes I think it is possible to add any type of code into a plugin. Although I have no experience with NativeScript, I have worked with Cordova and React Native plugins that contain Android code and they can include any level of native java that I've needed. My point is that you cannot listen for the HOME button without making a launcher-style app, so that should be where to start looking. – Richard Le Mesurier Oct 29 '18 at 15:01
  • @RichardLeMesurier But in the launcher style app, we have settings in the top notification drawer which will automatically navigate the user to the settings and hence user can make changes to the settings. So that won't work as Kiosk. I think I need to do a lot of research on this. – Sanketh Oct 29 '18 at 15:45
  • @Sanketh Yes, that's one of the problems from back in the day. But you said you didn't want to use COSU. Sending you a couple of examples I found. – Richard Le Mesurier Oct 29 '18 at 15:49
  • Here's the right way [using COSU](https://snow.dog/blog/kiosk-mode-android) – Richard Le Mesurier Oct 29 '18 at 15:49
  • Here's the older way [using various hacks and timers to make sure your app stays in front](http://www.andreas-schrade.de/2015/02/16/android-tutorial-how-to-create-a-kiosk-mode-in-android/) – Richard Le Mesurier Oct 29 '18 at 15:50
  • Personally last time I had this requirement I went with one of the 3rd party solutions like MobiLock (I think). – Richard Le Mesurier Oct 29 '18 at 15:51
  • COSU is something which we need to make our app as device owner using adb or programmatically (without an account setup in the device) which will not work in my case as I cannot do that in all the user devices. – Sanketh Oct 29 '18 at 15:53
  • 1
    MobiLock will be my last priority where they have their own pricing and stuff. I want to do something like a kiosk mode gets enabled after user login and disables after logout. I think Screen Pinning is the best way to do that. But should see how to do that as a Plugin in NativeScript – Sanketh Oct 29 '18 at 15:58

1 Answers1

0

Here is how you will extend the default activity in NativeScript. FYI, With v4.x NativeScript still uses activity, staring 5.x they introduced AppCompatActivity which is expected to be released in another few days.

Manoj
  • 21,753
  • 3
  • 20
  • 41