summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/touch/TouchEventTranslator.java
blob: 3fcda90848334d1c9eb8c253ecd614c032fd3121 (plain)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.touch;

import android.graphics.PointF;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.MotionEvent.PointerCoords;
import android.view.MotionEvent.PointerProperties;

import java.util.function.Consumer;

/**
 * To minimize the size of the MotionEvent, historic events are not copied and passed via the
 * listener.
 */
public class TouchEventTranslator {

    private static final String TAG = "TouchEventTranslator";
    private static final boolean DEBUG = false;

    private class DownState {
        long timeStamp;
        float downX;
        float downY;
        public DownState(long timeStamp, float downX, float downY) {
            this.timeStamp = timeStamp;
            this.downX = downX;
            this.downY = downY;
        }
    };
    private final DownState ZERO = new DownState(0, 0f, 0f);

    private final Consumer<MotionEvent> mListener;

    private final SparseArray<DownState> mDownEvents;
    private final SparseArray<PointF> mFingers;

    private final SparseArray<Pair<PointerProperties[], PointerCoords[]>> mCache;

    public TouchEventTranslator(Consumer<MotionEvent> listener) {
        mDownEvents = new SparseArray<>();
        mFingers = new SparseArray<>();
        mCache = new SparseArray<>();

        mListener = listener;
    }

    public void reset() {
        mDownEvents.clear();
        mFingers.clear();
    }

    public float getDownX() {
        return mDownEvents.get(0).downX;
    }

    public float getDownY() {
        return mDownEvents.get(0).downY;
    }

    public void setDownParameters(int idx, MotionEvent e) {
        DownState ev = new DownState(e.getEventTime(), e.getX(idx), e.getY(idx));
        mDownEvents.append(idx, ev);
    }

    public void dispatchDownEvents(MotionEvent ev) {
        for(int i = 0; i < ev.getPointerCount() && i < mDownEvents.size(); i++) {
            int pid = ev.getPointerId(i);
            put(pid, i, ev.getX(i), 0, mDownEvents.get(i).timeStamp, ev);
        }
    }

