/
MultiTouchDetector.java
226 lines (170 loc) · 5.02 KB
/
MultiTouchDetector.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package com.example.glassremote;
import java.util.Calendar;
import android.view.GestureDetector;
import android.view.MotionEvent;
public class MultiTouchDetector implements GestureDetector.OnGestureListener {
boolean[] hasBegun = new boolean[3];
FingerThread[] fingerThread = new FingerThread[3];
int fingersDown=0;
TapThread tThread;
GestureDetector gestureDetector;
MultiTouchListener listener;
@SuppressWarnings("deprecation")
public MultiTouchDetector(MultiTouchListener listener){
gestureDetector = new GestureDetector(this);
this.listener=listener;
}
public void onTouchEvent(MotionEvent event){
gestureDetector.onTouchEvent(event);
int numFingers = event.getPointerCount();
int fingerIndex = numFingers-1;
switch (event.getActionIndex()){
case (MotionEvent.ACTION_UP):
case (MotionEvent.ACTION_DOWN):
FingerThread currentThread = fingerThread[fingerIndex];
if (currentThread !=null){
currentThread.setStartTime(Calendar.getInstance().getTimeInMillis());
}
if (!hasFingersDown()){
scrollIndex=0;
tThread = new TapThread(numFingers);
tThread.start();
}
else if (tThread!=null){
if (numFingers>tThread.getNumFingers()){
tThread.setNumFingers(numFingers);
}
}
if (!hasBegun[fingerIndex]){
hasBegun[fingerIndex]=true;
if (numFingers > fingersDown){
fingersDown = numFingers;
}
currentThread = new FingerThread(numFingers);
currentThread.start();
}
//UNIMPLEMENTED
case (MotionEvent.ACTION_MOVE):
case (MotionEvent.ACTION_OUTSIDE):
case (MotionEvent.ACTION_POINTER_DOWN):
case (MotionEvent.ACTION_POINTER_UP):
case (MotionEvent.ACTION_SCROLL):
}
}
public synchronized boolean hasFingersDown(){
for (boolean b: hasBegun){
if (b) return true;
}
return false;
}
private class FingerThread extends Thread{
private final long TIMEOUT= 500L;
private long startTime;
private int numFingers;
public FingerThread(int numFingers){
scrollIndex=0;
this.numFingers=numFingers;
startTime = Calendar.getInstance().getTimeInMillis();
}
public void setStartTime(long newTime){
startTime=newTime;
}
@Override
synchronized public void run(){
while (Calendar.getInstance().getTimeInMillis()-startTime < TIMEOUT ){
//SPIN
}
int fingersIndex = numFingers-1;
hasBegun[fingersIndex]=false;
for (int i = 0; i<3; i++){
if (hasBegun[2-i]){
fingersDown = 2-i+1;
break;
}
fingersDown=0;
}
}
}
boolean notScrolling=true;
private class TapThread extends Thread{
private final long TIMEOUT= 700L;
private final long NOFINGERTIMEOUT = 300L;
private long startTime;
private int numFingers;
private long timeRunning=0;
public TapThread(int numFingers){
this.numFingers=numFingers;
startTime = Calendar.getInstance().getTimeInMillis();
notScrolling=true;
}
public void setNumFingers(int newNumFingers){
this.numFingers=newNumFingers;
}
public int getNumFingers(){
return numFingers;
}
@Override
synchronized public void run(){
//WHILE THERE ARE FINGERS AND WE HAVEN'T TIMED OUT
while (hasFingersDown() && Calendar.getInstance().getTimeInMillis()-startTime < this.TIMEOUT ){
timeRunning =Calendar.getInstance().getTimeInMillis()-startTime; //SPIN
}
//IF THE FINGERS ARE LIFTED BEFORE TIMEOUT, TAP
if (!hasFingersDown()){
long noFingersStart = Calendar.getInstance().getTimeInMillis();
while (Calendar.getInstance().getTimeInMillis()- noFingersStart < NOFINGERTIMEOUT){
if (hasFingersDown()){
return;
}
}
}
//MAKE SURE FINGERS STAY OFF FOR A FEW MILIS
if (!hasFingersDown()){
if (notScrolling){
listener.onTapUp(numFingers);
}
scrollIndex = 0;
}
}
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
int scrollIndex = 0;
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
final float THRESHOLD = 15;
final int TIMESTOSCROLL=5;
if (Math.abs(distanceX)>THRESHOLD){
scrollIndex++;
if (scrollIndex>TIMESTOSCROLL){
notScrolling=false;
listener.onScroll(e1, e2, distanceX, distanceY, fingersDown);
}
}
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}