From 6fde42ba11a6b253a3e325f41eaa919119d44c58 Mon Sep 17 00:00:00 2001 From: h4h13 Date: Sun, 4 Aug 2019 21:26:32 +0530 Subject: [PATCH] Added favorite icon to notifications --- .../base/AbsMusicServiceActivity.kt | 2 ++ .../retromusic/auto/AutoMusicProvider.java | 31 ++++++++++++------- .../material/MaterialControlsFragment.kt | 4 +-- .../retromusic/service/MusicService.java | 12 +++++-- .../notification/PlayingNotificationImpl24.kt | 14 +++++---- .../monkey/retromusic/util/MusicUtil.java | 2 ++ .../main/res/drawable/ic_pause_white_24dp.xml | 14 +++++++++ .../main/res/drawable/ic_pause_white_48dp.xml | 24 ++++++++++++++ ..._white_big.xml => ic_pause_white_64dp.xml} | 0 .../res/drawable/ic_play_arrow_white_48dp.xml | 24 ++++++++++++++ .../drawable/ic_repeat_white_circle_24dp.xml | 26 ++++++++++++++++ .../res/drawable/ic_shuffle_off_circled.xml | 23 ++++++++++++++ .../res/drawable/ic_shuffle_on_circled.xml | 28 +++++++++++++++++ .../ic_skip_next_round_white_32dp.xml | 24 ++++++++++++++ .../ic_skip_previous_round_white_32dp.xml | 10 ++++++ .../fragment_material_playback_controls.xml | 2 +- 16 files changed, 216 insertions(+), 24 deletions(-) create mode 100644 app/src/main/res/drawable/ic_pause_white_48dp.xml rename app/src/main/res/drawable/{ic_pause_white_big.xml => ic_pause_white_64dp.xml} (100%) create mode 100644 app/src/main/res/drawable/ic_play_arrow_white_48dp.xml create mode 100644 app/src/main/res/drawable/ic_repeat_white_circle_24dp.xml create mode 100644 app/src/main/res/drawable/ic_shuffle_off_circled.xml create mode 100644 app/src/main/res/drawable/ic_shuffle_on_circled.xml create mode 100644 app/src/main/res/drawable/ic_skip_next_round_white_32dp.xml create mode 100644 app/src/main/res/drawable/ic_skip_previous_round_white_32dp.xml diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsMusicServiceActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsMusicServiceActivity.kt index 305f6da7..57ee45a7 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsMusicServiceActivity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsMusicServiceActivity.kt @@ -67,6 +67,7 @@ abstract class AbsMusicServiceActivity : AbsBaseActivity(), MusicServiceEventLis filter.addAction(META_CHANGED) filter.addAction(QUEUE_CHANGED) filter.addAction(MEDIA_STORE_CHANGED) + filter.addAction(FAVORITE_STATE_CHANGED) registerReceiver(musicStateReceiver, filter) @@ -146,6 +147,7 @@ abstract class AbsMusicServiceActivity : AbsBaseActivity(), MusicServiceEventLis val activity = reference.get() if (activity != null && action != null) { when (action) { + FAVORITE_STATE_CHANGED, META_CHANGED -> activity.onPlayingMetaChanged() QUEUE_CHANGED -> activity.onQueueChanged() PLAY_STATE_CHANGED -> activity.onPlayStateChanged() diff --git a/app/src/main/java/code/name/monkey/retromusic/auto/AutoMusicProvider.java b/app/src/main/java/code/name/monkey/retromusic/auto/AutoMusicProvider.java index ea13395c..f02c7714 100644 --- a/app/src/main/java/code/name/monkey/retromusic/auto/AutoMusicProvider.java +++ b/app/src/main/java/code/name/monkey/retromusic/auto/AutoMusicProvider.java @@ -22,6 +22,7 @@ import android.os.AsyncTask; import android.support.v4.media.MediaBrowserCompat; import android.support.v4.media.MediaDescriptionCompat; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import java.lang.ref.WeakReference; @@ -314,7 +315,9 @@ public class AutoMusicProvider { } } - public List getChildren(String mediaId, Resources resources) { + @Nullable + public List getChildren(@NonNull String mediaId, + @NonNull Resources resources) { List mediaItems = new ArrayList<>(); if (!AutoMediaIDHelper.isBrowseable(mediaId)) { @@ -325,28 +328,33 @@ public class AutoMusicProvider { case AutoMediaIDHelper.MEDIA_ID_ROOT: //mediaItems.add(createBrowsableMediaItem(AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_HISTORY, resources.getString(R.string.history_label), R.drawable.ic_access_time_white_24dp)); //mediaItems.add(createBrowsableMediaItem(AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_TOP_TRACKS, resources.getString(R.string.top_tracks_label), R.drawable.ic_trending_up_white_24dp)); - //mediaItems.add(createBrowsableMediaItem(AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_PLAYLIST, resources.getString(R.string.playlists_label), R.drawable.ic_queue_music_white_24dp)); + mediaItems.add( createBrowsableMediaItem( AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_ALBUM, - resources.getString(R.string.albums_label), - R.drawable.ic_album_white_24dp)); + resources.getString(R.string.albums_label) + )); mediaItems.add( createBrowsableMediaItem( AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_ARTIST, - resources.getString(R.string.artists_label), - R.drawable.ic_artist_white_24dp)); + resources.getString(R.string.artists_label) + )); + mediaItems.add( + createBrowsableMediaItem( + AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_PLAYLIST, + resources.getString(R.string.playlists_label) + )); mediaItems.add( createBrowsableMediaItem( AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_GENRE, - resources.getString(R.string.genres_label), - R.drawable.ic_guitar_acoustic_white_24dp)); - mediaItems.add( + resources.getString(R.string.genres_label) + )); + /* mediaItems.add( createPlayableMediaItem( AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_SHUFFLE, resources.getString(R.string.action_shuffle_all), R.drawable.ic_shuffle_white_24dp, - "")); + ""));*/ //mediaItems.add(createBrowsableMediaItem(AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_QUEUE, resources.getString(R.string.queue_label), R.drawable.ic_playlist_play_white_24dp)); break; @@ -386,7 +394,6 @@ public class AutoMusicProvider { break; case AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_QUEUE: - // TODO: auto scroll to current track, indicate that it's playing for (final Uri uri : getQueue()) { mediaItems.add(createPlayableMediaItem(mediaId, uri, uri.getPathSegments().get(PATH_SEGMENT_TITLE), uri.getPathSegments().get(PATH_SEGMENT_ARTIST))); } @@ -396,7 +403,7 @@ public class AutoMusicProvider { return mediaItems; } - private MediaBrowserCompat.MediaItem createBrowsableMediaItem(String mediaId, String title, int iconDrawableId) { + private MediaBrowserCompat.MediaItem createBrowsableMediaItem(String mediaId, String title) { MediaDescriptionCompat.Builder builder = new MediaDescriptionCompat.Builder(); builder.setMediaId(mediaId) .setTitle(title); diff --git a/app/src/main/java/code/name/monkey/retromusic/fragments/player/material/MaterialControlsFragment.kt b/app/src/main/java/code/name/monkey/retromusic/fragments/player/material/MaterialControlsFragment.kt index 81eb4d45..498fa3fd 100644 --- a/app/src/main/java/code/name/monkey/retromusic/fragments/player/material/MaterialControlsFragment.kt +++ b/app/src/main/java/code/name/monkey/retromusic/fragments/player/material/MaterialControlsFragment.kt @@ -23,7 +23,6 @@ import code.name.monkey.retromusic.service.MusicService import code.name.monkey.retromusic.util.MusicUtil import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.ViewUtil -import kotlinx.android.synthetic.main.fragment_material_playback_controls.* import kotlinx.android.synthetic.main.fragment_material_playback_controls.nextButton import kotlinx.android.synthetic.main.fragment_material_playback_controls.playPauseButton import kotlinx.android.synthetic.main.fragment_material_playback_controls.previousButton @@ -34,7 +33,6 @@ import kotlinx.android.synthetic.main.fragment_material_playback_controls.songCu import kotlinx.android.synthetic.main.fragment_material_playback_controls.songTotalTime import kotlinx.android.synthetic.main.fragment_material_playback_controls.text import kotlinx.android.synthetic.main.fragment_material_playback_controls.title -import kotlinx.android.synthetic.main.fragment_player_playback_controls.* /** * @author Hemanth S (h4h13). @@ -140,7 +138,7 @@ class MaterialControlsFragment : AbsPlayerControlsFragment() { private fun updatePlayPauseDrawableState() { if (MusicPlayerRemote.isPlaying) { - playPauseButton.setImageResource(R.drawable.ic_pause_white_big); + playPauseButton.setImageResource(R.drawable.ic_pause_white_64dp); } else { playPauseButton.setImageResource(R.drawable.ic_play_arrow_white_64dp); } diff --git a/app/src/main/java/code/name/monkey/retromusic/service/MusicService.java b/app/src/main/java/code/name/monkey/retromusic/service/MusicService.java index b5913c34..9547121c 100644 --- a/app/src/main/java/code/name/monkey/retromusic/service/MusicService.java +++ b/app/src/main/java/code/name/monkey/retromusic/service/MusicService.java @@ -211,6 +211,12 @@ public class MusicService extends MediaBrowserServiceCompat implements SharedPre } }; private PlayingNotification playingNotification; + private final BroadcastReceiver updateFavoriteReceiver = new BroadcastReceiver() { + @Override + public void onReceive(final Context context, final Intent intent) { + updateNotification(); + } + }; private AudioManager audioManager; private MediaSessionCompat mediaSession; private PowerManager.WakeLock wakeLock; @@ -324,6 +330,7 @@ public class MusicService extends MediaBrowserServiceCompat implements SharedPre uiThreadHandler = new Handler(); registerReceiver(widgetIntentReceiver, new IntentFilter(APP_WIDGET_UPDATE)); + registerReceiver(updateFavoriteReceiver, new IntentFilter(FAVORITE_STATE_CHANGED)); initNotification(); @@ -698,13 +705,13 @@ public class MusicService extends MediaBrowserServiceCompat implements SharedPre if (getRepeatMode() == REPEAT_MODE_THIS) { repeatIcon = R.drawable.ic_repeat_one_white_24dp; } else if (getRepeatMode() == REPEAT_MODE_ALL) { - repeatIcon = R.drawable.ic_repeat_white_24dp; + repeatIcon = R.drawable.ic_repeat_white_circle_24dp; } stateBuilder.addCustomAction(new PlaybackStateCompat.CustomAction.Builder( CYCLE_REPEAT, getString(R.string.action_cycle_repeat), repeatIcon) .build()); - final int shuffleIcon = getShuffleMode() == SHUFFLE_MODE_NONE ? R.drawable.ic_shuffle_white_24dp : R.drawable.ic_shuffle_white_24dp; + final int shuffleIcon = getShuffleMode() == SHUFFLE_MODE_NONE ? R.drawable.ic_shuffle_off_circled : R.drawable.ic_shuffle_on_circled; stateBuilder.addCustomAction(new PlaybackStateCompat.CustomAction.Builder( TOGGLE_SHUFFLE, getString(R.string.action_toggle_shuffle), shuffleIcon) .build()); @@ -1189,6 +1196,7 @@ public class MusicService extends MediaBrowserServiceCompat implements SharedPre } songPlayCountHelper.notifyPlayStateChanged(isPlaying); break; + case FAVORITE_STATE_CHANGED: case META_CHANGED: updateNotification(); updateMediaSessionMetaData(); diff --git a/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotificationImpl24.kt b/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotificationImpl24.kt index e97f482b..d6c4d8d2 100644 --- a/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotificationImpl24.kt +++ b/app/src/main/java/code/name/monkey/retromusic/service/notification/PlayingNotificationImpl24.kt @@ -24,6 +24,7 @@ import android.graphics.drawable.Drawable import android.os.Build import android.text.Html import androidx.core.app.NotificationCompat +import androidx.media.app.NotificationCompat.* import code.name.monkey.retromusic.R import code.name.monkey.retromusic.activities.MainActivity import code.name.monkey.retromusic.glide.GlideApp @@ -48,9 +49,9 @@ class PlayingNotificationImpl24 : PlayingNotification() { val isPlaying = service.isPlaying val isFavorite = MusicUtil.isFavorite(service, song) val playButtonResId = if (isPlaying) - R.drawable.ic_pause_white_24dp + R.drawable.ic_pause_white_48dp else - R.drawable.ic_play_arrow_white_32dp + R.drawable.ic_play_arrow_white_48dp val favoriteResId = if (isFavorite) R.drawable.ic_favorite_white_24dp @@ -114,12 +115,12 @@ class PlayingNotificationImpl24 : PlayingNotification() { retrievePlaybackAction(ACTION_QUIT)) val previousAction = NotificationCompat.Action( - R.drawable.ic_skip_previous_white_24dp, + R.drawable.ic_skip_previous_round_white_32dp, service.getString(R.string.action_previous), retrievePlaybackAction(ACTION_REWIND)) val nextAction = NotificationCompat.Action( - R.drawable.ic_skip_next_white_24dp, + R.drawable.ic_skip_next_round_white_32dp, service.getString(R.string.action_next), retrievePlaybackAction(ACTION_SKIP)) @@ -134,15 +135,16 @@ class PlayingNotificationImpl24 : PlayingNotification() { .setSubText(Html.fromHtml("" + song.albumName + "")) .setOngoing(isPlaying) .setShowWhen(false) + .addAction(toggleFavorite) .addAction(previousAction) .addAction(playPauseAction) .addAction(nextAction) .addAction(closeAction) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - builder.setStyle(androidx.media.app.NotificationCompat.MediaStyle() + builder.setStyle(MediaStyle() .setMediaSession(service.mediaSession.sessionToken) - .setShowActionsInCompactView(0, 1, 2, 3, 4)) + .setShowActionsInCompactView( 1, 2, 3)) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O && PreferenceUtil.getInstance().coloredNotification()) { builder.color = color diff --git a/app/src/main/java/code/name/monkey/retromusic/util/MusicUtil.java b/app/src/main/java/code/name/monkey/retromusic/util/MusicUtil.java index 9c2d4a4f..5c9337d3 100644 --- a/app/src/main/java/code/name/monkey/retromusic/util/MusicUtil.java +++ b/app/src/main/java/code/name/monkey/retromusic/util/MusicUtil.java @@ -51,6 +51,7 @@ import code.name.monkey.retromusic.model.Genre; import code.name.monkey.retromusic.model.Playlist; import code.name.monkey.retromusic.model.Song; import code.name.monkey.retromusic.model.lyrics.AbsSynchronizedLyrics; +import code.name.monkey.retromusic.service.MusicService; public class MusicUtil { @@ -408,6 +409,7 @@ public class MusicUtil { PlaylistsUtil.addToPlaylist(context, song, getOrCreateFavoritesPlaylist(context).id, false); } + context.sendBroadcast(new Intent(MusicService.FAVORITE_STATE_CHANGED)); } public static boolean isFavoritePlaylist(@NonNull final Context context, diff --git a/app/src/main/res/drawable/ic_pause_white_24dp.xml b/app/src/main/res/drawable/ic_pause_white_24dp.xml index 34aa67ae..c5d23ca3 100644 --- a/app/src/main/res/drawable/ic_pause_white_24dp.xml +++ b/app/src/main/res/drawable/ic_pause_white_24dp.xml @@ -1,4 +1,18 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_pause_white_big.xml b/app/src/main/res/drawable/ic_pause_white_64dp.xml similarity index 100% rename from app/src/main/res/drawable/ic_pause_white_big.xml rename to app/src/main/res/drawable/ic_pause_white_64dp.xml diff --git a/app/src/main/res/drawable/ic_play_arrow_white_48dp.xml b/app/src/main/res/drawable/ic_play_arrow_white_48dp.xml new file mode 100644 index 00000000..14b0a078 --- /dev/null +++ b/app/src/main/res/drawable/ic_play_arrow_white_48dp.xml @@ -0,0 +1,24 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_repeat_white_circle_24dp.xml b/app/src/main/res/drawable/ic_repeat_white_circle_24dp.xml new file mode 100644 index 00000000..aaac7a36 --- /dev/null +++ b/app/src/main/res/drawable/ic_repeat_white_circle_24dp.xml @@ -0,0 +1,26 @@ + + + + + + diff --git a/app/src/main/res/drawable/ic_shuffle_off_circled.xml b/app/src/main/res/drawable/ic_shuffle_off_circled.xml new file mode 100644 index 00000000..43bd4ca3 --- /dev/null +++ b/app/src/main/res/drawable/ic_shuffle_off_circled.xml @@ -0,0 +1,23 @@ + + + + + diff --git a/app/src/main/res/drawable/ic_shuffle_on_circled.xml b/app/src/main/res/drawable/ic_shuffle_on_circled.xml new file mode 100644 index 00000000..9e33f075 --- /dev/null +++ b/app/src/main/res/drawable/ic_shuffle_on_circled.xml @@ -0,0 +1,28 @@ + + + + + + diff --git a/app/src/main/res/drawable/ic_skip_next_round_white_32dp.xml b/app/src/main/res/drawable/ic_skip_next_round_white_32dp.xml new file mode 100644 index 00000000..94f5a32e --- /dev/null +++ b/app/src/main/res/drawable/ic_skip_next_round_white_32dp.xml @@ -0,0 +1,24 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_skip_previous_round_white_32dp.xml b/app/src/main/res/drawable/ic_skip_previous_round_white_32dp.xml new file mode 100644 index 00000000..76861cee --- /dev/null +++ b/app/src/main/res/drawable/ic_skip_previous_round_white_32dp.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_material_playback_controls.xml b/app/src/main/res/layout/fragment_material_playback_controls.xml index 1c56895c..5aa6a99a 100644 --- a/app/src/main/res/layout/fragment_material_playback_controls.xml +++ b/app/src/main/res/layout/fragment_material_playback_controls.xml @@ -187,7 +187,7 @@ android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="?attr/roundSelector" - app:srcCompat="@drawable/ic_pause_white_big" + app:srcCompat="@drawable/ic_pause_white_64dp" tools:tint="@color/md_black_1000" />