Android: Simplify media button intent generation in the widget and cleanup RockboxService accordingly.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29404 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Thomas Martitz 2011-02-26 19:54:03 +00:00
parent 75aa83526f
commit eb01664804
4 changed files with 69 additions and 52 deletions

View file

@ -45,6 +45,7 @@ public class RockboxActivity extends Activity
WindowManager.LayoutParams.FLAG_FULLSCREEN); WindowManager.LayoutParams.FLAG_FULLSCREEN);
Intent intent = new Intent(this, RockboxService.class); Intent intent = new Intent(this, RockboxService.class);
intent.setAction(Intent.ACTION_MAIN);
intent.putExtra("callback", new ResultReceiver(new Handler(getMainLooper())) { intent.putExtra("callback", new ResultReceiver(new Handler(getMainLooper())) {
private ProgressDialog loadingdialog; private ProgressDialog loadingdialog;

View file

@ -105,8 +105,8 @@ public class RockboxService extends Service
private void do_start(Intent intent) private void do_start(Intent intent)
{ {
LOG("Start Service"); LOG("Start RockboxService (Intent: " + intent.getAction() + ")");
if (intent != null && intent.hasExtra("callback")) if (intent.hasExtra("callback"))
resultReceiver = (ResultReceiver) intent.getParcelableExtra("callback"); resultReceiver = (ResultReceiver) intent.getParcelableExtra("callback");
if (!rockbox_running) if (!rockbox_running)
@ -114,32 +114,18 @@ public class RockboxService extends Service
if (resultReceiver != null) if (resultReceiver != null)
resultReceiver.send(RESULT_LIB_LOADED, null); resultReceiver.send(RESULT_LIB_LOADED, null);
if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON))
if (intent != null && intent.getAction() != null) {
{ /* give it a bit of time so we can register button presses
if (!rockbox_running) * sleeping longer doesn't work here, apparently Android
{ /* give it a bit of time so we can register button presses * surpresses long sleeps during intent handling */
* sleeping longer doesn't work here, apparently Android try {
* surpresses long sleeps during intent handling */ Thread.sleep(50);
try { } catch (InterruptedException e) { }
Thread.sleep(50);
}
catch (InterruptedException e) { }
}
if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) KeyEvent kev = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
{ RockboxFramebuffer.buttonHandler(kev.getKeyCode(),
KeyEvent kev = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); kev.getAction() == KeyEvent.ACTION_DOWN);
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 */ /* (Re-)attach the media button receiver, in case it has been lost */

View file

@ -29,7 +29,6 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.CheckBox; import android.widget.CheckBox;
@ -81,7 +80,7 @@ public class RockboxWidgetConfigure extends Activity
saveWidgetPref(context, mAppWidgetId, state); saveWidgetPref(context, mAppWidgetId, state);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RockboxWidgetProvider.updateAppWidget(context, appWidgetManager, mAppWidgetId, null); RockboxWidgetProvider.getInstance().updateAppWidget(context, appWidgetManager, mAppWidgetId, null);
Intent result = new Intent(); Intent result = new Intent();
result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);

View file

@ -24,7 +24,6 @@ package org.rockbox.widgets;
import org.rockbox.R; import org.rockbox.R;
import org.rockbox.RockboxActivity; import org.rockbox.RockboxActivity;
import org.rockbox.RockboxService; import org.rockbox.RockboxService;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider; import android.appwidget.AppWidgetProvider;
@ -32,17 +31,19 @@ import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import java.util.ArrayList;
public class RockboxWidgetProvider extends AppWidgetProvider public class RockboxWidgetProvider extends AppWidgetProvider
{ {
static RockboxWidgetProvider mInstance;
public RockboxWidgetProvider()
{
super();
mInstance = this;
}
@Override @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{ {
@ -54,6 +55,12 @@ public class RockboxWidgetProvider extends AppWidgetProvider
} }
} }
public static RockboxWidgetProvider getInstance()
{
/* no new instance here, instanced by android */
return mInstance;
}
@Override @Override
public void onDeleted(Context context, int[] appWidgetIds) public void onDeleted(Context context, int[] appWidgetIds)
@ -74,9 +81,9 @@ public class RockboxWidgetProvider extends AppWidgetProvider
public void onReceive(Context context, Intent intent) public void onReceive(Context context, Intent intent)
{ {
String action = intent.getAction(); String action = intent.getAction();
if (intent.getAction().equals("org.rockbox.TrackUpdateInfo") || if (action.equals("org.rockbox.TrackUpdateInfo") ||
intent.getAction().equals("org.rockbox.TrackFinish") || action.equals("org.rockbox.TrackFinish") ||
intent.getAction().equals("org.rockbox.UpdateState")) action.equals("org.rockbox.UpdateState"))
{ {
AppWidgetManager gm = AppWidgetManager.getInstance(context); AppWidgetManager gm = AppWidgetManager.getInstance(context);
int[] appWidgetIds = gm.getAppWidgetIds(new ComponentName(context, this.getClass())); int[] appWidgetIds = gm.getAppWidgetIds(new ComponentName(context, this.getClass()));
@ -92,7 +99,7 @@ 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); AppWidgetProviderInfo provider = appWidgetManager.getAppWidgetInfo(appWidgetId);
RemoteViews views = null; RemoteViews views = null;
@ -104,38 +111,42 @@ public class RockboxWidgetProvider extends AppWidgetProvider
RockboxWidgetConfigure.WidgetPref state = RockboxWidgetConfigure.loadWidgetPref(context, appWidgetId); RockboxWidgetConfigure.WidgetPref state = RockboxWidgetConfigure.loadWidgetPref(context, appWidgetId);
/* enable/disable PREV */
if (state.enablePrev) if (state.enablePrev)
{ {
intent = new Intent("org.rockbox.Prev", Uri.EMPTY, context, RockboxService.class); views.setOnClickPendingIntent(R.id.prev,
pendingIntent = PendingIntent.getService(context, 0, intent, 0); RockboxMediaIntent.newPendingIntent(context,
views.setOnClickPendingIntent(R.id.prev, pendingIntent); KeyEvent.KEYCODE_MEDIA_PREVIOUS));
} }
else else
views.setViewVisibility(R.id.prev, View.GONE); views.setViewVisibility(R.id.prev, View.GONE);
/* enable/disable PLAY/PAUSE */
if (state.enablePlayPause) if (state.enablePlayPause)
{ {
intent = new Intent("org.rockbox.PlayPause", Uri.EMPTY, context, RockboxService.class); views.setOnClickPendingIntent(R.id.playPause,
pendingIntent = PendingIntent.getService(context, 0, intent, 0); RockboxMediaIntent.newPendingIntent(context,
views.setOnClickPendingIntent(R.id.playPause, pendingIntent); KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
} }
else else
views.setViewVisibility(R.id.playPause, View.GONE); views.setViewVisibility(R.id.playPause, View.GONE);
/* enable/disable NEXT */
if (state.enableNext) if (state.enableNext)
{ {
intent = new Intent("org.rockbox.Next", Uri.EMPTY, context, RockboxService.class); views.setOnClickPendingIntent(R.id.next,
pendingIntent = PendingIntent.getService(context, 0, intent, 0); RockboxMediaIntent.newPendingIntent(context,
views.setOnClickPendingIntent(R.id.next, pendingIntent); KeyEvent.KEYCODE_MEDIA_NEXT));
} }
else else
views.setViewVisibility(R.id.next, View.GONE); views.setViewVisibility(R.id.next, View.GONE);
/* enable/disable STOP */
if (state.enableStop) if (state.enableStop)
{ {
intent = new Intent("org.rockbox.Stop", Uri.EMPTY, context, RockboxService.class); views.setOnClickPendingIntent(R.id.stop,
pendingIntent = PendingIntent.getService(context, 0, intent, 0); RockboxMediaIntent.newPendingIntent(context,
views.setOnClickPendingIntent(R.id.stop, pendingIntent); KeyEvent.KEYCODE_MEDIA_STOP));
} }
else else
views.setViewVisibility(R.id.stop, View.GONE); views.setViewVisibility(R.id.stop, View.GONE);
@ -166,6 +177,26 @@ public class RockboxWidgetProvider extends AppWidgetProvider
} }
appWidgetManager.updateAppWidget(appWidgetId, views); appWidgetManager.updateAppWidget(appWidgetId, views);
} }
private static class RockboxMediaIntent extends Intent
{
private RockboxMediaIntent(Context c, int keycode)
{
super(ACTION_MEDIA_BUTTON, Uri.EMPTY, c, RockboxService.class);
putExtra(EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
keycode));
}
public static PendingIntent newPendingIntent(Context c, int keycode)
{
/* Use keycode as request to code to prevent successive
* PendingIntents from overwritting one another.
* This seems hackish but at least it works.
* see: http://code.google.com/p/android/issues/detail?id=863
*/
return PendingIntent.getService(c, keycode, new RockboxMediaIntent(c, keycode), 0);
}
}
} }