Cleaned up media button handling
This commit is contained in:
parent
5d58808d07
commit
3e5ba30a66
2 changed files with 10 additions and 9 deletions
|
|
@ -83,11 +83,17 @@ public class MediaButtonIntentReceiver extends WakefulBroadcastReceiver {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(final Context context, final Intent intent) {
|
public void onReceive(final Context context, final Intent intent) {
|
||||||
if (DEBUG) Log.v(TAG, "Received intent: " + intent);
|
if (DEBUG) Log.v(TAG, "Received intent: " + intent);
|
||||||
|
if (handleIntent(context, intent) && isOrderedBroadcast()) {
|
||||||
|
abortBroadcast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean handleIntent(final Context context, final Intent intent) {
|
||||||
final String intentAction = intent.getAction();
|
final String intentAction = intent.getAction();
|
||||||
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
|
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
|
||||||
final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
|
final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
|
||||||
if (event == null) {
|
if (event == null) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
final int keycode = event.getKeyCode();
|
final int keycode = event.getKeyCode();
|
||||||
|
|
@ -147,14 +153,12 @@ public class MediaButtonIntentReceiver extends WakefulBroadcastReceiver {
|
||||||
} else {
|
} else {
|
||||||
startService(context, command);
|
startService(context, command);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isOrderedBroadcast()) {
|
|
||||||
abortBroadcast();
|
|
||||||
}
|
|
||||||
releaseWakeLockIfHandlerIdle();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void startService(Context context, String command) {
|
private static void startService(Context context, String command) {
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,6 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
private AudioManager audioManager;
|
private AudioManager audioManager;
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
private MediaSessionCompat mediaSession;
|
private MediaSessionCompat mediaSession;
|
||||||
private MediaButtonIntentReceiver mediaButtonIntentReceiver = new MediaButtonIntentReceiver();
|
|
||||||
private PowerManager.WakeLock wakeLock;
|
private PowerManager.WakeLock wakeLock;
|
||||||
private PlaybackHandler playerHandler;
|
private PlaybackHandler playerHandler;
|
||||||
private final AudioManager.OnAudioFocusChangeListener audioFocusListener = new AudioManager.OnAudioFocusChangeListener() {
|
private final AudioManager.OnAudioFocusChangeListener audioFocusListener = new AudioManager.OnAudioFocusChangeListener() {
|
||||||
|
|
@ -268,8 +267,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
|
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
|
||||||
mediaButtonIntentReceiver.onReceive(MusicService.this, mediaButtonEvent);
|
return MediaButtonIntentReceiver.handleIntent(MusicService.this, mediaButtonEvent);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -281,7 +279,6 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
|
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
|
||||||
MediaButtonReceiver.handleIntent(mediaSession, intent);
|
|
||||||
if (intent != null) {
|
if (intent != null) {
|
||||||
if (intent.getAction() != null) {
|
if (intent.getAction() != null) {
|
||||||
restoreQueuesAndPositionIfNecessary();
|
restoreQueuesAndPositionIfNecessary();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue