/* Copyright (c) 2009-2011, BogDan Vatra All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the BogDan Vatra nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY BogDan Vatra ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BogDan Vatra BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include static JavaVM *m_javaVM = NULL; static JNIEnv *m_env = NULL; static jobject objptr; static QSemaphore m_quitAppSemaphore; static QList m_applicationParams; static const char * const QtNativeClassPathName = "org/kde/necessitas/industrius/QtNative"; extern "C" int main(int, char **); //use the standard main method to start the application static void * startMainMethod(void * /*data*/) { char ** params; params=(char**)malloc(sizeof(char*)*m_applicationParams.length()); for (int i=0;iAttachCurrentThread(&env, NULL)<0) { qCritical()<<"AttachCurrentThread failed"; return false; } jclass applicationClass = env->GetObjectClass(objptr); if (applicationClass){ jmethodID quitApp = env->GetStaticMethodID(applicationClass, "quitApp", "()V"); env->CallStaticVoidMethod(applicationClass, quitApp); } m_javaVM->DetachCurrentThread(); return NULL; } static jboolean startQtApp(JNIEnv* env, jobject /*object*/, jstring paramsString, jstring environmentString) { qDebug()<<"startQtApp"; const char * nativeString = env->GetStringUTFChars(environmentString, 0); QByteArray string=nativeString; env->ReleaseStringUTFChars(environmentString, nativeString); m_applicationParams=string.split('\t'); qDebug()<<"environmentString"<GetStringUTFChars(paramsString, 0); string=nativeString; env->ReleaseStringUTFChars(paramsString, nativeString); qDebug()<<"paramsString"<FindClass(className); if (clazz == NULL) { __android_log_print(ANDROID_LOG_FATAL,"Qt", "Native registration unable to find class '%s'", className); return JNI_FALSE; } jmethodID constr = env->GetMethodID(clazz, "", "()V"); if(!constr) { __android_log_print(ANDROID_LOG_FATAL,"Qt", "Native registration unable to find constructor for class '%s'", className); return JNI_FALSE;; } jobject obj = env->NewObject(clazz, constr); objptr = env->NewGlobalRef(obj); if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) { __android_log_print(ANDROID_LOG_FATAL,"Qt", "RegisterNatives failed for '%s'", className); return JNI_FALSE; } return JNI_TRUE; } /* * Register native methods for all classes we know about. */ static int registerNatives(JNIEnv* env) { if (!registerNativeMethods(env, QtNativeClassPathName, methods, sizeof(methods) / sizeof(methods[0]))) return JNI_FALSE; return JNI_TRUE; } typedef union { JNIEnv* nativeEnvironment; void* venv; } UnionJNIEnvToVoid; Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* /*reserved*/) { __android_log_print(ANDROID_LOG_INFO,"Qt", "qt start"); UnionJNIEnvToVoid uenv; uenv.venv = NULL; m_javaVM = 0; if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) { __android_log_print(ANDROID_LOG_FATAL,"Qt","GetEnv failed"); return -1; } m_env = uenv.nativeEnvironment; if (!registerNatives(m_env)) { __android_log_print(ANDROID_LOG_FATAL, "Qt", "registerNatives failed"); return -1; } m_javaVM = vm; return JNI_VERSION_1_4; }