Commit dd48d605 authored by François Grisez's avatar François Grisez

Factorisation of wakelock.c

parent 7b27084d
......@@ -2,18 +2,41 @@
#include "belle-sip/utils.h"
#include <pthread.h>
static JavaVM *_jvm = NULL;
static jobject _powerManager = NULL;
static pthread_key_t jniEnvKey;
struct _WakeLock {
JavaVM *jvm;
jobject powerManager;
pthread_key_t jniEnvKey;
jint PARTIAL_WAKE_LOCK;
jmethodID newWakeLockID;
jmethodID acquireID;
jmethodID releaseID;
};
typedef struct _WakeLock WakeLock;
static WakeLock ctx = {
.jvm = NULL,
.powerManager = NULL
};
static JNIEnv *get_jni_env(void);
static void jni_key_cleanup(void *data);
void bellesip_wake_lock_init(JNIEnv *env, jobject pm) {
if(_jvm == NULL) {
(*env)->GetJavaVM(env, &_jvm);
_powerManager = (*env)->NewGlobalRef(env, pm);
pthread_key_create(&jniEnvKey, jni_key_cleanup);
if(ctx.jvm == NULL) {
(*env)->GetJavaVM(env, &ctx.jvm);
ctx.powerManager = (*env)->NewGlobalRef(env, pm);
pthread_key_create(&ctx.jniEnvKey, jni_key_cleanup);
jclass powerManagerClass = (*env)->FindClass(env, "android/os/PowerManager");
jclass wakeLockClass = (*env)->FindClass(env, "android/os/PowerManager$WakeLock");
jfieldID fieldID = (*env)->GetStaticFieldID(env, powerManagerClass, "PARTIAL_WAKE_LOCK", "I");
ctx.PARTIAL_WAKE_LOCK = (*env)->GetStaticIntField(env, powerManagerClass, fieldID);
ctx.newWakeLockID = (*env)->GetMethodID(env, powerManagerClass, "newWakeLock", "(ILjava/lang/String;)Landroid/os/PowerManager$WakeLock;");
ctx.acquireID = (*env)->GetMethodID(env, wakeLockClass, "acquire", "()V");
ctx.releaseID = (*env)->GetMethodID(env, wakeLockClass, "release", "()V");
belle_sip_message("bellesip_wake_lock_init(): initialization succeed");
} else {
belle_sip_error("bellesip_wake_lock_init(): the wakelock system has already been initialized");
......@@ -21,20 +44,20 @@ void bellesip_wake_lock_init(JNIEnv *env, jobject pm) {
}
void bellesip_wake_lock_uninit(JNIEnv *env) {
_jvm = NULL;
if(_powerManager != NULL) {
(*env)->DeleteGlobalRef(env, _powerManager);
_powerManager = NULL;
ctx.jvm = NULL;
if(ctx.powerManager != NULL) {
(*env)->DeleteGlobalRef(env, ctx.powerManager);
ctx.powerManager = NULL;
}
}
static void jni_key_cleanup(void *data) {
belle_sip_message("Thread end. Cleanup wake lock jni environment");
JNIEnv *env = pthread_getspecific(jniEnvKey);
JNIEnv *env = pthread_getspecific(ctx.jniEnvKey);
if(env != NULL) {
if(_jvm != NULL) {
(*_jvm)->DetachCurrentThread(_jvm);
pthread_setspecific(jniEnvKey, NULL);
if(ctx.jvm != NULL) {
(*ctx.jvm)->DetachCurrentThread(ctx.jvm);
pthread_setspecific(ctx.jniEnvKey, NULL);
} else {
belle_sip_fatal("Wake lock cleanup. No JVM found");
}
......@@ -43,11 +66,11 @@ static void jni_key_cleanup(void *data) {
static JNIEnv *get_jni_env(void) {
JNIEnv *jenv = NULL;
if(_jvm != NULL) {
jenv = pthread_getspecific(jniEnvKey);
if(ctx.jvm != NULL) {
jenv = pthread_getspecific(ctx.jniEnvKey);
if(jenv == NULL) {
if((*_jvm)->AttachCurrentThread(_jvm, &jenv, NULL) == JNI_OK) {
pthread_setspecific(jniEnvKey, jenv);
if((*ctx.jvm)->AttachCurrentThread(ctx.jvm, &jenv, NULL) == JNI_OK) {
pthread_setspecific(ctx.jniEnvKey, jenv);
belle_sip_message("get_jni_env(): thread successfuly attached");
} else {
belle_sip_fatal("get_jni_env(): thread could not be attached to the JVM");
......@@ -61,25 +84,15 @@ static JNIEnv *get_jni_env(void) {
}
unsigned long wake_lock_acquire(const char *tag) {
if(_jvm != NULL && _powerManager != NULL) {
if(ctx.jvm != NULL && ctx.powerManager != NULL) {
JNIEnv *env;
if(env = get_jni_env()) {
jclass PowerManager = (*env)->FindClass(env, "android/os/PowerManager");
jclass WakeLock = (*env)->FindClass(env, "android/os/PowerManager$WakeLock");
jfieldID fieldID = (*env)->GetStaticFieldID(env, PowerManager, "PARTIAL_WAKE_LOCK", "I");
jint PARTIAL_WAKE_LOCK = (*env)->GetStaticIntField(env, PowerManager, fieldID);
jstring tagString = (*env)->NewStringUTF(env, tag);
jmethodID newWakeLockID = (*env)->GetMethodID(env, PowerManager, "newWakeLock", "(ILjava/lang/String;)Landroid/os/PowerManager$WakeLock;");
jobject lock = (*env)->CallObjectMethod(env, _powerManager, newWakeLockID, PARTIAL_WAKE_LOCK, tagString);
jobject lock = (*env)->CallObjectMethod(env, ctx.powerManager, ctx.newWakeLockID, ctx.PARTIAL_WAKE_LOCK, tagString);
(*env)->DeleteLocalRef(env, tagString);
jmethodID acquireID = (*env)->GetMethodID(env, WakeLock, "acquire", "()V");
(*env)->CallVoidMethod(env, lock, acquireID);
belle_sip_message("bellesip_wake_lock_acquire(): Android wake lock acquired [ref=%p]", (void *)lock);
(*env)->CallVoidMethod(env, lock, ctx.acquireID);
lock = (*env)->NewGlobalRef(env, lock);
belle_sip_message("bellesip_wake_lock_acquire(): Android wake lock acquired [ref=%p]", (void *)lock);
return (unsigned long)lock;
} else {
belle_sip_error("bellesip_wake_lock_acquire(): cannot attach current thread to the JVM");
......@@ -93,13 +106,12 @@ unsigned long wake_lock_acquire(const char *tag) {
void wake_lock_release(unsigned long id) {
if(_jvm != NULL && _powerManager != NULL) {
if(ctx.jvm != NULL && ctx.powerManager != NULL) {
if(id != 0) {
jobject lock = (jobject)id;
JNIEnv *env;
if(env = get_jni_env()) {
jclass WakeLock = (*env)->FindClass(env, "android/os/PowerManager$WakeLock");
(*env)->CallVoidMethod(env, lock, (*env)->GetMethodID(env, WakeLock, "release", "()V"));
(*env)->CallVoidMethod(env, lock, ctx.releaseID);
belle_sip_message("wake_lock_release(): Android wake lock released [ref=%p]", (void *)lock);
(*env)->DeleteGlobalRef(env, lock);
} else {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment