Permalink
Browse files

Listen for song change in AppWidgetProvider

  • Loading branch information...
1 parent 4acc55f commit 11d175cb9402b482c7f8b3d4650ef8c16bfc1c62 @kreed committed Mar 18, 2010
View
1 AndroidManifest.xml
@@ -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"
View
BIN res/drawable/icon_selected.png
Deleted file not rendered
View
10 res/drawable/icon_variable.xml
@@ -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
9 res/layout/default_widget.xml
@@ -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
2 res/xml/one_cell_widget.xml
@@ -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
31 src/org/kreed/vanilla/OneCellWidget.java
@@ -6,41 +6,42 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+import android.util.Log;
import android.util.TypedValue;
import android.widget.RemoteViews;
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
29 src/org/kreed/vanilla/PlaybackService.java
@@ -54,7 +54,6 @@
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 void onDestroy()
}
mNotificationManager.cancel(NOTIFICATION_ID);
- resetWidgets();
-
if (mWakeLock != null && mWakeLock.isHeld())
mWakeLock.release();
}
@@ -240,9 +237,6 @@ public void onReceive(Context content, Intent intent)
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 void run()
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 void run()
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 void run()
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 void updateState(int state)
broadcastChange(oldState, state, song);
boolean cancelNotification = updateNotification();
- updateWidgets(song);
if (mState != oldState) {
if (mState == STATE_PLAYING)
@@ -650,16 +639,6 @@ private Song getSong(int delta)
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;

0 comments on commit 11d175c

Please sign in to comment.