update song artist/title info on the widget
This commit is contained in:
parent
21c2efed39
commit
5b8ddee28e
3 changed files with 47 additions and 16 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue