Android: Fix some race conditions and crashes on startup.
[kugel-rb.git] / apps / hosted / notification.c
blob4dd1bece3dad8a70df8fb2657af76e8c3f1e17ed
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Thomas Martitz
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 ****************************************************************************/
22 #include <jni.h>
23 #include <stdio.h>
24 #include "notification.h"
25 #include "appevents.h"
26 #include "metadata.h"
27 #include "misc.h"
29 extern JNIEnv *env_ptr;
30 extern jclass RockboxService_class;
31 extern jobject RockboxService_instance;
33 static jmethodID updateNotification, finishNotification;
34 static jobject NotificationManager_instance;
35 static jstring title, artist, album;
37 #define NZV(a) (a && a[0])
40 * notify about track change, and show track info */
41 static void track_changed_callback(void *param)
43 struct mp3entry* id3 = (struct mp3entry*)param;
44 JNIEnv e = *env_ptr;
45 if (id3)
47 /* passing NULL to DeleteLocalRef() is OK */
48 e->DeleteLocalRef(env_ptr, title);
49 e->DeleteLocalRef(env_ptr, artist);
50 e->DeleteLocalRef(env_ptr, album);
52 char buf[200];
53 const char * ptitle = id3->title;
54 if (!ptitle)
55 { /* pass the filename as title if id3 info isn't available */
56 ptitle =
57 strip_extension(buf, sizeof(buf), strrchr(id3->path,'/') + 1);
60 title = e->NewStringUTF(env_ptr, ptitle);
61 artist = e->NewStringUTF(env_ptr, id3->artist ?: "");
62 album = e->NewStringUTF(env_ptr, id3->album ?: "");
64 e->CallVoidMethod(env_ptr, NotificationManager_instance,
65 updateNotification, title, artist, album);
70 * notify about track finishing */
71 static void track_finished_callback(void *param)
73 (void)param;
74 JNIEnv e = *env_ptr;
75 e->CallVoidMethod(env_ptr, NotificationManager_instance,
76 finishNotification);
79 void notification_init(void)
81 JNIEnv e = *env_ptr;
82 jfieldID nNM = e->GetFieldID(env_ptr, RockboxService_class,
83 "fg_runner", "Lorg/rockbox/Helper/RunForegroundManager;");
84 NotificationManager_instance = e->GetObjectField(env_ptr,
85 RockboxService_instance, nNM);
86 if (NotificationManager_instance == NULL)
88 DEBUGF("Failed to get RunForegroundManager instance. Performance will be bad");
89 return;
92 jclass class = e->GetObjectClass(env_ptr, NotificationManager_instance);
93 updateNotification = e->GetMethodID(env_ptr, class, "updateNotification",
94 "(Ljava/lang/String;"
95 "Ljava/lang/String;"
96 "Ljava/lang/String;)V");
97 finishNotification = e->GetMethodID(env_ptr, class, "finishNotification",
98 "()V");
100 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
101 add_event(PLAYBACK_EVENT_TRACK_FINISH, false, track_finished_callback);