diff options
Diffstat (limited to 'apps/hosted/android')
-rw-r--r-- | apps/hosted/android/keyboard.c | 119 | ||||
-rw-r--r-- | apps/hosted/android/notification.c | 147 | ||||
-rw-r--r-- | apps/hosted/android/notification.h | 27 | ||||
-rw-r--r-- | apps/hosted/android/yesno.c | 121 |
4 files changed, 414 insertions, 0 deletions
diff --git a/apps/hosted/android/keyboard.c b/apps/hosted/android/keyboard.c new file mode 100644 index 0000000000..9407d970fd --- /dev/null +++ b/apps/hosted/android/keyboard.c | |||
@@ -0,0 +1,119 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2010 Jonathan Gordon | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include "config.h" | ||
22 | |||
23 | #if (CONFIG_PLATFORM&PLATFORM_ANDROID) | ||
24 | #include <jni.h> | ||
25 | #include <stdbool.h> | ||
26 | #include "string-extra.h" | ||
27 | #include "kernel.h" | ||
28 | #include "lang.h" | ||
29 | |||
30 | extern JNIEnv *env_ptr; | ||
31 | static jclass RockboxKeyboardInput_class; | ||
32 | static jobject RockboxKeyboardInput_instance; | ||
33 | static jmethodID kbd_inputfunc; | ||
34 | static struct semaphore kbd_wakeup; | ||
35 | static bool accepted; | ||
36 | static jstring new_string; | ||
37 | |||
38 | JNIEXPORT void JNICALL | ||
39 | Java_org_rockbox_RockboxKeyboardInput_put_1result(JNIEnv *env, jobject this, | ||
40 | jboolean _accepted, | ||
41 | jstring _new_string) | ||
42 | { | ||
43 | (void)env;(void)this; | ||
44 | |||
45 | accepted = (bool)_accepted; | ||
46 | if (accepted) | ||
47 | { | ||
48 | new_string = _new_string; | ||
49 | (*env)->NewGlobalRef(env, new_string); /* prevet GC'ing */ | ||
50 | } | ||
51 | semaphore_release(&kbd_wakeup); | ||
52 | } | ||
53 | |||
54 | static void kdb_init(void) | ||
55 | { | ||
56 | JNIEnv e = *env_ptr; | ||
57 | static jmethodID kbd_is_usable; | ||
58 | if (RockboxKeyboardInput_class == NULL) | ||
59 | { | ||
60 | semaphore_init(&kbd_wakeup, 1, 0); | ||
61 | /* get the class and its constructor */ | ||
62 | RockboxKeyboardInput_class = e->FindClass(env_ptr, | ||
63 | "org/rockbox/RockboxKeyboardInput"); | ||
64 | jmethodID constructor = e->GetMethodID(env_ptr, | ||
65 | RockboxKeyboardInput_class, | ||
66 | "<init>", "()V"); | ||
67 | RockboxKeyboardInput_instance = e->NewObject(env_ptr, | ||
68 | RockboxKeyboardInput_class, | ||
69 | constructor); | ||
70 | kbd_inputfunc = e->GetMethodID(env_ptr, RockboxKeyboardInput_class, | ||
71 | "kbd_input", | ||
72 | "(Ljava/lang/String;" | ||
73 | "Ljava/lang/String;" | ||
74 | "Ljava/lang/String;)V"); | ||
75 | kbd_is_usable = e->GetMethodID(env_ptr, RockboxKeyboardInput_class, | ||
76 | "is_usable", "()Z"); | ||
77 | } | ||
78 | |||
79 | /* need to get it every time incase the activity died/restarted */ | ||
80 | while (!e->CallBooleanMethod(env_ptr, RockboxKeyboardInput_instance, | ||
81 | kbd_is_usable)) | ||
82 | sleep(HZ/10); | ||
83 | } | ||
84 | |||
85 | int kbd_input(char* text, int buflen) | ||
86 | { | ||
87 | JNIEnv e = *env_ptr; | ||
88 | jstring str = e->NewStringUTF(env_ptr, text); | ||
89 | jstring ok_text = e->NewStringUTF(env_ptr, str(LANG_KBD_OK)); | ||
90 | jstring cancel_text = e->NewStringUTF(env_ptr, str(LANG_KBD_CANCEL)); | ||
91 | const char *utf8_string; | ||
92 | kdb_init(); | ||
93 | |||
94 | e->CallVoidMethod(env_ptr, RockboxKeyboardInput_instance,kbd_inputfunc, | ||
95 | str, ok_text, cancel_text); | ||
96 | |||
97 | semaphore_wait(&kbd_wakeup, TIMEOUT_BLOCK); | ||
98 | |||
99 | if (accepted) | ||
100 | { | ||
101 | utf8_string = e->GetStringUTFChars(env_ptr, new_string, 0); | ||
102 | strlcpy(text, utf8_string, buflen); | ||
103 | e->ReleaseStringUTFChars(env_ptr, new_string, utf8_string); | ||
104 | e->DeleteGlobalRef(env_ptr, new_string); | ||
105 | } | ||
106 | e->DeleteLocalRef(env_ptr, str); | ||
107 | e->DeleteLocalRef(env_ptr, ok_text); | ||
108 | e->DeleteLocalRef(env_ptr, cancel_text); | ||
109 | |||
110 | return !accepted; /* return 0 on success */ | ||
111 | } | ||
112 | |||
113 | int load_kbd(unsigned char* filename) | ||
114 | { | ||
115 | (void)filename; | ||
116 | return 1; | ||
117 | } | ||
118 | |||
119 | #endif | ||
diff --git a/apps/hosted/android/notification.c b/apps/hosted/android/notification.c new file mode 100644 index 0000000000..443200698c --- /dev/null +++ b/apps/hosted/android/notification.c | |||
@@ -0,0 +1,147 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2010 Thomas Martitz | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include <jni.h> | ||
23 | #include <stdio.h> | ||
24 | #include <sys/mman.h> | ||
25 | #include "notification.h" | ||
26 | #include "appevents.h" | ||
27 | #include "metadata.h" | ||
28 | #include "albumart.h" | ||
29 | #include "misc.h" | ||
30 | #include "thread.h" | ||
31 | #include "debug.h" | ||
32 | |||
33 | extern JNIEnv *env_ptr; | ||
34 | extern jclass RockboxService_class; | ||
35 | extern jobject RockboxService_instance; | ||
36 | |||
37 | static jmethodID updateNotification, finishNotification; | ||
38 | static jobject NotificationManager_instance; | ||
39 | static jstring title, artist, album, albumart; | ||
40 | |||
41 | /* completely arbitrary dimensions. neded for find_albumart() */ | ||
42 | static const struct dim dim = { .width = 200, .height = 200 }; | ||
43 | #define NZV(a) (a && a[0]) | ||
44 | |||
45 | /* | ||
46 | * notify about track change, and show track info */ | ||
47 | static void track_changed_callback(void *param) | ||
48 | { | ||
49 | struct mp3entry* id3 = (struct mp3entry*)param; | ||
50 | JNIEnv e = *env_ptr; | ||
51 | if (id3) | ||
52 | { | ||
53 | /* passing NULL to DeleteLocalRef() is OK */ | ||
54 | e->DeleteLocalRef(env_ptr, title); | ||
55 | e->DeleteLocalRef(env_ptr, artist); | ||
56 | e->DeleteLocalRef(env_ptr, album); | ||
57 | e->DeleteLocalRef(env_ptr, albumart); | ||
58 | |||
59 | char buf[200]; | ||
60 | const char * ptitle = id3->title; | ||
61 | if (!ptitle && *id3->path) | ||
62 | { /* pass the filename as title if id3 info isn't available */ | ||
63 | ptitle = strip_extension(buf, sizeof(buf), strrchr(id3->path,'/') + 1); | ||
64 | } | ||
65 | |||
66 | title = e->NewStringUTF(env_ptr, ptitle ?: ""); | ||
67 | artist = e->NewStringUTF(env_ptr, id3->artist ?: ""); | ||
68 | album = e->NewStringUTF(env_ptr, id3->album ?: ""); | ||
69 | |||
70 | albumart = NULL; | ||
71 | if (id3->embed_albumart && id3->albumart.type == AA_TYPE_JPG) | ||
72 | { /* extract albumart to a temporary file using mmap() */ | ||
73 | snprintf(buf, sizeof(buf), "/sdcard/rockbox/.temp_albumart_%d.jpg", | ||
74 | thread_self()); | ||
75 | int dst_fd = creat(buf, 0666); | ||
76 | if (dst_fd >= 0) | ||
77 | { | ||
78 | int src_fd = open(id3->path, O_RDONLY); | ||
79 | off_t o_pos = id3->albumart.pos; | ||
80 | off_t pa_pos = o_pos & ~(sysconf(_SC_PAGE_SIZE) - 1); | ||
81 | if (src_fd >= 0) | ||
82 | { /* align to page boundary */ | ||
83 | int pos_diff = o_pos - pa_pos; | ||
84 | unsigned char* p = mmap(NULL, id3->albumart.size + pos_diff, | ||
85 | PROT_READ, MAP_SHARED, src_fd, pa_pos); | ||
86 | if (p != MAP_FAILED) | ||
87 | { | ||
88 | write(dst_fd, p + pos_diff, id3->albumart.size); | ||
89 | munmap(p, id3->albumart.size + pos_diff); | ||
90 | albumart = e->NewStringUTF(env_ptr, buf); | ||
91 | } | ||
92 | close(src_fd); | ||
93 | } | ||
94 | close(dst_fd); | ||
95 | } | ||
96 | } | ||
97 | else if (find_albumart(id3, buf, sizeof(buf), &dim)) | ||
98 | { | ||
99 | albumart = e->NewStringUTF(env_ptr, buf); | ||
100 | } | ||
101 | |||
102 | e->CallVoidMethod(env_ptr, NotificationManager_instance, | ||
103 | updateNotification, title, artist, album, albumart); | ||
104 | } | ||
105 | } | ||
106 | |||
107 | /* | ||
108 | * notify about track finishing */ | ||
109 | static void track_finished_callback(void *param) | ||
110 | { | ||
111 | (void)param; | ||
112 | JNIEnv e = *env_ptr; | ||
113 | e->CallVoidMethod(env_ptr, NotificationManager_instance, | ||
114 | finishNotification); | ||
115 | |||
116 | /* delete temporary albumart file */ | ||
117 | char buf[MAX_PATH]; | ||
118 | snprintf(buf, sizeof(buf), "/sdcard/rockbox/.temp_albumart_%d.jpg", | ||
119 | thread_self()); | ||
120 | unlink(buf); | ||
121 | } | ||
122 | |||
123 | void notification_init(void) | ||
124 | { | ||
125 | JNIEnv e = *env_ptr; | ||
126 | jfieldID nNM = e->GetFieldID(env_ptr, RockboxService_class, | ||
127 | "fg_runner", "Lorg/rockbox/Helper/RunForegroundManager;"); | ||
128 | NotificationManager_instance = e->GetObjectField(env_ptr, | ||
129 | RockboxService_instance, nNM); | ||
130 | if (NotificationManager_instance == NULL) | ||
131 | { | ||
132 | DEBUGF("Failed to get RunForegroundManager instance. Performance will be bad"); | ||
133 | return; | ||
134 | } | ||
135 | |||
136 | jclass class = e->GetObjectClass(env_ptr, NotificationManager_instance); | ||
137 | updateNotification = e->GetMethodID(env_ptr, class, "updateNotification", | ||
138 | "(Ljava/lang/String;" | ||
139 | "Ljava/lang/String;" | ||
140 | "Ljava/lang/String;" | ||
141 | "Ljava/lang/String;)V"); | ||
142 | finishNotification = e->GetMethodID(env_ptr, class, "finishNotification", | ||
143 | "()V"); | ||
144 | |||
145 | add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback); | ||
146 | add_event(PLAYBACK_EVENT_TRACK_FINISH, false, track_finished_callback); | ||
147 | } | ||
diff --git a/apps/hosted/android/notification.h b/apps/hosted/android/notification.h new file mode 100644 index 0000000000..d182c7f8e7 --- /dev/null +++ b/apps/hosted/android/notification.h | |||
@@ -0,0 +1,27 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2010 Thomas Martitz | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #ifndef __NOTIFICATION_H__ | ||
23 | #define __NOTIFICATION_H__ | ||
24 | |||
25 | void notification_init(void); | ||
26 | |||
27 | #endif | ||
diff --git a/apps/hosted/android/yesno.c b/apps/hosted/android/yesno.c new file mode 100644 index 0000000000..a1de64e3f9 --- /dev/null +++ b/apps/hosted/android/yesno.c | |||
@@ -0,0 +1,121 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2010 Jonathan Gordon | ||
11 | * | ||
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. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include "config.h" | ||
22 | |||
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" | ||
31 | |||
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 semaphore yesno_done; | ||
37 | static bool ret; | ||
38 | |||
39 | JNIEXPORT void JNICALL | ||
40 | Java_org_rockbox_RockboxYesno_put_1result(JNIEnv *env, jobject this, jboolean result) | ||
41 | { | ||
42 | (void)env; | ||
43 | (void)this; | ||
44 | ret = (bool)result; | ||
45 | semaphore_release(&yesno_done); | ||
46 | } | ||
47 | |||
48 | static void yesno_init(void) | ||
49 | { | ||
50 | JNIEnv e = *env_ptr; | ||
51 | static jmethodID yesno_is_usable; | ||
52 | if (RockboxYesno_class == NULL) | ||
53 | { | ||
54 | semaphore_init(&yesno_done, 1, 0); | ||
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", | ||
66 | "(Ljava/lang/String;" | ||
67 | "Ljava/lang/String;" | ||
68 | "Ljava/lang/String;)V"); | ||
69 | yesno_is_usable = e->GetMethodID(env_ptr, RockboxYesno_class, | ||
70 | "is_usable", "()Z"); | ||
71 | } | ||
72 | /* need to get it every time incase the activity died/restarted */ | ||
73 | while (!e->CallBooleanMethod(env_ptr, RockboxYesno_instance, | ||
74 | yesno_is_usable)) | ||
75 | sleep(HZ/10); | ||
76 | } | ||
77 | |||
78 | jstring build_message(const struct text_message *message) | ||
79 | { | ||
80 | char msg[1024] = ""; | ||
81 | JNIEnv e = *env_ptr; | ||
82 | int i; | ||
83 | for(i=0; i<message->nb_lines; i++) | ||
84 | { | ||
85 | char* text = P2STR((unsigned char *)message->message_lines[i]); | ||
86 | if (i>0) | ||
87 | strlcat(msg, "\n", 1024); | ||
88 | strlcat(msg, text, 1024); | ||
89 | } | ||
90 | /* make sure the questions end in a ?, for some reason they dont! */ | ||
91 | if (!strrchr(msg, '?')) | ||
92 | strlcat(msg, "?", 1024); | ||
93 | return e->NewStringUTF(env_ptr, msg); | ||
94 | } | ||
95 | |||
96 | enum yesno_res gui_syncyesno_run(const struct text_message * main_message, | ||
97 | const struct text_message * yes_message, | ||
98 | const struct text_message * no_message) | ||
99 | { | ||
100 | (void)yes_message; | ||
101 | (void)no_message; | ||
102 | yesno_init(); | ||
103 | |||
104 | JNIEnv e = *env_ptr; | ||
105 | jstring message = build_message(main_message); | ||
106 | jstring yes = (*env_ptr)->NewStringUTF(env_ptr, str(LANG_SET_BOOL_YES)); | ||
107 | jstring no = (*env_ptr)->NewStringUTF(env_ptr, str(LANG_SET_BOOL_NO)); | ||
108 | |||
109 | e->CallVoidMethod(env_ptr, RockboxYesno_instance, yesno_func, | ||
110 | message, yes, no); | ||
111 | |||
112 | semaphore_wait(&yesno_done, TIMEOUT_BLOCK); | ||
113 | |||
114 | e->DeleteLocalRef(env_ptr, message); | ||
115 | e->DeleteLocalRef(env_ptr, yes); | ||
116 | e->DeleteLocalRef(env_ptr, no); | ||
117 | |||
118 | return ret ? YESNO_YES : YESNO_NO; | ||
119 | } | ||
120 | |||
121 | #endif | ||