Skip to content

Commit e7d7146

Browse files
fix(widget): complete playback service updates and clean extraneous strings
1 parent edc6c04 commit e7d7146

3 files changed

Lines changed: 56 additions & 20 deletions

File tree

app/src/main/java/org/akanework/gramophone/logic/GramophonePlaybackService.kt

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ import org.akanework.gramophone.logic.utils.exoplayer.GramophoneExtractorsFactor
137137
import org.akanework.gramophone.logic.utils.exoplayer.GramophoneMediaSourceFactory
138138
import org.akanework.gramophone.logic.utils.exoplayer.GramophoneRenderFactory
139139
import org.akanework.gramophone.ui.AudioPreviewActivity
140+
import org.akanework.gramophone.ui.CardWidgetProvider
140141
import org.akanework.gramophone.ui.LyricWidgetProvider
141142
import org.akanework.gramophone.ui.MainActivity
142143
import org.akanework.gramophone.ui.fragments.compose.MqState.Companion.CLIENT_QB_REFRESH_ALL
@@ -804,6 +805,55 @@ class GramophonePlaybackService : MediaLibraryService(), MediaSessionService.Lis
804805
return onSetRating(session, controller, mediaItemId, rating)
805806
}
806807

808+
fun toggleCurrentItemFavorite() {
809+
val item = endedWorkaroundPlayer?.currentMediaItem ?: return
810+
val currentIsHeart = (item.mediaMetadata.userRating as? HeartRating)?.isHeart == true
811+
val newIsHeart = !currentIsHeart
812+
lifecycleScope.launch(Dispatchers.Default) {
813+
val song = Entry.ofMediaItem(item) ?: return@launch
814+
val uriIn = gramophoneApplication.reader.playlistListFlow.map { it.find { p ->
815+
p is Favorite } }.first()?.id?.let {
816+
ContentUris.withAppendedId(@Suppress("deprecation")
817+
MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, it)
818+
}
819+
val token = if (uriIn != null) {
820+
MediaStoreCompat.needRequestBytesWrite(this@GramophonePlaybackService, uriIn)
821+
} else {
822+
MediaStoreCompat.needRequestCreate(this@GramophonePlaybackService,
823+
ItemManipulator.getDefaultPlaylistFile(ItemManipulator.FAVORITES).path)
824+
}
825+
if (token == null) {
826+
try {
827+
val uri = uriIn ?: ItemManipulator.createPlaylist(
828+
this@GramophonePlaybackService, ItemManipulator
829+
.getDefaultPlaylistFile(ItemManipulator.FAVORITES))
830+
val readback = if (uriIn != null) ItemManipulator.readbackPlaylist(
831+
this@GramophonePlaybackService, uri) else
832+
PlaylistSerializer.Playlist.create()
833+
val newSongs = if (newIsHeart) {
834+
readback.entries + song
835+
} else {
836+
readback.entries.filter { !song.fuzzyEquals(it) }
837+
}
838+
ItemManipulator.setPlaylistContent(this@GramophonePlaybackService, uri,
839+
readback.copy(entries = newSongs), uriIn == null)
840+
} catch (e: Exception) {
841+
Log.e(TAG, "failed to toggle favorite on ${item.mediaId}", e)
842+
}
843+
}
844+
withContext(Dispatchers.Main) {
845+
val updatedItem = item.buildUpon().setMediaMetadata(
846+
item.mediaMetadata.buildUpon()
847+
.setUserRating(HeartRating(newIsHeart))
848+
.build()
849+
).build()
850+
val currentIndex = endedWorkaroundPlayer?.currentMediaItemIndex ?: 0
851+
endedWorkaroundPlayer?.replaceMediaItem(currentIndex, updatedItem)
852+
CardWidgetProvider.update(this@GramophonePlaybackService)
853+
}
854+
}
855+
}
856+
807857
// When destroying, we should release server side player
808858
// alongside with the mediaSession.
809859
override fun onDestroy() {
@@ -828,6 +878,7 @@ class GramophonePlaybackService : MediaLibraryService(), MediaSessionService.Lis
828878
mediaSession = null
829879
broadcastAudioSessionClose()
830880
LyricWidgetProvider.update(this)
881+
CardWidgetProvider.update(this)
831882
internalPlaybackThread.quitSafely()
832883
super.onDestroy()
833884
Log.i(TAG, "-onDestroy()")
@@ -1785,10 +1836,13 @@ class GramophonePlaybackService : MediaLibraryService(), MediaSessionService.Lis
17851836
}
17861837