    public void processMotionEvent(MotionEvent ev) {
        if (DEBUG) {
            printSamples(TAG + " processMotionEvent", ev);
        }
        int index = ev.getActionIndex();
        float x = ev.getX(index);
        float y = ev.getY(index) - mDownEvents.get(index, ZERO).downY;
        switch (ev.getActionMasked()) {
            case MotionEvent.ACTION_POINTER_DOWN:
                int pid = ev.getPointerId(index);
                if(mFingers.get(pid, null) != null) {
                    for(int i=0; i < ev.getPointerCount(); i++) {
                        pid = ev.getPointerId(i);
                        position(pid, x, y);
                    }
                    generateEvent(ev.getAction(), ev);
                } else {
                    put(pid, index, x, y, ev);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                for(int i=0; i < ev.getPointerCount(); i++) {
                    pid = ev.getPointerId(i);
                    position(pid, x, y);
                }
                generateEvent(ev.getAction(), ev);
                break;
            case MotionEvent.ACTION_POINTER_UP:
            case MotionEvent.ACTION_UP:
                pid = ev.getPointerId(index);
                lift(pid, index, x, y, ev);
                break;
            case MotionEvent.ACTION_CANCEL:
                cancel(ev);
                break;
            default:
                Log.v(TAG, "Didn't process ");
                printSamples(TAG, ev);

        }
    }

    private TouchEventTranslator put(int id, int index, float x, float y, MotionEvent ev) {
        return put(id, index, x, y, ev.getEventTime(), ev);
    }

    private TouchEventTranslator put(int id, int index, float x, float y, long ms, MotionEvent ev) {
        checkFingerExistence(id, false);
        boolean isInitialDown = (mFingers.size() == 0);

        mFingers.put(id, new PointF(x, y));
        int n = mFingers.size();

        if (mCache.get(n) == null) {
            PointerProperties[] properties = new PointerProperties[n];
            PointerCoords[] coords = new PointerCoords[n];
            for (int i = 0; i < n; i++) {
                properties[i] = new PointerProperties();
                coords[i] = new PointerCoords();
            }
            mCache.put(n, new Pair(properties, coords));
        }

        int action;
        if (isInitialDown) {
            action = MotionEvent.ACTION_DOWN;
        } else {
            action = MotionEvent.ACTION_POINTER_DOWN;
            // Set the id of the changed pointer.
            action |= index << MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        }
        generateEvent(action, ms, ev);
        return this;
    }

    public TouchEventTranslator position(int id, float x, float y) {
        checkFingerExistence(id, true);
        mFingers.get(id).set(x, y);
        return this;
    }

    private TouchEventTranslator lift(int id, int index, MotionEvent ev) {
        checkFingerExistence(id, true);
        boolean isFinalUp = (mFingers.size() == 1);
        int action;
        if (isFinalUp) {
            action = MotionEvent.ACTION_UP;
        } else {
            action = MotionEvent.ACTION_POINTER_UP;
            // Set the id of the changed pointer.
            action |= index << MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        }
        generateEvent(action, ev);
        mFingers.remove(id);
        return this;
    }

    private TouchEventTranslator lift(int id, int index, float x, float y, MotionEvent ev) {
        checkFingerExistence(id, true);
        mFingers.get(id).set(x, y);
        return lift(id, index, ev);
    }

    public TouchEventTranslator cancel(MotionEvent ev) {
        generateEvent(MotionEvent.ACTION_CANCEL, ev);
        mFingers.clear();
        return this;
    }

    private void checkFingerExistence(int id, boolean shouldExist) {
        if (shouldExist != (mFingers.get(id, null) != null)) {
            throw new IllegalArgumentException(
                    shouldExist ? "Finger does not exist" : "Finger already exists");
        }
    }


    /**
     * Used to debug MotionEvents being sent/received.
     */
    public void printSamples(String msg, MotionEvent ev) {
        System.out.printf("%s %s", msg, MotionEvent.actionToString(ev.getActionMasked()));
        final int pointerCount = ev.getPointerCount();
        System.out.printf("#%d/%d", ev.getActionIndex(), pointerCount);
        System.out.printf(" t=%d:", ev.getEventTime());
        for (int p = 0; p < pointerCount; p++) {
            System.out.printf("  id=%d: (%f,%f)",
                    ev.getPointerId(p), ev.getX(p), ev.getY(p));
        }
        System.out.println();
    }

    private void generateEvent(int action, MotionEvent ev) {
        generateEvent(action, ev.getEventTime(), ev);
    }

    private void generateEvent(int action, long ms, MotionEvent ev) {
        Pair<PointerProperties[], PointerCoords[]> state = getFingerState();
        MotionEvent event = MotionEvent.obtain(
                mDownEvents.get(0).timeStamp,
                ms,
                action,
                state.first.length,
                state.first,
                state.second,
                ev.getMetaState(),
                ev.getButtonState() /* buttonState */,
                ev.getXPrecision() /* xPrecision */,
                ev.getYPrecision() /* yPrecision */,
                ev.getDeviceId(),
                ev.getEdgeFlags(),
                ev.getSource(),
                ev.getFlags() /* flags */);
        if (DEBUG) {
            printSamples(TAG + " generateEvent", event);
        }
        if (event.getPointerId(event.getActionIndex()) < 0) {
            printSamples(TAG + "generateEvent", event);
            throw new IllegalStateException(event.getActionIndex() + " not found in MotionEvent");
        }
        mListener.accept(event);
        event.recycle();
    }

    /**
     * Returns the description of the fingers' state expected by MotionEvent.
     */
    private Pair<PointerProperties[], PointerCoords[]> getFingerState() {
        int nFingers = mFingers.size();

        Pair<PointerProperties[], PointerCoords[]> result = mCache.get(nFingers);
        PointerProperties[] properties = result.first;
        PointerCoords[] coordinates = result.second;

        int index = 0;
        for (int i = 0; i < mFingers.size(); i++) {
            int id = mFingers.keyAt(i);
            PointF location = mFingers.get(id);

            PointerProperties property = properties[i];
            property.id = id;
            property.toolType = MotionEvent.TOOL_TYPE_FINGER;
            properties[index] = property;

            PointerCoords coordinate = coordinates[i];
            coordinate.x = location.x;
            coordinate.y = location.y;
            coordinate.pressure = 1.0f;
            coordinates[index] = coordinate;

            index++;
        }
        return mCache.get(nFingers);
    }
}