Android: Fix crash when playback is passing invalid metadata around.
[kugel-rb.git] / apps / hosted / notification.c
blobfaa1247860fd8f1a9c0ded7c4fcf54986a8afc79
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 "albumart.h"
28 #include "misc.h"
29 #include "debug.h"
31 extern JNIEnv *env_ptr;
32 extern jclass RockboxService_class;
33 extern jobject RockboxService_instance;
35 static jmethodID updateNotification, finishNotification;
36 static jobject NotificationManager_instance;
37 static jstring title, artist, album, albumart;
39 /* completely arbitrary dimensions. neded for find_albumart() */
40 static const struct dim dim = { .width = 200, .height = 200 };
41 #define NZV(a) (a && a[0])
44 * notify about track change, and show track info */
45 static void track_changed_callback(void *param)
47 struct mp3entry* id3 = (struct mp3entry*)param;
48 JNIEnv e = *env_ptr;
49 if (id3)
51 /* passing NULL to DeleteLocalRef() is OK */
52 e->DeleteLocalRef(env_ptr, title);
53 e->DeleteLocalRef(env_ptr, artist);
54 e->DeleteLocalRef(env_ptr, album);
55 e->DeleteLocalRef(env_ptr, albumart);
57 char buf[200];
58 const char * ptitle = id3->title;
59 if (!ptitle && *id3->path)
60 { /* pass the filename as title if id3 info isn't available */
61 ptitle = strip_extension(buf, sizeof(buf), strrchr(id3->path,'/') + 1);
64 title = e->NewStringUTF(env_ptr, ptitle ?: "");
65 artist = e->NewStringUTF(env_ptr, id3->artist ?: "");
66 album = e->NewStringUTF(env_ptr, id3->album ?: "");
68 albumart = NULL;
69 if (find_albumart(id3, buf, sizeof(buf), &dim))
70 albumart = e->NewStringUTF(env_ptr, buf);
72 e->CallVoidMethod(env_ptr, NotificationManager_instance,
73 updateNotification, title, artist, album, albumart);
78 * notify about track finishing */
79 static void track_finished_callback(void *param)
81 (void)param;
82 JNIEnv e = *env_ptr;
83 e->CallVoidMethod(env_ptr, NotificationManager_instance,
84 finishNotification);
87 void notification_init(void)
89 JNIEnv e = *env_ptr;
90 jfieldID nNM = e->GetFieldID(env_ptr, RockboxService_class,
91 "fg_runner", "Lorg/rockbox/Helper/RunForegroundManager;");
92 NotificationManager_instance = e->GetObjectField(env_ptr,
93 RockboxService_instance, nNM);
94 if (NotificationManager_instance == NULL)
96 DEBUGF("Failed to get RunForegroundManager instance. Performance will be bad");
97 return;
100 jclass class = e->GetObjectClass(env_ptr, NotificationManager_instance);
101 updateNotification = e->GetMethodID(env_ptr, class, "updateNotification",
102 "(Ljava/lang/String;"
103 "Ljava/lang/String;"
104 "Ljava/lang/String;"
105 "Ljava/lang/String;)V");
106 finishNotification = e->GetMethodID(env_ptr, class, "finishNotification",
107 "()V");
109 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
110 add_event(PLAYBACK_EVENT_TRACK_FINISH, false, track_finished_callback);