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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/***************************************************************************
* __________ __ ___.
* 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.
*
****************************************************************************/
package org.rockbox.Helper;
import org.rockbox.RockboxFramebuffer;
import org.rockbox.RockboxService;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.view.KeyEvent;
public class MediaButtonReceiver
{
/* A note on the API being used. 2.2 introduces a new and sane API
* for handling multimedia button presses
* http://android-developers.blogspot.com/2010/06/allowing-applications-to-play-nicer.html
*
* the old API is flawed. It doesn't have management for
* concurrent media apps
*
* if multiple media apps are running
* probably all of them want to respond to media keys
*
* it's not clear which app wins, it depends on the
* priority set for the IntentFilter (see below)
*
* so this all might or might not work on < 2.2 */
IMultiMediaReceiver api;
public MediaButtonReceiver(Context c)
{
try {
api = new NewApi(c);
} catch (Throwable t) {
/* Throwable includes Exception and the expected
* NoClassDefFoundError */
api = new OldApi(c);
Logger.i("MediaButtonReceiver: Falling back to compatibility API");
}
}
public void register()
{
api.register();
}
public void unregister()
{
api.unregister();
}
/* helper class for the manifest */
public static class MediaReceiver extends BroadcastReceiver
{
private void startService(Context c, Intent baseIntent)
{
baseIntent.setClass(c, RockboxService.class);
c.startService(baseIntent);
}
@Override
public void onReceive(Context context, Intent intent)
{
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()))
{
KeyEvent key = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (key.getAction() == KeyEvent.ACTION_UP)
{ /* pass the pressed key to Rockbox, starting it if needed */
RockboxService s = RockboxService.getInstance();
if (s == null || !s.isRockboxRunning())
startService(context, intent);
else if (RockboxFramebuffer.buttonHandler(key.getKeyCode(), false))
abortBroadcast();
}
}
}
}
private interface IMultiMediaReceiver
{
void register();
void unregister();
}
private static class NewApi
implements IMultiMediaReceiver, AudioManager.OnAudioFocusChangeListener
{
private AudioManager audio_manager;
private ComponentName receiver_name;
private boolean running = false;
NewApi(Context c)
{
audio_manager = (AudioManager)c.getSystemService(Context.AUDIO_SERVICE);
receiver_name = new ComponentName(c, MediaReceiver.class);
}
public void register()
{
try {
audio_manager.registerMediaButtonEventReceiver(receiver_name);
audio_manager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
running = true;
} catch (Exception e) {
// Nothing
e.printStackTrace();
}
}
public void unregister()
{
try
{
audio_manager.unregisterMediaButtonEventReceiver(receiver_name);
audio_manager.abandonAudioFocus(this);
running = false;
} catch (Exception e) {
// Nothing
e.printStackTrace();
}
}
public void onAudioFocusChange(int focusChange)
{
Logger.d("Audio focus" + ((focusChange>0)?"gained":"lost")+
": "+ focusChange);
if (running)
{ /* Play nice and stop for the the other app */
if (focusChange == AudioManager.AUDIOFOCUS_LOSS)
RockboxFramebuffer.buttonHandler(KeyEvent.KEYCODE_MEDIA_STOP, false);
}
}
}
private static class OldApi implements IMultiMediaReceiver
{
private static final IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
private MediaReceiver receiver;
private Context context;
OldApi(Context c)
{
filter.setPriority(1); /* 1 higher than the built-in media player */
receiver = new MediaReceiver();
context = c;
}
public void register()
{
context.registerReceiver(receiver, filter);
}
public void unregister()
{
context.unregisterReceiver(receiver);
}
}
}
|