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

View File

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