obey useHeadsetControls()

This commit is contained in:
Adrian Ulrich 2015-12-21 12:56:17 +01:00
parent deb5637c17
commit 05c6f013d0
5 changed files with 31 additions and 15 deletions

View File

@ -104,7 +104,7 @@ public class MediaButtonReceiver extends BroadcastReceiver {
*
* @param context A context to use.
*/
private static boolean useHeadsetControls(Context context)
public static boolean useHeadsetControls(Context context)
{
if (sUseControls == -1) {
SharedPreferences settings = PlaybackService.getSettings(context);

View File

@ -500,7 +500,7 @@ public final class PlaybackService extends Service
getContentResolver().registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true, mObserver);
mRemoteControlClient = new RemoteControl().getClient(this);
mRemoteControlClient.registerRemote();
mRemoteControlClient.initializeRemote();
mLooper = thread.getLooper();
mHandler = new Handler(mLooper, this);
@ -838,6 +838,7 @@ public final class PlaybackService extends Service
mScrobble = settings.getBoolean(PrefKeys.SCROBBLE, PrefDefaults.SCROBBLE);
} else if (PrefKeys.MEDIA_BUTTON.equals(key) || PrefKeys.MEDIA_BUTTON_BEEP.equals(key)) {
MediaButtonReceiver.reloadPreference(this);
mRemoteControlClient.initializeRemote();
} else if (PrefKeys.COVER_ON_LOCKSCREEN.equals(key)) {
mRemoteControlClient.reloadPreference();
} else if (PrefKeys.USE_IDLE_TIMEOUT.equals(key) || PrefKeys.IDLE_TIMEOUT.equals(key)) {

View File

@ -37,7 +37,7 @@ public class RemoteControl {
* Interface definition of our RemoteControl API
*/
public interface Client {
public void registerRemote();
public void initializeRemote();
public void unregisterRemote();
public void reloadPreference();
public void updateRemote(Song song, int state, boolean keepPaused);

View File

@ -33,7 +33,6 @@ import android.media.AudioManager;
import android.media.MediaMetadataRetriever;
import android.media.RemoteControlClient;
public class RemoteControlImplKitKat implements RemoteControl.Client {
/**
* Context of this instance
@ -63,8 +62,12 @@ public class RemoteControlImplKitKat implements RemoteControl.Client {
*
* @param am The AudioManager service.
*/
public void registerRemote()
{
public void initializeRemote() {
// make sure there is only one registered remote
unregisterRemote();
if (MediaButtonReceiver.useHeadsetControls(mContext) == false)
return;
// Receive 'background' play button events
AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
ComponentName receiver = new ComponentName(mContext.getPackageName(), MediaButtonReceiver.class.getName());
@ -91,9 +94,13 @@ public class RemoteControlImplKitKat implements RemoteControl.Client {
* Unregisters a remote control client
*/
public void unregisterRemote() {
AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
ComponentName receiver = new ComponentName(mContext.getPackageName(), MediaButtonReceiver.class.getName());
audioManager.unregisterMediaButtonEventReceiver(receiver);
if (mRemote != null) {
AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
ComponentName receiver = new ComponentName(mContext.getPackageName(), MediaButtonReceiver.class.getName());
audioManager.unregisterMediaButtonEventReceiver(receiver);
audioManager.unregisterRemoteControlClient(mRemote);
mRemote = null;
}
}
/**

View File

@ -55,8 +55,13 @@ public class RemoteControlImplLp implements RemoteControl.Client {
/**
* Registers a new MediaSession on the device
*/
public void registerRemote() {
mMediaSession = new MediaSession(mContext, "VanillaMusic");
public void initializeRemote() {
// make sure there is only one registered remote
unregisterRemote();
if (MediaButtonReceiver.useHeadsetControls(mContext) == false)
return;
mMediaSession = new MediaSession(mContext, "Vanilla Music");
mMediaSession.setCallback(new MediaSession.Callback() {
@Override
@ -75,11 +80,11 @@ public class RemoteControlImplLp implements RemoteControl.Client {
}
});
// This intent will be used to receive button events while our session is NOT active
Intent intent = new Intent();
intent.setComponent(new ComponentName(mContext.getPackageName(), MediaButtonReceiver.class.getName()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
// This Seems to overwrite our MEDIA_BUTTON intent filter and there seems to be no way to unregister it
// Well: We intent to keep this around as long as possible anyway. But WHY ANDROID?!
mMediaSession.setMediaButtonReceiver(pendingIntent);
mMediaSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
}
@ -88,8 +93,11 @@ public class RemoteControlImplLp implements RemoteControl.Client {
* Unregisters a registered media session
*/
public void unregisterRemote() {
mMediaSession.setActive(false);
mMediaSession.release();
if (mMediaSession != null) {
mMediaSession.setActive(false);
mMediaSession.release();
mMediaSession = null;
}
}
/**