Skip to content

Commit 913f33c

Browse files
committed
create and reference library projects for cardboard
1 parent 858b36c commit 913f33c

File tree

3 files changed

+190
-61
lines changed

3 files changed

+190
-61
lines changed

src/processing/mode/android/AndroidBuild.java

Lines changed: 132 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,83 @@ public File createProject() throws IOException, SketchException {
233233
// TODO: temporary hack until I find a better way to include the cardboard aar
234234
// packages included in the cardboard SDK:
235235

236-
File audioJarFile = mode.getContentFile("mode/cardboard-audio-classes.jar");
237-
File commonJarFile = mode.getContentFile("mode/cardboard-common-classes.jar");
238-
File coreJarFile = mode.getContentFile("mode/cardboard-core-classes.jar");
236+
////////////////////////////////////////////////////////////////////////
237+
// first step: unpack the cardboard packages in the project's
238+
// libs folder:
239+
File audioZipFile = mode.getContentFile("mode/cardboard_audio.zip");
240+
File commonZipFile = mode.getContentFile("mode/cardboard_common.zip");
241+
File coreZipFile = mode.getContentFile("mode/cardboard_core.zip");
242+
AndroidMode.extractFolder(audioZipFile, libsFolder, true);
243+
AndroidMode.extractFolder(commonZipFile, libsFolder, true);
244+
AndroidMode.extractFolder(coreZipFile, libsFolder, true);
245+
File audioLibsFolder = new File(libsFolder, "cardboard_audio");
246+
File commonLibsFolder = new File(libsFolder, "cardboard_common");
247+
File coreLibsFolder = new File(libsFolder, "cardboard_core");
248+
249+
////////////////////////////////////////////////////////////////////////
250+
// second step: determine target id
251+
String targetID = "";
239252

240-
Util.copyFile(audioJarFile, new File(libsFolder, "cardboard-audio-classes.jar"));
241-
Util.copyFile(commonJarFile, new File(libsFolder, "cardboard-common-classes.jar"));
242-
Util.copyFile(coreJarFile, new File(libsFolder, "cardboard-core-classes.jar"));
243-
}
253+
final String[] params = {
254+
sdk.getAndroidToolPath(),
255+
"list", "targets"
256+
};
244257

245-
258+
ProcessHelper p = new ProcessHelper(params);
259+
try {
260+
final ProcessResult abiListResult = p.execute();
261+
String id = null;
262+
String platform = null;
263+
String api = null;
264+
for (String line : abiListResult) {
265+
if (line.indexOf("id:") == 0) {
266+
String[] parts = line.substring(4).split("or");
267+
if (parts.length == 2) {
268+
id = parts[0];
269+
platform = parts[1].replaceAll("\"", "").trim();
270+
}
271+
// System.out.println("***********");
272+
// System.out.println("ID: " + id);
273+
// System.out.println("PLATFORM: " + platform);
274+
}
275+
276+
String[] mapi = PApplet.match(line, "API\\slevel:\\s(\\S+)");
277+
if (mapi != null) {
278+
api = mapi[1];
279+
// System.out.println("API: " + api);
280+
}
281+
282+
if (platform != null && platform.equals(sdkTarget) &&
283+
api != null && api.equals(sdkVersion)) {
284+
targetID = id;
285+
break;
286+
}
287+
}
288+
} catch (InterruptedException e) {}
289+
290+
System.out.println("TARGET ID: " + targetID);
291+
292+
////////////////////////////////////////////////////////////////////////
293+
// third step: create library projects
294+
boolean audioRes = createLibraryProject("cardboard_audio", targetID,
295+
audioLibsFolder.getAbsolutePath(), "com.google.vr.cardboard.vrtoolkit.vraudio");
296+
boolean commonRes = createLibraryProject("cardboard_common", targetID,
297+
commonLibsFolder.getAbsolutePath(), "com.google.vr.cardboard");
298+
boolean coreRes = createLibraryProject("cardboard_core", targetID,
299+
coreLibsFolder.getAbsolutePath(), "com.google.vrtoolkit.cardboard");
300+
301+
////////////////////////////////////////////////////////////////////////
302+
// fourth step: reference library projects from main project
303+
if (audioRes && commonRes && coreRes) {
304+
System.out.println("Library projects created succesfully in " + libsFolder.toString());
305+
audioRes = referenceLibraryProject(targetID, tmpFolder.getAbsolutePath(), "libs/cardboard_audio");
306+
commonRes = referenceLibraryProject(targetID, tmpFolder.getAbsolutePath(), "libs/cardboard_common");
307+
coreRes = referenceLibraryProject(targetID, tmpFolder.getAbsolutePath(), "libs/cardboard_core");
308+
if (audioRes && commonRes && coreRes) {
309+
System.out.println("Library projects referenced succesfully!");
310+
}
311+
}
312+
}
246313

