Skip to content

Instantly share code, notes, and snippets.

@kmaglione
Created February 4, 2017 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmaglione/f955b9ce72850e1046e6c36459ddaeb1 to your computer and use it in GitHub Desktop.
Save kmaglione/f955b9ce72850e1046e6c36459ddaeb1 to your computer and use it in GitHub Desktop.
// Copyright (c) 2008-2014 by Kris Maglione <maglione.k@gmail.com>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE file included with this file.
var EXPORTED_SYMBOLS = ["adb"];
const OPTIONS_DIALOG = "adbify://content/options.xul";
var { Overlay, overlay } = require("overlay");
var { prefs } = require("prefs");
var { util } = require("util");
var { DOM, XUL } = require("dom");
lazyRequire("config", ["config", "dialogs"]);
lazyRequire("io", ["File", "io"]);
lazyRequire("messages", ["_"]);
lazyRequire("services", ["services"]);
var adb = Singleton("adb", {
get XPI_PATH() "/sdcard/" + config.prefs.get("fennec-app-id") + ".xpi",
REMOTE_ENABLED: "devtools.debugger.remote-enabled",
REMOTE_PORT: "devtools.debugger.remote-port",
init: function init() {
config.prefs.defaults.set("adb", "adb");
config.prefs.defaults.set("device-id",
services.environment.get("ANDROID_SERIAL"));
config.prefs.defaults.set("fennec-app-id",
"org.mozilla.firefox_beta");
config.prefs.defaults.set("remote-port",
prefs.defaults.get(this.REMOTE_PORT));
this.wasRemoteEnabled = prefs.defaults.get(this.REMOTE_ENABLED);
prefs.defaults.set(this.REMOTE_ENABLED, true);
},
cleanup: function cleanup() {
prefs.defaults.set(this.REMOTE_ENABLED, this.wasRemoteEnabled);
},
get adbPath() io.pathSearch(config.prefs.get("adb")),
_adb: function _adb(blocking, args) {
args = Array.slice(args);
let deviceId = config.prefs.get("device-id");
if (deviceId)
args = ["-s", deviceId].concat(args);
util.dump([this.adbPath.path].concat(args).map(io.quoteArg).join(" "));
io.run(this.adbPath, args, blocking);
},
adb: function adb() this._adb(false, arguments),
adbBlocking: function adbBlocking() this._adb(true, arguments),
checkADBPath: function checkADBPath(window) {
if (!(this.adbPath && this.adbPath.exists())) {
services.prompt.alert(null, "ADB Not Found",
"Please set a valid location for the ADB executable");
window.openDialog(OPTIONS_DIALOG, null, "chrome,dialog,centerscreen");
return false;
}
return true;
},
installXPI: function installXPI(url) {
let self = this;
function finish(file) {
self.adbBlocking("push", file.path, self.XPI_PATH);
self.adb("shell", "am", "start",
"-a", "android.intent.action.VIEW",
"-c", "android.intent.category.DEFAULT",
"-d", "file://" + self.XPI_PATH,
"-n", config.prefs.get("fennec-app-id") + "/.App");
}
let uri = util.newURI(url);
if (uri instanceof Ci.nsIFileURL)
finish(uri.file);
else {
let persist = services.Persist();
persist.persistFlags = persist.PERSIST_FLAGS_FROM_CACHE
| persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
let file = io.createTempFile("adbify.xpi");
let window = services.windowMediator.getMostRecentWindow("navigator:browser");
let downloadListener = new window.DownloadListener(window,
services.Transfer(uri, File(file).URI, "",
null, null, null, persist, false));
persist.progressListener = update(Object.create(downloadListener), {
onStateChange: util.wrapCallback(function onStateChange(progress, request, flags, status) {
let res = onStateChange.superapply(this, arguments);
if ((flags & Ci.nsIWebProgressListener.STATE_STOP) && status == 0)
util.trapErrors(finish, this, file);
return res;
})
});
persist.saveURI(uri, null, null, null, null, file.file, null);
}
},
forwardDebugPort: function forwardDebugPort() {
this.adbBlocking("forward",
"tcp:" + prefs.get(this.REMOTE_PORT),
"tcp:" + config.prefs.get("remote-port"));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment