Android: Use wakeup objects instead of polling for the dialog results in the keyboard...
[maemo-rb.git] / apps / hosted / yesno.c
blob2a8c02edd533b3d12e5673a8adc025c93a5414ed
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 jclass RockboxYesno_class = NULL;
34 static jobject RockboxYesno_instance = NULL;
35 static jmethodID yesno_func;
36 static struct wakeup yesno_wakeup;
37 static bool ret;
39 JNIEXPORT void JNICALL
40 Java_org_rockbox_RockboxYesno_put_1result(JNIEnv *env, jobject this, jboolean result)
42 (void)env;
43 (void)this;
44 ret = (bool)result;
45 wakeup_signal(&yesno_wakeup);
48 static void yesno_init(void)
50 JNIEnv e = *env_ptr;
51 static jmethodID yesno_is_usable;
52 if (RockboxYesno_class == NULL)
54 wakeup_init(&yesno_wakeup);
55 /* get the class and its constructor */
56 RockboxYesno_class = e->FindClass(env_ptr,
57 "org/rockbox/RockboxYesno");
58 jmethodID constructor = e->GetMethodID(env_ptr,
59 RockboxYesno_class,
60 "<init>", "()V");
61 RockboxYesno_instance = e->NewObject(env_ptr,
62 RockboxYesno_class,
63 constructor);
64 yesno_func = e->GetMethodID(env_ptr, RockboxYesno_class,
65 "yesno_display", "(Ljava/lang/String;)V");
66 yesno_is_usable = e->GetMethodID(env_ptr, RockboxYesno_class,
67 "is_usable", "()Z");
69 /* need to get it every time incase the activity died/restarted */
70 while (!e->CallBooleanMethod(env_ptr, RockboxYesno_instance,
71 yesno_is_usable))
72 sleep(HZ/10);
75 jstring build_message(const struct text_message *message)
77 char msg[1024] = "";
78 JNIEnv e = *env_ptr;
79 int i;
80 for(i=0; i<message->nb_lines; i++)
82 char* text = P2STR((unsigned char *)message->message_lines[i]);
83 if (i>0)
84 strlcat(msg, "\n", 1024);
85 strlcat(msg, text, 1024);
87 /* make sure the questions end in a ?, for some reason they dont! */
88 if (!strrchr(msg, '?'))
89 strlcat(msg, "?", 1024);
90 return e->NewStringUTF(env_ptr, msg);
93 enum yesno_res gui_syncyesno_run(const struct text_message * main_message,
94 const struct text_message * yes_message,
95 const struct text_message * no_message)
97 (void)yes_message;
98 (void)no_message;
99 yesno_init();
101 JNIEnv e = *env_ptr;
102 jstring message = build_message(main_message);
104 e->CallVoidMethod(env_ptr, RockboxYesno_instance, yesno_func, message);
106 wakeup_wait(&yesno_wakeup, TIMEOUT_BLOCK);
108 e->DeleteLocalRef(env_ptr, message);
110 return ret ? YESNO_YES : YESNO_NO;
113 #endif