Skip to content

Commit

Permalink
Merge pull request #518 from avuton/next
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
abarisain committed Apr 8, 2014
2 parents 3d7b0a0 + 2c7d3b9 commit 04e5257
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 135 deletions.
85 changes: 0 additions & 85 deletions MPDroid/src/com/namelessdev/mpdroid/AudioFocusHelper.java

This file was deleted.

68 changes: 27 additions & 41 deletions MPDroid/src/com/namelessdev/mpdroid/NotificationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@
* A service that handles the Notification, RemoteControlClient, MediaButtonReceiver and
* incoming MPD command intents.
*/
final public class NotificationService extends Service implements MusicFocusable,
StatusChangeListener {
final public class NotificationService extends Service implements StatusChangeListener {

private final static String TAG = "NotificationService";

Expand Down Expand Up @@ -120,15 +119,10 @@ public void handleMessage(Message msg) {
// The component name of MusicIntentReceiver, for use with media button and remote control APIs
private ComponentName mMediaButtonReceiverComponent = null;

/**
* If not available, this will be null. Always check for null before using
*/
private AudioFocusHelper mAudioFocusHelper = null;

private int mAudioFocus = AudioFocusHelper.NO_FOCUS_NO_DUCK;

private AudioManager mAudioManager = null;

private boolean isAudioFocusedOnThis = false;

private NotificationManager mNotificationManager = null;

private Notification mNotification = null;
Expand Down Expand Up @@ -237,8 +231,6 @@ public void onCreate() {
stopSelf();
}

mAudioFocusHelper = new AudioFocusHelper(app, this);

//TODO: Acquire a network wake lock here if the user wants us to !
//Otherwise we'll just shut down on screen off and reconnect on screen on
//Tons of work ahead
Expand Down Expand Up @@ -302,6 +294,14 @@ public int onStartCommand(Intent intent, int flags, int startId) {
break;
}

/** If a local user begins mpdroid again by intent, try to regain audio focus. */
switch (action) {
case ACTION_PLAY:
case ACTION_NEXT:
case ACTION_PREVIOUS:
tryToGetAudioFocus();
}

switch (action) {
case ACTION_CLOSE_NOTIFICATION:
stopSelf();
Expand Down Expand Up @@ -406,33 +406,19 @@ protected void onPostExecute(Boolean shouldPause) {
}.execute(app);
}

private void giveUpAudioFocus() {
Log.d(TAG, "Giving up audio focus.");
if (mAudioFocus == AudioFocusHelper.FOCUSED && mAudioFocusHelper != null
&& mAudioFocusHelper
.abandonFocus()) {
mAudioFocus = AudioFocusHelper.NO_FOCUS_NO_DUCK;
}
}

/**
* We try to get audio focus, but don't really try too hard.
* We just want the lock screen cover art.
*/
private void tryToGetAudioFocus() {
if (mAudioFocus != AudioFocusHelper.FOCUSED && mAudioFocusHelper != null
&& mAudioFocusHelper
.requestFocus()) {
Log.d(TAG, "Trying to gain audio focus.");
mAudioFocus = AudioFocusHelper.FOCUSED;
}
}
if ((streamingServiceWoundDown || !app.getApplicationState().streamingMode) &&
!isAudioFocusedOnThis) {
Log.d(TAG, "requesting audio focus");
final int result = mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);

final public void onGainedAudioFocus() {
Log.d(TAG, "Gained audio focus.");
mAudioFocus = AudioFocusHelper.FOCUSED;
}

final public void onLostAudioFocus(boolean canDuck) {
Log.d(TAG, "Lost audio focus.");
mAudioFocus = canDuck ? AudioFocusHelper.NO_FOCUS_CAN_DUCK
: AudioFocusHelper.NO_FOCUS_NO_DUCK;
isAudioFocusedOnThis = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
}
}

/**
Expand Down Expand Up @@ -463,7 +449,7 @@ private void updatePlayingInfo(final MPDStatus _mpdStatus) {
Log.d(TAG, "updatePlayingInfo(int,MPDStatus)");

final MPDStatus mpdStatus = _mpdStatus == null ? getStatus() : _mpdStatus;
int state = -1;
int state;

if (mediaPlayerServiceIsBuffering) {
state = RemoteControlClient.PLAYSTATE_BUFFERING;
Expand Down Expand Up @@ -545,8 +531,8 @@ private void updateAlbumCoverWithCached() {
} else {
mAlbumCover = Tools
.decodeSampledBitmapFromPath(coverArtPath, getResources()
.getDimensionPixelSize(
android.R.dimen.notification_large_icon_width),
.getDimensionPixelSize(
android.R.dimen.notification_large_icon_width),
getResources()
.getDimensionPixelSize(
android.R.dimen.notification_large_icon_height),
Expand Down Expand Up @@ -724,8 +710,6 @@ public void onDestroy() {
mNotificationManager.cancel(NOTIFICATION_ID);
}

giveUpAudioFocus();

if (mAudioManager != null) {
if (mRemoteControlClient != null) {
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_STOPPED);
Expand All @@ -735,6 +719,8 @@ public void onDestroy() {
if (mMediaButtonReceiverComponent != null) {
mAudioManager.unregisterMediaButtonEventReceiver(mMediaButtonReceiverComponent);
}

mAudioManager.abandonAudioFocus(null);
}
app.getApplicationState().notificationMode = false;
}
Expand Down
20 changes: 11 additions & 9 deletions MPDroid/src/com/namelessdev/mpdroid/StreamingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ final public void onAudioFocusChange(int focusChange) {
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
mediaPlayer.setVolume(1f, 1f);
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
windDownResources(ACTION_STREAMING_STOP);
sendIntent(NotificationService.ACTION_PAUSE, NotificationService.class);
}
}

Expand Down Expand Up @@ -301,6 +301,8 @@ final public void onCreate() {
stopSelf();
}

audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Expand All @@ -325,15 +327,13 @@ private void windUpResources() {
Log.d(TAG, "Winding up resources.");

if (mWakeLock == null) {
final PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
mWakeLock.setReferenceCounted(false);
}

mWakeLock.acquire();

audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

Expand Down Expand Up @@ -397,6 +397,10 @@ final public void onDestroy() {

stopControlHandlers();

if (audioManager != null) {
audioManager.abandonAudioFocus(this);
}

/** Remove the current MPD listeners */
app.oMPDAsyncHelper.removeStatusChangeListener(this);

Expand Down Expand Up @@ -438,10 +442,6 @@ final public boolean onError(MediaPlayer mp, int what, int extra) {
/** Either way we need to stop streaming. */
windDownResources(ACTION_STREAMING_STOP);

/** onError will often happen if we stop in the middle of preparing. */
if (isPlaying) {
tryToStream();
}
errorIterator += 1;
return true;
}
Expand All @@ -454,11 +454,13 @@ final public boolean onError(MediaPlayer mp, int what, int extra) {
@Override
final public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "StreamingService.onPrepared()");
final int focusResult = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);

/**
* Not to be playing here is unlikely but it's a race we need to avoid.
*/
if (isPlaying) {
if (isPlaying && focusResult == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
sendIntent(ACTION_BUFFERING_END, NotificationService.class);
mediaPlayer.start();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ private byte[] getCoverBytes(String[] coverUrls, CoverInfo coverInfo) {
d(CoverManager.class.getSimpleName(),
"Cover downloaded for " + coverInfo.getAlbum() + " from " + url
+ ", size=" + coverBytes.length);
break;
}
} catch (Exception e) {
w(CoverManager.class.getSimpleName(), "Cover get bytes failure : " + e);
Expand Down

0 comments on commit 04e5257

Please sign in to comment.