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

Integration of Android's wake locks

parent 0837e201
......@@ -43,6 +43,10 @@
#include "belle-sip/belle-sdp.h"
#include "belle-sip/bodyhandler.h"
#ifdef ANDROID
#include "belle-sip/wakelock.h"
#endif
#define BELLE_SIP_POINTER_TO_INT(p) ((int)(long)(p))
#define BELLE_SIP_INT_TO_POINTER(i) ((void*)(long)(i))
......
#ifndef WAKE_LOCK_H
#define WAKE_LOCK_H
#include <jni.h>
extern void bellesip_wake_lock_init_jni(JNIEnv *env, jobject powerManager);
#endif // WALE_LOCK_H
......@@ -21,6 +21,10 @@
#include <limits.h>
#include <ctype.h>
#ifdef ANDROID
#include "wakelock_internal.h"
#endif
static void channel_prepare_continue(belle_sip_channel_t *obj);
static void channel_process_queue(belle_sip_channel_t *obj);
static void channel_begin_send_background_task(belle_sip_channel_t *obj);
......@@ -1299,7 +1303,17 @@ belle_sip_channel_t *belle_sip_channel_find_from_list(belle_sip_list_t *l, int a
return chan;
}
#if !TARGET_OS_IPHONE
#ifdef ANDROID
unsigned int belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data){
return wake_lock_acquire();
}
void belle_sip_end_background_task(unsigned int id){
wake_lock_release(id);
}
#elif !TARGET_OS_IPHONE
/*defines stubs*/
unsigned int belle_sip_begin_background_task(const char *name, belle_sip_background_task_end_callback_t cb, void *data){
......
#include "wakelock_internal.h"
#include "belle-sip/utils.h"
static JavaVM *_jvm = NULL;
static jobject _powerManager = NULL;
void bellesip_wake_lock_init(JavaVM *jvm, jobject powerManager) {
if(_jvm == NULL) {
_jvm = jvm;
_powerManager = powerManager;
} else {
ms_error("bellesip_wake_lock_init(): the wakelock system has already been initialized");
}
}
unsigned long wake_lock_acquire(const char *tag) {
if(_jvm != NULL && _powerManager != NULL) {
JNIEnv *env;
if(AttachCurrentThread(_jvm, &env, NULL) == JNI_OK) {
jclass PowerManager = FindClass(env, "android/os/PowerManager");
jclass WakeLock = FindClass(env, "android/os/PowerManager/WakeLock");
jint PARTIAL_WAKE_LOCK = getStaticIntField(
env,
powerManager,
GetFieldId(env, PowerManager, "PARTIAL_WAKE_LOCK", "I")
);
jobject lock = CallObjectMethod(
env,
_powerManager,
GetMethodID(env, PowerManager, "newWakeLock", "(ILjava/lang/String;)Landroid/os/PowerManager/WakeLock;" ),
PARTIAL_WAKE_LOCK,
tag);
CallVoidMethod(
env,
lock,
GetMethodID(env, WakeLock, "acquire", ""));
DetachCurrentThread(_jvm);
return (unsigned long)NewGlobalRef(env, lock);
} else {
ms_error("bellesip_wake_lock_acquire(): cannot attach current thread to the JVM");
}
} else {
ms_error("bellesip_wake_lock_acquire(): cannot acquire wake lock. No jvm found");
}
return 0;
}
void wake_lock_release(unsigned long id) {
if(_jvm != NULL && _powerManager != NULL) {
if(id != 0) {
jobject lock = (jobject)id;
JNIEnv *env;
if(AttachCurrentThread(_jvm, &env, NULL) == JNI_OK) {
jclass WakeLock = FindClass(env, "android/os/PowerManager/WakeLock");
CallVoidMethod(
env,
lock,
GetMethodID(env, WakeLock, "release", "()V"));
DeleteGlobalRef(env, lock);
DetachCurrentThread(_jvm);
} else {
ms_error("bellesip_wake_lock_release(): cannot attach current thread to the JVM");
}
}
} else {
ms_error("wake_lock_release(): cannot release wake lock. No jvm found");
}
}
#include "belle-sip/wakelock.h"
extern unsigned long wake_lock_acquire(const char *tag);
extern void wake_lock_release(unsigned long id);
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