17871838
private fun sendLyricNow(new: Boolean) {
1788-
if (new)
1839+
if (new) {
17891840
LyricWidgetProvider.update(this)
1790-
else
1841+
CardWidgetProvider.update(this)
1842+
} else {
17911843
LyricWidgetProvider.adapterUpdate(this)
1844+
CardWidgetProvider.update(this)
1845+
}
17921846
val isStatusBarLyricsEnabled = prefs.getBooleanStrict("status_bar_lyrics", false)
17931847
val highlightedLyric = if (isStatusBarLyricsEnabled && controller?.playWhenReady == true)
17941848
getCurrentLyricIndex(false)?.let {

app/src/main/res/values-zh-rCN/strings.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,11 @@
155155
<string name="tab_order_summary">应用将在启动时显示第一个标签页。分隔线后的标签页将被隐藏。</string>
156156
<string name="settings_status_bar_lyrics_title">状态栏歌词</string>
157157
<string name="settings_status_bar_lyrics_summary">启用魅族状态栏歌词(仅适用于部分设备)</string>
158-
<string name="settings_notification_lyrics_title">通知栏歌词</string>
159-
<string name="settings_notification_lyrics_summary">在媒体通知中显示当前播放歌词</string>
160158
<string name="settings_lyrics_ui">启用新歌词界面</string>
161159
<string name="lyric_widget_description">显示当前播放媒体歌词的小部件</string>
162160
<string name="card_widget_title">卡片小组件</string>
163161
<string name="card_widget_description">提供音乐播放控制的桌面小组件</string>
164162
<string name="pause">暂停</string>
165-
<string name="settings_rotate_cookie_button">旋转播放按钮</string>
166-
<string name="settings_rotate_cookie_button_summary">播放时让饼干样式的播放/暂停按钮细微地转动</string>
167-
<string name="settings_swipe_to_switch_track">手势切歌</string>
168-
<string name="settings_swipe_to_switch_track_summary">在播放器封面上左右滑动以切换上一曲/下一曲</string>
169-
<string name="settings_always_skip_previous">总是跳至上一曲</string>
170-
<string name="settings_always_skip_previous_summary">点按上一曲按钮时直接跳至上一首曲目,而非跳回开头</string>
171163
<string name="scroll_to_albums">滚动到专辑</string>
172164
<string name="scroll_to_songs">滚动到歌曲</string>
173165
<string name="expand_less">关闭</string>

app/src/main/res/values/strings.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,21 +392,11 @@
392392
<string name="settings_status_bar_lyrics_title">Status bar lyrics</string>
393393
<!-- Settings toggle to enable showing lyrics in status bar (requires meizu phone). Subtext -->
394394
<string name="settings_status_bar_lyrics_summary">Enable MeiZu method of displaying lyrics in status bar (only available on some devices)</string>
395-
<!-- Settings toggle to enable showing lyrics in media notification. Title -->
396-
<string name="settings_notification_lyrics_title">Notification lyrics</string>
397-
<!-- Settings toggle to enable showing lyrics in media notification. Subtext -->
398-
<string name="settings_notification_lyrics_summary">Display lyrics as the notification title and artist - title as subtitle</string>
399395
<!-- Description shown about lyric widget in Android launcher / home screen's widget selector on Android 12+ -->
400396
<string name="lyric_widget_description">Widget that shows lyrics of currently playing media</string>
401397
<string name="card_widget_title">Card widget</string>
402398
<string name="card_widget_description">Widget that provides music playback controls</string>
403399
<string name="pause">Pause</string>
404-
<string name="settings_rotate_cookie_button">Rotate play button</string>
405-
<string name="settings_rotate_cookie_button_summary">Subtly rotate the cookie-style play/pause button while playing</string>
406-
<string name="settings_swipe_to_switch_track">Swipe to switch track</string>
407-
<string name="settings_swipe_to_switch_track_summary">Swipe left or right on the album cover to switch tracks</string>
408-
<string name="settings_always_skip_previous">Always skip to previous track</string>
409-
<string name="settings_always_skip_previous_summary">Directly switch to previous track when pressing previous button instead of seeking to start</string>
410400
<!-- Context menu item to delete song/album/playlist -->
411401
<string name="delete">Delete</string>
412402
<!-- Dialog text to confirm user wants to delete a specific song or playlist with user defined name -->

0 commit comments

Comments
 (0)