2010-12-10 18:41:09 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Thomas Martitz
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
#include <stdio.h>
|
2011-03-05 18:05:26 +00:00
|
|
|
#include <sys/mman.h>
|
2010-12-10 18:41:09 +00:00
|
|
|
#include "notification.h"
|
|
|
|
#include "appevents.h"
|
|
|
|
#include "metadata.h"
|
2011-02-27 20:47:44 +00:00
|
|
|
#include "albumart.h"
|
2010-12-10 18:41:09 +00:00
|
|
|
#include "misc.h"
|
2011-03-05 18:05:26 +00:00
|
|
|
#include "thread.h"
|
2011-02-19 17:12:12 +00:00
|
|
|
#include "debug.h"
|
2010-12-10 18:41:09 +00:00
|
|
|
|
2011-03-16 14:33:55 +00:00
|
|
|
extern JNIEnv *env_ptr;
|
2010-12-10 18:41:09 +00:00
|
|
|
extern jclass RockboxService_class;
|
|
|
|
extern jobject RockboxService_instance;
|
|
|
|
|
2011-01-29 20:47:32 +00:00
|
|
|
static jmethodID updateNotification, finishNotification;
|
2010-12-10 18:41:09 +00:00
|
|
|
static jobject NotificationManager_instance;
|
2011-02-27 20:47:44 +00:00
|
|
|
static jstring title, artist, album, albumart;
|
2010-12-10 18:41:09 +00:00
|
|
|
|
2011-02-27 20:47:44 +00:00
|
|
|
/* completely arbitrary dimensions. neded for find_albumart() */
|
2011-02-27 21:21:13 +00:00
|
|
|
static const struct dim dim = { .width = 200, .height = 200 };
|
2010-12-10 18:41:09 +00:00
|
|
|
#define NZV(a) (a && a[0])
|
|
|
|
|
|
|
|
/*
|
|
|
|
* notify about track change, and show track info */
|
2014-03-14 22:15:16 +00:00
|
|
|
static void track_changed_callback(unsigned short id, void *param)
|
2010-12-10 18:41:09 +00:00
|
|
|
{
|
2014-03-14 22:15:16 +00:00
|
|
|
(void)id;
|
2013-07-12 16:06:38 +00:00
|
|
|
struct mp3entry* id3 = ((struct track_event *)param)->id3;
|
2010-12-10 18:41:09 +00:00
|
|
|
JNIEnv e = *env_ptr;
|
|
|
|
if (id3)
|
|
|
|
{
|
|
|
|
/* passing NULL to DeleteLocalRef() is OK */
|
2011-01-24 17:10:56 +00:00
|
|
|
e->DeleteLocalRef(env_ptr, title);
|
|
|
|
e->DeleteLocalRef(env_ptr, artist);
|
|
|
|
e->DeleteLocalRef(env_ptr, album);
|
2011-02-27 20:47:44 +00:00
|
|
|
e->DeleteLocalRef(env_ptr, albumart);
|
2010-12-10 18:41:09 +00:00
|
|
|
|
|
|
|
char buf[200];
|
2011-01-24 17:10:56 +00:00
|
|
|
const char * ptitle = id3->title;
|
2011-02-28 00:59:44 +00:00
|
|
|
if (!ptitle && *id3->path)
|
2010-12-10 18:41:09 +00:00
|
|
|
{ /* pass the filename as title if id3 info isn't available */
|
2011-02-28 00:59:44 +00:00
|
|
|
ptitle = strip_extension(buf, sizeof(buf), strrchr(id3->path,'/') + 1);
|
2010-12-10 18:41:09 +00:00
|
|
|
}
|
|
|
|
|
2011-02-28 00:59:44 +00:00
|
|
|
title = e->NewStringUTF(env_ptr, ptitle ?: "");
|
2011-01-24 17:10:56 +00:00
|
|
|
artist = e->NewStringUTF(env_ptr, id3->artist ?: "");
|
|
|
|
album = e->NewStringUTF(env_ptr, id3->album ?: "");
|
2010-12-10 18:41:09 +00:00
|
|
|
|
2011-02-27 20:47:44 +00:00
|
|
|
albumart = NULL;
|
2011-12-22 18:48:43 +00:00
|
|
|
if (id3->has_embedded_albumart && id3->albumart.type == AA_TYPE_JPG)
|
2011-03-05 18:05:26 +00:00
|
|
|
{ /* extract albumart to a temporary file using mmap() */
|
|
|
|
snprintf(buf, sizeof(buf), "/sdcard/rockbox/.temp_albumart_%d.jpg",
|
|
|
|
thread_self());
|
|
|
|
int dst_fd = creat(buf, 0666);
|
|
|
|
if (dst_fd >= 0)
|
|
|
|
{
|
|
|
|
int src_fd = open(id3->path, O_RDONLY);
|
|
|
|
off_t o_pos = id3->albumart.pos;
|
|
|
|
off_t pa_pos = o_pos & ~(sysconf(_SC_PAGE_SIZE) - 1);
|
|
|
|
if (src_fd >= 0)
|
|
|
|
{ /* align to page boundary */
|
|
|
|
int pos_diff = o_pos - pa_pos;
|
|
|
|
unsigned char* p = mmap(NULL, id3->albumart.size + pos_diff,
|
|
|
|
PROT_READ, MAP_SHARED, src_fd, pa_pos);
|
|
|
|
if (p != MAP_FAILED)
|
|
|
|
{
|
|
|
|
write(dst_fd, p + pos_diff, id3->albumart.size);
|
|
|
|
munmap(p, id3->albumart.size + pos_diff);
|
|
|
|
albumart = e->NewStringUTF(env_ptr, buf);
|
|
|
|
}
|
|
|
|
close(src_fd);
|
|
|
|
}
|
|
|
|
close(dst_fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (find_albumart(id3, buf, sizeof(buf), &dim))
|
|
|
|
{
|
2011-02-27 20:47:44 +00:00
|
|
|
albumart = e->NewStringUTF(env_ptr, buf);
|
2011-03-05 18:05:26 +00:00
|
|
|
}
|
2011-02-27 20:47:44 +00:00
|
|
|
|
2010-12-10 18:41:09 +00:00
|
|
|
e->CallVoidMethod(env_ptr, NotificationManager_instance,
|
2011-02-27 20:47:44 +00:00
|
|
|
updateNotification, title, artist, album, albumart);
|
2010-12-10 18:41:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-29 20:47:32 +00:00
|
|
|
/*
|
|
|
|
* notify about track finishing */
|
2014-03-14 22:15:16 +00:00
|
|
|
static void track_finished_callback(unsigned short id, void *param)
|
2011-01-29 20:47:32 +00:00
|
|
|
{
|
2014-03-14 22:15:16 +00:00
|
|
|
(void)id;
|
2013-07-12 16:06:38 +00:00
|
|
|
if (((struct track_event *)param)->flags & TEF_REWIND)
|
|
|
|
return; /* Not a true track end */
|
|
|
|
|
2011-01-29 20:47:32 +00:00
|
|
|
JNIEnv e = *env_ptr;
|
|
|
|
e->CallVoidMethod(env_ptr, NotificationManager_instance,
|
|
|
|
finishNotification);
|
2011-03-05 18:05:26 +00:00
|
|
|
|
|
|
|
/* delete temporary albumart file */
|
|
|
|
char buf[MAX_PATH];
|
|
|
|
snprintf(buf, sizeof(buf), "/sdcard/rockbox/.temp_albumart_%d.jpg",
|
|
|
|
thread_self());
|
|
|
|
unlink(buf);
|
2011-01-29 20:47:32 +00:00
|
|
|
}
|
|
|
|
|
2010-12-10 18:41:09 +00:00
|
|
|
void notification_init(void)
|
|
|
|
{
|
|
|
|
JNIEnv e = *env_ptr;
|
|
|
|
jfieldID nNM = e->GetFieldID(env_ptr, RockboxService_class,
|
2012-03-30 09:21:31 +00:00
|
|
|
"mFgRunner", "Lorg/rockbox/Helper/RunForegroundManager;");
|
2011-03-11 19:23:00 +00:00
|
|
|
NotificationManager_instance = e->GetObjectField(env_ptr,
|
|
|
|
RockboxService_instance, nNM);
|
|
|
|
if (NotificationManager_instance == NULL)
|
2011-02-19 16:42:44 +00:00
|
|
|
{
|
|
|
|
DEBUGF("Failed to get RunForegroundManager instance. Performance will be bad");
|
|
|
|
return;
|
|
|
|
}
|
2010-12-10 18:41:09 +00:00
|
|
|
|
|
|
|
jclass class = e->GetObjectClass(env_ptr, NotificationManager_instance);
|
|
|
|
updateNotification = e->GetMethodID(env_ptr, class, "updateNotification",
|
|
|
|
"(Ljava/lang/String;"
|
|
|
|
"Ljava/lang/String;"
|
2011-02-27 20:47:44 +00:00
|
|
|
"Ljava/lang/String;"
|
2010-12-10 18:41:09 +00:00
|
|
|
"Ljava/lang/String;)V");
|
2011-01-29 20:47:32 +00:00
|
|
|
finishNotification = e->GetMethodID(env_ptr, class, "finishNotification",
|
|
|
|
"()V");
|
2011-01-24 17:10:56 +00:00
|
|
|
|
2014-03-14 22:43:45 +00:00
|
|
|
add_event(PLAYBACK_EVENT_TRACK_CHANGE, track_changed_callback);
|
|
|
|
add_event(PLAYBACK_EVENT_TRACK_FINISH, track_finished_callback);
|
2010-12-10 18:41:09 +00:00
|
|
|
}
|