2019-08-19 16:50:33 +02:00
|
|
|
package com.github.apognu.otter
|
|
|
|
|
|
|
|
import android.app.Application
|
|
|
|
import androidx.appcompat.app.AppCompatDelegate
|
2020-06-14 14:59:50 +02:00
|
|
|
import com.github.apognu.otter.playback.QueueManager
|
2020-06-20 15:42:10 +02:00
|
|
|
import com.github.apognu.otter.utils.*
|
2020-06-10 16:25:20 +02:00
|
|
|
import com.google.android.exoplayer2.database.ExoDatabaseProvider
|
2020-06-14 14:59:50 +02:00
|
|
|
import com.google.android.exoplayer2.offline.DefaultDownloadIndex
|
|
|
|
import com.google.android.exoplayer2.offline.DefaultDownloaderFactory
|
|
|
|
import com.google.android.exoplayer2.offline.DownloadManager
|
|
|
|
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper
|
2020-06-10 16:25:20 +02:00
|
|
|
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor
|
2020-06-20 15:42:10 +02:00
|
|
|
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor
|
2020-06-10 16:25:20 +02:00
|
|
|
import com.google.android.exoplayer2.upstream.cache.SimpleCache
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.preference.PowerPreference
|
2019-10-22 20:03:52 +02:00
|
|
|
import kotlinx.coroutines.channels.BroadcastChannel
|
|
|
|
import kotlinx.coroutines.channels.Channel
|
|
|
|
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
|
2020-06-10 23:59:09 +02:00
|
|
|
import java.text.SimpleDateFormat
|
|
|
|
import java.util.*
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
class Otter : Application() {
|
2019-10-22 20:03:52 +02:00
|
|
|
companion object {
|
|
|
|
private var instance: Otter = Otter()
|
|
|
|
|
|
|
|
fun get(): Otter = instance
|
|
|
|
}
|
|
|
|
|
2020-06-10 23:59:09 +02:00
|
|
|
var defaultExceptionHandler: Thread.UncaughtExceptionHandler? = null
|
|
|
|
|
2019-10-31 00:46:35 +01:00
|
|
|
val eventBus: BroadcastChannel<Event> = BroadcastChannel(10)
|
2019-10-22 20:03:52 +02:00
|
|
|
val commandBus: Channel<Command> = Channel(10)
|
|
|
|
val requestBus: BroadcastChannel<Request> = BroadcastChannel(10)
|
|
|
|
val progressBus: BroadcastChannel<Triple<Int, Int, Int>> = ConflatedBroadcastChannel()
|
|
|
|
|
2020-06-14 14:59:50 +02:00
|
|
|
private val exoDatabase: ExoDatabaseProvider by lazy { ExoDatabaseProvider(this) }
|
2020-06-20 15:42:10 +02:00
|
|
|
|
2020-06-14 14:59:50 +02:00
|
|
|
val exoCache: SimpleCache by lazy {
|
|
|
|
PowerPreference.getDefaultFile().getInt("media_cache_size", 1).toLong().let {
|
|
|
|
SimpleCache(
|
|
|
|
cacheDir.resolve("media"),
|
|
|
|
LeastRecentlyUsedCacheEvictor(it * 1024 * 1024 * 1024),
|
|
|
|
exoDatabase
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-06-20 15:42:10 +02:00
|
|
|
|
|
|
|
val exoDownloadCache: SimpleCache by lazy {
|
|
|
|
SimpleCache(
|
|
|
|
cacheDir.resolve("downloads"),
|
|
|
|
NoOpCacheEvictor(),
|
|
|
|
exoDatabase
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-14 14:59:50 +02:00
|
|
|
val exoDownloadManager: DownloadManager by lazy {
|
2020-06-20 15:42:10 +02:00
|
|
|
DownloaderConstructorHelper(exoDownloadCache, QueueManager.factory(this)).run {
|
2020-06-14 14:59:50 +02:00
|
|
|
DownloadManager(this@Otter, DefaultDownloadIndex(exoDatabase), DefaultDownloaderFactory(this))
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 16:25:20 +02:00
|
|
|
|
2019-08-19 16:50:33 +02:00
|
|
|
override fun onCreate() {
|
|
|
|
super.onCreate()
|
|
|
|
|
2020-06-10 23:59:09 +02:00
|
|
|
defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
|
|
|
|
|
|
|
|
Thread.setDefaultUncaughtExceptionHandler(CrashReportHandler())
|
|
|
|
|
2019-10-22 20:03:52 +02:00
|
|
|
instance = this
|
|
|
|
|
2019-08-19 16:50:33 +02:00
|
|
|
when (PowerPreference.getDefaultFile().getString("night_mode")) {
|
|
|
|
"on" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
|
|
|
|
"off" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
|
|
|
else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 23:59:09 +02:00
|
|
|
|
|
|
|
inner class CrashReportHandler : Thread.UncaughtExceptionHandler {
|
|
|
|
override fun uncaughtException(t: Thread, e: Throwable) {
|
|
|
|
val now = Date(Date().time - (5 * 60 * 1000))
|
|
|
|
val formatter = SimpleDateFormat("MM-dd kk:mm:ss.000", Locale.US)
|
|
|
|
|
|
|
|
Runtime.getRuntime().exec(listOf("logcat", "-d", "-T", formatter.format(now)).toTypedArray()).also {
|
|
|
|
it.inputStream.bufferedReader().also { reader ->
|
|
|
|
val builder = StringBuilder()
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
builder.appendln(reader.readLine() ?: break)
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.appendln(e.toString())
|
|
|
|
|
|
|
|
Cache.set(this@Otter, "crashdump", builder.toString().toByteArray())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultExceptionHandler?.uncaughtException(t, e)
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|