-
Notifications
You must be signed in to change notification settings - Fork 1
/
AdendaSampleGestureListener.java
193 lines (160 loc) · 5.45 KB
/
AdendaSampleGestureListener.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
package com.adenda.example;
import sdk.adenda.modules.AdendaGlobal;
import sdk.adenda.widget.AdendaUnlockInterface;
import android.content.Context;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.GestureDetector.SimpleOnGestureListener;
public class AdendaSampleGestureListener extends SimpleOnGestureListener
{
private Context mContext;
private DisplayMetrics mScreenMetrics;
private int mPendingAction;
private AdendaUnlockInterface mAdendaUnlockInterface;
public AdendaSampleGestureListener(Context context)
{
mContext = context;
mScreenMetrics = new DisplayMetrics();
if (context != null)
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(mScreenMetrics);
mPendingAction = -1;
}
public void setAdendaUnlockInterface( AdendaUnlockInterface adendaUnlockInterface) {
mAdendaUnlockInterface = adendaUnlockInterface;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
//Log.d(getClass().getSimpleName(), "onFling: " + event1.toString()+event2.toString());
if (mAdendaUnlockInterface == null)
return false;
if (event1 == null || event2 == null)
return false;
float diffY = event1.getRawY() - event2.getRawY();
float diffX = event1.getRawX() - event2.getRawX();
// Check for a 'minimum' fling
if (Math.abs(diffY) < mScreenMetrics.heightPixels/4 && Math.abs(diffX) < mScreenMetrics.widthPixels/4)
return false;
// If we already have an action pending, then don't bother triggering another
if (mPendingAction >= 0)
return false;
// If it was a swipe up
if (mContext != null && diffY > 0 && Math.abs(diffX) < Math.abs(diffY))
// Perform unlock action
simpleUnlock();
// If it was a swipe down
else if (mContext != null && diffY < 0 && Math.abs(diffX) < Math.abs(diffY))
// Launch Google Now
launchGoogleNow();
// If it was a swipe left
else if (mContext != null && diffX > 0 && Math.abs(diffY) < Math.abs(diffX))
// Launch Camera
launchCamera();
// If it was a swipe right
else if (mContext != null && diffX < 0 && Math.abs(diffY) < Math.abs(diffX))
// Launch Action
engageContent();
return true;
}
@Override
public void onLongPress(MotionEvent event) {
//Log.d(getClass().getSimpleName(), "onLongPress: " + event.toString());
}
@Override
public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY)
{
if (mContext == null || event1 == null || event2 == null)
return false;
float diffY = event1.getRawY() - event2.getRawY();
float diffX = event1.getRawX() - event2.getRawX();
// If it was a swipe up
if (diffY > 0 && Math.abs(diffX) < Math.abs(diffY))
{
float trigger = mScreenMetrics.heightPixels/3;
if ( Math.abs(diffY) >= trigger)
// Just unlock
mPendingAction = 0;
else
mPendingAction = -1;
}
// If it was a swipe down
else if (diffY < 0 && Math.abs(diffX) < Math.abs(diffY))
{
float trigger = mScreenMetrics.heightPixels/3;
if ( Math.abs(diffY) >= trigger)
// Launch Google Now
mPendingAction = 3;
else
mPendingAction = -1;
}
// If it was a swipe left
else if (diffX > 0 && Math.abs(diffY) < Math.abs(diffX))
{
float trigger = mScreenMetrics.widthPixels/3;
if ( Math.abs(diffX) >= trigger)
// Launch Camera
mPendingAction = 1;
else
mPendingAction = -1;
}
// If it was a swipe right
else if (diffX < 0 && Math.abs(diffY) < Math.abs(diffX))
{
float trigger = mScreenMetrics.widthPixels/3;
if ( Math.abs(diffX) >= trigger)
// Launch Action
mPendingAction = 2;
else
mPendingAction = -1;
}
return true;
}
public void onUp(MotionEvent e)
{
// If we have a pending action, execute it
if (mPendingAction >= 0)
triggerAction( mPendingAction);
}
private void triggerAction(int pendingAction)
{
switch(pendingAction)
{
case 0:
// Perform unlock action
simpleUnlock();
break;
case 1:
launchCamera();
break;
case 2:
engageContent();
break;
case 3:
launchGoogleNow();
break;
default:
}
}
private void simpleUnlock()
{
if (mAdendaUnlockInterface != null)
mAdendaUnlockInterface.simpleUnlock();
}
private void launchCamera()
{
// Launch Camera
if (mAdendaUnlockInterface != null)
mAdendaUnlockInterface.unlockAndPerformIntent(AdendaGlobal.getActionIntentFromString( mContext, MediaStore.ACTION_IMAGE_CAPTURE));
}
private void engageContent()
{
if (mAdendaUnlockInterface != null)
mAdendaUnlockInterface.unlockAndEngage();
}
private void launchGoogleNow()
{
if (mAdendaUnlockInterface != null)
mAdendaUnlockInterface.unlockAndPerformIntent(AdendaGlobal.getActionIntentFromString( mContext, "com.google.android.googlequicksearchbox"));
}
}