From 5b8ddee28e714f69e90b1c0f57c50f4a2570f3ee Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 18 Jan 2016 23:25:54 +0100 Subject: [PATCH] update song artist/title info on the widget --- .../simplemobiletools/com/Events.java | 20 +++++++++++--- .../simplemobiletools/com/MusicService.java | 16 ++++++----- .../com/MyWidgetProvider.java | 27 ++++++++++++++----- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/musicplayer/simplemobiletools/com/Events.java b/app/src/main/java/musicplayer/simplemobiletools/com/Events.java index 45bbc3ad..9cb76c5a 100644 --- a/app/src/main/java/musicplayer/simplemobiletools/com/Events.java +++ b/app/src/main/java/musicplayer/simplemobiletools/com/Events.java @@ -1,15 +1,27 @@ package musicplayer.simplemobiletools.com; public class Events { - public static class MusicPrevious { + public static class PreviousSong { } - public static class MusicPlayPause { + public static class PlayPauseSong { } - public static class MusicNext { + public static class NextSong { } - public static class MusicStop { + public static class StopSong { + } + + public static class SongChanged { + private Song song; + + SongChanged(Song song) { + this.song = song; + } + + public Song getSong() { + return song; + } } } diff --git a/app/src/main/java/musicplayer/simplemobiletools/com/MusicService.java b/app/src/main/java/musicplayer/simplemobiletools/com/MusicService.java index ae8d20cd..035daebb 100644 --- a/app/src/main/java/musicplayer/simplemobiletools/com/MusicService.java +++ b/app/src/main/java/musicplayer/simplemobiletools/com/MusicService.java @@ -35,8 +35,11 @@ public class MusicService extends Service super.onCreate(); songs = new ArrayList<>(); playedSongIDs = new ArrayList<>(); - bus = BusProvider.getInstance(); - bus.register(this); + + if (bus == null) { + bus = BusProvider.getInstance(); + bus.register(this); + } initMediaPlayer(); } @@ -136,6 +139,7 @@ public class MusicService extends Service } player.prepareAsync(); + bus.post(new Events.SongChanged(currSong)); } public Song getCurrSong() { @@ -200,12 +204,12 @@ public class MusicService extends Service } @Subscribe - public void musicPreviousEvent(Events.MusicPrevious event) { + public void previousSongEvent(Events.PreviousSong event) { playPreviousSong(); } @Subscribe - public void musicPlayPauseEvent(Events.MusicPlayPause event) { + public void playPauseSongEvent(Events.PlayPauseSong event) { if (isPlaying()) pauseSong(); else @@ -213,12 +217,12 @@ public class MusicService extends Service } @Subscribe - public void musicNextEvent(Events.MusicNext event) { + public void nextSongEvent(Events.NextSong event) { playNextSong(); } @Subscribe - public void musicStopEvent(Events.MusicStop event) { + public void stopSongEvent(Events.StopSong event) { stopSong(); } } diff --git a/app/src/main/java/musicplayer/simplemobiletools/com/MyWidgetProvider.java b/app/src/main/java/musicplayer/simplemobiletools/com/MyWidgetProvider.java index 89326572..f83f2341 100644 --- a/app/src/main/java/musicplayer/simplemobiletools/com/MyWidgetProvider.java +++ b/app/src/main/java/musicplayer/simplemobiletools/com/MyWidgetProvider.java @@ -9,6 +9,7 @@ import android.content.Intent; import android.widget.RemoteViews; import com.squareup.otto.Bus; +import com.squareup.otto.Subscribe; public class MyWidgetProvider extends AppWidgetProvider { private static final String PREVIOUS = "previous"; @@ -48,8 +49,22 @@ public class MyWidgetProvider extends AppWidgetProvider { remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); widgetManager = AppWidgetManager.getInstance(context); widgetIds = widgetManager.getAppWidgetIds(component); - bus = BusProvider.getInstance(); - bus.register(this); + if (bus == null) { + bus = BusProvider.getInstance(); + bus.register(this); + } + } + + @Subscribe + public void songChangedEvent(Events.SongChanged event) { + final Song newSong = event.getSong(); + remoteViews.setTextViewText(R.id.songTitle, newSong.getTitle()); + remoteViews.setTextViewText(R.id.songArtist, newSong.getArtist()); + updateWidget(); + } + + private void updateWidget() { + widgetManager.updateAppWidget(widgetIds, remoteViews); } @Override @@ -60,16 +75,16 @@ public class MyWidgetProvider extends AppWidgetProvider { final String action = intent.getAction(); switch (action) { case PREVIOUS: - bus.post(new Events.MusicPrevious()); + bus.post(new Events.PreviousSong()); break; case PLAYPAUSE: - bus.post(new Events.MusicPlayPause()); + bus.post(new Events.PlayPauseSong()); break; case NEXT: - bus.post(new Events.MusicNext()); + bus.post(new Events.NextSong()); break; case STOP: - bus.post(new Events.MusicStop()); + bus.post(new Events.StopSong()); break; default: super.onReceive(context, intent);