Skip to content

Commit 2bb8453

Browse files
committed
watch faces with Android2D renderer are working
1 parent 3ce4fc5 commit 2bb8453

File tree

6 files changed

+134
-40
lines changed

6 files changed

+134
-40
lines changed

core/src/processing/android/PWatchFaceCanvas.java

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
import android.graphics.Canvas;
55
import android.graphics.Rect;
66
import android.support.wearable.watchface.CanvasWatchFaceService;
7+
import android.support.wearable.watchface.WatchFaceService;
78
import android.support.wearable.watchface.WatchFaceStyle;
89
import android.util.DisplayMetrics;
10+
import android.view.MotionEvent;
911
import android.view.SurfaceHolder;
1012
import android.view.WindowManager;
1113
import processing.core.PApplet;
14+
import android.graphics.Bitmap;
1215
import processing.core.PGraphicsAndroid2D;
16+
import processing.event.MouseEvent;
17+
1318

1419
public class PWatchFaceCanvas extends CanvasWatchFaceService implements AppComponent {
1520
private DisplayMetrics metrics;
@@ -69,6 +74,7 @@ public void onCreate(SurfaceHolder surfaceHolder) {
6974
.setAcceptsTapEvents(true)
7075
.build());
7176
if (sketch != null) {
77+
PGraphicsAndroid2D.useBitmap = false;
7278
sketch.initSurface(PWatchFaceCanvas.this, null);
7379
sketch.start();
7480
}
@@ -83,7 +89,7 @@ private void invalidateIfNecessary() {
8389
@Override
8490
public void onAmbientModeChanged(boolean inAmbientMode) {
8591
super.onAmbientModeChanged(inAmbientMode);
86-
// invalidate();
92+
invalidateIfNecessary();
8793
// call new event handlers in sketch (?)
8894
}
8995

@@ -96,7 +102,6 @@ public void onVisibilityChanged(boolean visible) {
96102
} else {
97103
sketch.onPause();
98104
}
99-
// startTimerIfNecessary();
100105
}
101106

102107

@@ -114,12 +119,86 @@ public void onTimeTick() {
114119

115120
@Override
116121
public void onDraw(Canvas canvas, Rect bounds) {
117-
super.onDraw(canvas, bounds);
118-
PGraphicsAndroid2D g2 = (PGraphicsAndroid2D)sketch.g;
119-
g2.canvas = canvas;
120-
sketch.handleDraw();
122+
super.onDraw(canvas, bounds);
123+
PGraphicsAndroid2D g2 = (PGraphicsAndroid2D)sketch.g;
124+
g2.canvas = canvas;
125+
sketch.handleDraw();
126+
}
127+
128+
@Override
129+
public void onTapCommand(
130+
@TapType int tapType, int x, int y, long eventTime) {
131+
switch (tapType) {
132+
case WatchFaceService.TAP_TYPE_TOUCH:
133+
// The system sends the first command, TAP_TYPE_TOUCH, when the user initially touches the screen
134+
// if (withinTapRegion(x, y)) {
135+
// // Provide visual feedback of touch event
136+
// startTapHighlight(x, y, eventTime);
137+
// }
138+
sketch.postEvent(new MouseEvent(null, eventTime,
139+
MouseEvent.PRESS, 0,
140+
x, y, LEFT, 1));
141+
invalidate();
142+
break;
143+
144+
145+
case WatchFaceService.TAP_TYPE_TAP:
146+
// Before sending the next command, the system judges whether the contact is a single tap,
147+
// which is the only gesture allowed. If the user immediately lifts their finger,
148+
// the system determines that a single tap took place, and forwards a TAP_TYPE_TAP event
149+
sketch.postEvent(new MouseEvent(null, eventTime,
150+
MouseEvent.RELEASE, 0,
151+
x, y, LEFT, 1));
152+
153+
// hideTapHighlight();
154+
// if (withinTapRegion(x, y)) {
155+
// // Implement the tap action
156+
// // (e.g. show detailed step count)
157+
// onWatchFaceTap();
158+
// }
159+
160+
161+
invalidate();
162+
break;
163+
164+
case WatchFaceService.TAP_TYPE_TOUCH_CANCEL:
165+
// If the user does not immediately lift their finger, the system forwards a
166+
// TAP_TYPE_TOUCH_CANCEL event. Once the user has triggered a TAP_TYPE_TOUCH_CANCEL event,
167+
// they cannot trigger a TAP_TYPE_TAP event until they make a new contact with the screen.
168+
//hideTapHighlight();
169+
170+
// New type of event...
171+
sketch.postEvent(new MouseEvent(null, eventTime,
172+
MouseEvent.RELEASE, 0,
173+
x, y, LEFT, 1));
174+
invalidate();
175+
break;
176+
177+
default:
178+
super.onTapCommand(tapType, x, y, eventTime);
179+
break;
180+
}
181+
}
121182

122-
// watchFace.draw(canvas, bounds);
183+
184+
@Override
185+
public void onTouchEvent(MotionEvent event) {
186+
PApplet.println("touch even:" + event.toString());
187+
sketch.surfaceTouchEvent(event);
188+
super.onTouchEvent(event);
123189
}
190+
191+
@Override
192+
public void onDestroy() {
193+
super.onDestroy();
194+
sketch.onDestroy();
195+
}
196+
}
197+
198+
199+
@Override
200+
public void onDestroy() {
201+
sketch.onDestroy();
202+
super.onDestroy();
124203
}
125204
}

