Make media button play/pause pause immediately.

It provides a faster response and there isn't any reason not to---a double
click will restart playback.
This commit is contained in:
Christopher Eby 2011-08-30 01:53:50 -05:00
parent 69bbfbb255
commit 339a9fcf85

View File

@ -30,8 +30,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.media.AudioManager; import android.media.AudioManager;
import android.os.Handler; import android.os.SystemClock;
import android.os.Message;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.util.Log; import android.util.Log;
@ -41,7 +40,7 @@ import android.view.KeyEvent;
* Handle a provided MediaButton event and take the appropriate action in * Handle a provided MediaButton event and take the appropriate action in
* PlaybackService. * PlaybackService.
*/ */
public class MediaButtonHandler implements Handler.Callback { public class MediaButtonHandler {
/** /**
* If another button event is received before this time in milliseconds * If another button event is received before this time in milliseconds
* expires, the event with be considered a double click. * expires, the event with be considered a double click.
@ -52,10 +51,6 @@ public class MediaButtonHandler implements Handler.Callback {
* The current global instance of this class. * The current global instance of this class.
*/ */
private static MediaButtonHandler mInstance; private static MediaButtonHandler mInstance;
/**
* The Handler for delayed processing.
*/
private Handler mHandler;
/** /**
* Whether the headset controls should be used. 1 for yes, 0 for no, -1 for * Whether the headset controls should be used. 1 for yes, 0 for no, -1 for
* uninitialized. * uninitialized.
@ -66,6 +61,10 @@ public class MediaButtonHandler implements Handler.Callback {
* uninitialized. * uninitialized.
*/ */
private int mInCall = -1; private int mInCall = -1;
/**
* Time of the last play/pause click. Used to detect double-clicks.
*/
private long mLastClickTime;
private static AudioManager mAudioManager; private static AudioManager mAudioManager;
private static Method mRegisterMediaButtonEventReceiver; private static Method mRegisterMediaButtonEventReceiver;
@ -91,8 +90,6 @@ public class MediaButtonHandler implements Handler.Callback {
*/ */
private MediaButtonHandler() private MediaButtonHandler()
{ {
mHandler = new Handler(this);
Context context = ContextApplication.getContext(); Context context = ContextApplication.getContext();
mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
@ -185,13 +182,12 @@ public class MediaButtonHandler implements Handler.Callback {
// double click: next track // double click: next track
if (action == KeyEvent.ACTION_DOWN) { if (action == KeyEvent.ACTION_DOWN) {
if (mHandler.hasMessages(MSG_SINGLE_PRESS_TIMEOUT)) { long time = SystemClock.uptimeMillis();
// double click if (time - mLastClickTime < DOUBLE_CLICK_DELAY)
mHandler.removeMessages(MSG_SINGLE_PRESS_TIMEOUT);
act(PlaybackService.ACTION_NEXT_SONG_AUTOPLAY); act(PlaybackService.ACTION_NEXT_SONG_AUTOPLAY);
} else { else
mHandler.sendEmptyMessageDelayed(MSG_SINGLE_PRESS_TIMEOUT, DOUBLE_CLICK_DELAY); act(PlaybackService.ACTION_TOGGLE_PLAYBACK);
} mLastClickTime = time;
} }
break; break;
case KeyEvent.KEYCODE_MEDIA_NEXT: case KeyEvent.KEYCODE_MEDIA_NEXT:
@ -222,25 +218,6 @@ public class MediaButtonHandler implements Handler.Callback {
return processKey(event); return processKey(event);
} }
/**
* A delayed message that performs the single press action after the double
* click period has expired.
*/
private static final int MSG_SINGLE_PRESS_TIMEOUT = 0;
public boolean handleMessage(Message message)
{
switch (message.what) {
case MSG_SINGLE_PRESS_TIMEOUT:
act(PlaybackService.ACTION_TOGGLE_PLAYBACK);
break;
default:
return false;
}
return true;
}
/** /**
* Request focus on the media buttons from AudioManager. * Request focus on the media buttons from AudioManager.
*/ */