Skip to content
Snippets Groups Projects
FFA.kt 2.67 KiB
Newer Older
  • Learn to ignore specific revisions
  • package audio.funkwhale.ffa
    
    Antoine POPINEAU's avatar
    Antoine POPINEAU committed
    
    import android.app.Application
    
    import android.content.Context
    
    Antoine POPINEAU's avatar
    Antoine POPINEAU committed
    import androidx.appcompat.app.AppCompatDelegate
    
    Ryan Harg's avatar
    Ryan Harg committed
    import audio.funkwhale.ffa.koin.authModule
    import audio.funkwhale.ffa.koin.exoplayerModule
    
    Ryan Harg's avatar
    Ryan Harg committed
    import audio.funkwhale.ffa.utils.*
    
    Antoine POPINEAU's avatar
    Antoine POPINEAU committed
    import com.preference.PowerPreference
    
    import kotlinx.coroutines.channels.BroadcastChannel
    import kotlinx.coroutines.channels.ConflatedBroadcastChannel
    
    import org.koin.core.context.startKoin
    
    import java.text.SimpleDateFormat
    import java.util.*
    
    Antoine POPINEAU's avatar
    Antoine POPINEAU committed
    
    
    Ryan Harg's avatar
    Ryan Harg committed
    class FFA : Application() {
    
    Ryan Harg's avatar
    Ryan Harg committed
        private var instance: FFA = FFA()
    
    Ryan Harg's avatar
    Ryan Harg committed
        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()
    
    
    Antoine POPINEAU's avatar
    Antoine POPINEAU committed
      override fun onCreate() {
        super.onCreate()
    
    
    Ryan Harg's avatar
    Ryan Harg committed
          modules(
            authModule(),
            exoplayerModule(this@FFA)
          )
    
        defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
    
        Thread.setDefaultUncaughtExceptionHandler(CrashReportHandler())
    
    
        instance = this
    
        PowerPreference.init(this)
    
    
    Antoine POPINEAU's avatar
    Antoine POPINEAU committed
        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 {
    
        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)
    
    
    Ryan Harg's avatar
    Ryan Harg committed
          Runtime.getRuntime().exec(listOf("logcat", "-d", "-T", formatter.format(now)).toTypedArray())
            .also {
              it.inputStream.bufferedReader().also { reader ->
                val builder = StringBuilder()
    
    Ryan Harg's avatar
    Ryan Harg committed
                while (true) {
                  builder.appendLine(reader.readLine() ?: break)
                }
    
    Ryan Harg's avatar
    Ryan Harg committed
                builder.appendLine(e.toString())
    
                FFACache.set(this@FFA, "crashdump", builder.toString().toByteArray())
    
    Ryan Harg's avatar
    Ryan Harg committed
              }
    
            }
    
          defaultExceptionHandler?.uncaughtException(t, e)
        }
      }
    
    Ryan Harg's avatar
    Ryan Harg committed
    }