📄 observerjni.cpp
字号:
/*
Copyright (c) 2008, Intel Corporation.
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 Intel Corporation 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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 "IntelMobileJNI.h"#ifdef SHOW_MEMORY_LEAK#include "DebugHeap.h"#define new new(__FILE__, __LINE__)#define malloc(size) (void *)new BYTE[size]#define free(o) delete o#endifusing namespace Intel::Mobile::BaseAPI;/* * Class: com_intel_mobile_base_Observer * Method: constructor * Signature: ()V */JNIEXPORT void JNICALL Java_com_intel_mobile_base_Observer_constructor( JNIEnv *JniEnv, jobject JObject ){ try{ // Create an instance of the class (native) ObserverWrapper * TheObserverWrapper = new ObserverWrapper( JniEnv, JObject ); if(TheObserverWrapper==NULL)printf("TheObserverWrapper NULL\n"); // Set the nativeObjRef member of the object (java)// JniEnv->SetLongField( JObject, FieldId, (jlong) TheObserverWrapper ); Set_nativeObjRef( JniEnv, JObject, (jlong) TheObserverWrapper );// printf("observer constructor 2\n"); } catch (IntelMobileException Ex) { printf("i want to object constructor,but fail1.\n"); ThrowException( JniEnv, Ex ); } catch (...) { printf("i want to object constructor,but fail....\n"); ThrowUnknownException( JniEnv, IntelMobileText("Observer"), IntelMobileText("constructor") ); }}/* * Class: com_intel_mobile_base_Observer * Method: close * Signature: ()V */JNIEXPORT void JNICALL Java_com_intel_mobile_base_Observer_Close( JNIEnv *JniEnv, jobject JObject ){ try { jclass JClass = JniEnv->GetObjectClass( JObject ); jfieldID FieldId = JniEnv->GetFieldID( JClass, "nativeObjRef", "J" ); ObserverWrapper * TheObserverWrapper = (ObserverWrapper *) JniEnv->GetLongField( JObject, FieldId ); if (TheObserverWrapper != NULL ) { JniEnv->DeleteGlobalRef( TheObserverWrapper->_ObserverRef ); delete TheObserverWrapper; JniEnv->SetLongField( JObject, FieldId, NULL ); } } catch (IntelMobileException Ex) { ThrowException( JniEnv, Ex ); } catch (...) { ThrowUnknownException( JniEnv, IntelMobileText("Observer"), IntelMobileText("close()") ); }}// ------------------------- ObserverWrapper --------------------------/* * When a event is triggered from native, it should be notified to the java client (we call it ClientObserver here). * This is like an asynchronous callback from native to binding. So two things we must get in native to fulfill the process, * 1. One is the reference to ClientObserver. We create a global reference across different callings. * As a result an explicit calling to method close() at client side is necessary to release this global reference. * 2. The other is the JNI interface pointer. * Just like virtual table pointer in COM, there's a function interface pointer in JNI to access the client JVM. * This pointer is called JNIEnv*, and its value is thread related. * At first, we create a ClientObserver and save the client JVM object at native side. * When an event is triggered from native, it's a separate thread compared with the main thread. * Here we need to get the JNIEnv* dynamically which is adhere to the client JVM current thread (may be the main thread). * Just call JVM->AttachCurrentThread() for start using, and JVM->DetachCurrentThread for end using. */ObserverWrapper::ObserverWrapper( JNIEnv *JniEnv, jobject JObject ){ JniEnv->GetJavaVM( &_JVM ); this->_ObserverRef = JniEnv->NewGlobalRef( JObject );}void ObserverWrapper::Notify( const Event& event ){ JNIEnv * JniEnv; this->_JVM->AttachCurrentThread( (void**) &JniEnv, NULL ); jclass jclassEvent = JniEnv->FindClass( "com/intel/mobile/base/Event" ); jmethodID MethodId = JniEnv->GetMethodID( jclassEvent, "<init>", "(J)V" ); jobject jobjectEvent = JniEnv->NewObject( jclassEvent, MethodId, (jlong) &event ); jclass jclassObserver = JniEnv->GetObjectClass( _ObserverRef ); jmethodID jmethodIDNotify = JniEnv->GetMethodID( jclassObserver, "Notify", "(Lcom/intel/mobile/base/Event;)V" ); JniEnv->CallVoidMethod( _ObserverRef, jmethodIDNotify, jobjectEvent ); this->_JVM->DetachCurrentThread();}jobject ObserverWrapper::GetObserver(){ return this->_ObserverRef;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -