Android: use NewGlobalRef for references that are used globally

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29568 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Maurus Cuelenaere 2011-03-11 18:34:31 +00:00
parent 7228f2fa9f
commit 2e5b7aebde
6 changed files with 64 additions and 29 deletions

View file

@ -59,14 +59,17 @@ static void kdb_init(void)
{ {
semaphore_init(&kbd_wakeup, 1, 0); semaphore_init(&kbd_wakeup, 1, 0);
/* get the class and its constructor */ /* get the class and its constructor */
RockboxKeyboardInput_class = e->FindClass(env_ptr, jclass kbInput_class = e->FindClass(env_ptr,
"org/rockbox/RockboxKeyboardInput"); "org/rockbox/RockboxKeyboardInput");
RockboxKeyboardInput_class = e->NewGlobalRef(env_ptr, kbInput_class);
jmethodID constructor = e->GetMethodID(env_ptr, jmethodID constructor = e->GetMethodID(env_ptr,
RockboxKeyboardInput_class, RockboxKeyboardInput_class,
"<init>", "()V"); "<init>", "()V");
RockboxKeyboardInput_instance = e->NewObject(env_ptr, jobject kbInput_instance = e->NewObject(env_ptr,
RockboxKeyboardInput_class, RockboxKeyboardInput_class,
constructor); constructor);
RockboxKeyboardInput_instance = e->NewGlobalRef(env_ptr,
kbInput_instance);
kbd_inputfunc = e->GetMethodID(env_ptr, RockboxKeyboardInput_class, kbd_inputfunc = e->GetMethodID(env_ptr, RockboxKeyboardInput_class,
"kbd_input", "kbd_input",
"(Ljava/lang/String;" "(Ljava/lang/String;"

View file

@ -125,13 +125,14 @@ void notification_init(void)
JNIEnv e = *env_ptr; JNIEnv e = *env_ptr;
jfieldID nNM = e->GetFieldID(env_ptr, RockboxService_class, jfieldID nNM = e->GetFieldID(env_ptr, RockboxService_class,
"fg_runner", "Lorg/rockbox/Helper/RunForegroundManager;"); "fg_runner", "Lorg/rockbox/Helper/RunForegroundManager;");
NotificationManager_instance = e->GetObjectField(env_ptr, jobject nMN_instance = e->GetObjectField(env_ptr,
RockboxService_instance, nNM); RockboxService_instance, nNM);
if (NotificationManager_instance == NULL) if (nMN_instance == NULL)
{ {
DEBUGF("Failed to get RunForegroundManager instance. Performance will be bad"); DEBUGF("Failed to get RunForegroundManager instance. Performance will be bad");
return; return;
} }
NotificationManager_instance = e->NewGlobalRef(env_ptr, nMN_instance);
jclass class = e->GetObjectClass(env_ptr, NotificationManager_instance); jclass class = e->GetObjectClass(env_ptr, NotificationManager_instance);
updateNotification = e->GetMethodID(env_ptr, class, "updateNotification", updateNotification = e->GetMethodID(env_ptr, class, "updateNotification",

View file

@ -30,7 +30,6 @@
#include "kernel.h" #include "kernel.h"
extern JNIEnv *env_ptr; extern JNIEnv *env_ptr;
static jclass RockboxYesno_class = NULL;
static jobject RockboxYesno_instance = NULL; static jobject RockboxYesno_instance = NULL;
static jmethodID yesno_func; static jmethodID yesno_func;
static struct semaphore yesno_done; static struct semaphore yesno_done;
@ -49,24 +48,25 @@ static void yesno_init(void)
{ {
JNIEnv e = *env_ptr; JNIEnv e = *env_ptr;
static jmethodID yesno_is_usable; static jmethodID yesno_is_usable;
if (RockboxYesno_class == NULL) if (RockboxYesno_instance == NULL)
{ {
semaphore_init(&yesno_done, 1, 0); semaphore_init(&yesno_done, 1, 0);
/* get the class and its constructor */ /* get the class and its constructor */
RockboxYesno_class = e->FindClass(env_ptr, jclass yesno_class = e->FindClass(env_ptr,
"org/rockbox/RockboxYesno"); "org/rockbox/RockboxYesno");
jmethodID constructor = e->GetMethodID(env_ptr, jmethodID constructor = e->GetMethodID(env_ptr,
RockboxYesno_class, yesno_class,
"<init>", "()V"); "<init>", "()V");
RockboxYesno_instance = e->NewObject(env_ptr, jobject yesno_instance = e->NewObject(env_ptr,
RockboxYesno_class, yesno_class,
constructor); constructor);
yesno_func = e->GetMethodID(env_ptr, RockboxYesno_class, RockboxYesno_instance = e->NewGlobalRef(env_ptr, yesno_instance);
yesno_func = e->GetMethodID(env_ptr, yesno_class,
"yesno_display", "yesno_display",
"(Ljava/lang/String;" "(Ljava/lang/String;"
"Ljava/lang/String;" "Ljava/lang/String;"
"Ljava/lang/String;)V"); "Ljava/lang/String;)V");
yesno_is_usable = e->GetMethodID(env_ptr, RockboxYesno_class, yesno_is_usable = e->GetMethodID(env_ptr, yesno_class,
"is_usable", "()Z"); "is_usable", "()Z");
} }
/* need to get it every time incase the activity died/restarted */ /* need to get it every time incase the activity died/restarted */

View file

@ -47,8 +47,16 @@ static bool display_on;
void connect_with_java(JNIEnv* env, jobject fb_instance) void connect_with_java(JNIEnv* env, jobject fb_instance)
{ {
JNIEnv e = *env; JNIEnv e = *env;
static bool have_class;
RockboxFramebuffer_instance = fb_instance; /* Update RockboxFramebuffer_instance */
if (!e->IsSameObject(env, RockboxFramebuffer_instance, fb_instance)) {
if (RockboxFramebuffer_instance != NULL)
e->DeleteGlobalRef(env, RockboxFramebuffer_instance);
RockboxFramebuffer_instance = e->NewGlobalRef(env, fb_instance);
}
static bool have_class = false;
if (!have_class) if (!have_class)
{ {
jclass fb_class = e->GetObjectClass(env, fb_instance); jclass fb_class = e->GetObjectClass(env, fb_instance);
@ -70,10 +78,13 @@ void connect_with_java(JNIEnv* env, jobject fb_instance)
java_lcd_init = e->GetMethodID(env, fb_class, java_lcd_init = e->GetMethodID(env, fb_class,
"java_lcd_init", "java_lcd_init",
"(IILjava/nio/ByteBuffer;)V"); "(IILjava/nio/ByteBuffer;)V");
native_buffer = e->NewDirectByteBuffer(env, jobject buffer = e->NewDirectByteBuffer(env,
lcd_framebuffer, lcd_framebuffer,
(jlong)sizeof(lcd_framebuffer)); (jlong)sizeof(lcd_framebuffer));
native_buffer = e->NewGlobalRef(env, buffer);
have_class = true; have_class = true;
} }
/* we need to setup parts for the java object every time */ /* we need to setup parts for the java object every time */
@ -81,6 +92,12 @@ void connect_with_java(JNIEnv* env, jobject fb_instance)
(jint)LCD_WIDTH, (jint)LCD_HEIGHT, native_buffer); (jint)LCD_WIDTH, (jint)LCD_HEIGHT, native_buffer);
} }
void lcd_deinit(void)
{
(*env_ptr)->DeleteGlobalRef(env_ptr, RockboxFramebuffer_instance);
(*env_ptr)->DeleteGlobalRef(env_ptr, native_buffer);
}
/* /*
* Do nothing here and connect with the java object later (if it isn't already) * Do nothing here and connect with the java object later (if it isn't already)
*/ */

View file

@ -35,7 +35,6 @@ static char *pcm_data_start;
static jmethodID play_pause_method; static jmethodID play_pause_method;
static jmethodID stop_method; static jmethodID stop_method;
static jmethodID set_volume_method; static jmethodID set_volume_method;
static jclass RockboxPCM_class;
static jobject RockboxPCM_instance; static jobject RockboxPCM_instance;
@ -155,10 +154,11 @@ void pcm_play_dma_init(void)
**/ **/
JNIEnv e = *env_ptr; JNIEnv e = *env_ptr;
/* get the class and its constructor */ /* get the class and its constructor */
RockboxPCM_class = e->FindClass(env_ptr, "org/rockbox/RockboxPCM"); jclass RockboxPCM_class = e->FindClass(env_ptr, "org/rockbox/RockboxPCM");
jmethodID constructor = e->GetMethodID(env_ptr, RockboxPCM_class, "<init>", "()V"); jmethodID constructor = e->GetMethodID(env_ptr, RockboxPCM_class, "<init>", "()V");
/* instance = new RockboxPCM() */ /* instance = new RockboxPCM() */
RockboxPCM_instance = e->NewObject(env_ptr, RockboxPCM_class, constructor); jobject PCM_instance = e->NewObject(env_ptr, RockboxPCM_class, constructor);
RockboxPCM_instance = e->NewGlobalRef(env_ptr, PCM_instance);
/* cache needed methods */ /* cache needed methods */
play_pause_method = e->GetMethodID(env_ptr, RockboxPCM_class, "play_pause", "(Z)V"); play_pause_method = e->GetMethodID(env_ptr, RockboxPCM_class, "play_pause", "(Z)V");
set_volume_method = e->GetMethodID(env_ptr, RockboxPCM_class, "set_volume", "(I)V"); set_volume_method = e->GetMethodID(env_ptr, RockboxPCM_class, "set_volume", "(I)V");
@ -167,6 +167,11 @@ void pcm_play_dma_init(void)
pcm_play_get_more_callback((void*)&pcm_data_start, &pcm_data_size); pcm_play_get_more_callback((void*)&pcm_data_start, &pcm_data_size);
} }
void pcm_deinit(void)
{
(*env_ptr)->DeleteGlobalRef(env_ptr, RockboxPCM_instance);
}
void pcm_postinit(void) void pcm_postinit(void)
{ {
} }

View file

@ -36,10 +36,18 @@ uintptr_t *stackend;
extern int main(void); extern int main(void);
extern void telephony_init_device(void); extern void telephony_init_device(void);
extern void lcd_deinit(void);
extern void pcm_deinit(void);
void system_exception_wait(void) { } void system_exception_wait(void) { }
void system_reboot(void) { } void system_reboot(void) { }
void power_off(void) { }
void power_off(void)
{
lcd_deinit();
pcm_deinit();
(*env_ptr)->DeleteGlobalRef(env_ptr, RockboxService_class);
}
void system_init(void) void system_init(void)
{ {
@ -51,18 +59,19 @@ void system_init(void)
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_org_rockbox_RockboxService_main(JNIEnv *env, jobject this) Java_org_rockbox_RockboxService_main(JNIEnv *env, jobject this)
{ {
jclass class = (*env)->GetObjectClass(env, this);
/* hack!!! we can't have a valid stack pointer otherwise. /* hack!!! we can't have a valid stack pointer otherwise.
* but we don't really need it anyway, thread.c only needs it * but we don't really need it anyway, thread.c only needs it
* for overflow detection which doesn't apply for the main thread * for overflow detection which doesn't apply for the main thread
* (it's managed by the OS) */ * (it's managed by the OS) */
(void)env;
(void)this;
volatile uintptr_t stack = 0; volatile uintptr_t stack = 0;
stackbegin = stackend = (uintptr_t*) &stack; stackbegin = stackend = (uintptr_t*) &stack;
env_ptr = env; env_ptr = env;
RockboxService_instance = this;
RockboxService_class = (*env)->GetObjectClass(env, this); RockboxService_instance = (*env)->NewGlobalRef(env, this);
RockboxService_class = (*env)->NewGlobalRef(env, class);
main(); main();
} }