247314
// Copy the data folder (if one exists) to the project's 'assets' folder
248315
final File sketchDataFolder = sketch.getDataFolder();
@@ -257,9 +324,66 @@ public File createProject() throws IOException, SketchException {
257324
Util.copyDir(sketchResFolder, resFolder);
258325
}
259326
}
327+
260328
return tmpFolder;
261329
}
262330

331+
332+
protected boolean createLibraryProject(String name, String target,
333+
String path, String pck) {
334+
final String[] params = {
335+
sdk.getAndroidToolPath(),
336+
"create", "lib-project",
337+
"--name", name,
338+
"--target", target,
339+
"--path", path,
340+
"--package", pck
341+
};
342+
343+
ProcessHelper p = new ProcessHelper(params);
344+
ProcessResult pr;
345+
try {
346+
pr = p.execute();
347+
} catch (InterruptedException | IOException e) {
348+
e.printStackTrace();
349+
return false;
350+
}
351+
352+
if (pr.succeeded()) {
353+
return true;
354+
} else {
355+
System.err.println(pr.getStderr());
356+
Messages.showWarning("Failed to create library project", "Something wrong happened", null);
357+
return false;
358+
}
359+
}
360+
361+
protected boolean referenceLibraryProject(String target, String path, String lib) {
362+
final String[] params = {
363+
sdk.getAndroidToolPath(),
364+
"update", "project",
365+
"--target", target,
366+
"--path", path,
367+
"--library", lib
368+
};
369+
370+
ProcessHelper p = new ProcessHelper(params);
371+
ProcessResult pr;
372+
try {
373+
pr = p.execute();
374+
} catch (InterruptedException | IOException e) {
375+
e.printStackTrace();
376+
return false;
377+
}
378+
379+
if (pr.succeeded()) {
380+
return true;
381+
} else {
382+
System.err.println(pr.getStderr());
383+
Messages.showWarning("Failed to add library project", "Something wrong happened", null);
384+
return false;
385+
}
386+
}
263387

