1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 package org
.rockbox
.Helper
;
24 import org
.rockbox
.RockboxFramebuffer
;
25 import org
.rockbox
.RockboxService
;
26 import android
.content
.BroadcastReceiver
;
27 import android
.content
.ComponentName
;
28 import android
.content
.Context
;
29 import android
.content
.Intent
;
30 import android
.content
.IntentFilter
;
31 import android
.media
.AudioManager
;
32 import android
.view
.KeyEvent
;
34 public class MediaButtonReceiver
36 /* A note on the API being used. 2.2 introduces a new and sane API
37 * for handling multimedia button presses
38 * http://android-developers.blogspot.com/2010/06/allowing-applications-to-play-nicer.html
40 * the old API is flawed. It doesn't have management for
41 * concurrent media apps
43 * if multiple media apps are running
44 * probably all of them want to respond to media keys
46 * it's not clear which app wins, it depends on the
47 * priority set for the IntentFilter (see below)
49 * so this all might or might not work on < 2.2 */
51 IMultiMediaReceiver api
;
53 public MediaButtonReceiver(Context c
)
57 } catch (Throwable t
) {
58 /* Throwable includes Exception and the expected
59 * NoClassDefFoundError */
64 public void register()
69 public void unregister()
74 /* helper class for the manifest */
75 public static class MediaReceiver
extends BroadcastReceiver
77 private void startService(Context c
, Intent baseIntent
)
79 baseIntent
.setClass(c
, RockboxService
.class);
80 c
.startService(baseIntent
);
83 public void onReceive(Context context
, Intent intent
)
85 if (Intent
.ACTION_MEDIA_BUTTON
.equals(intent
.getAction()))
87 KeyEvent key
= (KeyEvent
)intent
.getParcelableExtra(Intent
.EXTRA_KEY_EVENT
);
88 if (key
.getAction() == KeyEvent
.ACTION_UP
)
89 { /* pass the pressed key to Rockbox, starting it if needed */
90 RockboxService s
= RockboxService
.get_instance();
91 if (s
== null || !s
.isRockboxRunning())
92 startService(context
, intent
);
93 else if (RockboxFramebuffer
.buttonHandler(key
.getKeyCode(), false))
100 private interface IMultiMediaReceiver
106 private static class NewApi
107 implements IMultiMediaReceiver
, AudioManager
.OnAudioFocusChangeListener
109 private AudioManager audio_manager
;
110 private ComponentName receiver_name
;
111 private boolean running
= false;
115 audio_manager
= (AudioManager
)c
.getSystemService(Context
.AUDIO_SERVICE
);
116 receiver_name
= new ComponentName(c
, MediaReceiver
.class);
119 public void register()
122 audio_manager
.registerMediaButtonEventReceiver(receiver_name
);
123 audio_manager
.requestAudioFocus(this, AudioManager
.STREAM_MUSIC
, AudioManager
.AUDIOFOCUS_GAIN
);
125 } catch (Exception e
) {
131 public void unregister()
135 audio_manager
.unregisterMediaButtonEventReceiver(receiver_name
);
136 audio_manager
.abandonAudioFocus(this);
138 } catch (Exception e
) {
144 public void onAudioFocusChange(int focusChange
)
146 Logger
.d("Audio focus" + ((focusChange
>0)?
"gained":"lost")+
149 { /* Play nice and stop for the the other app */
150 if (focusChange
== AudioManager
.AUDIOFOCUS_LOSS
)
151 RockboxFramebuffer
.buttonHandler(KeyEvent
.KEYCODE_MEDIA_STOP
, false);
157 private static class OldApi
implements IMultiMediaReceiver
159 private static final IntentFilter filter
= new IntentFilter(Intent
.ACTION_MEDIA_BUTTON
);
160 private MediaReceiver receiver
;
161 private Context context
;
164 filter
.setPriority(1); /* 1 higher than the built-in media player */
165 receiver
= new MediaReceiver();
169 public void register()
171 context
.registerReceiver(receiver
, filter
);
174 public void unregister()
176 context
.unregisterReceiver(receiver
);