63 lines
2.1 KiB
Kotlin
63 lines
2.1 KiB
Kotlin
|
|
package at.lockstep.player
|
||
|
|
|
||
|
|
import android.app.Application
|
||
|
|
import android.app.NotificationChannel
|
||
|
|
import android.app.NotificationManager
|
||
|
|
import android.os.Build
|
||
|
|
import at.lockstep.jukebox.Jukebox
|
||
|
|
import at.lockstep.jukebox.PlaylistRepository
|
||
|
|
import at.lockstep.player.data.db.AppDatabase
|
||
|
|
import okhttp3.Interceptor
|
||
|
|
import java.util.concurrent.atomic.AtomicReference
|
||
|
|
|
||
|
|
class LockstepApplication : Application() {
|
||
|
|
private val spotifyAccessTokenRef = AtomicReference<String?>(null)
|
||
|
|
|
||
|
|
val database: AppDatabase by lazy { AppDatabase.getInstance(this) }
|
||
|
|
|
||
|
|
val playlistRepository: PlaylistRepository by lazy {
|
||
|
|
Jukebox.playlistRepository(
|
||
|
|
this,
|
||
|
|
Interceptor { chain ->
|
||
|
|
val token = spotifyAccessTokenRef.get()
|
||
|
|
val req =
|
||
|
|
if (!token.isNullOrBlank()) {
|
||
|
|
chain.request().newBuilder()
|
||
|
|
.header("Authorization", "Bearer $token")
|
||
|
|
.build()
|
||
|
|
} else {
|
||
|
|
chain.request()
|
||
|
|
}
|
||
|
|
chain.proceed(req)
|
||
|
|
},
|
||
|
|
"${BuildConfig.LOCKSTEP_API_BASE_URL.trimEnd('/')}/",
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
fun setSpotifyAccessTokenForApi(token: String?) {
|
||
|
|
spotifyAccessTokenRef.set(token)
|
||
|
|
}
|
||
|
|
|
||
|
|
override fun onCreate() {
|
||
|
|
super.onCreate()
|
||
|
|
ensurePlaybackChannel()
|
||
|
|
}
|
||
|
|
|
||
|
|
private fun ensurePlaybackChannel() {
|
||
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
|
||
|
|
val manager = getSystemService(NotificationManager::class.java) ?: return
|
||
|
|
val channel =
|
||
|
|
NotificationChannel(
|
||
|
|
PLAYBACK_NOTIFICATION_CHANNEL_ID,
|
||
|
|
getString(R.string.notification_channel_playback_name),
|
||
|
|
NotificationManager.IMPORTANCE_LOW,
|
||
|
|
).apply { description = getString(R.string.notification_channel_playback_description) }
|
||
|
|
manager.createNotificationChannel(channel)
|
||
|
|
}
|
||
|
|
|
||
|
|
companion object {
|
||
|
|
const val PLAYBACK_NOTIFICATION_CHANNEL_ID = "lockstep_playback"
|
||
|
|
const val PLAYBACK_NOTIFICATION_ID = 1001
|
||
|
|
}
|
||
|
|
}
|