Do less work when widgets are inactive

This commit is contained in:
Christopher Eby 2011-09-17 02:12:57 -05:00
parent 34624b00c3
commit 4ba2eddd80
5 changed files with 104 additions and 102 deletions

View File

@ -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<Activity> list = mActivities;
if (list != null) {
for (int i = list.size(); --i != -1; ) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
* Copyright (C) 2010, 2011 Christopher Eby <kreed@kreed.org>
*
* 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);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
* Copyright (C) 2010, 2011 Christopher Eby <kreed@kreed.org>
*
* 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);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
* Copyright (C) 2010, 2011 Christopher Eby <kreed@kreed.org>
*
* 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);
}
}

View File

@ -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);