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
|
/*
* Copyright (C) 2013 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.dreams.phototable;
import android.content.Context;
import android.content.res.Resources;
import android.view.MotionEvent;
/**
* Detect and dispatch edge events.
*/
public class DragGestureDetector {
@SuppressWarnings("unused")
private static final String TAG = "DragGestureDetector";
private final PhotoTable mTable;
private final float mTouchGain;
private float[] mLast;
private float[] mCurrent;
private boolean mDrag;
public DragGestureDetector(Context context, PhotoTable table) {
Resources res = context.getResources();
mTouchGain = res.getInteger(R.integer.generalized_touch_gain) / 1000000f;
mTable = table;
mLast = new float[2];
mCurrent = new float[2];
}
private void computeAveragePosition(MotionEvent event, float[] position) {
computeAveragePosition(event, position, -1);
}
private void computeAveragePosition(MotionEvent event, float[] position, int ignore) {
final int pointerCount = event.getPointerCount();
position[0] = 0f;
position[1] = 0f;
float count = 0f;
for (int p = 0; p < pointerCount; p++) {
if (p != ignore) {
position[0] += event.getX(p);
position[1] += event.getY(p);
count += 1f;
}
}
position[0] /= count;
position[1] /= count;
}
public boolean onTouchEvent(MotionEvent event) {
int index = event.getActionIndex();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
computeAveragePosition(event, mLast);
mDrag = false;
break;
case MotionEvent.ACTION_POINTER_DOWN:
mDrag = mTable.hasFocus();
computeAveragePosition(event, mLast);
break;
case MotionEvent.ACTION_POINTER_UP:
computeAveragePosition(event, mLast, index);
break;
case MotionEvent.ACTION_MOVE:
computeAveragePosition(event, mCurrent);
if (mDrag) {
move(event, false);
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (mDrag) {
move(event, true);
}
mDrag = false;
break;
}
if (mDrag) {
mTable.refreshFocus();
}
return mDrag;
}
private void move(MotionEvent event, boolean drop) {
mTable.move(mTable.getFocus(),
mTouchGain * (mCurrent[0] - mLast[0]),
mTouchGain * (mCurrent[1] - mLast[1]),
drop);
mLast[0] = mCurrent[0];
mLast[1] = mCurrent[1];
}
}
|