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);
Intent intent = new Intent(this, RockboxService.class);
intent.setAction(Intent.ACTION_MAIN);
intent.putExtra("callback", new ResultReceiver(new Handler(getMainLooper())) {
private ProgressDialog loadingdialog;

View file

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

View file

@ -24,7 +24,6 @@ package org.rockbox.widgets;
import org.rockbox.R;
import org.rockbox.RockboxActivity;
import org.rockbox.RockboxService;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
@ -32,17 +31,19 @@ import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews;
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 +55,12 @@ public class RockboxWidgetProvider extends AppWidgetProvider
}
}
public static RockboxWidgetProvider getInstance()
{
/* no new instance here, instanced by android */
return mInstance;
}
@Override
public void onDeleted(Context context, int[] appWidgetIds)
@ -74,9 +81,9 @@ public class RockboxWidgetProvider extends AppWidgetProvider
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (intent.getAction().equals("org.rockbox.TrackUpdateInfo") ||
intent.getAction().equals("org.rockbox.TrackFinish") ||
intent.getAction().equals("org.rockbox.UpdateState"))
if (action.equals("org.rockbox.TrackUpdateInfo") ||
action.equals("org.rockbox.TrackFinish") ||
action.equals("org.rockbox.UpdateState"))
{
AppWidgetManager gm = AppWidgetManager.getInstance(context);
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);
RemoteViews views = null;
@ -104,38 +111,42 @@ public class RockboxWidgetProvider extends AppWidgetProvider
RockboxWidgetConfigure.WidgetPref state = RockboxWidgetConfigure.loadWidgetPref(context, appWidgetId);
/* enable/disable PREV */
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);
views.setOnClickPendingIntent(R.id.prev,
RockboxMediaIntent.newPendingIntent(context,
KeyEvent.KEYCODE_MEDIA_PREVIOUS));
}
else
views.setViewVisibility(R.id.prev, View.GONE);
/* enable/disable PLAY/PAUSE */
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);
views.setOnClickPendingIntent(R.id.playPause,
RockboxMediaIntent.newPendingIntent(context,
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
}
else
views.setViewVisibility(R.id.playPause, View.GONE);
/* enable/disable NEXT */
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);
views.setOnClickPendingIntent(R.id.next,
RockboxMediaIntent.newPendingIntent(context,
KeyEvent.KEYCODE_MEDIA_NEXT));
}
else
views.setViewVisibility(R.id.next, View.GONE);
/* enable/disable STOP */
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);
views.setOnClickPendingIntent(R.id.stop,
RockboxMediaIntent.newPendingIntent(context,
KeyEvent.KEYCODE_MEDIA_STOP));
}
else
views.setViewVisibility(R.id.stop, View.GONE);
@ -166,6 +177,26 @@ public class RockboxWidgetProvider extends AppWidgetProvider
}
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);
}
}
}