do not use EventBus at the widget, use casual broadcasts

This commit is contained in:
tibbi 2020-04-19 10:06:34 +02:00
parent 5eb8364cb4
commit 28a637ec88
6 changed files with 62 additions and 73 deletions

View file

@ -47,6 +47,7 @@ import kotlinx.android.synthetic.main.item_navigation.*
import kotlinx.android.synthetic.main.item_navigation.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.File
import java.util.*
@ -638,7 +639,7 @@ class MainActivity : SimpleActivity(), SongListListener {
private fun getSongsAdapter() = songs_list.adapter as? SongAdapter
@Subscribe
@Subscribe(threadMode = ThreadMode.MAIN)
fun songChangedEvent(event: Events.SongChanged) {
if (!wasInitialPlaylistSet) {
return
@ -650,32 +651,32 @@ class MainActivity : SimpleActivity(), SongListListener {
updateAlbumCover()
}
@Subscribe
@Subscribe(threadMode = ThreadMode.MAIN)
fun songStateChanged(event: Events.SongStateChanged) {
val isPlaying = event.isPlaying
play_pause_btn.setImageDrawable(resources.getDrawable(if (isPlaying) R.drawable.ic_pause_vector else R.drawable.ic_play_vector))
getSongsAdapter()?.updateSongState(isPlaying)
}
@Subscribe
@Subscribe(threadMode = ThreadMode.MAIN)
fun playlistUpdated(event: Events.PlaylistUpdated) {
wasInitialPlaylistSet = true
fillSongsListView(event.songs.clone() as ArrayList<Song>)
}
@Subscribe
@Subscribe(threadMode = ThreadMode.MAIN)
fun progressUpdated(event: Events.ProgressUpdated) {
val progress = event.progress
song_progressbar.progress = progress
getSongsAdapter()?.updateSongProgress(progress)
}
@Subscribe
@Subscribe(threadMode = ThreadMode.MAIN)
fun noStoragePermission(event: Events.NoStoragePermission) {
toast(R.string.no_storage_permissions)
}
@Subscribe
@Subscribe(threadMode = ThreadMode.MAIN)
fun sleepTimerChanged(event: Events.SleepTimerChanged) {
sleep_timer_holder.beVisible()
sleep_timer_value.text = event.seconds.getFormattedDuration()

View file

@ -7,10 +7,7 @@ import android.util.TypedValue
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.databases.SongsDatabase
import com.simplemobiletools.musicplayer.helpers.CALL_SETUP_AFTER
import com.simplemobiletools.musicplayer.helpers.Config
import com.simplemobiletools.musicplayer.helpers.PAUSE
import com.simplemobiletools.musicplayer.helpers.REFRESH_LIST
import com.simplemobiletools.musicplayer.helpers.*
import com.simplemobiletools.musicplayer.interfaces.PlaylistsDao
import com.simplemobiletools.musicplayer.interfaces.SongsDao
import com.simplemobiletools.musicplayer.models.Playlist
@ -92,3 +89,19 @@ fun Context.deletePlaylists(playlists: ArrayList<Playlist>) {
songsDAO.removePlaylistSongs(it.id)
}
}
fun Context.broadcastUpdateWidgetSong(newSong: Song?) {
Intent(this, MyWidgetProvider::class.java).apply {
putExtra(NEW_SONG, newSong)
action = SONG_CHANGED
sendBroadcast(this)
}
}
fun Context.broadcastUpdateWidgetSongState(isPlaying: Boolean) {
Intent(this, MyWidgetProvider::class.java).apply {
putExtra(IS_PLAYING, isPlaying)
action = SONG_STATE_CHANGED
sendBroadcast(this)
}
}

View file

@ -29,6 +29,10 @@ const val SKIP_BACKWARD = PATH + "SKIP_BACKWARD"
const val SKIP_FORWARD = PATH + "SKIP_FORWARD"
const val REMOVE_CURRENT_SONG = PATH + "REMOVE_CURRENT_SONG"
const val REMOVE_SONG_IDS = PATH + "REMOVE_SONG_IDS"
const val NEW_SONG = "NEW_SONG"
const val IS_PLAYING = "IS_PLAYING"
const val SONG_CHANGED = "SONG_CHANGED"
const val SONG_STATE_CHANGED = "SONG_STATE_CHANGED"
// shared preferences
const val SHUFFLE = "shuffle"

View file

@ -15,15 +15,11 @@ import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.activities.SplashActivity
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.sendIntent
import com.simplemobiletools.musicplayer.models.Events
import com.simplemobiletools.musicplayer.models.Song
import com.simplemobiletools.musicplayer.services.MusicService
import com.simplemobiletools.musicplayer.services.MusicService.Companion.mCurrSong
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
class MyWidgetProvider : AppWidgetProvider() {
private var mBus: EventBus? = null
private var mContext: Context? = null
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
@ -41,8 +37,6 @@ class MyWidgetProvider : AppWidgetProvider() {
updatePlayPauseButton(views, MusicService.getIsPlaying())
appWidgetManager.updateAppWidget(it, views)
}
registerBus()
}
private fun getComponentName() = ComponentName(mContext!!, MyWidgetProvider::class.java)
@ -60,12 +54,12 @@ class MyWidgetProvider : AppWidgetProvider() {
views.setOnClickPendingIntent(id, pendingIntent)
}
@Subscribe
fun songChangedEvent(event: Events.SongChanged) {
private fun songChanged(intent: Intent) {
val song = intent.getSerializableExtra(NEW_SONG) as? Song ?: return
val appWidgetManager = AppWidgetManager.getInstance(mContext)
appWidgetManager.getAppWidgetIds(getComponentName()).forEach {
val views = getRemoteViews(appWidgetManager, mContext!!, it)
updateSongInfo(views, event.song)
updateSongInfo(views, song)
appWidgetManager.updateAppWidget(it, views)
}
}
@ -82,12 +76,12 @@ class MyWidgetProvider : AppWidgetProvider() {
views.setTextViewText(R.id.song_info_artist, artist)
}
@Subscribe
fun songStateChanged(event: Events.SongStateChanged) {
private fun songStateChanged(intent: Intent) {
val isPlaying = intent.getBooleanExtra(IS_PLAYING, false)
val appWidgetManager = AppWidgetManager.getInstance(mContext)
appWidgetManager.getAppWidgetIds(getComponentName()).forEach {
val views = getRemoteViews(appWidgetManager, mContext!!, it)
updatePlayPauseButton(views, event.isPlaying)
updatePlayPauseButton(views, isPlaying)
appWidgetManager.updateAppWidget(it, views)
}
}
@ -118,18 +112,14 @@ class MyWidgetProvider : AppWidgetProvider() {
override fun onReceive(context: Context, intent: Intent) {
mContext = context
val action = intent.action
registerBus()
when (action) {
SONG_CHANGED -> songChanged(intent)
SONG_STATE_CHANGED -> songStateChanged(intent)
PREVIOUS, PLAYPAUSE, NEXT -> context.sendIntent(action)
else -> super.onReceive(context, intent)
}
}
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
super.onDeleted(context, appWidgetIds)
unregisterBus()
}
private fun setupButtons(views: RemoteViews) {
setupIntent(views, PREVIOUS, R.id.previous_btn)
setupIntent(views, PLAYPAUSE, R.id.play_pause_btn)
@ -155,22 +145,4 @@ class MyWidgetProvider : AppWidgetProvider() {
val layoutId = if (minHeight < context.config.initialWidgetHeight / 2) R.layout.small_widget else R.layout.widget
return RemoteViews(context.packageName, layoutId)
}
private fun registerBus() {
try {
if (mBus == null) {
mBus = EventBus.getDefault()
}
mBus!!.register(this)
} catch (ignored: Exception) {
}
}
private fun unregisterBus() {
try {
mBus?.unregister(this)
} catch (ignored: Exception) {
}
}
}

View file

@ -3,15 +3,15 @@ package com.simplemobiletools.musicplayer.models
import java.util.*
class Events {
class SongChanged internal constructor(val song: Song?)
class SongChanged(val song: Song?)
class SongStateChanged internal constructor(val isPlaying: Boolean)
class SongStateChanged(val isPlaying: Boolean)
class PlaylistUpdated internal constructor(val songs: ArrayList<Song>)
class PlaylistUpdated(val songs: ArrayList<Song>)
class ProgressUpdated internal constructor(val progress: Int)
class ProgressUpdated(val progress: Int)
class SleepTimerChanged internal constructor(val seconds: Int)
class SleepTimerChanged(val seconds: Int)
class NoStoragePermission internal constructor()
class NoStoragePermission
}

View file

@ -33,10 +33,7 @@ import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.activities.MainActivity
import com.simplemobiletools.musicplayer.databases.SongsDatabase
import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.getPlaylistSongs
import com.simplemobiletools.musicplayer.extensions.sendIntent
import com.simplemobiletools.musicplayer.extensions.songsDAO
import com.simplemobiletools.musicplayer.extensions.*
import com.simplemobiletools.musicplayer.helpers.*
import com.simplemobiletools.musicplayer.models.Events
import com.simplemobiletools.musicplayer.models.Song
@ -249,9 +246,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
mSongs.clear()
ensureBackgroundThread {
getSortedSongs()
Handler(Looper.getMainLooper()).post {
EventBus.getDefault().post(Events.PlaylistUpdated(mSongs))
}
EventBus.getDefault().post(Events.PlaylistUpdated(mSongs))
if (intent.getBooleanExtra(CALL_SETUP_AFTER, false)) {
mPlayOnPrepare = false
@ -329,15 +324,13 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
}
private fun updateUI() {
Handler(Looper.getMainLooper()).post {
if (mPlayer != null) {
EventBus.getDefault().post(Events.PlaylistUpdated(mSongs))
mCurrSongCover = getAlbumImage(mCurrSong).first
EventBus.getDefault().post(Events.SongChanged(mCurrSong))
if (mPlayer != null) {
EventBus.getDefault().post(Events.PlaylistUpdated(mSongs))
mCurrSongCover = getAlbumImage(mCurrSong).first
broadcastSongChange(mCurrSong)
val secs = mPlayer!!.currentPosition / 1000
EventBus.getDefault().post(Events.ProgressUpdated(secs))
}
val secs = mPlayer!!.currentPosition / 1000
EventBus.getDefault().post(Events.ProgressUpdated(secs))
}
songStateChanged(getIsPlaying())
}
@ -685,9 +678,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
private fun songChanged(song: Song?) {
val albumImage = getAlbumImage(song)
mCurrSongCover = albumImage.first
Handler(Looper.getMainLooper()).post {
EventBus.getDefault().post(Events.SongChanged(song))
}
broadcastSongChange(song)
val lockScreenImage = if (albumImage.second) albumImage.first else null
val metadata = MediaMetadataCompat.Builder()
@ -697,6 +688,16 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
mMediaSession?.setMetadata(metadata)
}
private fun broadcastSongChange(song: Song?) {
broadcastUpdateWidgetSong(song)
EventBus.getDefault().post(Events.SongChanged(song))
}
private fun broadcastSongStateChange(isPlaying: Boolean) {
broadcastUpdateWidgetSongState(isPlaying)
EventBus.getDefault().post(Events.SongStateChanged(isPlaying))
}
// do not just return the album cover, but also a boolean to indicate if it a real cover, or just the placeholder
private fun getAlbumImage(song: Song?): Pair<Bitmap, Boolean> {
if (File(song?.path ?: "").exists()) {
@ -823,9 +824,7 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
handleProgressHandler(isPlaying)
setupNotification()
mMediaSession?.isActive = isPlaying
Handler(Looper.getMainLooper()).post {
EventBus.getDefault().post(Events.SongStateChanged(isPlaying))
}
broadcastSongStateChange(isPlaying)
if (isPlaying) {
val filter = IntentFilter(Intent.ACTION_HEADSET_PLUG)