Last active
August 29, 2015 14:11
-
-
Save ispedals/693af8c7c815408a5890 to your computer and use it in GitHub Desktop.
Example using JNI.jsm to create a card for Ankidroid in Firefox for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
//JNI.jsm introduced requires Firefox for Android 34 | |
const { classes: Cc, interfaces: Ci, utils: Cu } = Components; | |
Cu.import("resource://gre/modules/Services.jsm"); | |
Cu.import("resource://gre/modules/XPCOMUtils.jsm"); | |
XPCOMUtils.defineLazyGetter(this, "JNI", function() { | |
let scope = {}; | |
Cu.import("resource://gre/modules/JNI.jsm", scope); | |
return scope.JNI; | |
}); | |
function launch() { | |
let jenv; | |
try { | |
jenv = JNI.GetForThread(); | |
let GeckoAppShell = JNI.LoadClass(jenv, "org.mozilla.gecko.GeckoAppShell", { | |
static_methods: [{ | |
name: "getContext", | |
sig: "()Landroid/content/Context;" | |
}] | |
}); | |
let Intent = JNI.LoadClass(jenv, "android.content.Intent", { | |
constructors: [{ | |
name: "<init>", | |
sig: "(Ljava/lang/String;)V" | |
}], | |
methods: [{ | |
name: "putExtra", | |
sig: "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;" | |
}] | |
}); | |
let Context = JNI.LoadClass(jenv, "android.content.Context", { | |
methods: [{ | |
name: "startActivity", | |
sig: "(Landroid/content/Intent;)V" | |
}] | |
}); | |
let context = GeckoAppShell.getContext(); | |
let intent = Intent["new"]("org.openintents.action.CREATE_FLASHCARD"); | |
intent.putExtra("SOURCE_TEXT", "Some Text"); | |
intent.putExtra("TARGET_TEXT", "Other Text"); | |
context.startActivity(intent); | |
} finally { | |
if (jenv) { | |
JNI.UnloadClasses(jenv); | |
} | |
} | |
} | |
var menuID = null; | |
function loadIntoWindow(window) { | |
menuID = window.NativeWindow.menu.add("Launch Intent", null, launch); | |
} | |
function unloadFromWindow(window) { | |
window.NativeWindow.menu.remove(menuID); | |
} | |
/** | |
* bootstrap.js API | |
*/ | |
var windowListener = { | |
onOpenWindow: function(aWindow) { | |
// Wait for the window to finish loading | |
function loadListener() { | |
domWindow.removeEventListener("load", loadListener, false); | |
loadIntoWindow(domWindow); | |
}; | |
domWindow.addEventListener("load", loadListener, false); | |
}, | |
onCloseWindow: function(aWindow) {}, | |
onWindowTitleChange: function(aWindow, aTitle) {} | |
}; | |
function startup(aData, aReason) { | |
// Load into any existing windows | |
let windows = Services.wm.getEnumerator("navigator:browser"); | |
while (windows.hasMoreElements()) { | |
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow); | |
loadIntoWindow(domWindow); | |
} | |
// Load into any new windows | |
Services.wm.addListener(windowListener); | |
} | |
function shutdown(aData, aReason) { | |
// When the application is shutting down we normally don't have to clean | |
// up any UI changes made | |
if (aReason == APP_SHUTDOWN) { | |
return; | |
} | |
// Stop listening for new windows | |
Services.wm.removeListener(windowListener); | |
// Unload from any existing windows | |
let windows = Services.wm.getEnumerator("navigator:browser"); | |
while (windows.hasMoreElements()) { | |
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow); | |
unloadFromWindow(domWindow); | |
} | |
} | |
function install(aData, aReason) {} | |
function uninstall(aData, aReason) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment