diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index b575690b..e3b04b73 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -31,11 +31,11 @@
android:name="android.appwidget.provider"
android:resource="@xml/one_cell_widget" />
-
-
-
-
-
+
+
+
+
+
diff --git a/src/org/kreed/vanilla/MediaButtonReceiver.java b/src/org/kreed/vanilla/MediaButtonReceiver.java
index eeba2409..abce72e7 100644
--- a/src/org/kreed/vanilla/MediaButtonReceiver.java
+++ b/src/org/kreed/vanilla/MediaButtonReceiver.java
@@ -1,148 +1,109 @@
+/*
+ * Copyright (C) 2010 Christopher Eby
+ *
+ * This file is part of Vanilla Music Player.
+ *
+ * Vanilla Music Player is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or (at your
+ * option) any later version.
+ *
+ * Vanilla Music Player is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
package org.kreed.vanilla;
+import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.view.KeyEvent;
import android.os.Handler;
import android.os.Message;
-import android.util.Log;
public class MediaButtonReceiver extends BroadcastReceiver {
- private static final int MSG_LONGPRESS_TIMEOUT = 1;
private static final int MSG_MEDIA_CLICK = 2;
- private static final int LONG_PRESS_DELAY = 1000;
private static final int DOUBLE_CLICK_DELAY = 300;
-
- private static long mLastClickTime = 0;
- private static boolean mDown = false;
- private static boolean mCheckDoubleClick = false;
- //private static boolean mLaunched = false;
- private static Handler mHandler= new Handler(){
+ private static boolean mIgnoreNextUp;
+
+ private static Handler mHandler = new Handler() {
@Override
- public void handleMessage(Message msg){
- Context context = (Context)msg.obj;
- Intent i = new Intent(context, PlaybackService.class);
- Log.d("MAGNUS", "msg.what "+ msg.what);
- switch (msg.what){
- case MSG_LONGPRESS_TIMEOUT:
-
-/* Context context = (Context)msg.obj;
- Intent i = new Intent(context, PlaybackService.class);
-
- i.setAction(PlaybackService.NEXT_SONG);
- context.startService(i);
-*/
- break;
- case MSG_MEDIA_CLICK:
- if (mCheckDoubleClick) {
- i.setAction(PlaybackService.NEXT_SONG);
- mCheckDoubleClick = false;
- }
- else {
- i.setAction(PlaybackService.TOGGLE_PLAYBACK);
- }
- context.startService(i);
- break;
- default:
- Log.d("MAGNUS", "msg.what");
+ public void handleMessage(Message msg)
+ {
+ switch (msg.what) {
+ case MSG_MEDIA_CLICK:
+ try {
+ ((PendingIntent)msg.obj).send();
+ } catch (PendingIntent.CanceledException e) {
}
+ break;
+ }
}
};
-
-
- @Override
- public void onReceive(Context context, Intent intent) {
- String intentAction = intent.getAction();
- /*if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) {
- Intent i = new Intent(context, MediaPlaybackService.class);
- i.setAction(MediaPlaybackService.SERVICECMD);
- i.putExtra(MediaPlaybackService.CMDNAME, MediaPlaybackService.CMDPAUSE);
- context.startService(i);
- } else*/
-
- Log.d("MAGNUS", "action "+intentAction);
-
- if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
- KeyEvent event = (KeyEvent)
- intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
-
- if (event == null) {
- return;
- }
+ private static Intent getCommand(Context context, String action)
+ {
+ return new Intent(context, PlaybackService.class).setAction(action);
+ }
- int keycode = event.getKeyCode();
- int action = event.getAction();
- long eventtime = event.getEventTime();
+ @Override
+ public void onReceive(Context context, Intent intent)
+ {
+ if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
+ KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
+ if (event == null)
+ return;
- // single quick press: pause/resume.
- // double press: next track
- // long press: start auto-shuffle mode.
-
- Intent i = new Intent(context, PlaybackService.class);
- String command = null;
- switch (keycode) {
- case KeyEvent.KEYCODE_HEADSETHOOK:
- case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
- command = PlaybackService.TOGGLE_PLAYBACK;
- i.setAction(PlaybackService.TOGGLE_PLAYBACK);
- break;
- case KeyEvent.KEYCODE_MEDIA_NEXT:
- // not sure this works
- command = PlaybackService.NEXT_SONG;
- i.setAction(PlaybackService.NEXT_SONG);
- break;
- case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
- // not sure this works
- command = PlaybackService.PREVIOUS_SONG;
- i.setAction(PlaybackService.PREVIOUS_SONG);
- break;
- }
-
- Log.d("MAGNUS", "MediaButtonReceiver.onReceive - keyEvent "+ keycode
- +" - command "+ command
- +" - mLastClickTime "+ mLastClickTime
- +" - eventtime "+ eventtime
- );
+ int action = event.getAction();
- if (command != null) {
- if (action == KeyEvent.ACTION_DOWN) {
- if (mDown) {
- if (PlaybackService.TOGGLE_PLAYBACK.equals(command)
- && mLastClickTime != 0
- && eventtime - mLastClickTime > LONG_PRESS_DELAY) {
- mHandler.sendMessage(
- mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context));
- }
- } else {
-
- if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
- // unique button ?
- // we need to check if it it repeated
- if ( eventtime - mLastClickTime < DOUBLE_CLICK_DELAY ) {
- // double
- mCheckDoubleClick = true;
- }
- else {
- mHandler.sendMessageDelayed(
- mHandler.obtainMessage(MSG_MEDIA_CLICK, context), DOUBLE_CLICK_DELAY);
- }
- mLastClickTime = eventtime;
- mDown = true;
- }
- else {
- context.startService(i);
- }
-
- }
- } else {
- mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT);
- mDown = false;
- }
- }
- }
- abortBroadcast();
- }
-}
\ No newline at end of file
+ switch (event.getKeyCode()) {
+ case KeyEvent.KEYCODE_HEADSETHOOK:
+ case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
+ // single quick press: pause/resume.
+ // double press: next track
+ // long press: unused (could also do next track? open player?)
+
+ if (action == KeyEvent.ACTION_UP && mIgnoreNextUp) {
+ mIgnoreNextUp = false;
+ return;
+ }
+
+ if (mHandler.hasMessages(MSG_MEDIA_CLICK)) {
+ // double press
+ if (action == KeyEvent.ACTION_DOWN) {
+ mHandler.removeMessages(MSG_MEDIA_CLICK);
+ context.startService(getCommand(context, PlaybackService.NEXT_SONG));
+ mIgnoreNextUp = true;
+ }
+ } else {
+ // single press
+ if (action == KeyEvent.ACTION_UP) {
+ Intent command = getCommand(context, PlaybackService.TOGGLE_PLAYBACK);
+ PendingIntent pendingIntent = PendingIntent.getService(context, 0, command, 0);
+ Message message = mHandler.obtainMessage(MSG_MEDIA_CLICK, pendingIntent);
+ mHandler.sendMessageDelayed(message, DOUBLE_CLICK_DELAY);
+ }
+ }
+ break;
+ case KeyEvent.KEYCODE_MEDIA_NEXT:
+ if (action == KeyEvent.ACTION_DOWN)
+ context.startService(getCommand(context, PlaybackService.NEXT_SONG));
+ break;
+ case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
+ if (action == KeyEvent.ACTION_DOWN)
+ //context.startService(getCommand(context, PlaybackService.PREVIOUS_SONG));
+ break;
+ default:
+ return;
+ }
+
+ abortBroadcast();
+ }
+ }
+}