From 2bdeb1440547f11113161150e1b342d5da0a2ce7 Mon Sep 17 00:00:00 2001 From: Imanol Fernandez Date: Thu, 24 Oct 2019 21:35:42 +0200 Subject: [PATCH] Make RootWidget focus more reliable. Fixes #2055. --- .../vrbrowser/ui/widgets/RootWidget.java | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/RootWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/RootWidget.java index 88beb6216..f138aa489 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/RootWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/RootWidget.java @@ -2,10 +2,11 @@ import android.content.Context; import android.util.AttributeSet; -import android.view.View; +import android.view.MotionEvent; public class RootWidget extends UIWidget { private Runnable mOnClickCallback; + private boolean mTouched = true; public RootWidget(Context aContext) { super(aContext); @@ -31,14 +32,29 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) { private void initialize(Context aContext) { setFocusable(true); + } - setOnClickListener(v -> { - requestFocus(); - requestFocusFromTouch(); - if (mOnClickCallback != null) { - mOnClickCallback.run(); - } - }); + @Override + public boolean onTouchEvent(MotionEvent event) { + switch (event.getAction()) { + case MotionEvent.ACTION_DOWN: + mTouched = true; + break; + case MotionEvent.ACTION_UP: + if (mTouched) { + mTouched = false; + requestFocus(); + requestFocusFromTouch(); + if (mOnClickCallback != null) { + mOnClickCallback.run(); + } + } + break; + case MotionEvent.ACTION_CANCEL: + mTouched = false; + break; + } + return super.onTouchEvent(event); } public void setClickCallback(Runnable aRunnable) {