Add try/catch block when inserting an event to mitigate SQLITE_CONSTRAINT_FOREIGNKEY

Caused by ExoPlayer
This commit is contained in:
vfsfitvnm 2022-10-14 12:12:33 +02:00
parent 0de5330676
commit 554dea3fba
2 changed files with 13 additions and 7 deletions

View file

@ -2,6 +2,7 @@ package it.vfsfitvnm.vimusic
import android.content.ContentValues
import android.content.Context
import android.database.SQLException
import android.database.sqlite.SQLiteDatabase.CONFLICT_IGNORE
import android.os.Parcel
import androidx.core.database.getFloatOrNull
@ -51,6 +52,7 @@ import it.vfsfitvnm.vimusic.models.SongAlbumMap
import it.vfsfitvnm.vimusic.models.SongArtistMap
import it.vfsfitvnm.vimusic.models.SongPlaylistMap
import it.vfsfitvnm.vimusic.models.SortedSongPlaylistMap
import kotlin.jvm.Throws
import kotlinx.coroutines.flow.Flow
@Dao
@ -326,7 +328,8 @@ interface Database {
@Query("DELETE FROM Event WHERE songId = :songId")
fun clearEventsFor(songId: String)
@Insert
@Insert(onConflict = OnConflictStrategy.IGNORE)
@Throws(SQLException::class)
fun insert(event: Event)
@Insert(onConflict = OnConflictStrategy.REPLACE)

View file

@ -11,6 +11,7 @@ import android.content.Intent
import android.content.IntentFilter
import android.content.SharedPreferences
import android.content.res.Configuration
import android.database.SQLException
import android.graphics.Bitmap
import android.graphics.Color
import android.media.MediaMetadata
@ -299,13 +300,15 @@ class PlayerService : InvincibleService(), Player.Listener, PlaybackStatsListene
if (totalPlayTimeMs > 30000) {
query {
Database.insert(
Event(
songId = mediaItem.mediaId,
timestamp = System.currentTimeMillis(),
playTime = totalPlayTimeMs
try {
Database.insert(
Event(
songId = mediaItem.mediaId,
timestamp = System.currentTimeMillis(),
playTime = totalPlayTimeMs
)
)
)
} catch (_: SQLException) { }
}
}
}