Android: Partly revert r29569 and only call the new getJavaEnvironment() when needed.
[maemo-rb.git] / apps / hosted / android / yesno.c
blobf08e7b2dfb14119198145492502a92d3d7b180f7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Jonathan Gordon
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
23 #if (CONFIG_PLATFORM&PLATFORM_ANDROID)
24 #include <jni.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include "yesno.h"
28 #include "settings.h"
29 #include "lang.h"
30 #include "kernel.h"
32 extern JNIEnv *env_ptr;
33 static jobject RockboxYesno_instance = NULL;
34 static jmethodID yesno_func;
35 static struct semaphore yesno_done;
36 static bool ret;
38 JNIEXPORT void JNICALL
39 Java_org_rockbox_RockboxYesno_put_1result(JNIEnv *env, jobject this, jboolean result)
41 (void)env;
42 (void)this;
43 ret = (bool)result;
44 semaphore_release(&yesno_done);
47 static void yesno_init(void)
49 JNIEnv e = *env_ptr;
50 static jmethodID yesno_is_usable;
51 if (RockboxYesno_instance == NULL)
53 semaphore_init(&yesno_done, 1, 0);
54 /* get the class and its constructor */
55 jclass RockboxYesno_class = e->FindClass(env_ptr,
56 "org/rockbox/RockboxYesno");
57 jmethodID constructor = e->GetMethodID(env_ptr,
58 RockboxYesno_class,
59 "<init>", "()V");
60 RockboxYesno_instance = e->NewObject(env_ptr,
61 RockboxYesno_class,
62 constructor);
63 yesno_func = e->GetMethodID(env_ptr, RockboxYesno_class,
64 "yesno_display",
65 "(Ljava/lang/String;"
66 "Ljava/lang/String;"
67 "Ljava/lang/String;)V");
68 yesno_is_usable = e->GetMethodID(env_ptr, RockboxYesno_class,
69 "is_usable", "()Z");
71 /* need to get it every time incase the activity died/restarted */
72 while (!e->CallBooleanMethod(env_ptr, RockboxYesno_instance,
73 yesno_is_usable))
74 sleep(HZ/10);
77 jstring build_message(const struct text_message *message)
79 char msg[1024] = "";
80 JNIEnv e = *env_ptr;
81 int i;
82 for(i=0; i<message->nb_lines; i++)
84 char* text = P2STR((unsigned char *)message->message_lines[i]);
85 if (i>0)
86 strlcat(msg, "\n", 1024);
87 strlcat(msg, text, 1024);
89 /* make sure the questions end in a ?, for some reason they dont! */
90 if (!strrchr(msg, '?'))
91 strlcat(msg, "?", 1024);
92 return e->NewStringUTF(env_ptr, msg);
95 enum yesno_res gui_syncyesno_run(const struct text_message * main_message,
96 const struct text_message * yes_message,
97 const struct text_message * no_message)
99 (void)yes_message;
100 (void)no_message;
101 yesno_init();
103 JNIEnv e = *env_ptr;
104 jstring message = build_message(main_message);
105 jstring yes = (*env_ptr)->NewStringUTF(env_ptr, str(LANG_SET_BOOL_YES));
106 jstring no = (*env_ptr)->NewStringUTF(env_ptr, str(LANG_SET_BOOL_NO));
108 e->CallVoidMethod(env_ptr, RockboxYesno_instance, yesno_func,
109 message, yes, no);
111 semaphore_wait(&yesno_done, TIMEOUT_BLOCK);
113 e->DeleteLocalRef(env_ptr, message);
114 e->DeleteLocalRef(env_ptr, yes);
115 e->DeleteLocalRef(env_ptr, no);
117 return ret ? YESNO_YES : YESNO_NO;
120 #endif