Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Play when headset is plugged" support #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/com/android/music/MediaPlaybackService.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ public class MediaPlaybackService extends Service implements
private RemoteControlClient mRemoteControlClient;
private Timer timer = new Timer();

private HeadsetReceiver headsetReceiver;

private Handler mMediaplayerHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Expand Down Expand Up @@ -534,12 +536,21 @@ public void onCreate() {
// case.
Message msg = mDelayedStopHandler.obtainMessage();
mDelayedStopHandler.sendMessageDelayed(msg, IDLE_DELAY);

//Headset
IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
headsetReceiver = new HeadsetReceiver();
registerReceiver( headsetReceiver, receiverFilter );
mService = IMediaPlaybackService.Stub.asInterface(mBinder);
}

@Override
public void onDestroy() {
sensorMan.unregisterListener(this);

//headset
unregisterReceiver(headsetReceiver);

// Check that we're not being destroyed while something is still
// playing.
if (isPlaying()) {
Expand Down Expand Up @@ -2813,4 +2824,21 @@ public void shakingStopped() {

}

public class HeadsetReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (!isInitialStickyBroadcast() && intent.getAction().equals(Intent.ACTION_HEADSET_PLUG) && intent.getIntExtra("state", 0)==1) {
try {
if (mService != null) {
if (!mService.isPlaying()) {
mService.play();
}
}
} catch (RemoteException ex) {
}
}
}
}

}