-
Notifications
You must be signed in to change notification settings - Fork 12
/
TouchSimulator.java
124 lines (96 loc) · 4.04 KB
/
TouchSimulator.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
package es.shyri.touchmapper.output;
import android.view.MotionEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import es.shyri.touchmapper.EventInput;
import es.shyri.touchmapper.log.Log;
import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_POINTER_DOWN;
import static android.view.MotionEvent.ACTION_POINTER_INDEX_MASK;
import static android.view.MotionEvent.ACTION_POINTER_INDEX_SHIFT;
import static android.view.MotionEvent.ACTION_POINTER_UP;
import static android.view.MotionEvent.ACTION_UP;
import static android.view.MotionEvent.TOOL_TYPE_FINGER;
/**
* Created by shyri on 08/09/17.
*/
public class TouchSimulator {
EventInput eventInput = new EventInput();
ArrayList<MotionEvent.PointerCoords> pointerCoordses;
ArrayList<MotionEvent.PointerProperties> pointerPropertieses;
int pointers = 0;
public TouchSimulator() throws Exception {
pointerCoordses = new ArrayList<>();
pointerPropertieses = new ArrayList<>();
}
private int addPointer(int mapId) {
MotionEvent.PointerCoords pointerCoords = new MotionEvent.PointerCoords();
MotionEvent.PointerProperties pointerProperties = new MotionEvent.PointerProperties();
pointerProperties.id = mapId;
pointerCoordses.add(pointerCoords);
pointerPropertieses.add(pointerProperties);
return pointerCoordses.size() - 1;
}
private void removePointer(int pointerIndex) {
pointerCoordses.remove(pointerIndex);
pointerPropertieses.remove(pointerIndex);
}
public void simulateTouch(int mapId,
int originalAction,
int x,
int y) throws InvocationTargetException, IllegalAccessException {
int pointerIndex;
int unmaskedAction = originalAction;
if (originalAction == ACTION_DOWN) {
pointers++;
pointerIndex = addPointer(mapId);
if (pointers > 1) {
unmaskedAction = getUnmaskedAction(ACTION_POINTER_DOWN, pointerIndex);
}
} else if (originalAction == ACTION_UP) {
if (pointers > 0) {
pointers--;
pointerIndex = getPointerIndex(mapId);
if (pointers > 0) {
unmaskedAction = getUnmaskedAction(ACTION_POINTER_UP, pointerIndex);
}
} else {
// No pointer to handle
return;
}
} else {
pointerIndex = getPointerIndex(mapId);
}
Log.l("Simulating touch " + originalAction + " in " + x + "," + y + " pointers: " + pointers);
MotionEvent.PointerCoords pointerCoords = pointerCoordses.get(pointerIndex);
MotionEvent.PointerProperties pointerProperties = pointerPropertieses.get(pointerIndex);
pointerCoords.x = x;
pointerCoords.y = y;
pointerProperties.clear();
pointerProperties.id = mapId;
pointerProperties.toolType = TOOL_TYPE_FINGER;
pointerCoords.clear();
pointerCoords.x = x;
pointerCoords.y = y;
pointerCoords.pressure = 1.0f;
eventInput.injectTouch(unmaskedAction,
pointerPropertieses.toArray(new MotionEvent.PointerProperties[pointerPropertieses.size()]),
pointerCoordses.toArray(new MotionEvent.PointerCoords[pointerCoordses.size()]));
if (originalAction == ACTION_UP) {
removePointer(pointerIndex);
}
}
private int getPointerIndex(int pointerId) {
int pointerIndex = -1;
for (MotionEvent.PointerProperties pointerProperties : pointerPropertieses) {
pointerIndex++;
if (pointerProperties.id == pointerId) {
break;
}
}
return pointerIndex;
}
private int getUnmaskedAction(int action, int pointerIndex) {
return action | ((pointerIndex << ACTION_POINTER_INDEX_SHIFT) & ACTION_POINTER_INDEX_MASK);
}
}