0

I'm using a transparent screenoverlay to detect when a user long presses the power key of their phone (or rather, when the shutdown option dialog appears), this is working fine.

Unfortunately, when this screenoverlay is active, the soft keyboard stops appearing and that is a problem for me. How can I prevent that?

The code I'm using is heavily based on this: Detect power button long press

public void warnOnShutdown() {
    if (Settings.canDrawOverlays(this)) {
        LinearLayout linearLayout = new LinearLayout(getApplicationContext()) {
            public void onCloseSystemDialogs(String reason) {
                if ("globalactions".equals(reason)) {
                    AntitheftStateManager.setShuttingDown(AntitheftService.this, true);
                }
            }

            @Override
            public boolean dispatchKeyEvent(KeyEvent event) {
                return super.dispatchKeyEvent(event);
            }
        };

        linearLayout.setFocusable(true);

        View view = LayoutInflater.from(this).inflate(R.layout.system_overlay, linearLayout);
        WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        //params
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                100,
                100,
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_FULLSCREEN
                        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
        windowManager.addView(view, params);
    }
}

Layout:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="1dp"
    android:layout_height="1dp"
    android:orientation="vertical">

</LinearLayout>

Edit:

I should probably mention that the LinearLayout is attached to the window manager from a service, that means that the keyboard is not just blocked for my app, it's blocked for the whole phone as long as the service is running.

Community
  • 1
  • 1
Syzygy
  • 578
  • 7
  • 27
  • have you tried this InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(yourView, InputMethodManager.SHOW_IMPLICIT); – DkThakur Apr 04 '17 at 09:32
  • @DkThakur I tried it now and it's not working. Something I might have to specifically mention is that the screen overlay is running in a service attached to the window manager, so this is also happening outside of my app, (e.g. blocking input for the browser) – Syzygy Apr 04 '17 at 09:52

1 Answers1

0

That's what happens when someone elses code is taken at face value. Turns out that changing the type from TYPE_SYSTEM_ALERT to TYPE_SYSTEM_OVERLAY solved my problem.

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            100,
            100,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                    | WindowManager.LayoutParams.FLAG_FULLSCREEN
                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            PixelFormat.TRANSLUCENT);
Syzygy
  • 578
  • 7
  • 27