-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathOpenSettingsDialogFragment.java
85 lines (71 loc) · 2.17 KB
/
OpenSettingsDialogFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package org.medicmobile.webapp.mobile;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import androidx.annotation.Nullable;
import java.time.Clock;
@SuppressLint("ValidFragment")
public class OpenSettingsDialogFragment extends Fragment {
private final View view;
private int fingerTapCount = 0;
private long lastTimeTap = 0;
private GestureHandler swipeGesture;
private static final int TIME_BETWEEN_TAPS = 500;
private final OnTouchListener onTouchListener = new OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View view, MotionEvent event) {
countTaps(event);
onSwipe(event);
return false;
}
};
public OpenSettingsDialogFragment(View view) {
this.view = view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view.setOnTouchListener(onTouchListener);
}
private void countTaps(MotionEvent event) {
if (event.getPointerCount() != 1 || event.getActionMasked() != MotionEvent.ACTION_DOWN) {
return;
}
long currentTime = Clock.systemUTC().millis();
fingerTapCount = lastTimeTap + TIME_BETWEEN_TAPS >= currentTime ? fingerTapCount + 1 : 1;
lastTimeTap = currentTime;
}
private boolean hasTapEnough() {
// 5 taps by the user + 1 for the swipe right.
return fingerTapCount == 6;
}
private void onSwipe(MotionEvent event) {
if (event.getPointerCount() != 2) {
return;
}
switch (event.getActionMasked()) {
case MotionEvent.ACTION_POINTER_DOWN:
swipeGesture = hasTapEnough() ? new GestureHandler(event) : null;
return;
case MotionEvent.ACTION_MOVE:
if (swipeGesture != null && hasTapEnough() && swipeGesture.isSwipeRight(event)) {
openSettings();
}
return;
case MotionEvent.ACTION_POINTER_UP:
swipeGesture = null;
return;
}
}
private void openSettings() {
Activity activity = getActivity();
startActivity(new Intent(activity, SettingsDialogActivity.class));
activity.finish();
}
}