summaryrefslogtreecommitdiff
path: root/apps/hosted/notification.c
blob: 13c88eca4b208bfc9af24c6f421036c3e01d8994 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/***************************************************************************
 *             __________               __   ___.
 *   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>
#include "notification.h"
#include "appevents.h"
#include "metadata.h"
#include "misc.h"

extern JNIEnv *env_ptr;
extern jclass RockboxService_class;
extern jobject RockboxService_instance;

static jmethodID updateNotification;
static jobject NotificationManager_instance;
static jstring headline, content, ticker;

#define NZV(a) (a && a[0])

/*
 * notify about track change, and show track info */
static void track_changed_callback(void *param)
{
    struct mp3entry* id3 = (struct mp3entry*)param;
    JNIEnv e = *env_ptr;
    if (id3)
    {
        /* passing NULL to DeleteLocalRef() is OK */
        e->DeleteLocalRef(env_ptr, headline);
        e->DeleteLocalRef(env_ptr, content);
        e->DeleteLocalRef(env_ptr, ticker);

        char buf[200];
        const char * title = id3->title;
        if (!title)
        {   /* pass the filename as title if id3 info isn't available */
            title =
                strip_extension(buf, sizeof(buf), strrchr(id3->path,'/') + 1);
        }

        /* do text for the notification area in the scrolled-down statusbar */
        headline = e->NewStringUTF(env_ptr, title);

        /* add a \n so that android does split into two lines */
        snprintf(buf, sizeof(buf), "%s\n%s", id3->artist ?: "", id3->album ?: "");
        content = e->NewStringUTF(env_ptr, buf);

        /* now do the text for the notification in the statusbar itself */
        if (NZV(id3->artist))
        {   /* title - artist */
            snprintf(buf, sizeof(buf), "%s - %s", title, id3->artist);
            ticker = e->NewStringUTF(env_ptr, buf);
        }
        else
        {   /* title */
            ticker = e->NewStringUTF(env_ptr, title);
        }
        e->CallVoidMethod(env_ptr, NotificationManager_instance,
                      updateNotification, headline, content, ticker);
    }
}

void notification_init(void)
{
    JNIEnv e = *env_ptr;
    jfieldID nNM = e->GetFieldID(env_ptr, RockboxService_class,
                    "fg_runner", "Lorg/rockbox/Helper/RunForegroundManager;");
    NotificationManager_instance = e->GetObjectField(env_ptr,
                                                RockboxService_instance, nNM);

    jclass class = e->GetObjectClass(env_ptr, NotificationManager_instance);
    updateNotification = e->GetMethodID(env_ptr, class, "updateNotification",
                                         "(Ljava/lang/String;"
                                         "Ljava/lang/String;"
                                         "Ljava/lang/String;)V");
    add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, track_changed_callback);
}