fix crash when removing notification

This commit is contained in:
Bnyro 2022-11-19 15:18:05 +01:00
parent 47fccb760a
commit 499c629923
4 changed files with 12 additions and 16 deletions

View File

@ -9,7 +9,6 @@ import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import android.widget.Toast
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.R
@ -21,7 +20,6 @@ import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.extensions.awaitQuery
@ -117,7 +115,7 @@ class BackgroundMode : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
try {
// clear the playing queue
PlayingQueue.clear()
PlayingQueue.resetToDefaults()
// get the intent arguments
videoId = intent?.getStringExtra(IntentData.videoId)!!
@ -201,9 +199,8 @@ class BackgroundMode : Service() {
} else if (PlayerHelper.watchPositionsEnabled) {
try {
val watchPosition = awaitQuery {
DatabaseHolder.Database.watchPositionDao().findById(videoId)
Database.watchPositionDao().findById(videoId)
}
Log.e("position", watchPosition.toString())
streams?.duration?.let {
if (watchPosition != null && watchPosition.position < it * 1000 * 0.9) {
player?.seekTo(watchPosition.position)
@ -355,7 +352,7 @@ class BackgroundMode : Service() {
*/
override fun onDestroy() {
// clear the playing queue
PlayingQueue.clear()
PlayingQueue.resetToDefaults()
if (this::nowPlayingNotification.isInitialized) nowPlayingNotification.destroySelfAndPlayer()

View File

@ -193,7 +193,7 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
context?.hideKeyboard(view)
// clear the playing queue
PlayingQueue.clear()
PlayingQueue.resetToDefaults()
changeOrientationMode()
@ -502,7 +502,6 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
super.onDestroy()
try {
// clear the playing queue
PlayingQueue.clear()
PlayingQueue.resetToDefaults()
saveWatchPosition()
@ -849,8 +848,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
// save the watch position when paused
if (playbackState == PlaybackState.STATE_PAUSED) {
val watchPosition = WatchPosition(videoId!!, exoPlayer.currentPosition)
query {
Database.watchPositionDao().insertAll(WatchPosition(videoId!!, exoPlayer.currentPosition))
Database.watchPositionDao().insertAll(watchPosition)
}
}

View File

@ -213,17 +213,17 @@ class NowPlayingNotification(
* Destroy the [NowPlayingNotification]
*/
fun destroySelfAndPlayer() {
playerNotification?.setPlayer(null)
mediaSession.isActive = false
mediaSession.release()
mediaSessionConnector.setPlayer(null)
playerNotification?.setPlayer(null)
player.stop()
player.release()
val notificationManager = context.getSystemService(
Context.NOTIFICATION_SERVICE
) as NotificationManager
notificationManager.cancel(PLAYER_NOTIFICATION_ID)
player.stop()
player.release()
}
}

View File

@ -61,8 +61,6 @@ object PlayingQueue {
fun isEmpty() = queue.isEmpty()
fun clear() = queue.clear()
fun size() = queue.size
fun currentIndex(): Int {
@ -139,5 +137,6 @@ object PlayingQueue {
fun resetToDefaults() {
repeatQueue = false
onQueueTapListener = {}
queue.clear()
}
}