core/src/processing/android/PWatchFaceGLES.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void onGlSurfaceCreated(int width, int height) {
103103
@Override
104104
public void onAmbientModeChanged(boolean inAmbientMode) {
105105
super.onAmbientModeChanged(inAmbientMode);
106-
// invalidate();
106+
invalidateIfNecessary();
107107
// call new event handlers in sketch (?)
108108
}
109109

@@ -116,7 +116,6 @@ public void onVisibilityChanged(boolean visible) {
116116
} else {
117117
sketch.onPause();
118118
}
119-
// startTimerIfNecessary();
120119
}
121120

122121

@@ -135,20 +134,11 @@ public void onTimeTick() {
135134
@Override
136135
public void onDraw() {
137136
super.onDraw();
138-
139-
PApplet.println("Calling handleDraw: " + sketch.width + " " + sketch.height);
137+
// PApplet.println("Calling handleDraw: " + sketch.width + " " + sketch.height);
140138
sketch.handleDraw();
141139
}
142140

143141

144-
@Override
145-
public void onTouchEvent(MotionEvent event) {
146-
PApplet.println("touch even:" + event.toString());
147-
sketch.surfaceTouchEvent(event);
148-
super.onTouchEvent(event);
149-
}
150-
151-
152142
@Override
153143
public void onTapCommand(
154144
@TapType int tapType, int x, int y, long eventTime) {
@@ -204,6 +194,14 @@ public void onTapCommand(
204194
}
205195
}
206196

197+
198+
@Override
199+
public void onTouchEvent(MotionEvent event) {
200+
PApplet.println("touch even:" + event.toString());
201+
sketch.surfaceTouchEvent(event);
202+
super.onTouchEvent(event);
203+
}
204+
207205
@Override
208206
public void onDestroy() {
209207
super.onDestroy();

core/src/processing/core/PApplet.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,13 @@ public void settings() {
605605
// TODO this is only used by A2D, when finishing up a draw. but if the
606606
// surfaceview has changed, then it might belong to an a3d surfaceview. hrm.
607607
public SurfaceHolder getSurfaceHolder() {
608-
return surface.getSurfaceView().getHolder();
609-
// return surfaceHolder;
608+
SurfaceView view = surface.getSurfaceView();
609+
if (view == null) {
610+
// Watch faces don't have a surface view associated to them.
611+
return null;
612+
} else {
613+
return view.getHolder();
614+
}
610615
}
611616

612617

core/src/processing/core/PGraphicsAndroid2D.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* with the original (desktop) version of Processing.
4343
*/
4444
public class PGraphicsAndroid2D extends PGraphics {
45+
static public boolean useBitmap = true;
4546

4647
public Canvas canvas; // like g2 for PGraphicsJava2D
4748

@@ -140,9 +141,11 @@ public void setSize(int iwidth, int iheight) { // ignore
140141

141142
@Override
142143
protected void allocate() {
143-
if (bitmap != null) bitmap.recycle();
144-
bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
145-
canvas = new Canvas(bitmap);
144+
if (useBitmap) {
145+
if (bitmap != null) bitmap.recycle();
146+
bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
147+
canvas = new Canvas(bitmap);
148+
}
146149
// image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
147150
// canvas = (Graphics2D) image.getGraphics();
148151
}
@@ -203,6 +206,8 @@ public void beginDraw() {
203206

204207
@Override
205208
public void endDraw() {
209+
if (bitmap == null) return;
210+
206211
// hm, mark pixels as changed, because this will instantly do a full
207212
// copy of all the pixels to the surface.. so that's kind of a mess.
208213
//updatePixels();
@@ -214,15 +219,18 @@ public void endDraw() {
214219
// }
215220

216221
if (primaryGraphics) {
217-
Canvas screen = null;
218-
try {
219-
screen = parent.getSurfaceHolder().lockCanvas(null);
220-
if (screen != null) {
221-
screen.drawBitmap(bitmap, new Matrix(), null);
222-
}
223-
} finally {
224-
if (screen != null) {
225-
parent.getSurfaceHolder().unlockCanvasAndPost(screen);
222+
SurfaceHolder holder = parent.getSurfaceHolder();
223+
if (holder != null) {
224+
Canvas screen = null;
225+
try {
226+
screen = holder.lockCanvas(null);
227+
if (screen != null) {
228+
screen.drawBitmap(bitmap, new Matrix(), null);
229+
}
230+
} finally {
231+
if (screen != null) {
232+
parent.getSurfaceHolder().unlockCanvasAndPost(screen);
233+
}
226234
}
227235
}
228236
} else {
@@ -1938,6 +1946,8 @@ public void endRaw() {
19381946

19391947
@Override
19401948
public void loadPixels() {
1949+
if (bitmap == null) return;
1950+
19411951
if ((pixels == null) || (pixels.length != width * height)) {
19421952
pixels = new int[width * height];
19431953
}
@@ -1955,6 +1965,8 @@ public void loadPixels() {
19551965
*/
19561966
@Override
19571967
public void updatePixels() {
1968+
if (bitmap == null) return;
1969+
19581970
// WritableRaster raster = ((BufferedImage) image).getRaster();
19591971
// raster.setDataElements(0, 0, width, height, pixels);
19601972
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
@@ -1995,7 +2007,7 @@ public void resize(int wide, int high) {
19952007

19962008
@Override
19972009
public int get(int x, int y) {
1998-
if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) return 0;
2010+
if ((bitmap == null) || (x < 0) || (y < 0) || (x >= width) || (y >= height)) return 0;
19992011
// WritableRaster raster = ((BufferedImage) image).getRaster();
20002012
// raster.getDataElements(x, y, getset);
20012013
// return getset[0];
@@ -2030,7 +2042,7 @@ public PImage get() {
20302042

20312043
@Override
20322044
public void set(int x, int y, int argb) {
2033-
if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) return;
2045+
if ((bitmap == null) || (x < 0) || (y < 0) || (x >= width) || (y >= height)) return;
20342046
// getset[0] = argb;
20352047
// WritableRaster raster = ((BufferedImage) image).getRaster();
20362048
// raster.setDataElements(x, y, getset);
@@ -2163,6 +2175,8 @@ public void mask(PImage alpha) {
21632175
@Override
21642176
public void copy(int sx, int sy, int sw, int sh,
21652177
int dx, int dy, int dw, int dh) {
2178+
if (bitmap == null) return;
2179+
21662180
// Bitmap bitsy = Bitmap.createBitmap(image, sx, sy, sw, sh);
21672181
// rect.set(dx, dy, dx + dw, dy + dh);
21682182
// canvas.drawBitmap(bitsy,

core/src/processing/opengl/PSurfaceGLES.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public void dispose() {
282282
private final Handler handler = new Handler();
283283
private final Runnable drawRunnable = new Runnable() {
284284
public void run() {
285-
if (sketch != null) {
285+
if (surface != null) {
286286
surface.requestRender();
287287
}
288288
scheduleNextDraw();
@@ -315,25 +315,21 @@ private void requestNextDraw() {
315315

316316

317317
public void startThread() {
318-
if (component.getKind() == AppComponent.WATCHFACE_GLES) return;
319318
requestNextDraw();
320319
}
321320

322321

323322
public void pauseThread() {
324-
if (component.getKind() == AppComponent.WATCHFACE_GLES) return;
325323
pauseNextDraw();
326324
}
327325

328326

329327
public void resumeThread() {
330-
if (component.getKind() == AppComponent.WATCHFACE_GLES) return;
331328
scheduleNextDraw();
332329
}
333330

334331

335332
public boolean stopThread() {
336-
if (component.getKind() == AppComponent.WATCHFACE_GLES) return true;
337333
pauseNextDraw();
338334
return true;
339335
}

src/processing/mode/android/AndroidBuild.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class AndroidBuild extends JavaBuild {
5050
static public final int WATCHFACE = 2;
5151
static public final int CARDBOARD = 3;
5252

53+
// TODO: make package parameter in the config file, as well as the SDK version
54+
// to download (latest, specific number)
5355
// static final String basePackage = "changethispackage.beforesubmitting.tothemarket";
5456
static final String basePackage = "processing.test";
5557
static public final int min_sdk_fragment = 14; // Ice Cream Sandwich

0 commit comments

Comments
 (0)