diff options
Diffstat (limited to 'app/src/main/java/github/daneren2005/dsub/util/compat/RemoteControlClientLP.java')
-rw-r--r-- | app/src/main/java/github/daneren2005/dsub/util/compat/RemoteControlClientLP.java | 116 |
1 files changed, 88 insertions, 28 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/util/compat/RemoteControlClientLP.java b/app/src/main/java/github/daneren2005/dsub/util/compat/RemoteControlClientLP.java index e2cbb307..b6a38199 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/compat/RemoteControlClientLP.java +++ b/app/src/main/java/github/daneren2005/dsub/util/compat/RemoteControlClientLP.java @@ -25,6 +25,7 @@ import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.media.AudioAttributes; +import android.media.MediaDescription; import android.media.MediaMetadata; import android.media.Rating; import android.media.RemoteControlClient; @@ -33,7 +34,9 @@ import android.media.session.PlaybackState; import android.os.Build; import android.os.Bundle; import android.support.v7.media.MediaRouter; -import android.util.Log; + +import java.util.ArrayList; +import java.util.List; import github.daneren2005.dsub.R; import github.daneren2005.dsub.activity.SubsonicActivity; @@ -43,7 +46,6 @@ import github.daneren2005.dsub.service.DownloadFile; import github.daneren2005.dsub.service.DownloadService; import github.daneren2005.dsub.util.Constants; import github.daneren2005.dsub.util.ImageLoader; -import github.daneren2005.dsub.util.UpdateHelper; @TargetApi(Build.VERSION_CODES.LOLLIPOP) public class RemoteControlClientLP extends RemoteControlClientBase { @@ -51,10 +53,19 @@ public class RemoteControlClientLP extends RemoteControlClientBase { private static final String CUSTOM_ACTION_THUMBS_UP = "github.daneren2005.dsub.THUMBS_UP"; private static final String CUSTOM_ACTION_THUMBS_DOWN = "github.daneren2005.dsub.THUMBS_DOWN"; private static final String CUSTOM_ACTION_STAR = "github.daneren2005.dsub.STAR"; + // Copied from MediaControlConstants so I did not have to include the entire Wear SDK just for these constant + private static final String SHOW_ON_WEAR = "android.support.wearable.media.extra.CUSTOM_ACTION_SHOW_ON_WEAR"; + private static final String WEAR_RESERVE_SKIP_TO_NEXT = "android.support.wearable.media.extra.RESERVE_SLOT_SKIP_TO_NEXT"; + private static final String WEAR_RESERVE_SKIP_TO_PREVIOUS = "android.support.wearable.media.extra.RESERVE_SLOT_SKIP_TO_PREVIOUS"; + // These constants don't seem to exist anywhere in the SDK. Grabbed from Google's sample media player app + private static final String AUTO_RESERVE_SKIP_TO_NEXT = "com.google.android.gms.car.media.ALWAYS_RESERVE_SPACE_FOR.ACTION_SKIP_TO_NEXT"; + private static final String AUTO_RESERVE_SKIP_TO_PREVIOUS = "com.google.android.gms.car.media.ALWAYS_RESERVE_SPACE_FOR.ACTION_SKIP_TO_PREVIOUS"; protected MediaSession mediaSession; protected DownloadService downloadService; protected ImageLoader imageLoader; + protected List<DownloadFile> currentQueue; + protected int previousState; @Override public void register(Context context, ComponentName mediaButtonReceiverComponent) { @@ -75,16 +86,19 @@ public class RemoteControlClientLP extends RemoteControlClientBase { mediaSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS); mediaSession.setCallback(new EventCallback()); - if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { - mediaSession.setRatingType(Rating.RATING_THUMB_UP_DOWN); - } - AudioAttributes.Builder audioAttributesBuilder = new AudioAttributes.Builder(); audioAttributesBuilder.setUsage(AudioAttributes.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC); mediaSession.setPlaybackToLocal(audioAttributesBuilder.build()); mediaSession.setActive(true); + Bundle sessionExtras = new Bundle(); + sessionExtras.putBoolean(WEAR_RESERVE_SKIP_TO_PREVIOUS, true); + sessionExtras.putBoolean(WEAR_RESERVE_SKIP_TO_NEXT, true); + sessionExtras.putBoolean(AUTO_RESERVE_SKIP_TO_PREVIOUS, true); + sessionExtras.putBoolean(AUTO_RESERVE_SKIP_TO_NEXT, true); + mediaSession.setExtras(sessionExtras); + imageLoader = SubsonicActivity.getStaticImageLoader(context); } @@ -122,11 +136,14 @@ public class RemoteControlClientLP extends RemoteControlClientBase { DownloadFile downloadFile = downloadService.getCurrentPlaying(); if(downloadFile != null) { - addCustomActions(downloadFile.getSong(), builder); + MusicDirectory.Entry entry = downloadFile.getSong(); + addCustomActions(entry, builder); + builder.setActiveQueueItemId(entry.getId().hashCode()); } PlaybackState playbackState = builder.build(); mediaSession.setPlaybackState(playbackState); + previousState = state; } @Override @@ -138,6 +155,11 @@ public class RemoteControlClientLP extends RemoteControlClientBase { } } + @Override + public void metadataChanged(MusicDirectory.Entry currentSong) { + setPlaybackState(previousState); + } + public void setMetadata(MusicDirectory.Entry currentSong, Bitmap bitmap) { MediaMetadata.Builder builder = new MediaMetadata.Builder(); builder.putString(MediaMetadata.METADATA_KEY_ARTIST, (currentSong == null) ? null : currentSong.getArtist()) @@ -150,17 +172,6 @@ public class RemoteControlClientLP extends RemoteControlClientBase { .putLong(MediaMetadata.METADATA_KEY_DURATION, (currentSong == null) ? 0 : ((currentSong.getDuration() == null) ? 0 : (currentSong.getDuration() * 1000))); - int userRating = currentSong.getRating(); - Rating rating; - if(userRating == 1) { - rating = Rating.newThumbRating(false); - } else if(userRating == 5) { - rating = Rating.newThumbRating(true); - } else { - rating = Rating.newUnratedRating(Rating.RATING_THUMB_UP_DOWN); - } - builder.putRating(MediaMetadata.METADATA_KEY_USER_RATING, rating); - if(bitmap != null) { builder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, bitmap); } @@ -183,31 +194,68 @@ public class RemoteControlClientLP extends RemoteControlClientBase { router.setMediaSession(null); } + @Override + public void updatePlaylist(List<DownloadFile> playlist) { + List<MediaSession.QueueItem> queue = new ArrayList<>(); + + for(DownloadFile file: playlist) { + MusicDirectory.Entry entry = file.getSong(); + + MediaDescription description = new MediaDescription.Builder() + .setMediaId(entry.getId()) + .setTitle(entry.getTitle()) + .setSubtitle(entry.getAlbumDisplay()) + .build(); + MediaSession.QueueItem item = new MediaSession.QueueItem(description, entry.getId().hashCode()); + queue.add(item); + } + + mediaSession.setQueue(queue); + currentQueue = playlist; + } + public MediaSession getMediaSession() { return mediaSession; } protected long getPlaybackActions() { - return PlaybackState.ACTION_PLAY | + long actions = PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_SEEK_TO | - PlaybackState.ACTION_SKIP_TO_NEXT | - PlaybackState.ACTION_SKIP_TO_PREVIOUS; + PlaybackState.ACTION_SKIP_TO_QUEUE_ITEM; + + int currentIndex = downloadService.getCurrentPlayingIndex(); + int size = downloadService.size(); + if(currentIndex > 0) { + actions |= PlaybackState.ACTION_SKIP_TO_PREVIOUS; + } + if(currentIndex < size - 1) { + actions |= PlaybackState.ACTION_SKIP_TO_NEXT; + } + + return actions; } protected void addCustomActions(MusicDirectory.Entry currentSong, PlaybackState.Builder builder) { + Bundle showOnWearExtras = new Bundle(); + showOnWearExtras.putBoolean(SHOW_ON_WEAR, true); + + int rating = currentSong.getRating(); PlaybackState.CustomAction thumbsUp = new PlaybackState.CustomAction.Builder(CUSTOM_ACTION_THUMBS_UP, - downloadService.getString(R.string.download_thumbs_up), - R.drawable.ic_action_rating_good_selected).build(); + downloadService.getString(R.string.download_thumbs_up), + rating == 5 ? R.drawable.ic_action_rating_good_selected : R.drawable.ic_action_rating_good) + .setExtras(showOnWearExtras).build(); PlaybackState.CustomAction thumbsDown = new PlaybackState.CustomAction.Builder(CUSTOM_ACTION_THUMBS_DOWN, - downloadService.getString(R.string.download_thumbs_down), - R.drawable.ic_action_rating_bad_selected).build(); + downloadService.getString(R.string.download_thumbs_down), + rating == 1 ? R.drawable.ic_action_rating_bad_selected : R.drawable.ic_action_rating_bad) + .setExtras(showOnWearExtras).build(); PlaybackState.CustomAction star = new PlaybackState.CustomAction.Builder(CUSTOM_ACTION_STAR, - downloadService.getString(R.string.common_star), - R.drawable.ic_toggle_star).build(); + downloadService.getString(R.string.common_star), + currentSong.isStarred() ? R.drawable.ic_toggle_star : R.drawable.ic_toggle_star_outline) + .setExtras(showOnWearExtras).build(); - builder.addCustomAction(star).addCustomAction(thumbsDown).addCustomAction(thumbsUp); + builder.addCustomAction(thumbsDown).addCustomAction(star).addCustomAction(thumbsUp); } private class EventCallback extends MediaSession.Callback { @@ -241,6 +289,18 @@ public class RemoteControlClientLP extends RemoteControlClientBase { } @Override + public void onSkipToQueueItem(long queueId) { + if(currentQueue != null) { + for(DownloadFile file: currentQueue) { + if(file.getSong().getId().hashCode() == queueId) { + downloadService.play(file); + return; + } + } + } + } + + @Override public void onCustomAction(String action, Bundle extras) { if(CUSTOM_ACTION_THUMBS_UP.equals(action)) { downloadService.toggleRating(5); |