1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
diff --git a/android/src/org/rockbox/RockboxFramebuffer.java b/android/src/org/rockbox/RockboxFramebuffer.java
index a17bc90..669c964 100644
--- a/android/src/org/rockbox/RockboxFramebuffer.java
+++ b/android/src/org/rockbox/RockboxFramebuffer.java
@@ -132,6 +132,7 @@ public class RockboxFramebuffer extends SurfaceView

     public boolean onKeyUp(int keyCode, KeyEvent event)
     {
+        LOG("play pressed " + event.getKeyCode());
         return buttonHandler(keyCode, false);
     }

diff --git a/android/src/org/rockbox/RockboxService.java b/android/src/org/rockbox/RockboxService.java
index 4f0caa7..ba35e73 100644
--- a/android/src/org/rockbox/RockboxService.java
+++ b/android/src/org/rockbox/RockboxService.java
@@ -129,17 +129,9 @@ public class RockboxService extends Service

             if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON))
             {
-                KeyEvent kev = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
+                KeyEvent kev = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
                 RockboxFramebuffer.buttonHandler(kev.getKeyCode(), kev.getAction() == KeyEvent.ACTION_DOWN);
             }
-            else if (intent.getAction().equals("org.rockbox.PlayPause"))
-                RockboxFramebuffer.buttonHandler(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, false);
-            else if (intent.getAction().equals("org.rockbox.Prev"))
-                RockboxFramebuffer.buttonHandler(KeyEvent.KEYCODE_MEDIA_PREVIOUS, false);
-            else if (intent.getAction().equals("org.rockbox.Next"))
-                RockboxFramebuffer.buttonHandler(KeyEvent.KEYCODE_MEDIA_NEXT, false);
-            else if (intent.getAction().equals("org.rockbox.Stop"))
-                RockboxFramebuffer.buttonHandler(KeyEvent.KEYCODE_MEDIA_STOP, false);
         }

         /* (Re-)attach the media button receiver, in case it has been lost */
diff --git a/android/src/org/rockbox/widgets/RockboxWidgetConfigure.java b/android/src/org/rockbox/widgets/RockboxWidgetConfigure.java
index 82adb97..fdea9b4 100644
--- a/android/src/org/rockbox/widgets/RockboxWidgetConfigure.java
+++ b/android/src/org/rockbox/widgets/RockboxWidgetConfigure.java
@@ -81,7 +81,7 @@ public class RockboxWidgetConfigure extends Activity
             saveWidgetPref(context, mAppWidgetId, state);

             AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
-            RockboxWidgetProvider.updateAppWidget(context, appWidgetManager, mAppWidgetId, null);
+            RockboxWidgetProvider.getInstance().updateAppWidget(context, appWidgetManager, mAppWidgetId, null);

             Intent result = new Intent();
             result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
diff --git a/android/src/org/rockbox/widgets/RockboxWidgetProvider.java b/android/src/org/rockbox/widgets/RockboxWidgetProvider.java
index 756d9f9..5b83c37 100644
--- a/android/src/org/rockbox/widgets/RockboxWidgetProvider.java
+++ b/android/src/org/rockbox/widgets/RockboxWidgetProvider.java
@@ -34,6 +34,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.net.Uri;
+import android.os.SystemClock;
 import android.util.Log;
 import android.view.View;
 import android.view.KeyEvent;
@@ -43,6 +44,12 @@ import java.util.ArrayList;

 public class RockboxWidgetProvider extends AppWidgetProvider
 {
+    static RockboxWidgetProvider mInstance;
+    public RockboxWidgetProvider()
+    {
+        super();
+        mInstance = this;
+    }
     @Override
     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
     {
@@ -54,6 +61,11 @@ public class RockboxWidgetProvider extends AppWidgetProvider

         }
     }
+    
+    public static RockboxWidgetProvider getInstance()
+    {
+        return mInstance;
+    }

     @Override
     public void onDeleted(Context context, int[] appWidgetIds)
