forked from processing/processing-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidMode.java
More file actions
317 lines (252 loc) · 8.61 KB
/
AndroidMode.java
File metadata and controls
317 lines (252 loc) · 8.61 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2011-12 Ben Fry and Casey Reas
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.mode.android;
import processing.app.Base;
import processing.app.Library;
import processing.app.RunnerListener;
import processing.app.Sketch;
import processing.app.SketchException;
import processing.app.ui.Editor;
import processing.app.ui.EditorState;
import processing.mode.java.JavaMode;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AndroidMode extends JavaMode {
private AndroidSDK sdk;
private File coreZipLocation;
private AndroidRunner runner;
public static boolean sdkDownloadInProgress = false;
public AndroidMode(Base base, File folder) {
super(base, folder);
}
@Override
public Editor createEditor(Base base, String path, EditorState state) {
try {
return new AndroidEditor(base, path, state, this);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public String getTitle() {
return "Android";
}
public File[] getKeywordFiles() {
return new File[] {
Base.getContentFile("modes/java/keywords.txt")
};
}
public File[] getExampleCategoryFolders() {
return new File[] {
new File(examplesFolder, "Basics"),
new File(examplesFolder, "Topics"),
new File(examplesFolder, "Demos"),
new File(examplesFolder, "Sensors")
};
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
/** @return null so that it doesn't try to pass along the desktop version of core.jar */
public Library getCoreLibrary() {
return null;
}
protected File getCoreZipLocation() {
if (coreZipLocation == null) {
/*
// for debugging only, check to see if this is an svn checkout
File debugFile = new File("../../../android/core.zip");
if (!debugFile.exists() && Base.isMacOS()) {
// current path might be inside Processing.app, so need to go much higher
debugFile = new File("../../../../../../../android/core.zip");
}
if (debugFile.exists()) {
System.out.println("Using version of core.zip from local SVN checkout.");
// return debugFile;
coreZipLocation = debugFile;
}
*/
// otherwise do the usual
// return new File(base.getSketchbookFolder(), ANDROID_CORE_FILENAME);
coreZipLocation = getContentFile("android-core.zip");
}
return coreZipLocation;
}
// public AndroidSDK loadSDK() throws BadSDKException, IOException {
// if (sdk == null) {
// sdk = AndroidSDK.load();
// }
// return sdk;
// }
public void loadSDK() {
try {
sdk = AndroidSDK.load();
} catch (BadSDKException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void checkSDK(Editor parent) {
if (sdk == null) {
try {
sdk = AndroidSDK.load();
// FIXME REVERT THIS STATEMENT AFTER TESTING (should be ==)
if (sdk == null) {
sdk = AndroidSDK.locate(parent, this);
}
} catch (BadSDKException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (sdk == null) {
if (!sdkDownloadInProgress) {
Base.showWarning("It's gonna be a bad day",
"The Android SDK could not be loaded.\n" +
"Use of Android mode will be all but disabled.",
null);
}
}
}
public AndroidSDK getSDK() {
return sdk;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd.HHmm");
static public String getDateStamp() {
return dateFormat.format(new Date());
}
static public String getDateStamp(long stamp) {
return dateFormat.format(new Date(stamp));
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
// public void handleRun(Sketch sketch, RunnerListener listener) throws SketchException {
// JavaBuild build = new JavaBuild(sketch);
// String appletClassName = build.build();
// if (appletClassName != null) {
// runtime = new Runner(build, listener);
// runtime.launch(false);
// }
// }
public void handleRunEmulator(Sketch sketch, RunnerListener listener) throws SketchException, IOException {
listener.startIndeterminate();
listener.statusNotice("Starting build...");
AndroidBuild build = new AndroidBuild(sketch, this);
listener.statusNotice("Building Android project...");
build.build("debug");
boolean avd = AVD.ensureProperAVD(sdk);
if (!avd) {
SketchException se =
new SketchException("Could not create a virtual device for the emulator.");
se.hideStackTrace();
throw se;
}
listener.statusNotice("Running sketch on emulator...");
runner = new AndroidRunner(build, listener);
runner.launch(Devices.getInstance().getEmulator());
}
public void handleRunDevice(Sketch sketch, RunnerListener listener)
throws SketchException, IOException {
// JavaBuild build = new JavaBuild(sketch);
// String appletClassName = build.build();
// if (appletClassName != null) {
// runtime = new Runner(build, listener);
// runtime.launch(true);
// }
// try {
// runSketchOnDevice(Environment.getInstance().getHardware(), "debug", this);
// } catch (final MonitorCanceled ok) {
// sketchStopped();
// statusNotice("Canceled.");
// }
listener.startIndeterminate();
listener.statusNotice("Starting build...");
AndroidBuild build = new AndroidBuild(sketch, this);
listener.statusNotice("Building Android project...");
build.build("debug");
listener.statusNotice("Running sketch on device...");
runner = new AndroidRunner(build, listener);
runner.launch(Devices.getInstance().getHardware());
}
public void handleStop(RunnerListener listener) {
listener.statusNotice("");
listener.stopIndeterminate();
// if (runtime != null) {
// runtime.close(); // kills the window
// runtime = null; // will this help?
// }
if (runner != null) {
runner.close();
runner = null;
}
}
// public void handleExport(Sketch sketch, )
/*
protected void buildReleaseForExport(Sketch sketch, String target) throws MonitorCanceled {
// final IndeterminateProgressMonitor monitor =
// new IndeterminateProgressMonitor(this,
// "Building and exporting...",
// "Creating project...");
try {
AndroidBuild build = new AndroidBuild(sketch, sdk);
File tempFolder = null;
try {
tempFolder = build.createProject(target, getCoreZipLocation());
if (tempFolder == null) {
return;
}
} catch (IOException e) {
e.printStackTrace();
} catch (SketchException se) {
se.printStackTrace();
}
try {
if (monitor.isCanceled()) {
throw new MonitorCanceled();
}
monitor.setNote("Building release version...");
// if (!build.antBuild("release")) {
// return;
// }
if (monitor.isCanceled()) {
throw new MonitorCanceled();
}
// If things built successfully, copy the contents to the export folder
File exportFolder = build.createExportFolder();
if (exportFolder != null) {
Base.copyDir(tempFolder, exportFolder);
listener.statusNotice("Done with export.");
Base.openFolder(exportFolder);
} else {
listener.statusError("Could not copy files to export folder.");
}
} catch (IOException e) {
listener.statusError(e);
} finally {
build.cleanup();
}
} finally {
monitor.close();
}
}
@SuppressWarnings("serial")
private static class MonitorCanceled extends Exception {
}
*/
}