Skip to content

Commit 3b0e99d

Browse files
committed
use renderer setting to choose the correct service for watch faces
1 parent 780ca0d commit 3b0e99d

File tree

2 files changed

+130
-10
lines changed

2 files changed

+130
-10
lines changed

src/processing/mode/android/AndroidBuild.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import processing.app.exec.ProcessResult;
4040
import processing.core.PApplet;
4141
import processing.mode.java.JavaBuild;
42+
import processing.mode.java.preproc.SurfaceInfo;
4243

4344
import java.io.*;
4445
import java.security.Permission;
@@ -175,7 +176,7 @@ public File createProject() throws IOException, SketchException {
175176
// build the preproc and get to work
176177
AndroidPreprocessor preproc = new AndroidPreprocessor(sketch, getPackageName());
177178
// On Android, this init will throw a SketchException if there's a problem with size()
178-
preproc.initSketchSize(sketch.getMainProgram());
179+
SurfaceInfo info = preproc.initSketchSize(sketch.getMainProgram());
179180
preproc.initSketchSmooth(sketch.getMainProgram());
180181

181182
sketchClassName = preprocess(srcFolder, manifest.getPackageName(), preproc, false);
@@ -192,9 +193,7 @@ public File createProject() throws IOException, SketchException {
192193
final File resFolder = new File(tmpFolder, "res");
193194
writeRes(resFolder, sketchClassName);
194195

195-
196-
writeMainClass(srcFolder);
197-
196+
writeMainClass(srcFolder, preproc.getRenderer(info.getSettings()));
198197

199198
// new location for SDK Tools 17: /opt/android/tools/proguard/proguard-android.txt
200199
// File proguardSrc = new File(sdk.getSdkFolder(), "tools/lib/proguard.cfg");
@@ -969,13 +968,18 @@ private File mkdirs(final File parent, final String name) throws SketchException
969968
}
970969

971970

972-
private void writeMainClass(final File srcDirectory) {
971+
private void writeMainClass(final File srcDirectory, String renderer) {
972+
System.out.println("---------------> RENDERER: " + renderer);
973973
if (publishOption == FRAGMENT) {
974974
writeFragmentActivity(srcDirectory);
975975
} else if (publishOption == WALLPAPER) {
976976
writeWallpaperService(srcDirectory);
977977
} else if (publishOption == WATCHFACE) {
978-
writeWatchfaceService(srcDirectory);
978+
if (renderer != null && (renderer.equals("P2D") || renderer.equals("P3D"))) {
979+
writeWatchFaceGLESService(srcDirectory);
980+
} else {
981+
writeWatchFaceCanvasService(srcDirectory);
982+
}
979983
} else if (publishOption == CARDBOARD) {
980984
writeCardboardActivity(srcDirectory);
981985
}
@@ -1047,7 +1051,7 @@ private void writeWallpaperService(final File srcDirectory) {
10471051
}
10481052

10491053

1050-
private void writeWatchfaceService(final File srcDirectory) {
1054+
private void writeWatchFaceGLESService(final File srcDirectory) {
10511055
File mainServiceFile = new File(new File(srcDirectory, manifest.getPackageName().replace(".", "/")),
10521056
"MainService.java");
10531057
final PrintWriter writer = PApplet.createWriter(mainServiceFile);
@@ -1064,6 +1068,25 @@ private void writeWatchfaceService(final File srcDirectory) {
10641068
writer.flush();
10651069
writer.close();
10661070
}
1071+
1072+
1073+
private void writeWatchFaceCanvasService(final File srcDirectory) {
1074+
File mainServiceFile = new File(new File(srcDirectory, manifest.getPackageName().replace(".", "/")),
1075+
"MainService.java");
1076+
final PrintWriter writer = PApplet.createWriter(mainServiceFile);
1077+
writer.println("package " + manifest.getPackageName() +";");
1078+
writer.println("import processing.android.PWatchFaceCanvas;");
1079+
writer.println("import processing.core.PApplet;");
1080+
writer.println("public class MainService extends PWatchFaceCanvas {");
1081+
writer.println(" public MainService() {");
1082+
writer.println(" super();");
1083+
writer.println(" PApplet sketch = new " + sketchClassName + "();");
1084+
writer.println(" setSketch(sketch);");
1085+
writer.println(" }");
1086+
writer.println("}");
1087+
writer.flush();
1088+
writer.close();
1089+
}
10671090

10681091

10691092
private void writeCardboardActivity(final File srcDirectory) {

src/processing/mode/android/AndroidPreprocessor.java

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
import java.io.PrintWriter;
2727
//import java.io.Writer;
2828
import java.util.List;
29+
import java.util.regex.MatchResult;
30+
import java.util.regex.Pattern;
2931

3032
import processing.app.*;
3133
import processing.core.PApplet;
34+
import processing.data.StringList;
3235
import processing.mode.java.preproc.PdePreprocessor;
3336
//import processing.mode.java.preproc.PreprocessorResult;
3437
import processing.mode.java.preproc.SurfaceInfo;
@@ -37,8 +40,12 @@
3740

3841

3942
public class AndroidPreprocessor extends PdePreprocessor {
40-
Sketch sketch;
41-
String packageName;
43+
static private final Pattern VOID_SETUP_REGEX =
44+
Pattern.compile("(?:^|\\s|;)void\\s+setup\\s*\\(", Pattern.MULTILINE);
45+
static private final Pattern CLOSING_BRACE = Pattern.compile("\\}");
46+
47+
protected Sketch sketch;
48+
protected String packageName;
4249

4350
protected String smoothStatement;
4451
protected String sketchQuality;
@@ -76,7 +83,84 @@ public SurfaceInfo initSketchSize(String code) throws SketchException {
7683
sketchRenderer = surfaceInfo.getRenderer();*/
7784
return surfaceInfo;
7885
}
86+
87+
public String getRenderer(String code) {
88+
String uncommented = scrubComments(code);
89+
MatchResult setupMatch = findInCurrentScope(VOID_SETUP_REGEX, uncommented);
90+
String searchArea = null;
91+
if (setupMatch != null) {
92+
int start = uncommented.indexOf("{", setupMatch.end());
93+
if (start >= 0) {
94+
// Find a closing brace
95+
MatchResult match = findInCurrentScope(CLOSING_BRACE, uncommented, start);
96+
if (match != null) {
97+
searchArea = uncommented.substring(start + 1, match.end() - 1);
98+
} else {
99+
return null;
100+
}
101+
}
102+
}
103+
String[] sizeContents = matchMethod("size", searchArea);
104+
String[] fullContents = matchMethod("fullScreen", searchArea);
105+
if (sizeContents != null) {
106+
StringList args = breakCommas(sizeContents[1]);
107+
return (args.size() >= 3) ? args.get(2).trim() : null;
108+
}
109+
if (fullContents != null) {
110+
StringList args = breakCommas(fullContents[1]);
111+
if (args.size() > 0) { // might have no args
112+
String args0 = args.get(0).trim();
113+
if (args.size() == 1) {
114+
// could be either fullScreen(1) or fullScreen(P2D), figure out which
115+
if (args0.equals("SPAN") || PApplet.parseInt(args0, -1) != -1) {
116+
// it's the display parameter, not the renderer
117+
} else {
118+
return args0;
119+
}
120+
} else if (args.size() == 2) {
121+
return args0;
122+
} else {
123+
return null;
124+
}
125+
}
126+
}
127+
return null;
128+
}
79129

130+
static private StringList breakCommas(String contents) {
131+
StringList outgoing = new StringList();
132+
133+
boolean insideQuote = false;
134+
// The current word being read
135+
StringBuilder current = new StringBuilder();
136+
char[] chars = contents.toCharArray();
137+
for (int i = 0; i < chars.length; i++) {
138+
char c = chars[i];
139+
if (insideQuote) {
140+
current.append(c);
141+
if (c == '\"') {
142+
insideQuote = false;
143+
}
144+
} else {
145+
if (c == ',') {
146+
if (current.length() != 0) {
147+
outgoing.append(current.toString());
148+
current.setLength(0);
149+
}
150+
} else {
151+
current.append(c);
152+
if (c == '\"') {
153+
insideQuote = true;
154+
}
155+
}
156+
}
157+
}
158+
if (current.length() != 0) {
159+
outgoing.append(current.toString());
160+
}
161+
return outgoing;
162+
}
163+
80164

81165
public String[] initSketchSmooth(String code) throws SketchException {
82166
String[] info = parseSketchSmooth(code, true);
@@ -305,5 +389,18 @@ public String[] getDefaultImports() {
305389
"android.app.Fragment"
306390
};
307391
}
308-
*/
392+
*/
393+
394+
private class SurfaceInfoAndroid extends SurfaceInfo {
395+
// private String renderer;
396+
// SurfaceInfoAndroid(SurfaceInfo info) {
397+
//
398+
// }
399+
String getRenderer() {
400+
return renderer;
401+
}
402+
403+
404+
}
405+
309406
}

0 commit comments

Comments
 (0)