Skip to content

Commit

Permalink
Restructure and add adb connection support for webOS devices
Browse files Browse the repository at this point in the history
  • Loading branch information
JayCanuck committed Dec 1, 2014
1 parent bdf4206 commit d498c45
Show file tree
Hide file tree
Showing 61 changed files with 1,393 additions and 524 deletions.
3 changes: 1 addition & 2 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar-with-manifest: JAR building (if you are using a manifest)
-do-jar-without-manifest: JAR building (if you are not using a manifest)
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
Expand Down
128 changes: 65 additions & 63 deletions nbproject/build-impl.xml

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions nbproject/genfiles.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
build.xml.data.CRC32=f79bd1cf
build.xml.script.CRC32=4710a49c
build.xml.stylesheet.CRC32=28e38971@1.56.1.46
build.xml.script.CRC32=1c9e5284
build.xml.stylesheet.CRC32=8064a381@1.75.2.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=f79bd1cf
nbproject/build-impl.xml.script.CRC32=9b6b82d6
nbproject/build-impl.xml.stylesheet.CRC32=c6d2a60f@1.56.1.46
nbproject/build-impl.xml.script.CRC32=2c4d5fef
nbproject/build-impl.xml.stylesheet.CRC32=876e7a8f@1.75.2.48
File renamed without changes.
8 changes: 8 additions & 0 deletions nbproject/private/private.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
compile.on.save=false
do.depend=false
do.jar=true
file.reference.json.jar=C:\\Users\\Jason\\NetBeans Projects\\json.jar
file.reference.tar.jar=C:\\Users\\Jason\\NetBeans Projects\\tar.jar
javac.debug=true
javadoc.preview=true
user.properties.file=C:\\Users\\Jason\\AppData\\Roaming\\NetBeans\\8.0.1\\build.properties
7 changes: 7 additions & 0 deletions nbproject/private/private.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-private xmlns="http://www.netbeans.org/ns/project-private/1">
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group/>
</open-files>
</project-private>
26 changes: 26 additions & 0 deletions nbproject/project.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=
annotation.processing.run.all.processors=true
application.title=webOSLib
application.vendor=Jason
auxiliary.org-netbeans-spi-editor-hints-projects.perProjectHintSettingsFile=nbproject/cfg_hints.xml
build.classes.dir=${build.dir}/classes
build.classes.excludes=**/*.java,**/*.form
# This directory is removed when the project is cleaned:
Expand All @@ -18,17 +25,22 @@ debug.test.classpath=\
dist.dir=dist
dist.jar=${dist.dir}/webOSLib.jar
dist.javadoc.dir=${dist.dir}/javadoc
endorsed.classpath=
excludes=
file.reference.json.jar=C:\\Users\\Jason\\NetBeansProjects\\json.jar
file.reference.tar.jar=C:\\Users\\Jason\\NetBeansProjects\\tar.jar
includes=**
jar.archive.disabled=${jnlp.enabled}
jar.compress=false
jar.index=${jnlp.enabled}
javac.classpath=\
${file.reference.tar.jar}:\
${file.reference.json.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
javac.processorpath=\
${javac.classpath}
javac.source=1.5
javac.target=1.5
javac.test.classpath=\
Expand All @@ -47,7 +59,21 @@ javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
jnlp.codebase.type=no.codebase
jnlp.descriptor=application
jnlp.enabled=false
jnlp.mixed.code=default
jnlp.offline-allowed=false
jnlp.signed=false
jnlp.signing=
jnlp.signing.alias=
jnlp.signing.keystore=
# Optional override of default Codebase manifest attribute, use to prevent RIAs from being repurposed
manifest.custom.codebase=
# Optional override of default Permissions manifest attribute (supported values: sandbox, all-permissions)
manifest.custom.permissions=
meta.inf.dir=${src.dir}/META-INF
mkdist.disabled=false
platform.active=default_platform
run.classpath=\
${javac.classpath}:\
Expand Down
136 changes: 136 additions & 0 deletions src/ca/canuckcoding/adb/Adb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@

package ca.canuckcoding.adb;

import ca.canuckcoding.utils.TextStreamConsumer;
import ca.canuckcoding.webos.WebOSConnection;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;

/**
* @author Jason Robitaille
*/
public class Adb {
public static boolean isInstalled() {
boolean isInstalled = true;
attemptStartAdbServer();
try {
AdbSocket adb = new AdbSocket();
isInstalled = adb.sendCommand("host:version");
adb.close();
} catch(Exception e) {
isInstalled = false;
}
return isInstalled;
}

public static AdbDevice[] listDevices() throws AdbException {
ResourceBundle locale = ResourceBundle.getBundle("ca/canuckcoding/webos/Locale");
ArrayList<AdbDevice> devices = new ArrayList<AdbDevice>();
try {
AdbSocket adb = new AdbSocket();
if(adb.sendCommand("host:devices")) {
String line = adb.readline();
while(line!=null) {
if(line.length()>4) {
String[] parts = line.substring(4).split("\\s+");
if(parts.length>1) {
AdbDevice curr = new AdbDevice(adb.getHost(), adb.getPort(), parts[0]);
if(isWebOS(curr)) {
devices.add(curr);
}
}
}
line = adb.readline();
}
}
adb.close();
} catch(Exception e) {}
return devices.toArray(new AdbDevice[devices.size()]);
}

private static boolean isWebOS(AdbDevice device) {
boolean result = false;
try {
WebOSConnection con = device.connect();
try {
String out1 = con.runProgram("/bin/cat", new String[] {"/etc/os-release"});
result |= out1.toString().contains("webos");
}catch(Exception e1) {}
try {
String out2 = con.runProgram("/bin/cat", new String[] {"/etc/webos-release"});
result |= out2.toString().contains("luneos");
}catch(Exception e2) {}
} catch(Exception e) {}
return result;
}

public static void attemptStartAdbServer() {
try {
Process adb = Runtime.getRuntime().exec("adb devices");
doProcess(adb);
} catch (Exception e) {}
}

public static void launchTerminal(AdbDevice device) {
Process launcher = null;
try {
String platform = System.getProperty("os.name").toLowerCase();
if(platform.contains("windows") || platform.contains("linux")) {
String tmpFilePath = System.getProperty("java.io.tmpdir");
File script = new File(tmpFilePath, "adb.cmd");
if(script.exists()) {
script.delete();
}
BufferedWriter bw = new BufferedWriter(new FileWriter(script));
bw.write("adb -s " + device.getId() + " shell");
bw.flush();
bw.close();
if(platform.contains("windows")) {
launcher = Runtime.getRuntime().exec("cmd.exe /c start " +
script.getAbsolutePath());
} else if(platform.contains("linux")) {
launcher = Runtime.getRuntime().exec(new String[] {"xterm", "+hold",
"-e", "sh \"" + script.getAbsolutePath() + "\""});
}
} else if(platform.contains("mac")) {
launcher = Runtime.getRuntime().exec(new String[] {"/usr/bin/osascript",
"-e", "tell application \"Terminal\" to do script \"" +
"adb -s " + device.getId() + " shell\""});
}
if(launcher!=null) {
OutputStream os = launcher.getOutputStream();
os.flush();
os.close();
TextStreamConsumer stdout = new TextStreamConsumer(launcher.getInputStream());
stdout.start();
TextStreamConsumer stderr = new TextStreamConsumer(launcher.getErrorStream());
stderr.start();
}
} catch(Exception e) {
JOptionPane.showMessageDialog(null, "Unable to launch terminal access.\nMake sure adb "
+ "is in your PATH environment variable");
}
}

private static boolean doProcess(Process p) throws IOException, InterruptedException {
OutputStream os;
TextStreamConsumer stdout, stderr;
os = p.getOutputStream();
os.flush();
os.close();
stdout = new TextStreamConsumer(p.getInputStream());
stdout.start();
stderr = new TextStreamConsumer(p.getErrorStream());
stderr.start();
int exitCode = p.waitFor();
stdout.waitFor();
stderr.waitFor();
return (exitCode==0);
}
}
Loading

0 comments on commit d498c45

Please sign in to comment.