Improve widget response time

This commit is contained in:
Christopher Eby 2010-05-23 14:41:18 -05:00
parent b80fe99875
commit 2273f77ab2
3 changed files with 28 additions and 13 deletions

@ -51,8 +51,6 @@
android:label="Vanilla Music 1x1"> android:label="Vanilla Music 1x1">
<intent-filter> <intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="org.kreed.vanilla.event.CHANGED" />
<action android:name="org.kreed.vanilla.event.REPLACE_SONG" />
</intent-filter> </intent-filter>
<meta-data <meta-data
android:name="android.appwidget.provider" android:name="android.appwidget.provider"

@ -127,14 +127,15 @@ public class ContextApplication extends Application {
*/ */
public static void broadcast(Intent intent) public static void broadcast(Intent intent)
{ {
ArrayList<Activity> list = mActivities; OneCellWidget.receive(intent);
if (list == null)
return;
for (int i = list.size(); --i != -1; ) { ArrayList<Activity> list = mActivities;
Activity activity = list.get(i); if (list != null) {
if (activity instanceof PlaybackActivity) for (int i = list.size(); --i != -1; ) {
((PlaybackActivity)activity).receive(intent); Activity activity = list.get(i);
if (activity instanceof PlaybackActivity)
((PlaybackActivity)activity).receive(intent);
}
} }
if (mInstance != null) if (mInstance != null)

@ -27,6 +27,10 @@ import android.content.Intent;
import android.util.TypedValue; import android.util.TypedValue;
import android.widget.RemoteViews; import android.widget.RemoteViews;
/**
* Provider for the smallish one cell widget. Handles updating for current
* PlaybackService state.
*/
public class OneCellWidget extends AppWidgetProvider { public class OneCellWidget extends AppWidgetProvider {
@Override @Override
public void onUpdate(Context context, AppWidgetManager manager, int[] ids) public void onUpdate(Context context, AppWidgetManager manager, int[] ids)
@ -41,11 +45,17 @@ public class OneCellWidget extends AppWidgetProvider {
timeline.saveState(context, 0); timeline.saveState(context, 0);
} }
@Override /**
public void onReceive(Context context, Intent intent) * Receive a broadcast sent by the PlaybackService and update the widget
* accordingly.
*
* @param intent The intent that was broadcast.
*/
public static void receive(Intent intent)
{ {
String action = intent.getAction(); String action = intent.getAction();
if (PlaybackService.EVENT_CHANGED.equals(action) || PlaybackService.EVENT_REPLACE_SONG.equals(action)) { if (PlaybackService.EVENT_CHANGED.equals(action) || PlaybackService.EVENT_REPLACE_SONG.equals(action)) {
Context context = ContextApplication.getContext();
Song song = intent.getParcelableExtra("song"); Song song = intent.getParcelableExtra("song");
int state = intent.getIntExtra("state", -1); int state = intent.getIntExtra("state", -1);
@ -53,11 +63,17 @@ public class OneCellWidget extends AppWidgetProvider {
RemoteViews views = createViews(context, song, state); RemoteViews views = createViews(context, song, state);
AppWidgetManager.getInstance(context).updateAppWidget(widget, views); AppWidgetManager.getInstance(context).updateAppWidget(widget, views);
} else {
super.onReceive(context, intent);
} }
} }
/**
* Create the RemoteViews that will be used to update the widget.
*
* @param context A Context to use.
* @param song The current Song in PlaybackService.
* @param state The current PlaybackService state.
* @return A RemoteViews instance, ready to be sent with updateAppWidget.
*/
public static RemoteViews createViews(Context context, Song song, int state) public static RemoteViews createViews(Context context, Song song, int state)
{ {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.one_cell_widget); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.one_cell_widget);