Commit c3e33fb0 authored by Sébastien Toque's avatar Sébastien Toque
Browse files

Handle headset buttons

main play/pause/call button:
- one click => play/pause
- long click => previous
- double click => next

other buttons (play, pause, stop, next, previous) should also work if the headset is compatible with android
parent cf5aad85
...@@ -50,8 +50,10 @@ import android.os.Handler; ...@@ -50,8 +50,10 @@ import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.Message; import android.os.Message;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.SystemClock;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.util.Log; import android.util.Log;
import android.view.KeyEvent;
import android.widget.RemoteViews; import android.widget.RemoteViews;
public class AudioService extends Service { public class AudioService extends Service {
...@@ -70,6 +72,8 @@ public class AudioService extends Service { ...@@ -70,6 +72,8 @@ public class AudioService extends Service {
private boolean mShuffling = false; private boolean mShuffling = false;
private RepeatType mRepeating = RepeatType.None; private RepeatType mRepeating = RepeatType.None;
private boolean mDetectHeadset = true; private boolean mDetectHeadset = true;
private long mHeadsetDownTime = 0;
private long mHeadsetUpTime = 0;
@Override @Override
public void onCreate() { public void onCreate() {
...@@ -91,12 +95,14 @@ public class AudioService extends Service { ...@@ -91,12 +95,14 @@ public class AudioService extends Service {
mEventManager = EventManager.getIntance(); mEventManager = EventManager.getIntance();
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.setPriority(Integer.MAX_VALUE);
filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_BACKWARD); filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_BACKWARD);
filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_PLAY); filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_PLAY);
filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_STOP); filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_STOP);
filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_FORWARD); filter.addAction(VLCAppWidgetProvider.ACTION_WIDGET_FORWARD);
filter.addAction(Intent.ACTION_HEADSET_PLUG); filter.addAction(Intent.ACTION_HEADSET_PLUG);
filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY); filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
filter.addAction(Intent.ACTION_MEDIA_BUTTON);
registerReceiver(serviceReceiver, filter); registerReceiver(serviceReceiver, filter);
} }
...@@ -127,6 +133,9 @@ public class AudioService extends Service { ...@@ -127,6 +133,9 @@ public class AudioService extends Service {
return; return;
} }
/*
* widget events
*/
if (action.equalsIgnoreCase(VLCAppWidgetProvider.ACTION_WIDGET_PLAY)) { if (action.equalsIgnoreCase(VLCAppWidgetProvider.ACTION_WIDGET_PLAY)) {
if (mLibVLC.isPlaying() && mCurrentMedia != null) { if (mLibVLC.isPlaying() && mCurrentMedia != null) {
pause(); pause();
...@@ -148,6 +157,74 @@ public class AudioService extends Service { ...@@ -148,6 +157,74 @@ public class AudioService extends Service {
next(); next();
} }
/*
* headset controller events
*/
else if (action.equalsIgnoreCase(Intent.ACTION_MEDIA_BUTTON)) {
KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (mCurrentMedia == null || event == null)
return;
switch (event.getKeyCode())
{
/*
* one click => play/pause
* long click => previous
* double click => next
*/
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
long time = SystemClock.uptimeMillis();
switch (event.getAction())
{
case KeyEvent.ACTION_DOWN:
if (event.getRepeatCount() > 0)
break;
mHeadsetDownTime = time;
break;
case KeyEvent.ACTION_UP:
// long click
if (time - mHeadsetDownTime >= 1000) {
previous();
time = 0;
// double click
} else if (time - mHeadsetUpTime <= 500) {
next();
// block the double click event to prevent android from dialing last number
abortBroadcast();
}
// one click
else {
if (mLibVLC.isPlaying())
pause();
else
play();
}
mHeadsetUpTime = time;
break;
}
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
play();
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
pause();
break;
case KeyEvent.KEYCODE_MEDIA_STOP:
stop();
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
next();
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
previous();
break;
}
}
/*
* headset plug events
*/
if (mDetectHeadset) { if (mDetectHeadset) {
if (action.equalsIgnoreCase(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) { if (action.equalsIgnoreCase(AudioManager.ACTION_AUDIO_BECOMING_NOISY)) {
Log.i(TAG, "Headset Removed."); Log.i(TAG, "Headset Removed.");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment