funkwhale-app-android/app/src/main/java/audio/funkwhale/ffa/FFA.kt

94 lines
2.9 KiB
Kotlin
Raw Normal View History

package audio.funkwhale.ffa
2019-08-19 16:50:33 +02:00
import android.app.Application
import android.content.Context
2019-08-19 16:50:33 +02:00
import androidx.appcompat.app.AppCompatDelegate
2021-08-09 07:03:20 +02:00
import audio.funkwhale.ffa.koin.authModule
import audio.funkwhale.ffa.koin.exoplayerModule
2021-09-09 09:56:15 +02:00
import audio.funkwhale.ffa.utils.AppContext
import audio.funkwhale.ffa.utils.Command
import audio.funkwhale.ffa.utils.Event
import audio.funkwhale.ffa.utils.FFACache
import audio.funkwhale.ffa.utils.Request
2019-08-19 16:50:33 +02:00
import com.preference.PowerPreference
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
import org.koin.core.context.startKoin
import java.text.SimpleDateFormat
2021-09-09 09:56:15 +02:00
import java.util.Date
import java.util.Locale
2019-08-19 16:50:33 +02:00
2021-07-02 13:55:49 +02:00
class FFA : Application() {
companion object {
2021-07-21 09:11:07 +02:00
private var instance: FFA = FFA()
2021-07-21 09:11:07 +02:00
fun get(): FFA = instance
}
var defaultExceptionHandler: Thread.UncaughtExceptionHandler? = null
val eventBus: BroadcastChannel<Event> = BroadcastChannel(10)
val commandBus: BroadcastChannel<Command> = BroadcastChannel(10)
val requestBus: BroadcastChannel<Request> = BroadcastChannel(10)
val progressBus: BroadcastChannel<Triple<Int, Int, Int>> = ConflatedBroadcastChannel()
2019-08-19 16:50:33 +02:00
override fun onCreate() {
super.onCreate()
startKoin {
2021-08-09 07:03:20 +02:00
modules(
authModule(),
exoplayerModule(this@FFA)
)
}
defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler(CrashReportHandler())
2021-07-23 14:10:13 +02:00
instance = this
2021-08-06 21:16:46 +02:00
PowerPreference.init(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)
}
}
fun deleteAllData(context: Context) {
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).clear()
context.cacheDir.listFiles()?.forEach {
2020-06-21 18:51:22 +02:00
it.delete()
}
context.cacheDir.resolve("picasso-cache").deleteRecursively()
}
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)
2021-07-21 09:11:07 +02:00
Runtime.getRuntime().exec(listOf("logcat", "-d", "-T", formatter.format(now)).toTypedArray())
.also {
it.inputStream.bufferedReader().also { reader ->
val builder = StringBuilder()
2021-07-21 09:11:07 +02:00
while (true) {
builder.appendLine(reader.readLine() ?: break)
}
2021-07-21 09:11:07 +02:00
builder.appendLine(e.toString())
FFACache.set(this@FFA, "crashdump", builder.toString())
2021-07-21 09:11:07 +02:00
}
}
defaultExceptionHandler?.uncaughtException(t, e)
}
}
2021-07-02 13:55:49 +02:00
}