2019-08-19 16:50:33 +02:00
|
|
|
package com.github.apognu.otter.utils
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.app.Activity
|
|
|
|
import android.app.NotificationChannel
|
|
|
|
import android.app.NotificationManager
|
|
|
|
import android.content.BroadcastReceiver
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Build
|
|
|
|
import com.github.apognu.otter.R
|
|
|
|
import com.github.kittinunf.fuel.core.FuelManager
|
|
|
|
import com.github.kittinunf.fuel.core.Method
|
|
|
|
|
|
|
|
object AppContext {
|
|
|
|
const val PREFS_CREDENTIALS = "credentials"
|
|
|
|
|
|
|
|
const val NOTIFICATION_MEDIA_CONTROL = 1
|
2020-06-10 16:25:20 +02:00
|
|
|
const val NOTIFICATION_DOWNLOADS = 2
|
2019-08-19 16:50:33 +02:00
|
|
|
const val NOTIFICATION_CHANNEL_MEDIA_CONTROL = "mediacontrols"
|
2020-06-10 16:25:20 +02:00
|
|
|
const val NOTIFICATION_CHANNEL_DOWNLOADS = "downloads"
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2019-10-21 20:26:26 +02:00
|
|
|
const val PAGE_SIZE = 50
|
2019-08-19 16:50:33 +02:00
|
|
|
const val TRANSITION_DURATION = 300L
|
|
|
|
|
|
|
|
fun init(context: Activity) {
|
|
|
|
setupNotificationChannels(context)
|
|
|
|
|
|
|
|
// CastContext.getSharedInstance(context)
|
|
|
|
|
|
|
|
FuelManager.instance.addResponseInterceptor { next ->
|
|
|
|
{ request, response ->
|
|
|
|
if (request.method == Method.GET && response.statusCode == 200) {
|
|
|
|
var cacheId = request.url.path.toString()
|
|
|
|
|
|
|
|
request.url.query?.let {
|
|
|
|
cacheId = "$cacheId?$it"
|
|
|
|
}
|
|
|
|
|
|
|
|
Cache.set(context, cacheId, response.body().toByteArray())
|
|
|
|
}
|
|
|
|
|
|
|
|
next(request, response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("NewApi")
|
|
|
|
private fun setupNotificationChannels(context: Context) {
|
|
|
|
Build.VERSION_CODES.O.onApi {
|
|
|
|
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).let { manager ->
|
|
|
|
NotificationChannel(
|
|
|
|
NOTIFICATION_CHANNEL_MEDIA_CONTROL,
|
|
|
|
context.getString(R.string.playback_media_controls),
|
|
|
|
NotificationManager.IMPORTANCE_LOW
|
|
|
|
).run {
|
|
|
|
description = context.getString(R.string.playback_media_controls_description)
|
|
|
|
|
|
|
|
enableLights(false)
|
|
|
|
enableVibration(false)
|
|
|
|
setSound(null, null)
|
|
|
|
|
|
|
|
manager.createNotificationChannel(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 16:25:20 +02:00
|
|
|
|
|
|
|
Build.VERSION_CODES.O.onApi {
|
|
|
|
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).let { manager ->
|
|
|
|
NotificationChannel(
|
|
|
|
NOTIFICATION_CHANNEL_DOWNLOADS,
|
|
|
|
"Downloads",
|
|
|
|
NotificationManager.IMPORTANCE_LOW
|
|
|
|
).run {
|
|
|
|
description = "Downloads"
|
|
|
|
|
|
|
|
enableLights(false)
|
|
|
|
enableVibration(false)
|
|
|
|
setSound(null, null)
|
|
|
|
|
|
|
|
manager.createNotificationChannel(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class HeadphonesUnpluggedReceiver : BroadcastReceiver() {
|
|
|
|
override fun onReceive(context: Context?, intent: Intent?) {
|
|
|
|
CommandBus.send(Command.SetState(false))
|
|
|
|
}
|
|
|
|
}
|