Implemented pauseOnHeadsetDisconnect preference

This commit is contained in:
daniel oeh 2012-07-08 12:45:09 +02:00
parent 5c9a2d260d
commit d18def3fb3

View File

@ -5,9 +5,11 @@ import java.io.IOException;
import android.R; import android.R;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
@ -17,6 +19,7 @@ import android.media.MediaPlayer;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Binder; import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log; import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
@ -118,6 +121,8 @@ public class PlaybackService extends Service {
mediaButtonReceiver = new ComponentName(getPackageName(), mediaButtonReceiver = new ComponentName(getPackageName(),
MediaButtonReceiver.class.getName()); MediaButtonReceiver.class.getName());
audioManager.registerMediaButtonEventReceiver(mediaButtonReceiver); audioManager.registerMediaButtonEventReceiver(mediaButtonReceiver);
registerReceiver(headsetDisconnected, new IntentFilter(
Intent.ACTION_HEADSET_PLUG));
} }
@ -125,6 +130,7 @@ public class PlaybackService extends Service {
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
isRunning = false; isRunning = false;
unregisterReceiver(headsetDisconnected);
Log.d(TAG, "Service is about to be destroyed"); Log.d(TAG, "Service is about to be destroyed");
audioManager.unregisterMediaButtonEventReceiver(mediaButtonReceiver); audioManager.unregisterMediaButtonEventReceiver(mediaButtonReceiver);
audioManager.abandonAudioFocus(audioFocusChangeListener); audioManager.abandonAudioFocus(audioFocusChangeListener);
@ -421,6 +427,12 @@ public class PlaybackService extends Service {
} }
}; };
/**
* Saves the current position and pauses playback
*
* @param abandonFocus
* is true if the service should release audio focus
*/
public void pause(boolean abandonFocus) { public void pause(boolean abandonFocus) {
if (player.isPlaying()) { if (player.isPlaying()) {
Log.d(TAG, "Pausing playback."); Log.d(TAG, "Pausing playback.");
@ -548,20 +560,44 @@ public class PlaybackService extends Service {
private void updateWidget() { private void updateWidget() {
Log.d(TAG, "Sending widget update request"); Log.d(TAG, "Sending widget update request");
PlaybackService.this.sendBroadcast(new Intent(PlayerWidget.FORCE_WIDGET_UPDATE)); PlaybackService.this.sendBroadcast(new Intent(
PlayerWidget.FORCE_WIDGET_UPDATE));
} }
public PlayerStatus getStatus() { /**
return status; * Pauses playback when the headset is disconnected and the preference is
} * set
*/
private BroadcastReceiver headsetDisconnected = new BroadcastReceiver() {
private static final String TAG = "headsetDisconnected";
private static final int UNPLUGGED = 0;
public FeedMedia getMedia() { @Override
return media; public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
if (state != -1) {
Log.d(TAG, "Headset plug event. State is " + state);
boolean pauseOnDisconnect = PreferenceManager
.getDefaultSharedPreferences(
getApplicationContext())
.getBoolean(
PodcastApp.PREF_PAUSE_ON_HEADSET_DISCONNECT,
false);
Log.d(TAG, "pauseOnDisconnect preference is "
+ pauseOnDisconnect);
if (state == UNPLUGGED && pauseOnDisconnect
&& status == PlayerStatus.PLAYING) {
Log.d(TAG,
"Pausing playback because headset was disconnected");
pause(true);
} }
} else {
public MediaPlayer getPlayer() { Log.e(TAG, "Received invalid ACTION_HEADSET_PLUG intent");
return player;
} }
}
}
};
/** Periodically saves the position of the media file */ /** Periodically saves the position of the media file */
class PositionSaver extends AsyncTask<Void, Void, Void> { class PositionSaver extends AsyncTask<Void, Void, Void> {
@ -621,4 +657,16 @@ public class PlaybackService extends Service {
return shouldStream; return shouldStream;
} }
public PlayerStatus getStatus() {
return status;
}
public FeedMedia getMedia() {
return media;
}
public MediaPlayer getPlayer() {
return player;
}
} }