diff --git a/src/mrimap.cpp b/src/mrimap.cpp index 4c5cb066..d7a9ae8b 100644 --- a/src/mrimap.cpp +++ b/src/mrimap.cpp @@ -23,11 +23,6 @@ * Authors: Björn Petersen * Purpose: Reading from IMAP servers, see header for details. * - ******************************************************************************* - * - * TODO: On Android, I think, the thread must be marked using - * AttachCurrentThread(), see MailCore2 code and https://developer.android.com/training/articles/perf-jni.html - * ******************************************************************************/ @@ -37,6 +32,7 @@ #include "mrmailbox.h" #include "mrimap.h" +#include "mrosnative.h" class MrImapThreadVal @@ -297,7 +293,15 @@ WorkingThread_Exit: void MrImap::StartupHelper(MrImap* imap) // static function { + #if defined(__ANDROID) || defined(ANDROID) + MrAndroidSetupThread(); + #endif + imap->WorkingThread(); + + #if defined(__ANDROID) || defined(ANDROID) + MrAndroidUnsetupThread(); + #endif } diff --git a/src/mrosnative.cpp b/src/mrosnative.cpp new file mode 100644 index 00000000..b611d0b5 --- /dev/null +++ b/src/mrosnative.cpp @@ -0,0 +1,78 @@ +/******************************************************************************* + * + * Messenger Backend + * Copyright (C) 2016 Björn Petersen Software Design and Development + * Contact: r10s@b44t.com, http://b44t.com + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see http://www.gnu.org/licenses/ . + * + ******************************************************************************* + * + * File: mrosnative.cpp + * Authors: Björn Petersen + * Purpose: JNI handling, see header for details. + * + ******************************************************************************/ + + +#include + +#include "mrmailbox.h" +#include "mrosnative.h" + + +/******************************************************************************* + * Android Natives + ******************************************************************************/ + + +#if defined(__ANDROID) || defined(ANDROID) + + +static JavaVM* s_jvm = NULL; // TODO: how is this set up? +static jint s_version = 0; + + +JNIEXPORT void JNICALL Java_com_libmailcore_MainThreadUtils_setupNative(JNIEnv * env, jobject object) +{ + // AutoreleasePool * pool = new AutoreleasePool(); + + env->GetJavaVM(&s_jvm); + s_version = env->GetVersion(); + //s_mainThreadUtils = reinterpret_cast(env->NewGlobalRef(object)); + //jclass localClass = env->FindClass("com/libmailcore/MainThreadUtils"); + //s_mainThreadUtilsClass = reinterpret_cast(env->NewGlobalRef(localClass)); + //MCAssert(s_mainThreadUtilsClass != NULL); + //MCTypesUtilsInit(); + + //pool->release(); +} + + +void MrAndroidSetupThread(void) +{ + assert(s_jvm);; + + JNIEnv* env = NULL; + s_jvm->AttachCurrentThread(&env, NULL); +} + + +void MrAndroidUnsetupThread(void) +{ + s_jvm->DetachCurrentThread(); +} + + +#endif // defined(__ANDROID) || defined(ANDROID) diff --git a/src/mrosnative.h b/src/mrosnative.h new file mode 100644 index 00000000..9c3e4c62 --- /dev/null +++ b/src/mrosnative.h @@ -0,0 +1,41 @@ +/******************************************************************************* + * + * Messenger Backend + * Copyright (C) 2016 Björn Petersen Software Design and Development + * Contact: r10s@b44t.com, http://b44t.com + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see http://www.gnu.org/licenses/ . + * + ******************************************************************************* + * + * File: mrosnative.h + * Authors: Björn Petersen + * Purpose: OS-native depending part, eg. for attaching threads in JNI + * + ******************************************************************************/ + + +#ifndef __MROSNATIVE_H__ +#define __MROSNATIVE_H__ + + +#if defined(__ANDROID) || defined(ANDROID) + +void MrAndroidSetupThread(void); +void MrAndroidUnsetupThread(void); + +#endif // defined(__ANDROID) || defined(ANDROID) + + +#endif // __MROSNATIVE_H__