From 4879732b6e314d9cfa6a60de481a99495ca0c31a Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Tue, 3 Jun 2014 19:24:47 -0700 Subject: Add another state PAUSED_TEMP, abandon focus in PAUSED --- src/github/daneren2005/dsub/domain/PlayerState.java | 1 + .../daneren2005/dsub/service/DownloadService.java | 7 ++++++- src/github/daneren2005/dsub/util/Util.java | 19 +++++++++++++------ 3 files changed, 20 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/domain/PlayerState.java b/src/github/daneren2005/dsub/domain/PlayerState.java index 2b63077b..21f1b1a4 100644 --- a/src/github/daneren2005/dsub/domain/PlayerState.java +++ b/src/github/daneren2005/dsub/domain/PlayerState.java @@ -32,6 +32,7 @@ public enum PlayerState { STARTED(RemoteControlClient.PLAYSTATE_PLAYING), STOPPED(RemoteControlClient.PLAYSTATE_STOPPED), PAUSED(RemoteControlClient.PLAYSTATE_PAUSED), + PAUSED_TEMP(RemoteControlClient.PLAYSTATE_PAUSED), COMPLETED(RemoteControlClient.PLAYSTATE_STOPPED); private final int mRemoteControlClientPlayState; diff --git a/src/github/daneren2005/dsub/service/DownloadService.java b/src/github/daneren2005/dsub/service/DownloadService.java index cebc36e0..c1576fe2 100644 --- a/src/github/daneren2005/dsub/service/DownloadService.java +++ b/src/github/daneren2005/dsub/service/DownloadService.java @@ -23,6 +23,7 @@ import static github.daneren2005.dsub.domain.PlayerState.COMPLETED; import static github.daneren2005.dsub.domain.PlayerState.DOWNLOADING; import static github.daneren2005.dsub.domain.PlayerState.IDLE; import static github.daneren2005.dsub.domain.PlayerState.PAUSED; +import static github.daneren2005.dsub.domain.PlayerState.PAUSED_TEMP; import static github.daneren2005.dsub.domain.PlayerState.PREPARED; import static github.daneren2005.dsub.domain.PlayerState.PREPARING; import static github.daneren2005.dsub.domain.PlayerState.STARTED; @@ -913,6 +914,9 @@ public class DownloadService extends Service { } public synchronized void pause() { + pause(false); + } + public synchronized void pause(boolean temp) { try { if (playerState == STARTED) { if (remoteState != RemoteControlState.LOCAL) { @@ -920,7 +924,7 @@ public class DownloadService extends Service { } else { mediaPlayer.pause(); } - setPlayerState(PAUSED); + setPlayerState(temp ? PAUSED_TEMP : PAUSED); } } catch (Exception x) { handleError(x); @@ -1048,6 +1052,7 @@ public class DownloadService extends Service { } else { Util.hidePlayingNotification(this, this, handler); } + Util.abandonAudioFocus(this); } else if(hide) { Util.hidePlayingNotification(this, this, handler); } diff --git a/src/github/daneren2005/dsub/util/Util.java b/src/github/daneren2005/dsub/util/Util.java index 91cca04b..b62b3382 100644 --- a/src/github/daneren2005/dsub/util/Util.java +++ b/src/github/daneren2005/dsub/util/Util.java @@ -109,7 +109,7 @@ public final class Util { public static final String AVRCP_PLAYSTATE_CHANGED = "com.android.music.playstatechanged"; public static final String AVRCP_METADATA_CHANGED = "com.android.music.metachanged"; - private static boolean hasFocus = false; + private static OnAudioFocusChangeListener focusListener; private static boolean pauseFocus = false; private static boolean lowerFocus = false; @@ -1250,10 +1250,9 @@ public final class Util { @TargetApi(8) public static void requestAudioFocus(final Context context) { - if (Build.VERSION.SDK_INT >= 8 && !hasFocus) { + if (Build.VERSION.SDK_INT >= 8 && focusListener == null) { final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); - hasFocus = true; - audioManager.requestAudioFocus(new OnAudioFocusChangeListener() { + audioManager.requestAudioFocus(focusListener = new OnAudioFocusChangeListener() { public void onAudioFocusChange(int focusChange) { DownloadService downloadService = (DownloadService)context; if((focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) && !downloadService.isRemoteEnabled()) { @@ -1265,7 +1264,7 @@ public final class Util { downloadService.setVolume(0.1f); } else if(lossPref == 0 || (lossPref == 1 && focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)) { pauseFocus = true; - downloadService.pause(); + downloadService.pause(true); } } } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) { @@ -1277,7 +1276,7 @@ public final class Util { downloadService.setVolume(1.0f); } } else if(focusChange == AudioManager.AUDIOFOCUS_LOSS && !downloadService.isRemoteEnabled()) { - hasFocus = false; + focusListener = null; downloadService.pause(); audioManager.abandonAudioFocus(this); } @@ -1286,6 +1285,14 @@ public final class Util { } } + public static void abandonAudioFocus(Context context) { + if(focusListener != null) { + final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + audioManager.abandonAudioFocus(focusListener); + focusListener = null; + } + } + /** *

Broadcasts the given song info as the new song being played.

*/ -- cgit v1.2.3