/// --------------------------------------------------------------------------- /// Resumes playing the current song. /// --------------------------------------------------------------------------- void prJNI_SongResume() { JavaVM *pJavaVM = prJNI_GetVM(); PRASSERT(pJavaVM); if (pJavaVM) { bool isAttached = false; JNIEnv *env = NULL; // Get environment. if (!prJNI_GetEnv(&env, isAttached)) return; // Find class jclass cls = prJNI_GetAudioClass(env, "Music", isAttached); if (!cls) return; // Find the callBack method ID jmethodID method = env->GetStaticMethodID(cls, "songResume", "()V"); if (!method) { prTrace(LogError, "Failed to get method ID %s", "songResume"); if (isAttached) { pJavaVM->DetachCurrentThread(); } return; } // Call. env->CallStaticVoidMethod(cls, method); // And done if (isAttached) { pJavaVM->DetachCurrentThread(); } } }
JNIEnv *Kawaz::OUYA::OuyaController::getJNIEnv() { JavaVM* jvm = cocos2d::JniHelper::getJavaVM(); if (NULL == jvm) { CCLOG("Failed to get JNIEnv. JniHelper::getJavaVM() is NULL"); return NULL; } JNIEnv *env = NULL; // get jni environment jint ret = jvm->GetEnv((void**) &env, JNI_VERSION_1_4); switch (ret) { case JNI_OK: // Success! return env; case JNI_EDETACHED: // Thread not attached // TODO : If calling AttachCurrentThread() on a native thread // must call DetachCurrentThread() in future. // see: http://developer.android.com/guide/practices/design/jni.html if (jvm->AttachCurrentThread(&env, NULL) < 0) { CCLOG("Failed to get the environment using AttachCurrentThread()"); return NULL; } else { // Success : Attached and obtained JNIEnv! return env; } case JNI_EVERSION: // Cannot recover from this error CCLOG("JNI interface version 1.4 not supported"); default: CCLOG("Failed to get the environment using GetEnv()"); return NULL; } }
int createServiceProxy(int id) { JavaVM *jvm; /* representa uma VM Java */ JNIEnv *env; /* ponteiro para a interface de metodos nativos */ jvm = bftsmartdiversity::jvm; env = bftsmartdiversity::env; /* invoke the Main.test method using the JNI */ jclass cls = env->FindClass("bftsmart/diversity/ClientWrapper"); if (cls == NULL) { std::cout << "ERRO no FindClass diversity" << std::endl; jvm->DestroyJavaVM(); return 0x8100; } jint arg = (jint) id; jmethodID constru = env->GetMethodID(cls, "<init>", "(I)V"); if (constru == NULL) { std::cout << "ERRO ao construir wrapper getmid" << std::endl; jvm->DestroyJavaVM(); return 0x8200; } bftsmartdiversity::serviceProxy = env->NewObject(cls, constru, arg); if (bftsmartdiversity::serviceProxy == NULL) { std::cout << "ERRO ao construir wrapper int. srvprox" << std::endl; jvm->DestroyJavaVM(); return 0x8201; } bftsmartdiversity::invokeOrderedMethod = env->GetMethodID(cls, "invokeOrdered", "([B)[B"); bftsmartdiversity::invokeUnorderedMethod = env->GetMethodID(cls, "invokeUnordered", "([B)[B"); if (bftsmartdiversity::invokeOrderedMethod == NULL || bftsmartdiversity::invokeUnorderedMethod == NULL) { std::cout << "ERRO ao obter execute(un) ordered " << std::endl; jvm->DestroyJavaVM(); return 0x8107; } return 0x0; }
/// --------------------------------------------------------------------------- /// Logs out of facebook /// --------------------------------------------------------------------------- void prJNI_FacebookLogout() { JavaVM *pJavaVM = prJNI_GetVM(); PRASSERT(pJavaVM); if (pJavaVM) { bool isAttached = false; JNIEnv *env = NULL; // Get environment. if (!prJNI_GetEnv(&env, isAttached)) return; // Find class jclass cls = prJNI_GetFacebookClass(env, "Facebook", isAttached); if (!cls) return; // Find the callBack method ID jmethodID method = env->GetStaticMethodID(cls, "logOut", "()V"); if (!method) { __android_log_print(ANDROID_LOG_ERROR, "Proteus", "Failed to get method ID %s", "logOut"); if (isAttached) { pJavaVM->DetachCurrentThread(); } return; } // Call. env->CallStaticVoidMethod(cls, method); // And done if (isAttached) { pJavaVM->DetachCurrentThread(); } } }
JavaVMSingleton::JavaVMSingleton(const char* classPath, const char* javaLibPath) { JavaVM *jvms; jsize nVMs; if ( JNI_GetCreatedJavaVMs(&jvms, 1, &nVMs) == 0 ) { if (nVMs == 1) { JavaVM *jvm = jvms; JNIEnv *env; if( jvm->AttachCurrentThread((void **)&env, NULL) == 0) { init(jvm, env); return; } else throw VMOpenException(); } } init(classPath, javaLibPath); }
jobject LuaJavaBridge::checkVector(lua_State *L) { vector<string> strings; lua_pushnil(L); /* first key */ while (lua_next(L, -2)) { int index = lua_tonumber(L, -2) - 1; strings.push_back(checkObj(L)); lua_pop(L, 1); } JNIEnv *env = 0; JavaVM* jvm = cocos2d::JniHelper::getJavaVM(); jint ret = jvm->GetEnv((void**)&env, JNI_VERSION_1_4); switch (ret) { case JNI_OK: break; case JNI_EDETACHED : if (jvm->AttachCurrentThread(&env, NULL) < 0) { LOGD("Failed to get the environment using AttachCurrentThread()"); return NULL; } break; case JNI_EVERSION : default : LOGD("Failed to get the environment using GetEnv()"); return NULL; } PSJNIHelper::createVector(); for(vector<string>::iterator it = strings.begin(); it != strings.end(); ++it) { PSJNIHelper::pushVectorElement(*it); } return PSJNIHelper::getVector(); }
void GoogleStoreFront::getProducts(const char ** productIDs) const { __products.clear(); __requestedProducts.clear(); android_app* app = __state; JNIEnv* env = app->activity->env; JavaVM* vm = app->activity->vm; vm->AttachCurrentThread(&env, NULL); while (*productIDs) { __requestedProducts.insert(*productIDs); jstring paramString = env->NewStringUTF(*productIDs++); env->CallVoidMethod(app->activity->clazz, __midQueueSKURequest, paramString); } env->CallVoidMethod(app->activity->clazz, __midFlushSkuDetailsQueue); vm->DetachCurrentThread(); }
void ScilabJavaEnvironmentWrapper::unwraprowstring(int id, const ScilabStringStackAllocator & allocator) const { JNIEnv * curEnv = NULL; JavaVM * vm = getScilabJavaVM(); vm->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL); jobjectArray res = static_cast<jobjectArray>(curEnv->CallStaticObjectMethod(ScilabJavaObjectClass_, unwrapRowStringID_ , id)); if (curEnv->ExceptionCheck()) { throw GiwsException::JniCallMethodException(curEnv); } jint lenRow = curEnv->GetArrayLength(res); jboolean isCopy = JNI_FALSE; char ** addr = new char*[lenRow]; jstring * resString = new jstring[lenRow]; for (jsize i = 0; i < lenRow; i++) { resString[i] = reinterpret_cast<jstring>(curEnv->GetObjectArrayElement(res, i)); addr[i] = const_cast<char *>(curEnv->GetStringUTFChars(resString[i], &isCopy)); } allocator.allocate(1, lenRow, addr); for (jsize i = 0; i < lenRow; i++) { curEnv->ReleaseStringUTFChars(resString[i], addr[i]); curEnv->DeleteLocalRef(resString[i]); } delete[] addr; delete[] resString; curEnv->DeleteLocalRef(res); if (curEnv->ExceptionCheck()) { throw GiwsException::JniCallMethodException(curEnv); } }
void CLcxSndJni::Destroy() { if(0>m_fd) return; #if defined(__ANDROID__) JNIEnv* pEnv = NULL; JavaVM* pJvm = LCSYS::g_pJavaVM; jclass clzz = LCSYS::g_JniClzz; if(NULL == pJvm || 0 > pJvm->AttachCurrentThread(&pEnv, NULL)) { LOGE("SndJni::Play::Err::AttachCurrentThread::\n"); return; } pEnv->CallStaticIntMethod(clzz, Jni_SndDestroy, (jint)m_fd); #endif m_fd = -1; }
JniCallbackHelper::~JniCallbackHelper() { if (mMethodId != 0) { if (mEnv->ExceptionCheck()) { JNI_HELPER_LOGE( "There was an exception thrown from the Java method!!"); mEnv->ExceptionClear(); } } for (std::vector<jobject>::iterator it = mNewJavaObjects.begin(); it != mNewJavaObjects.end(); ++it) { mEnv->DeleteLocalRef(*it); } mEnv->DeleteGlobalRef(mJObject); if (JNI_EDETACHED == mGetEnvResult) { JavaVM * vm; mEnv->GetJavaVM(&vm); vm->DetachCurrentThread(); } }
void OpenSLMediaPlayerHQVisualizerJNIBinder::onLeaveInternalPeriodicCaptureThread( OpenSLMediaPlayerHQVisualizer *visualizer) noexcept { LOGD("onLeaveInternalPeriodicCaptureThread()"); JavaVM *vm = jvm_; if (jvm_attached_) { // release global references for (auto &t : jwaveform_data_) { t.release(env_); } for (auto &t : jfft_data_) { t.release(env_); } // detach (void)vm->DetachCurrentThread(); jvm_attached_ = false; } env_ = nullptr; }
INT CLcxSndJni::SetRepeat(INT Repeat) { m_rpt = Repeat; #if defined(__ANDROID__) JNIEnv* pEnv = NULL; JavaVM* pJvm = LCSYS::g_pJavaVM; jclass clzz = LCSYS::g_JniClzz; if(NULL == pJvm || 0 > pJvm->AttachCurrentThread(&pEnv, NULL)) { LOGE("SndJni::SetRepeat::Err::AttachCurrentThread::\n"); return LC_EFAIL; } jint rpt = ((INT)LCX_AV_INFINITE == m_rpt)? 1: 0; pEnv->CallStaticIntMethod(clzz, Jni_SndSetData, (jint)m_fd, 0x01, rpt); #endif return LC_OK; }
void JNIScheduler::registered(SchedulerDriver* driver, const FrameworkID& frameworkId, const MasterInfo& masterInfo) { jvm->AttachCurrentThread(JNIENV_CAST(&env), NULL); jclass clazz = env->GetObjectClass(jdriver); jfieldID scheduler = env->GetFieldID(clazz, "scheduler", "Lorg/apache/mesos/Scheduler;"); jobject jscheduler = env->GetObjectField(jdriver, scheduler); clazz = env->GetObjectClass(jscheduler); // sched.registered(driver, frameworkId, masterInfo); jmethodID registered = env->GetMethodID(clazz, "registered", "(Lorg/apache/mesos/SchedulerDriver;" "Lorg/apache/mesos/Protos$FrameworkID;" "Lorg/apache/mesos/Protos$MasterInfo;)V"); jobject jframeworkId = convert<FrameworkID>(env, frameworkId); jobject jmasterInfo = convert<MasterInfo>(env, masterInfo); env->ExceptionClear(); env->CallVoidMethod(jscheduler, registered, jdriver, jframeworkId, jmasterInfo); if (env->ExceptionCheck()) { env->ExceptionDescribe(); env->ExceptionClear(); jvm->DetachCurrentThread(); driver->abort(); return; } jvm->DetachCurrentThread(); }
int main(int argc, char** argv) { JavaVMInitArgs args; args.version = JNI_VERSION_1_1; args.nOptions = 1; args.options = new JavaVMOption[1]; args.options->optionString = (char*)"/home/benland100/Desktop/sjvm/javatest/"; JavaVM *vm; JNIEnv *env; JNI_CreateJavaVM(&vm,(void**)&env,(void*)&args); delete [] args.options; //jclass system = env->FindClass("java/lang/System"); //jclass printstream = env->FindClass("java/io/PrintStream"); //jfieldID outID = env->GetStaticFieldID(system,"out","Ljava/io/PrintStream;"); //jobject out = env->GetStaticObjectField(system,outID); //cout << (int)out << '\n'; //jclass mainclass = env->FindClass("smart/Client"); //jmethodID main = env->GetStaticMethodID(mainclass,"main","([Ljava/lang/String;)V"); //env->CallStaticVoidMethod(mainclass,main,env->NewObjectArray(0,env->FindClass("java/lang/String"),0)); //jobject obj = env->AllocObject(exception);//env->NewObject(exception,einit); //env->CallVoidMethod(obj,einit); //jclass test = env->FindClass("fields/subclass"); jclass mainclass = env->FindClass("javatest"); jmethodID main = env->GetStaticMethodID(mainclass,"main","([Ljava/lang/String;)V"); try { env->CallStaticVoidMethod(mainclass,main); if (env->ExceptionOccurred() == 0) { cout << "Executing Success\n"; } else { cout << "Damn, exceptions...\n"; } } catch ( ... ) { } env->DeleteLocalRef(mainclass); vm->DestroyJavaVM(); return 0; }
int closeKeyboardIME() { ANativeActivity *activity = sf::getNativeActivity(); JavaVM* vm = activity->vm; JNIEnv* env = activity->env; JavaVMAttachArgs attachargs; attachargs.version = JNI_VERSION_1_6; attachargs.name = "NativeThread"; attachargs.group = NULL; jint res = vm->AttachCurrentThread(&env, &attachargs); if (res == JNI_ERR) return EXIT_FAILURE; jclass natact = env->FindClass("android/app/NativeActivity"); jclass context = env->FindClass("android/content/Context"); jfieldID fid = env->GetStaticFieldID(context, "INPUT_METHOD_SERVICE", "Ljava/lang/String;"); jobject svcstr = env->GetStaticObjectField(context, fid); jmethodID getss = env->GetMethodID(natact, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); jobject imm_obj = env->CallObjectMethod(activity->clazz, getss, svcstr); jclass imm_cls = env->GetObjectClass(imm_obj); jmethodID toggleSoftInput = env->GetMethodID(imm_cls, "toggleSoftInput", "(II)V"); env->CallVoidMethod(imm_obj, toggleSoftInput, 1, 0); env->DeleteLocalRef(imm_obj); env->DeleteLocalRef(imm_cls); env->DeleteLocalRef(svcstr); env->DeleteLocalRef(context); env->DeleteLocalRef(natact); vm->DetachCurrentThread(); return EXIT_SUCCESS; }
void show_ads (void) { // Get the android application's activity. ANativeActivity* activity = gEngine.app->activity; JavaVM* jvm = activity->vm; JNIEnv* env = NULL; DTboolean needs_detach = false; int env_stat = jvm->GetEnv( (void**) &env, JNI_VERSION_1_6); if (env_stat == JNI_EDETACHED) { jvm->AttachCurrentThread(&env, 0); needs_detach = true; } ASSERT(env); jclass c_activity = env->GetObjectClass(activity->clazz); jmethodID m_show_ad_popup = env->GetMethodID(c_activity, "showAdPopup", "()V"); env->CallVoidMethod(activity->clazz, m_show_ad_popup); if (needs_detach) jvm->DetachCurrentThread(); }
INT CLcxSndJni::GetStatus() { #if defined(__ANDROID__) jint st = 0; JNIEnv* pEnv = NULL; JavaVM* pJvm = LCSYS::g_pJavaVM; jclass clzz = LCSYS::g_JniClzz; if(NULL == pJvm || 0 > pJvm->AttachCurrentThread(&pEnv, NULL)) { LOGE("SndJni::GetStatus::Err::AttachCurrentThread::\n"); return LC_EFAIL; } st = pEnv->CallStaticIntMethod(clzz, Jni_SndGetData, (jint)m_fd, 0x03); if(0 == st && LCX_AV_PLAY == m_state) m_state = LCX_AV_STOP; #endif return m_state; }
string callStringJavaMethod(const char* javaMethodName, string param1, string param2, string param3){ ANativeActivity* activity = NULL; JavaVM* jvm = NULL; JNIEnv* env = NULL; string result; if(prepareForCallJavaMethod(&activity, &jvm, &env)){ jclass clazz = env->GetObjectClass(activity->clazz); jmethodID methodID = env->GetMethodID(clazz, javaMethodName, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"); jstring jStringParam1 = env->NewStringUTF( param1.c_str() ); jstring jStringParam2 = env->NewStringUTF( param2.c_str() ); jstring jStringParam3 = env->NewStringUTF( param3.c_str() ); if( !jStringParam1 || !jStringParam2 || !jStringParam3 ) { __android_log_print(ANDROID_LOG_ERROR, "NativeActivityConnector#callStringJavaMethod", "Fallo al pasar string de ndk a java"); return result; } jstring jresult = (jstring) env->CallObjectMethod(activity->clazz, methodID, jStringParam1, jStringParam2, jStringParam3); result = jstringToString(jresult, env); env->DeleteLocalRef( jStringParam1 ); env->DeleteLocalRef( jStringParam2 ); env->DeleteLocalRef( jStringParam3 ); jvm->DetachCurrentThread(); } return result; }
JNIEnv * ofGetJNIEnv() { JNIEnv *env; JavaVM * vm = ofGetJavaVMPtr(); if(!vm) { ofLogError("ofAppAndroidWindow") << "couldn't get java virtual machine"; return NULL; } int getEnvStat = vm->GetEnv((void**) &env, JNI_VERSION_1_4); if (getEnvStat == JNI_EDETACHED) { if (vm->AttachCurrentThread(&env, NULL) != 0) { ofLogError("ofAppAndroidWindow") << "couldn't get environment using GetEnv()"; return NULL; } } else if (getEnvStat != JNI_OK) { ofLogError("ofAppAndroidWindow") << "couldn't get environment using GetEnv()"; return NULL; } return env; }
void CLcxSndJni::SetVolume(FLOAT fVol) { if(fVol<0) return; m_vol = fVol; #if defined(__ANDROID__) JNIEnv* pEnv = NULL; JavaVM* pJvm = LCSYS::g_pJavaVM; jclass clzz = LCSYS::g_JniClzz; if(NULL == pJvm || 0 > pJvm->AttachCurrentThread(&pEnv, NULL)) { LOGE("SndJni::SetVolume::Err::AttachCurrentThread::\n"); return; } jint vol = m_vol * 10000; pEnv->CallStaticIntMethod(clzz, Jni_SndSetData, (jint)m_fd, 0x02, vol); #endif }
void JNIExecutor::frameworkMessage(ExecutorDriver* driver, const string& data) { jvm->AttachCurrentThread(JNIENV_CAST(&env), nullptr); jclass clazz = env->GetObjectClass(jdriver); jfieldID executor = env->GetFieldID(clazz, "executor", "Lorg/apache/mesos/Executor;"); jobject jexecutor = env->GetObjectField(jdriver, executor); clazz = env->GetObjectClass(jexecutor); // executor.frameworkMessage(driver, data); jmethodID frameworkMessage = env->GetMethodID( clazz, "frameworkMessage", "(Lorg/apache/mesos/ExecutorDriver;" "[B)V"); // byte[] data = ..; jbyteArray jdata = env->NewByteArray(data.size()); env->SetByteArrayRegion(jdata, 0, data.size(), (jbyte*) data.data()); env->ExceptionClear(); env->CallVoidMethod(jexecutor, frameworkMessage, jdriver, jdata); if (env->ExceptionCheck()) { env->ExceptionDescribe(); env->ExceptionClear(); jvm->DetachCurrentThread(); driver->abort(); return; } jvm->DetachCurrentThread(); }
static bool set_wake_alarm_callout(uint64_t delay_millis, bool should_wake, alarm_cb cb, void *data) { JNIEnv *env; JavaVM *vm = AndroidRuntime::getJavaVM(); jint status = vm->GetEnv((void **)&env, JNI_VERSION_1_6); jboolean ret = JNI_FALSE; if (status != JNI_OK && status != JNI_EDETACHED) { ALOGE("%s unable to get environment for JNI call", __func__); return false; } if (status == JNI_EDETACHED && vm->AttachCurrentThread(&env, &sAttachArgs) != 0) { ALOGE("%s unable to attach thread to VM", __func__); return false; } sAlarmCallback = cb; sAlarmCallbackData = data; jboolean jshould_wake = should_wake ? JNI_TRUE : JNI_FALSE; if (sJniAdapterServiceObj) { ret = env->CallBooleanMethod(sJniAdapterServiceObj, method_setWakeAlarm, (jlong)delay_millis, jshould_wake); } else { ALOGE("JNI ERROR : JNI reference already cleaned : set_wake_alarm_callout", __FUNCTION__); } if (!ret) { sAlarmCallback = NULL; sAlarmCallbackData = NULL; } if (status == JNI_EDETACHED) { vm->DetachCurrentThread(); } return !!ret; }
ExecutorInfo JNIScheduler::getExecutorInfo(SchedulerDriver* driver) { jvm->AttachCurrentThread((void**) &env, NULL); jclass clazz = env->GetObjectClass(jdriver); jfieldID sched = env->GetFieldID(clazz, "sched", "Lorg/apache/mesos/Scheduler;"); jobject jsched = env->GetObjectField(jdriver, sched); clazz = env->GetObjectClass(jsched); // ExecutorInfo executor = sched.getExecutorInfo(driver); jmethodID getExecutorInfo = env->GetMethodID(clazz, "getExecutorInfo", "(Lorg/apache/mesos/SchedulerDriver;)" "Lorg/apache/mesos/Protos$ExecutorInfo;"); env->ExceptionClear(); jobject jexecutor = env->CallObjectMethod(jsched, getExecutorInfo, jdriver); if (env->ExceptionOccurred()) { env->ExceptionDescribe(); env->ExceptionClear(); jvm->DetachCurrentThread(); driver->stop(); this->error(driver, -1, "Java exception caught"); return ExecutorInfo(); } ExecutorInfo executor = construct<ExecutorInfo>(env, jexecutor); jvm->DetachCurrentThread(); return executor; }
std::string Database::getPath(std::string name) { std::string databasePath; JNIEnv* env; JavaVM* vm = engine->app->activity->vm; vm->AttachCurrentThread(&env, NULL); jclass clazz = env->GetObjectClass(engine->app->activity->clazz); jmethodID methodj = env->GetMethodID(clazz, "getDatabasePath", "(Ljava/lang/String;)Ljava/io/File;"); jobject filej = env->CallObjectMethod(engine->app->activity->clazz, methodj, env->NewStringUTF(name.c_str())); if (filej != NULL) { clazz = env->GetObjectClass(filej); methodj = env->GetMethodID(clazz, "getPath", "()Ljava/lang/String;"); jstring jstr = (jstring)env->CallObjectMethod(filej, methodj); if (jstr != NULL) { const char* str = env->GetStringUTFChars(jstr, NULL); databasePath = str; env->ReleaseStringUTFChars(jstr, str); } } vm->DetachCurrentThread(); return databasePath; }
string JNIScheduler::getFrameworkName(SchedulerDriver* driver) { jvm->AttachCurrentThread((void**) &env, NULL); jclass clazz = env->GetObjectClass(jdriver); jfieldID sched = env->GetFieldID(clazz, "sched", "Lorg/apache/mesos/Scheduler;"); jobject jsched = env->GetObjectField(jdriver, sched); clazz = env->GetObjectClass(jsched); // String name = sched.getFrameworkName(driver); jmethodID getFrameworkName = env->GetMethodID(clazz, "getFrameworkName", "(Lorg/apache/mesos/SchedulerDriver;)" "Ljava/lang/String;"); env->ExceptionClear(); jobject jname = env->CallObjectMethod(jsched, getFrameworkName, jdriver); if (env->ExceptionOccurred()) { env->ExceptionDescribe(); env->ExceptionClear(); jvm->DetachCurrentThread(); driver->stop(); this->error(driver, -1, "Java exception caught"); return ""; } string name = construct<string>(env, (jstring) jname); jvm->DetachCurrentThread(); return name; }
void JNIExecutor::frameworkMessage(ExecutorDriver* driver, const string& data) { jvm->AttachCurrentThread((void**) &env, NULL); jclass clazz = env->GetObjectClass(jdriver); jfieldID exec = env->GetFieldID(clazz, "exec", "Lorg/apache/mesos/Executor;"); jobject jexec = env->GetObjectField(jdriver, exec); clazz = env->GetObjectClass(jexec); // exec.frameworkMessage(driver, data); jmethodID frameworkMessage = env->GetMethodID(clazz, "frameworkMessage", "(Lorg/apache/mesos/ExecutorDriver;" "[B)V"); // byte[] data = ..; jbyteArray jdata = env->NewByteArray(data.size()); env->SetByteArrayRegion(jdata, 0, data.size(), (jbyte*) data.data()); env->ExceptionClear(); env->CallVoidMethod(jexec, frameworkMessage, jdriver, jdata); if (env->ExceptionOccurred()) { env->ExceptionDescribe(); env->ExceptionClear(); jvm->DetachCurrentThread(); driver->stop(); this->error(driver, -1, "Java exception caught"); return; } jvm->DetachCurrentThread(); }
// static void *TimedEventQueue::ThreadWrapper(void *me) { #ifdef ANDROID_SIMULATOR // The simulator runs everything as one process, so any // Binder calls happen on this thread instead of a thread // in another process. We therefore need to make sure that // this thread can do calls into interpreted code. // On the device this is not an issue because the remote // thread will already be set up correctly for this. JavaVM *vm; int numvms; JNI_GetCreatedJavaVMs(&vm, 1, &numvms); JNIEnv *env; vm->AttachCurrentThread(&env, NULL); #endif setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_FOREGROUND); static_cast<TimedEventQueue *>(me)->threadEntry(); #ifdef ANDROID_SIMULATOR vm->DetachCurrentThread(); #endif return NULL; }
void Instance::FinalizeCallback(JSObjectRef object) { JNIEnv *env; jvm->AttachCurrentThread(&env, NULL); jclass cls = env->GetObjectClass(thiz); jmethodID mid; do { mid = env->GetMethodID(cls,"finalizeCallback","(J)V"); if (!env->ExceptionCheck()) break; env->ExceptionClear(); cls = env->GetSuperclass(env->GetObjectClass(thiz)); if (cls == NULL || env->ExceptionCheck()) { return; } } while (true); env->CallVoidMethod(thiz, mid, (jlong)object); }
static unsigned int get_supported_rate(unsigned int prefered_rate) { //JNIEnv *jni_env = ms_get_jni_env(); JNIEnv *jni_env = NULL; JavaVM *jvm = ms_get_jvm(); if (jvm->AttachCurrentThread(&jni_env, NULL)!=0) { ms_fatal("AttachCurrentThread() failed !"); return 8000; } jclass audio_record_class = jni_env->FindClass("android/media/AudioRecord"); int size = jni_env->CallStaticIntMethod(audio_record_class ,jni_env->GetStaticMethodID(audio_record_class,"getMinBufferSize", "(III)I") ,prefered_rate ,2/*CHANNEL_CONFIGURATION_MONO*/ ,2/* ENCODING_PCM_16BIT */); if (size > 0) { jvm->DetachCurrentThread(); return prefered_rate; } else { ms_warning("Cannot configure recorder with rate [%i]",prefered_rate); if (prefered_rate>48000) { jvm->DetachCurrentThread(); return get_supported_rate(48000); } switch (prefered_rate) { case 12000: case 24000: return get_supported_rate(48000); case 48000: return get_supported_rate(44100); case 44100: return get_supported_rate(16000); case 16000: return get_supported_rate(8000); default: { ms_error("This Android sound card doesn't support any standard sample rate"); jvm->DetachCurrentThread(); return 0; } } jvm->DetachCurrentThread(); return 0; } }
static int SetThreadEvent(ThreadEvent event) { JavaVM* javaVm = AndroidRuntime::getJavaVM(); switch(event) { case ASSOCIATE_JVM: { if(sCallbackEnv != NULL) { ALOGE( "Attempted to associate callback in '%s'. Callback already associated.", __FUNCTION__ ); return FLP_RESULT_ERROR; } JavaVMAttachArgs args = { JNI_VERSION_1_6, "FLP Service Callback Thread", /* group */ NULL }; jint attachResult = javaVm->AttachCurrentThread(&sCallbackEnv, &args); if (attachResult != 0) { ALOGE("Callback thread attachment error: %d", attachResult); return FLP_RESULT_ERROR; } ALOGV("Callback thread attached: %p", sCallbackEnv); // Send the version to the upper layer. sCallbackEnv->CallVoidMethod( sCallbacksObj, sSetVersion, sFlpInterface->size == sizeof(FlpLocationInterface) ? 2 : 1 ); CheckExceptions(sCallbackEnv, __FUNCTION__); break; } case DISASSOCIATE_JVM: { if (!IsValidCallbackThread()) { ALOGE( "Attempted to dissasociate an unnownk callback thread : '%s'.", __FUNCTION__ ); return FLP_RESULT_ERROR; } if (javaVm->DetachCurrentThread() != 0) { return FLP_RESULT_ERROR; } sCallbackEnv = NULL; break; } default: ALOGE("Invalid ThreadEvent request %d", event); return FLP_RESULT_ERROR; } return FLP_RESULT_SUCCESS; }