Listen for song change in AppWidgetProvider

This commit is contained in:
Christopher Eby 2010-03-18 09:12:36 -05:00
parent 4acc55f25b
commit 11d175cb94
7 changed files with 22 additions and 60 deletions

View File

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/icon_selected" />
<item
android:state_focused="true"
android:drawable="@drawable/icon_selected" />
<item android:drawable="@drawable/icon" />
</selector>

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<ImageButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/stopped_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:background="@null"
android:src="@drawable/icon_variable" />

View File

@ -2,5 +2,5 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dip"
android:minHeight="72dip"
android:initialLayout="@layout/default_widget"
android:initialLayout="@layout/one_cell_widget"
/>

View File

@ -6,6 +6,7 @@ import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.util.TypedValue;
import android.widget.RemoteViews;
@ -13,34 +14,34 @@ public class OneCellWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager manager, int[] ids)
{
reset(context);
context.sendBroadcast(new Intent(PlaybackService.APPWIDGET_SMALL_UPDATE));
Log.i("VanillaMusic", "initial update");
}
private static void sendUpdate(Context context, RemoteViews views)
@Override
public void onReceive(Context context, Intent intent)
{
AppWidgetManager manager = AppWidgetManager.getInstance(context);
ComponentName widget = new ComponentName(context, OneCellWidget.class);
manager.updateAppWidget(widget, views);
if (PlaybackService.EVENT_CHANGED.equals(intent.getAction())) {
Song song = intent.getParcelableExtra("song");
boolean playing = intent.getIntExtra("newState", 0) == PlaybackService.STATE_PLAYING;
update(context, song, playing);
} else {
super.onReceive(context, intent);
}
}
public static void update(Context context, Song song)
public static void update(Context context, Song song, boolean playing)
{
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.one_cell_widget);
ComponentName widget = new ComponentName(context, OneCellWidget.class);
views.setOnClickPendingIntent(R.id.play_pause, PendingIntent.getBroadcast(context, 0, new Intent(PlaybackService.TOGGLE_PLAYBACK), 0));
views.setOnClickPendingIntent(R.id.next, PendingIntent.getBroadcast(context, 0, new Intent(PlaybackService.NEXT_SONG), 0));
if (song != null) {
int size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 72, context.getResources().getDisplayMetrics());
views.setImageViewBitmap(R.id.cover_view, CoverView.createMiniBitmap(song, size, size));
}
sendUpdate(context, views);
}
public static void reset(Context context)
{
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.default_widget);
views.setOnClickPendingIntent(R.id.stopped_text, PendingIntent.getService(context, 0, new Intent(context, PlaybackService.class), 0));
sendUpdate(context, views);
AppWidgetManager.getInstance(context).updateAppWidget(widget, views);
}
}

View File

@ -54,7 +54,6 @@ import android.util.Log;
public class PlaybackService extends Service implements Runnable, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, SharedPreferences.OnSharedPreferenceChangeListener {
private static final int NOTIFICATION_ID = 2;
public static final String APPWIDGET_SMALL_UPDATE = "org.kreed.vanilla.action.APPWIDGET_SMALL_UPDATE";
public static final String TOGGLE_PLAYBACK = "org.kreed.vanilla.action.TOGGLE_PLAYBACK";
public static final String NEXT_SONG = "org.kreed.vanilla.action.NEXT_SONG";
@ -172,8 +171,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
}
mNotificationManager.cancel(NOTIFICATION_ID);
resetWidgets();
if (mWakeLock != null && mWakeLock.isHeld())
mWakeLock.release();
}
@ -240,9 +237,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
go(0);
} else if (NEXT_SONG.equals(action)) {
go(1);
} else if (APPWIDGET_SMALL_UPDATE.equals(intent.getAction())) {
OneCellWidget.update(PlaybackService.this, getSong(0));
return;
}
}
};
@ -336,12 +330,11 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
Song song = new Song(ids[mCurrentSong]);
song.populate();
if (song.path == null) {
if (song.path == null)
stateLoaded = false;
} else {
else
broadcastChange(mState, mState, song);
updateWidgets(song);
}
ArrayList<Song> timeline = new ArrayList<Song>(n);
for (int i = 0; i != n; ++i)
@ -361,9 +354,7 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
if (!stateLoaded) {
retrieveSongs();
mSongTimeline = new ArrayList<Song>();
Song song = getSong(0);
broadcastChange(mState, mState, song);
updateWidgets(song);
broadcastChange(mState, mState, getSong(0));
}
if (stateLoaded)
@ -418,7 +409,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
filter.addAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
filter.addAction(TOGGLE_PLAYBACK);
filter.addAction(NEXT_SONG);
filter.addAction(APPWIDGET_SMALL_UPDATE);
registerReceiver(mReceiver, filter);
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
@ -477,7 +467,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
broadcastChange(oldState, state, song);
boolean cancelNotification = updateNotification();
updateWidgets(song);
if (mState != oldState) {
if (mState == STATE_PLAYING)
@ -650,16 +639,6 @@ public class PlaybackService extends Service implements Runnable, MediaPlayer.On
return song;
}
private void updateWidgets(Song song)
{
OneCellWidget.update(this, song);
}
private void resetWidgets()
{
OneCellWidget.reset(this);
}
private static final String STATE_FILE = "state";
private static final long STATE_FILE_MAGIC = 0x8a9d3f9fca31L;