From 2273f77ab29a4841e97e201a805f04babfe278fc Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Sun, 23 May 2010 14:41:18 -0500 Subject: [PATCH] Improve widget response time --- AndroidManifest.xml | 2 -- src/org/kreed/vanilla/ContextApplication.java | 15 ++++++------ src/org/kreed/vanilla/OneCellWidget.java | 24 +++++++++++++++---- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index fff43ff0..042b5356 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -51,8 +51,6 @@ android:label="Vanilla Music 1x1"> - - list = mActivities; - if (list == null) - return; + OneCellWidget.receive(intent); - for (int i = list.size(); --i != -1; ) { - Activity activity = list.get(i); - if (activity instanceof PlaybackActivity) - ((PlaybackActivity)activity).receive(intent); + ArrayList list = mActivities; + if (list != null) { + for (int i = list.size(); --i != -1; ) { + Activity activity = list.get(i); + if (activity instanceof PlaybackActivity) + ((PlaybackActivity)activity).receive(intent); + } } if (mInstance != null) diff --git a/src/org/kreed/vanilla/OneCellWidget.java b/src/org/kreed/vanilla/OneCellWidget.java index 670427e7..509e1b23 100644 --- a/src/org/kreed/vanilla/OneCellWidget.java +++ b/src/org/kreed/vanilla/OneCellWidget.java @@ -27,6 +27,10 @@ import android.content.Intent; import android.util.TypedValue; import android.widget.RemoteViews; +/** + * Provider for the smallish one cell widget. Handles updating for current + * PlaybackService state. + */ public class OneCellWidget extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager manager, int[] ids) @@ -41,11 +45,17 @@ public class OneCellWidget extends AppWidgetProvider { 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(); if (PlaybackService.EVENT_CHANGED.equals(action) || PlaybackService.EVENT_REPLACE_SONG.equals(action)) { + Context context = ContextApplication.getContext(); Song song = intent.getParcelableExtra("song"); int state = intent.getIntExtra("state", -1); @@ -53,11 +63,17 @@ public class OneCellWidget extends AppWidgetProvider { RemoteViews views = createViews(context, song, state); 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) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.one_cell_widget);