0

I am working on a retail app that needs to play a video fullscreen on loop and limit control over the device itself so the app cannot be closed by customers, the device tampered with, etc.

I have looked into Android's immersive mode, but that only gets me half way there. I need to be able to essentially disable both soft and hardware navigation keys as well as prevent the status bar dropdown from being opened.

Am I completely missing something with immersive mode that makes this possible or are there any hacks that would accomplish this task? Thank you.

Hybrid
  • 3
  • 2

1 Answers1

0

You could attempt to disable hardware navigation keys(sound keys, back press etc) disabling the back press and volume keys.

To disable back button

@Override
public void onBackPressed() {
// Leave it blank to disable it;
} 

To disable sound keys

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (action == KeyEvent.ACTION_UP) {
            //do nothing
        }
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN) {
            //do nothing
        }
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}

To Disable Status Bar Dropdown

super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
setContentView(R.layout.your_layout);

EDIT

To disable status drop down, use this method. This is an overlay over status bar and consumed all input events. It prevents the status from expanding.

Note:

  1. customViewGroup is custom class which extends any layout(frame,relative layout etc) and consumes touch event.
  2. To consume touch event override the onInterceptTouchEvent method of the view group and return true.

Android Manifest

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 

customViewGroup implementation

WindowManager manager = ((WindowManager) getApplicationContext()
            .getSystemService(Context.WINDOW_SERVICE));

WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|

            // this is to enable the notification to recieve touch events
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |

            // Draws over status bar
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

    localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
    localLayoutParams.height = (int) (50 * getResources()
            .getDisplayMetrics().scaledDensity);
    localLayoutParams.format = PixelFormat.TRANSPARENT;

    customViewGroup view = new customViewGroup(this);

    manager.addView(view, localLayoutParams);

To disable Soft Keys

The best way to disable the soft keys is by pinning the app (App Pinning). And finally create the app in immersive mode. You could also set your app as launcher. Then when home button is clicked your app will be called, keeping your app open.

To make this, add two categories.

<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />

Reference Documentation: https://developer.android.com/work/cosu.html

Abhi
  • 3,431
  • 1
  • 17
  • 35
  • The TYPE_SYSTEM_OVERLAY flag seems to render my VideoView useless, it plays audio but is completely black. Also, I have the back button situation resolved, it's moreso the overview/home buttons that I'm having trouble "disabling". – Hybrid Jul 02 '17 at 18:28
  • Did you try app pinning? Also please attach your activity. – Abhi Jul 02 '17 at 18:53
  • Wouldn't app pinning require 5.0+ as well as having to manually enable that on each device? – Hybrid Jul 06 '17 at 23:09
  • Yes it would. That is a better solution. However you could also set your app as launcher. – Abhi Jul 07 '17 at 00:03
  • Also, the customviewgroup method doesn't appear to work with 6.0 as it throws a "permission denied for this window type" error even with "android.permission.SYSTEM_ALERT_WINDOW" added to the manifest. – Hybrid Jul 07 '17 at 00:03
  • Please attach your activity. – Abhi Jul 07 '17 at 00:06
  • [This](https://stackoverflow.com/a/34061521/7461132) may solve the permissions error. – Abhi Jul 07 '17 at 00:19