Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 0a5c095

Browse files
MortimerGorobluemarvin
authored andcommitted
Add StickyTouchListener (#2096)
1 parent 46ccecc commit 0a5c095

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

app/src/common/shared/org/mozilla/vrbrowser/ui/views/HoneycombButton.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.util.AttributeSet;
99
import android.util.Log;
1010
import android.view.MotionEvent;
11+
import android.view.View;
1112
import android.widget.ImageView;
1213
import android.widget.LinearLayout;
1314
import android.widget.TextView;
@@ -19,6 +20,7 @@
1920
import org.mozilla.vrbrowser.R;
2021
import org.mozilla.vrbrowser.utils.DeviceType;
2122
import org.mozilla.vrbrowser.utils.SystemUtils;
23+
import org.mozilla.vrbrowser.utils.ViewUtils;
2224

2325
public class HoneycombButton extends LinearLayout {
2426

@@ -83,10 +85,12 @@ private void initialize(Context aContext) {
8385
inflate(aContext, R.layout.honeycomb_button, this);
8486

8587
setClickable(true);
88+
setLongClickable(false);
8689

8790
mIcon = findViewById(R.id.settings_button_icon);
8891
if (mIcon != null) {
8992
mIcon.setImageDrawable(mButtonIcon);
93+
mIcon.setClickable(false);
9094
}
9195

9296
mText = findViewById(R.id.settings_button_text);
@@ -95,16 +99,24 @@ private void initialize(Context aContext) {
9599
if (mButtonTextSize > 0) {
96100
mText.getLayoutParams().width = (int) mButtonTextSize;
97101
}
102+
mText.setClickable(false);
98103
}
99104

100105
mSecondaryText = findViewById(R.id.settings_secondary_text);
101106
if (mSecondaryText != null) {
102107
mSecondaryText.setText(mSecondaryButtonText);
108+
mSecondaryText.setClickable(false);
103109
}
104110

105111
setOnHoverListener((view, motionEvent) -> false);
106112
}
107113

114+
115+
@Override
116+
public void setOnClickListener(@Nullable OnClickListener aListener) {
117+
ViewUtils.setStickyClickListener(this, aListener);
118+
}
119+
108120
@Override
109121
public void setOnHoverListener(final OnHoverListener l) {
110122
super.setOnHoverListener((view, motionEvent) -> {

app/src/common/shared/org/mozilla/vrbrowser/utils/ViewUtils.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.text.style.ClickableSpan;
1111
import android.text.style.URLSpan;
1212
import android.util.Log;
13+
import android.view.MotionEvent;
1314
import android.view.View;
1415
import android.view.ViewGroup;
1516
import android.view.ViewParent;
@@ -170,4 +171,41 @@ public static void placeSelection(@NonNull EditText aView, int offset1, int offs
170171
int end = Math.max(offset1, offset2);
171172
aView.setSelection(start, end);
172173
}
174+
175+
static class StickyClickListener implements View.OnTouchListener {
176+
private boolean mTouched;
177+
private View.OnClickListener mClickListener;
178+
179+
StickyClickListener(View.OnClickListener aListener) {
180+
mClickListener = aListener;
181+
}
182+
183+
@Override
184+
public boolean onTouch(View view, MotionEvent event) {
185+
view.getParent().requestDisallowInterceptTouchEvent(true);
186+
switch (event.getAction()) {
187+
case MotionEvent.ACTION_DOWN:
188+
mTouched = true;
189+
break;
190+
case MotionEvent.ACTION_UP:
191+
if (mTouched && ViewUtils.isInsideView(view, (int)event.getRawX(), (int)event.getRawY())) {
192+
view.requestFocus();
193+
view.requestFocusFromTouch();
194+
if (mClickListener != null) {
195+
mClickListener.onClick(view);
196+
}
197+
}
198+
mTouched = false;
199+
break;
200+
case MotionEvent.ACTION_CANCEL:
201+
mTouched = false;
202+
break;
203+
}
204+
return false;
205+
}
206+
}
207+
208+
public static void setStickyClickListener(@NonNull View aView, View.OnClickListener aCallback) {
209+
aView.setOnTouchListener(new StickyClickListener(aCallback));
210+
}
173211
}

0 commit comments

Comments
 (0)