Last active
November 19, 2020 13:55
-
-
Save Noitidart/400261f83b1be721a93a to your computer and use it in GitHub Desktop.
_ff-addon-tutorial-jniAndroidToast - Using js-ctypes to use JNI to call functions from the Android API, this specific example does the same as the NativeWindow component, it creates a toast message as seen here: https://developer.mozilla.org/en-US/Add-ons/Firefox_for_Android/API/NativeWindow/toast
This file contains 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
var my_jenv = null; | |
try { | |
my_jenv = JNI.GetForThread(); | |
// We declare the classes, methods, etc we will be using: this part is not seen in the native example | |
var SIG = { | |
CharSequence: 'Ljava/lang/CharSequence;', | |
Context: 'Landroid/content/Context;', | |
GeckoAppShell: 'Lorg.mozilla.gecko.GeckoAppShell;', | |
Toast: 'Landroid/widget/Toast;', // can use .'s or /'s so `android.widget.Toast` and `android/widget/Toast` both work // from docs page header http://developer.android.com/reference/android/widget/Toast.html#makeText%28android.content.Context,%20int,%20int%29 | |
int: 'I', // from https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp9502 | |
void: 'V' // from http://www.rgagnon.com/javadetails/java-0286.html | |
}; | |
var Toast = JNI.LoadClass(my_jenv, SIG.Toast.substr(1, SIG.Toast.length - 2), { | |
static_fields: [ | |
{ name: 'LENGTH_SHORT', sig: SIG.int }], | |
static_methods: [ | |
{ name: 'makeText', sig: '(' + SIG.Context + SIG.CharSequence + SIG.int + ')' + SIG.Toast } // http://developer.android.com/reference/android/widget/Toast.html#makeText%28android.content.Context,%20int,%20int%29 | |
], | |
methods: [ | |
{ name: 'show', sig: '()' + SIG.void } // http://developer.android.com/reference/android/widget/Toast.html#show%28%29 | |
] | |
}); | |
var geckoAppShell = JNI.LoadClass(my_jenv, SIG.GeckoAppShell.substr(1, SIG.GeckoAppShell.length - 2), { | |
static_methods: [ | |
{ name: 'getContext', sig: '()' + SIG.Context } | |
] | |
}); | |
// ok this ends the JNI specific stuff, now below you will see native-by-jni side-by-side | |
// Context context = getApplicationContext(); | |
var context = geckoAppShell.getContext(); | |
// CharSequence text = 'Hello toast!'; | |
var text = 'Hello toast!'; | |
// int duration = Toast.LENGTH_SHORT; | |
var duration = Toast.LENGTH_SHORT; | |
// Toast toast = Toast.makeText(context, text, duration); | |
var toast = Toast.makeText(context, text, duration); | |
toast.show(); | |
} finally { | |
if (my_jenv) { | |
JNI.UnloadClasses(my_jenv); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README
Screencast to go along with this Gist showing you how to execute this code from WebIDE on your desktop to your mobile device: Youtube Screencast :: FxAndroid - JNI js-ctypes Toast
Rev1 - Rev8
Based on the original native code from the android docs page:
Revisions from the start of the conversion to the end of it, Rev8 works can copy-paste-run via webide it from scratchpad or drop it into your firefox for android addon, and then Rev9 is cleaned up version of it in preparation for upload to MDN
Rev9
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
http://developer.android.com/reference/android/widget/Toast.html#show%28%29
http://www.rgagnon.com/javadetails/java-0286.html
https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html#wp9502
http://mxr.mozilla.org/mozilla-central/source/mobile/android/modules/JNI.jsm#884 (its weird we thought this was evidence that if we pass to
LoadClass
withL
and;
around it, theJNI.jsm
would remove it, but it's not, possible bug? So I had to do the.substr
above)I switched to using
my_jenv
in place ofjenv
because the globalwindow
object has ajenv
variablehttps://gist.github.com/Noitidart/a67e7556027889070bb6#file-gistfile1-txt-L6-L10
These resources were used as reference and understanding of JNI.jsm was used but not needed for this specific example (but I did lift the
geckoShell
thing from here for theContext
) :Not possible without the chat from @capella, @wesj, and @rnewman:
Rev10
Rev11
getPreferredIconSize
method as its unused