@@ -92,10 +104,11 @@ public class RockboxWidgetProvider extends AppWidgetProvider
         }
     }

-     public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Intent args)
+     public void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Intent args)
      {
         AppWidgetProviderInfo provider = appWidgetManager.getAppWidgetInfo(appWidgetId);
         RemoteViews views = null;
+        RockboxMediaIntent mintent;
         views = new RemoteViews(context.getPackageName(), provider.initialLayout);

         Intent intent = new Intent(context, RockboxActivity.class);
@@ -106,36 +119,36 @@ public class RockboxWidgetProvider extends AppWidgetProvider

         if (state.enablePrev)
         {
-            intent = new Intent("org.rockbox.Prev", Uri.EMPTY, context, RockboxService.class);
-            pendingIntent = PendingIntent.getService(context, 0, intent, 0);
-            views.setOnClickPendingIntent(R.id.prev, pendingIntent);
+            mintent = new RockboxMediaIntent(context, 
+                                             KeyEvent.KEYCODE_MEDIA_PREVIOUS);
+            views.setOnClickPendingIntent(R.id.prev, mintent.getPendingIntent());
         }
         else
             views.setViewVisibility(R.id.prev, View.GONE);

         if (state.enablePlayPause)
         {
-            intent = new Intent("org.rockbox.PlayPause", Uri.EMPTY, context, RockboxService.class);
-            pendingIntent = PendingIntent.getService(context, 0, intent, 0);
-            views.setOnClickPendingIntent(R.id.playPause, pendingIntent);
+            RockboxMediaIntent nintent = new RockboxMediaIntent(context, 
+                                             KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
+            views.setOnClickPendingIntent(R.id.playPause, nintent.getPendingIntent());
         }
         else
             views.setViewVisibility(R.id.playPause, View.GONE);

         if (state.enableNext)
         {
-            intent = new Intent("org.rockbox.Next", Uri.EMPTY, context, RockboxService.class);
-            pendingIntent = PendingIntent.getService(context, 0, intent, 0);
-            views.setOnClickPendingIntent(R.id.next, pendingIntent);
+            mintent = new RockboxMediaIntent(context, 
+                                             KeyEvent.KEYCODE_MEDIA_NEXT);
+            views.setOnClickPendingIntent(R.id.next, mintent.getPendingIntent());
         }
         else
             views.setViewVisibility(R.id.next, View.GONE);

         if (state.enableStop)
         {
-            intent = new Intent("org.rockbox.Stop", Uri.EMPTY, context, RockboxService.class);
-            pendingIntent = PendingIntent.getService(context, 0, intent, 0);
-            views.setOnClickPendingIntent(R.id.stop, pendingIntent);
+            mintent = new RockboxMediaIntent(context, 
+                                             KeyEvent.KEYCODE_MEDIA_STOP);
+            views.setOnClickPendingIntent(R.id.stop, mintent.getPendingIntent());
         }
         else
             views.setViewVisibility(R.id.stop, View.GONE);
@@ -166,6 +179,26 @@ public class RockboxWidgetProvider extends AppWidgetProvider
         }

         appWidgetManager.updateAppWidget(appWidgetId, views);
-     }
+    }
+     
+    private class RockboxMediaIntent extends Intent
+    {
+        Context mContext;
+        int keycode;
+        public RockboxMediaIntent(Context c, int keycode)
+        {
+            super(ACTION_MEDIA_BUTTON, Uri.EMPTY, c, RockboxService.class);
+            mContext = c;
+            putExtra(EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
+                    keycode));
+            this.keycode = keycode;
+        }
+        
+        public PendingIntent getPendingIntent()
+        {
+            // Use keycode as request to code to prevent successive PendingIntents from overwritting one another. This seems hackish but at least it works.
+            return PendingIntent.getService(mContext, keycode, this, 0);
+        }
+    }
 }