Skip to content

Commit

Permalink
Add try/catch block when inserting an event to mitigate SQLITE_CONSTR…
Browse files Browse the repository at this point in the history
…AINT_FOREIGNKEY

Caused by ExoPlayer
  • Loading branch information
vfsfitvnm committed Oct 14, 2022
1 parent 0de5330 commit 554dea3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
5 changes: 4 additions & 1 deletion app/src/main/kotlin/it/vfsfitvnm/vimusic/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) { }
}
}
}
Expand Down

0 comments on commit 554dea3

Please sign in to comment.