From 4ba2eddd8062662027069c22a00efde34ba9ca9e Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Sat, 17 Sep 2011 02:12:57 -0500 Subject: [PATCH] Do less work when widgets are inactive --- src/org/kreed/vanilla/ContextApplication.java | 4 -- src/org/kreed/vanilla/FourLongWidget.java | 57 ++++++++----------- src/org/kreed/vanilla/FourSquareWidget.java | 57 ++++++++----------- src/org/kreed/vanilla/OneCellWidget.java | 56 ++++++++---------- src/org/kreed/vanilla/PlaybackService.java | 32 ++++++++++- 5 files changed, 104 insertions(+), 102 deletions(-) diff --git a/src/org/kreed/vanilla/ContextApplication.java b/src/org/kreed/vanilla/ContextApplication.java index 6806aef0..b1996c2a 100644 --- a/src/org/kreed/vanilla/ContextApplication.java +++ b/src/org/kreed/vanilla/ContextApplication.java @@ -159,10 +159,6 @@ public class ContextApplication extends Application { */ public static void broadcast(Intent intent) { - OneCellWidget.receive(intent); - FourLongWidget.receive(intent); - FourSquareWidget.receive(intent); - ArrayList list = mActivities; if (list != null) { for (int i = list.size(); --i != -1; ) { diff --git a/src/org/kreed/vanilla/FourLongWidget.java b/src/org/kreed/vanilla/FourLongWidget.java index df3884ca..3dab45e6 100644 --- a/src/org/kreed/vanilla/FourLongWidget.java +++ b/src/org/kreed/vanilla/FourLongWidget.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Christopher Eby + * Copyright (C) 2010, 2011 Christopher Eby * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -32,11 +32,24 @@ import android.view.View; import android.widget.RemoteViews; /** - * Simple 1x4 widget to show currently playing song, album art, and play/pause, - * next, and previous buttons. Uses artwork from the default Android music - * widget. + * 1x4 widget that shows title, artist, album art, a play/pause button, and a + * next button. */ public class FourLongWidget extends AppWidgetProvider { + private static boolean sEnabled; + + @Override + public void onEnabled(Context context) + { + sEnabled = true; + } + + @Override + public void onDisabled(Context context) + { + sEnabled = false; + } + @Override public void onUpdate(Context context, AppWidgetManager manager, int[] ids) { @@ -49,35 +62,16 @@ public class FourLongWidget extends AppWidgetProvider { state = service.getState(); } - updateWidget(context, manager, ids, song, state); + sEnabled = true; + updateWidget(context, manager, song, state); } /** - * Receive a broadcast sent by the PlaybackService and update the widget - * accordingly. - * - * @param intent The intent that was broadcast. + * Check if there are any instances of this widget placed. */ - public static void receive(Intent intent) + public static void checkEnabled(Context context, AppWidgetManager manager) { - String action = intent.getAction(); - if (PlaybackService.EVENT_CHANGED.equals(action) || PlaybackService.EVENT_REPLACE_SONG.equals(action)) { - Context context = ContextApplication.getContext(); - Song song; - if (intent.hasExtra("song")) - song = intent.getParcelableExtra("song"); - else - song = ContextApplication.getService().getSong(0); - int state; - if (intent.hasExtra("state")) - state = intent.getIntExtra("state", 0); - else - state = ContextApplication.getService().getState(); - - AppWidgetManager manager = AppWidgetManager.getInstance(context); - int[] ids = manager.getAppWidgetIds(new ComponentName(context, FourLongWidget.class)); - updateWidget(context, manager, ids, song, state); - } + sEnabled = manager.getAppWidgetIds(new ComponentName(context, FourLongWidget.class)).length != 0; } /** @@ -86,13 +80,12 @@ public class FourLongWidget extends AppWidgetProvider { * @param context A Context to use. * @param manager The AppWidgetManager that will be used to update the * widget. - * @param ids An array containing the ids of all the widgets to update. * @param song The current Song in PlaybackService. * @param state The current PlaybackService state. */ - public static void updateWidget(Context context, AppWidgetManager manager, int[] ids, Song song, int state) + public static void updateWidget(Context context, AppWidgetManager manager, Song song, int state) { - if (ids == null || ids.length == 0) + if (!sEnabled) return; RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.four_long_widget); @@ -136,6 +129,6 @@ public class FourLongWidget extends AppWidgetProvider { pendingIntent = PendingIntent.getService(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.next, pendingIntent); - manager.updateAppWidget(ids, views); + manager.updateAppWidget(new ComponentName(context, FourLongWidget.class), views); } } diff --git a/src/org/kreed/vanilla/FourSquareWidget.java b/src/org/kreed/vanilla/FourSquareWidget.java index c92e389a..6e0e03d5 100644 --- a/src/org/kreed/vanilla/FourSquareWidget.java +++ b/src/org/kreed/vanilla/FourSquareWidget.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Christopher Eby + * Copyright (C) 2010, 2011 Christopher Eby * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -32,11 +32,24 @@ import android.view.View; import android.widget.RemoteViews; /** - * Simple 1x4 widget to show currently playing song, album art, and play/pause, - * next, and previous buttons. Uses artwork from the default Android music - * widget. + * 2x2 widget that shows title, artist, a (hidden) play/pause button, a (hidden) + * next button, and cover art in the background. */ public class FourSquareWidget extends AppWidgetProvider { + private static boolean sEnabled; + + @Override + public void onEnabled(Context context) + { + sEnabled = true; + } + + @Override + public void onDisabled(Context context) + { + sEnabled = false; + } + @Override public void onUpdate(Context context, AppWidgetManager manager, int[] ids) { @@ -49,35 +62,16 @@ public class FourSquareWidget extends AppWidgetProvider { state = service.getState(); } - updateWidget(context, manager, ids, song, state); + sEnabled = true; + updateWidget(context, manager, song, state); } /** - * Receive a broadcast sent by the PlaybackService and update the widget - * accordingly. - * - * @param intent The intent that was broadcast. + * Check if there are any instances of this widget placed. */ - public static void receive(Intent intent) + public static void checkEnabled(Context context, AppWidgetManager manager) { - String action = intent.getAction(); - if (PlaybackService.EVENT_CHANGED.equals(action) || PlaybackService.EVENT_REPLACE_SONG.equals(action)) { - Context context = ContextApplication.getContext(); - Song song; - if (intent.hasExtra("song")) - song = intent.getParcelableExtra("song"); - else - song = ContextApplication.getService().getSong(0); - int state; - if (intent.hasExtra("state")) - state = intent.getIntExtra("state", 0); - else - state = ContextApplication.getService().getState(); - - AppWidgetManager manager = AppWidgetManager.getInstance(context); - int[] ids = manager.getAppWidgetIds(new ComponentName(context, FourSquareWidget.class)); - updateWidget(context, manager, ids, song, state); - } + sEnabled = manager.getAppWidgetIds(new ComponentName(context, FourSquareWidget.class)).length != 0; } /** @@ -86,13 +80,12 @@ public class FourSquareWidget extends AppWidgetProvider { * @param context A Context to use. * @param manager The AppWidgetManager that will be used to update the * widget. - * @param ids An array containing the ids of all the widgets to update. * @param song The current Song in PlaybackService. * @param state The current PlaybackService state. */ - public static void updateWidget(Context context, AppWidgetManager manager, int[] ids, Song song, int state) + public static void updateWidget(Context context, AppWidgetManager manager, Song song, int state) { - if (ids == null || ids.length == 0) + if (!sEnabled) return; RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.four_square_widget); @@ -135,6 +128,6 @@ public class FourSquareWidget extends AppWidgetProvider { pendingIntent = PendingIntent.getService(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.next, pendingIntent); - manager.updateAppWidget(ids, views); + manager.updateAppWidget(new ComponentName(context, FourSquareWidget.class), views); } } diff --git a/src/org/kreed/vanilla/OneCellWidget.java b/src/org/kreed/vanilla/OneCellWidget.java index 56206075..9393b3b2 100644 --- a/src/org/kreed/vanilla/OneCellWidget.java +++ b/src/org/kreed/vanilla/OneCellWidget.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Christopher Eby + * Copyright (C) 2010, 2011 Christopher Eby * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -33,10 +33,24 @@ import android.preference.PreferenceManager; import android.widget.RemoteViews; /** - * Provider for the smallish one cell widget. Handles updating for current - * PlaybackService state. + * 1x1 widget that shows title, album art, and hidden next and play/pause + * buttons. */ public class OneCellWidget extends AppWidgetProvider { + private static boolean sEnabled; + + @Override + public void onEnabled(Context context) + { + sEnabled = true; + } + + @Override + public void onDisabled(Context context) + { + sEnabled = false; + } + @Override public void onUpdate(Context context, AppWidgetManager manager, int[] ids) { @@ -49,35 +63,16 @@ public class OneCellWidget extends AppWidgetProvider { state = service.getState(); } - updateWidget(context, manager, ids, song, state); + sEnabled = true; + updateWidget(context, manager, song, state); } /** - * Receive a broadcast sent by the PlaybackService and update the widget - * accordingly. - * - * @param intent The intent that was broadcast. + * Check if there are any instances of this widget placed. */ - public static void receive(Intent intent) + public static void checkEnabled(Context context, AppWidgetManager manager) { - String action = intent.getAction(); - if (PlaybackService.EVENT_CHANGED.equals(action) || PlaybackService.EVENT_REPLACE_SONG.equals(action)) { - Context context = ContextApplication.getContext(); - Song song; - if (intent.hasExtra("song")) - song = intent.getParcelableExtra("song"); - else - song = ContextApplication.getService().getSong(0); - int state; - if (intent.hasExtra("state")) - state = intent.getIntExtra("state", 0); - else - state = ContextApplication.getService().getState(); - - AppWidgetManager manager = AppWidgetManager.getInstance(context); - int[] ids = manager.getAppWidgetIds(new ComponentName(context, OneCellWidget.class)); - updateWidget(context, manager, ids, song, state); - } + sEnabled = manager.getAppWidgetIds(new ComponentName(context, FourLongWidget.class)).length != 0; } /** @@ -86,13 +81,12 @@ public class OneCellWidget extends AppWidgetProvider { * @param context A Context to use. * @param manager The AppWidgetManager that will be used to update the * widget. - * @param ids An array containing the ids of all the widgets to update. * @param song The current Song in PlaybackService. * @param state The current PlaybackService state. */ - public static void updateWidget(Context context, AppWidgetManager manager, int[] ids, Song song, int state) + public static void updateWidget(Context context, AppWidgetManager manager, Song song, int state) { - if (ids == null || ids.length == 0) + if (!sEnabled) return; SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); @@ -124,6 +118,6 @@ public class OneCellWidget extends AppWidgetProvider { views.setTextViewText(R.id.title, song.title); } - manager.updateAppWidget(ids, views); + manager.updateAppWidget(new ComponentName(context, OneCellWidget.class), views); } } diff --git a/src/org/kreed/vanilla/PlaybackService.java b/src/org/kreed/vanilla/PlaybackService.java index 67333a3a..2bce2377 100644 --- a/src/org/kreed/vanilla/PlaybackService.java +++ b/src/org/kreed/vanilla/PlaybackService.java @@ -28,6 +28,7 @@ import java.util.ArrayList; import android.app.Activity; import android.app.NotificationManager; import android.app.Service; +import android.appwidget.AppWidgetManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -190,6 +191,8 @@ public final class PlaybackService extends Service implements Handler.Callback, mHandler = new Handler(mLooper, this); mHandler.sendEmptyMessage(POST_CREATE); + initWidgets(); + int state = 0; if (mTimeline.isRepeating()) state |= FLAG_REPEAT; @@ -217,9 +220,6 @@ public final class PlaybackService extends Service implements Handler.Callback, mHandler.sendMessageDelayed(mHandler.obtainMessage(CALL_GO, Integer.valueOf(0)), 400); } } else if (ACTION_NEXT_SONG.equals(action)) { - // Preemptively broadcast an update in attempt to hasten UI - // feedback. - broadcastReplaceSong(0, getSong(+1)); go(1, false); } else if (ACTION_NEXT_SONG_AUTOPLAY.equals(action)) { go(1, true); @@ -437,10 +437,36 @@ public final class PlaybackService extends Service implements Handler.Callback, intent.putExtra("time", uptime); ContextApplication.broadcast(intent); + updateWidgets(); + if (mScrobble) scrobble(); } + /** + * Check if there are any instances of each widget. + */ + private void initWidgets() + { + AppWidgetManager manager = AppWidgetManager.getInstance(this); + OneCellWidget.checkEnabled(this, manager); + FourSquareWidget.checkEnabled(this, manager); + FourLongWidget.checkEnabled(this, manager); + } + + /** + * Update the widgets with the current song and state. + */ + private void updateWidgets() + { + AppWidgetManager manager = AppWidgetManager.getInstance(this); + Song song = mCurrentSong; + int state = mState; + OneCellWidget.updateWidget(this, manager, song, state); + FourLongWidget.updateWidget(this, manager, song, state); + FourSquareWidget.updateWidget(this, manager, song, state); + } + private void scrobble() { assert(mScrobble == true);