Skip to content

Commit d922cd1

Browse files
committed
removed duplicated firing of mouse events in watch faces
1 parent c02b819 commit d922cd1

File tree

2 files changed

+51
-120
lines changed

2 files changed

+51
-120
lines changed

core/src/processing/android/PWatchFaceCanvas.java

Lines changed: 26 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public void onDestroy() {
139139
private class CanvasEngine extends CanvasWatchFaceService.Engine implements ServiceEngine {
140140
private PApplet sketch;
141141
private Method compUpdatedMethod;
142+
private Method tapCommandMethod;
142143
private boolean isRound = false;
143144
private Rect insets = new Rect();
144145
private boolean lowBitAmbient = false;
@@ -153,15 +154,26 @@ public void onCreate(SurfaceHolder surfaceHolder) {
153154
sketch = createSketch();
154155
PGraphicsAndroid2D.useBitmap = false;
155156
sketch.initSurface(PWatchFaceCanvas.this, null);
157+
initTapEvents();
156158
initComplications();
157159
requestPermissions();
158160
}
159161

160162

163+
private void initTapEvents() {
164+
try {
165+
tapCommandMethod = sketch.getClass().getMethod("onTapCommand",
166+
new Class[] {int.class, int.class, int.class, long.class});
167+
} catch (Exception e) {
168+
tapCommandMethod = null;
169+
}
170+
}
171+
172+
161173
private void initComplications() {
162174
try {
163175
compUpdatedMethod = sketch.getClass().getMethod("complicationsUpdated",
164-
new Class[] {int.class, ComplicationData.class});
176+
new Class[] {int.class, ComplicationData.class});
165177
} catch (Exception e) {
166178
compUpdatedMethod = null;
167179
}
@@ -228,9 +240,7 @@ public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int he
228240

229241

230242
@Override
231-
public void onPeekCardPositionUpdate(Rect rect) {
232-
233-
}
243+
public void onPeekCardPositionUpdate(Rect rect) { }
234244

235245

236246
@Override
@@ -251,65 +261,21 @@ public void onDraw(Canvas canvas, Rect bounds) {
251261

252262

253263
@Override
254-
public void onTapCommand(
255-
@TapType int tapType, int x, int y, long eventTime) {
256-
switch (tapType) {
257-
case WatchFaceService.TAP_TYPE_TOUCH:
258-
// The system sends the first command, TAP_TYPE_TOUCH, when the user initially touches the screen
259-
// if (withinTapRegion(x, y)) {
260-
// // Provide visual feedback of touch event
261-
// startTapHighlight(x, y, eventTime);
262-
// }
263-
sketch.postEvent(new MouseEvent(null, eventTime,
264-
MouseEvent.PRESS, 0,
265-
x, y, LEFT, 1));
266-
invalidate();
267-
break;
268-
269-
270-
case WatchFaceService.TAP_TYPE_TAP:
271-
// Before sending the next command, the system judges whether the contact is a single tap,
272-
// which is the only gesture allowed. If the user immediately lifts their finger,
273-
// the system determines that a single tap took place, and forwards a TAP_TYPE_TAP event
274-
sketch.postEvent(new MouseEvent(null, eventTime,
275-
MouseEvent.RELEASE, 0,
276-
x, y, LEFT, 1));
277-
278-
// hideTapHighlight();
279-
// if (withinTapRegion(x, y)) {
280-
// // Implement the tap action
281-
// // (e.g. show detailed step count)
282-
// onWatchFaceTap();
283-
// }
284-
285-
286-
invalidate();
287-
break;
288-
289-
case WatchFaceService.TAP_TYPE_TOUCH_CANCEL:
290-
// If the user does not immediately lift their finger, the system forwards a
291-
// TAP_TYPE_TOUCH_CANCEL event. Once the user has triggered a TAP_TYPE_TOUCH_CANCEL event,
292-
// they cannot trigger a TAP_TYPE_TAP event until they make a new contact with the screen.
293-
//hideTapHighlight();
294-
295-
// New type of event...
296-
sketch.postEvent(new MouseEvent(null, eventTime,
297-
MouseEvent.RELEASE, 0,
298-
x, y, LEFT, 1));
299-
invalidate();
300-
break;
301-
302-
default:
303-
super.onTapCommand(tapType, x, y, eventTime);
304-
break;
305-
}
264+
public void onTouchEvent(MotionEvent event) {
265+
super.onTouchEvent(event);
266+
if (sketch != null) sketch.surfaceTouchEvent(event);
306267
}
307268

308269

309270
@Override
310-
public void onTouchEvent(MotionEvent event) {
311-
super.onTouchEvent(event);
312-
if (sketch != null) sketch.surfaceTouchEvent(event);
271+
public void onTapCommand(
272+
@TapType int tapType, int x, int y, long eventTime) {
273+
if (tapCommandMethod != null) {
274+
try {
275+
tapCommandMethod.invoke(tapType, x, y, eventTime);
276+
} catch (Exception e) { }
277+
invalidate();
278+
}
313279
}
314280

315281

@@ -319,8 +285,7 @@ public void onComplicationDataUpdate(
319285
if (compUpdatedMethod != null) {
320286
try {
321287
compUpdatedMethod.invoke(complicationId, complicationData);
322-
} catch (Exception e) {
323-
}
288+
} catch (Exception e) { }
324289
invalidate();
325290
}
326291
}

core/src/processing/android/PWatchFaceGLES.java

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import android.support.wearable.watchface.Gles2WatchFaceService;
3131
import android.support.wearable.watchface.WatchFaceService;
3232
import android.support.wearable.watchface.WatchFaceStyle;
33+
import android.support.wearable.watchface.WatchFaceService.TapType;
3334
import android.util.DisplayMetrics;
3435
import android.view.MotionEvent;
3536
import android.view.SurfaceHolder;
@@ -138,6 +139,7 @@ private class GLES2Engine extends Gles2WatchFaceService.Engine implements
138139
ServiceEngine {
139140
private PApplet sketch;
140141
private Method compUpdatedMethod;
142+
private Method tapCommandMethod;
141143
private boolean isRound = false;
142144
private Rect insets = new Rect();
143145
private boolean lowBitAmbient = false;
@@ -151,6 +153,7 @@ public void onCreate(SurfaceHolder surfaceHolder) {
151153
.build());
152154
sketch = createSketch();
153155
sketch.initSurface(PWatchFaceGLES.this, null);
156+
initTapEvents();
154157
initComplications();
155158
requestPermissions();
156159
}
@@ -174,6 +177,16 @@ public void onGlSurfaceCreated(int width, int height) {
174177
}
175178

176179

180+
private void initTapEvents() {
181+
try {
182+
tapCommandMethod = sketch.getClass().getMethod("onTapCommand",
183+
new Class[] {int.class, int.class, int.class, long.class});
184+
} catch (Exception e) {
185+
tapCommandMethod = null;
186+
}
187+
}
188+
189+
177190
private void initComplications() {
178191
try {
179192
compUpdatedMethod = sketch.getClass().getMethod("complicationsUpdated",
@@ -231,9 +244,7 @@ public void onVisibilityChanged(boolean visible) {
231244

232245

233246
@Override
234-
public void onPeekCardPositionUpdate(Rect rect) {
235-
236-
}
247+
public void onPeekCardPositionUpdate(Rect rect) { }
237248

238249

239250
@Override
@@ -245,71 +256,26 @@ public void onTimeTick() {
245256
@Override
246257
public void onDraw() {
247258
super.onDraw();
248-
// PApplet.println("Calling handleDraw: " + sketch.width + " " + sketch.height);
249259
if (sketch != null) sketch.handleDraw();
250260
}
251261

252262

253263
@Override
254-
public void onTapCommand(
255-
@TapType int tapType, int x, int y, long eventTime) {
256-
switch (tapType) {
257-
case WatchFaceService.TAP_TYPE_TOUCH:
258-
// The system sends the first command, TAP_TYPE_TOUCH, when the user initially touches the screen
259-
// if (withinTapRegion(x, y)) {
260-
// // Provide visual feedback of touch event
261-
// startTapHighlight(x, y, eventTime);
262-
// }
263-
sketch.postEvent(new MouseEvent(null, eventTime,
264-
MouseEvent.PRESS, 0,
265-
x, y, LEFT, 1));
266-
invalidate();
267-
break;
268-
269-
270-
case WatchFaceService.TAP_TYPE_TAP:
271-
// Before sending the next command, the system judges whether the contact is a single tap,
272-
// which is the only gesture allowed. If the user immediately lifts their finger,
273-
// the system determines that a single tap took place, and forwards a TAP_TYPE_TAP event
274-
sketch.postEvent(new MouseEvent(null, eventTime,
275-
MouseEvent.RELEASE, 0,
276-
x, y, LEFT, 1));
277-
278-
// hideTapHighlight();
279-
// if (withinTapRegion(x, y)) {
280-
// // Implement the tap action
281-
// // (e.g. show detailed step count)
282-
// onWatchFaceTap();
283-
// }
284-
285-
286-
invalidate();
287-
break;
288-
289-
case WatchFaceService.TAP_TYPE_TOUCH_CANCEL:
290-
// If the user does not immediately lift their finger, the system forwards a
291-
// TAP_TYPE_TOUCH_CANCEL event. Once the user has triggered a TAP_TYPE_TOUCH_CANCEL event,
292-
// they cannot trigger a TAP_TYPE_TAP event until they make a new contact with the screen.
293-
//hideTapHighlight();
294-
295-
// New type of event...
296-
sketch.postEvent(new MouseEvent(null, eventTime,
297-
MouseEvent.RELEASE, 0,
298-
x, y, LEFT, 1));
299-
invalidate();
300-
break;
301-
302-
default:
303-
super.onTapCommand(tapType, x, y, eventTime);
304-
break;
305-
}
264+
public void onTouchEvent(MotionEvent event) {
265+
super.onTouchEvent(event);
266+
if (sketch != null) sketch.surfaceTouchEvent(event);
306267
}
307268

308269

309270
@Override
310-
public void onTouchEvent(MotionEvent event) {
311-
super.onTouchEvent(event);
312-
if (sketch != null) sketch.surfaceTouchEvent(event);
271+
public void onTapCommand(
272+
@TapType int tapType, int x, int y, long eventTime) {
273+
if (tapCommandMethod != null) {
274+
try {
275+
tapCommandMethod.invoke(tapType, x, y, eventTime);
276+
} catch (Exception e) { }
277+
invalidate();
278+
}
313279
}
314280

315281

0 commit comments

Comments
 (0)