264388
/**
265389
* The Android dex util pukes on paths containing spaces, which will happen

src/processing/mode/android/AndroidMode.java

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,20 @@
3232
import processing.app.ui.Editor;
3333
import processing.app.ui.EditorException;
3434
import processing.app.ui.EditorState;
35+
import processing.core.PApplet;
3536
import processing.mode.android.AndroidSDK.CancelException;
3637
import processing.mode.java.JavaMode;
3738

39+
import java.io.BufferedInputStream;
40+
import java.io.BufferedOutputStream;
3841
import java.io.File;
42+
import java.io.FileOutputStream;
3943
import java.io.IOException;
4044
import java.text.SimpleDateFormat;
4145
import java.util.Date;
46+
import java.util.Enumeration;
47+
import java.util.zip.ZipEntry;
48+
import java.util.zip.ZipFile;
4249

4350

4451
public class AndroidMode extends JavaMode {
@@ -301,7 +308,55 @@ public void handleStop(RunnerListener listener) {
301308
}
302309
}
303310

304-
311+
public static void extractFolder(File file, File newPath, boolean setExec) throws IOException {
312+
int BUFFER = 2048;
313+
ZipFile zip = new ZipFile(file);
314+
Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
315+
316+
// Process each entry
317+
while (zipFileEntries.hasMoreElements()) {
318+
// grab a zip file entry
319+
ZipEntry entry = zipFileEntries.nextElement();
320+
String currentEntry = entry.getName();
321+
File destFile = new File(newPath, currentEntry);
322+
//destFile = new File(newPath, destFile.getName());
323+
File destinationParent = destFile.getParentFile();
324+
325+
// create the parent directory structure if needed
326+
destinationParent.mkdirs();
327+
328+
String ext = PApplet.getExtension(currentEntry);
329+
if (setExec && ext.equals("unknown")) {
330+
// On some OS X machines the android binaries loose their executable
331+
// attribute, rendering the mode unusable
332+
destFile.setExecutable(true);
333+
}
334+
335+
if (!entry.isDirectory()) {
336+
// should preserve permissions
337+
// https://bitbucket.org/atlassian/amps/pull-requests/21/amps-904-preserve-executable-file-status/diff
338+
BufferedInputStream is = new BufferedInputStream(zip
339+
.getInputStream(entry));
340+
int currentByte;
341+
// establish buffer for writing file
342+
byte data[] = new byte[BUFFER];
343+
344+
// write the current file to disk
345+
FileOutputStream fos = new FileOutputStream(destFile);
346+
BufferedOutputStream dest = new BufferedOutputStream(fos,
347+
BUFFER);
348+
349+
// read and write until last byte is encountered
350+
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
351+
dest.write(data, 0, currentByte);
352+
}
353+
dest.flush();
354+
dest.close();
355+
is.close();
356+
}
357+
}
358+
zip.close();
359+
}
305360
// public void handleExport(Sketch sketch, )
306361

307362

src/processing/mode/android/SDKDownloader.java

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ public class SDKDownloader extends JDialog implements PropertyChangeListener {
7575
private AndroidSDK sdk;
7676
private boolean cancelled;
7777

78-
private int totalSize = 0;
79-
private static ZipFile zip;
78+
private int totalSize = 0;
8079

8180
class SDKUrlHolder {
8281
public String platformToolsUrl, buildToolsUrl, platformUrl, toolsUrl;
@@ -203,7 +202,7 @@ private void downloadAndUnpack(String urlString, File saveTo,
203202
inputStream.close();
204203
outputStream.close();
205204

206-
extractFolder(saveTo, unpackTo, setExec);
205+
AndroidMode.extractFolder(saveTo, unpackTo, setExec);
207206
}
208207

209208
private SDKUrlHolder getDownloadUrls(String repositoryUrl, String requiredHostOs) throws ParserConfigurationException, IOException, SAXException {
@@ -400,53 +399,4 @@ public void actionPerformed(ActionEvent actionEvent) {
400399
setResizable(false);
401400
setLocationRelativeTo(editor);
402401
}
403-
404-
static void extractFolder(File file, File newPath, boolean setExec) throws IOException {
405-
int BUFFER = 2048;
406-
zip = new ZipFile(file);
407-
Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
408-
409-
// Process each entry
410-
while (zipFileEntries.hasMoreElements()) {
411-
// grab a zip file entry
412-
ZipEntry entry = zipFileEntries.nextElement();
413-
String currentEntry = entry.getName();
414-
File destFile = new File(newPath, currentEntry);
415-
//destFile = new File(newPath, destFile.getName());
416-
File destinationParent = destFile.getParentFile();
417-
418-
// create the parent directory structure if needed
419-
destinationParent.mkdirs();
420-
421-
String ext = PApplet.getExtension(currentEntry);
422-
if (setExec && ext.equals("unknown")) {
423-
// On some OS X machines the android binaries loose their executable
424-
// attribute, rendering the mode unusable
425-
destFile.setExecutable(true);
426-
}
427-
428-
if (!entry.isDirectory()) {
429-
// should preserve permissions
430-
// https://bitbucket.org/atlassian/amps/pull-requests/21/amps-904-preserve-executable-file-status/diff
431-
BufferedInputStream is = new BufferedInputStream(zip
432-
.getInputStream(entry));
433-
int currentByte;
434-
// establish buffer for writing file
435-
byte data[] = new byte[BUFFER];
436-
437-
// write the current file to disk
438-
FileOutputStream fos = new FileOutputStream(destFile);
439-
BufferedOutputStream dest = new BufferedOutputStream(fos,
440-
BUFFER);
441-
442-
// read and write until last byte is encountered
443-
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
444-
dest.write(data, 0, currentByte);
445-
}
446-
dest.flush();
447-
dest.close();
448-
is.close();
449-
}
450-
}
451-
}
452402
}

0 commit comments

Comments
 (0)