Implement C-to-Java callback bridge.

This commit is contained in:
B. Petersen
2016-10-15 18:06:28 +02:00
parent 47261f0aa3
commit 176f0deccd
4 changed files with 38 additions and 8 deletions
+30 -7
View File
@@ -43,7 +43,7 @@
/* our log handler */
void mr_android_log_callback_(int type, const char* msg)
static void s_log_callback_(int type, const char* msg)
{
int prio;
@@ -59,16 +59,26 @@ void mr_android_log_callback_(int type, const char* msg)
/* globl stuff */
static void s_init_globals(JNIEnv *env)
static JNIEnv* s_env = NULL;
static jclass s_MrMailbox_class = NULL;
static jmethodID s_MrCallback_methodID = NULL;
static void s_init_globals(JNIEnv *env, jclass MrMailbox_class)
{
/* make sure, the intialisation is done only once */
static bool s_global_init_done = 0;
if( s_global_init_done ) { return; }
s_global_init_done = 1;
/* init globals */
mrlog_set_handler(mr_android_log_callback_);
/* init global callback */
mrlog_set_handler(s_log_callback_);
/* get a global pointer to the MrMailbox class and to the MrCallback method; note that this may _not_ work from other-than-java-main threads */
s_env = env;
s_MrMailbox_class = (*env)->NewGlobalRef(env, MrMailbox_class);
s_MrCallback_methodID = (*env)->GetStaticMethodID(env, MrMailbox_class, "MrCallback","(IJJ)J" /*signature as "(param)ret" with I=int, J=long*/ );
/* system-specific backend initialisations */
mrosnative_init_android(env); /*this should be called before any other "important" routine is called*/
}
@@ -80,11 +90,24 @@ static void s_init_globals(JNIEnv *env)
/* MrMailbox - new/delete */
static uintptr_t s_mailbox_callback_(mrmailbox_t* mailbox, int event, uintptr_t data1, uintptr_t data2)
{
jlong l;
if( s_env==NULL || s_MrMailbox_class==NULL || s_MrCallback_methodID==NULL ) {
s_log_callback_('e', "Callback called but JNI not ready.");
return 0;
}
l = (*s_env)->CallStaticLongMethod(s_env, s_MrMailbox_class, s_MrCallback_methodID, (jint)event, (jlong)data1, (jlong)data2);
return (uintptr_t)l;
}
JNIEXPORT jlong Java_org_telegram_messenger_MrMailbox_MrMailboxNew(JNIEnv *env, jclass c)
{
s_init_globals(env);
return (jlong)mrmailbox_new();
s_init_globals(env, c);
return (jlong)mrmailbox_new(s_mailbox_callback_, NULL);
}
@@ -419,7 +442,7 @@ JNIEXPORT void Java_org_telegram_messenger_MrMailbox_MrStockAddStr(JNIEnv* env,
JNIEXPORT jstring Java_org_telegram_messenger_MrMailbox_MrGetVersionStr(JNIEnv *env, jclass c)
{
s_init_globals(env);
s_init_globals(env, c);
const char* temp = mr_get_version_str();
jstring ret = JSTRING_NEW(temp);
free(temp);
@@ -295,6 +295,7 @@ public class ApplicationLoader extends Application {
// EDIT BY MR - create a MrMailbox object; as android stops the App by just killing it, we do never call MrMailboxUnref()
// however, we may want to to have a look at onPause() eg. of activities (eg. for flushing data, if needed)
MrMailbox.MrCallback(0, 0, 0); // do not remove this call; this makes sure, the function is not removed from build or warnings are printed!
MrMailbox.hMailbox = MrMailbox.MrMailboxNew();
MrMailbox.MrStockAddStr(2, LocaleController.getString("FromYou", R.string.FromYou));
@@ -82,6 +82,12 @@ public class MrMailbox {
return ret;
}
// this function is called from within the C-wrapper
public static long MrCallback(int event, long data1, long data2)
{
return 0;
}
// MrMailbox objects
public native static long MrMailboxNew (); // returns hMailbox which must be unref'd after usage (Names as mrmailbox_new don't work due to the additional underscore)
public native static int MrMailboxOpen (long hMailbox, String dbfile, String blobdir);
@@ -157,7 +157,7 @@ public class MrNameSettingsActivity extends BaseFragment {
String cmd = v.substring(1);
String execute_result = MrMailbox.MrMailboxExecute(MrMailbox.hMailbox, cmd);
if( execute_result==null || execute_result.isEmpty()) {
execute_result = "ERROR: Unknwon command.";
execute_result = "ERROR: Unknown command.";
}
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setMessage(execute_result);