2021-07-12 10:14:26 +02:00
|
|
|
package audio.funkwhale.ffa
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
import android.app.Application
|
2021-08-09 06:50:46 +02:00
|
|
|
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.FFACache
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.preference.PowerPreference
|
2021-08-09 06:50:46 +02:00
|
|
|
import org.koin.core.context.startKoin
|
2020-06-10 23:59:09 +02:00
|
|
|
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() {
|
2021-08-09 06:50:46 +02:00
|
|
|
|
2019-10-22 20:03:52 +02:00
|
|
|
companion object {
|
2021-07-21 09:11:07 +02:00
|
|
|
private var instance: FFA = FFA()
|
2019-10-22 20:03:52 +02:00
|
|
|
|
2021-07-21 09:11:07 +02:00
|
|
|
fun get(): FFA = instance
|
2019-10-22 20:03:52 +02:00
|
|
|
}
|
|
|
|
|
2020-06-10 23:59:09 +02:00
|
|
|
var defaultExceptionHandler: Thread.UncaughtExceptionHandler? = null
|
|
|
|
|
2019-08-19 16:50:33 +02:00
|
|
|
override fun onCreate() {
|
|
|
|
super.onCreate()
|
|
|
|
|
2021-08-09 06:50:46 +02:00
|
|
|
startKoin {
|
2021-08-09 07:03:20 +02:00
|
|
|
modules(
|
|
|
|
authModule(),
|
|
|
|
exoplayerModule(this@FFA)
|
|
|
|
)
|
2021-08-09 06:50:46 +02:00
|
|
|
}
|
|
|
|
|
2020-06-10 23:59:09 +02:00
|
|
|
defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
|
|
|
|
|
|
|
|
Thread.setDefaultUncaughtExceptionHandler(CrashReportHandler())
|
|
|
|
|
2021-07-23 14:10:13 +02:00
|
|
|
instance = this
|
2019-10-22 20:03:52 +02:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 23:59:09 +02:00
|
|
|
|
2021-08-09 06:50:46 +02:00
|
|
|
fun deleteAllData(context: Context) {
|
2020-06-21 18:06:38 +02:00
|
|
|
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).clear()
|
|
|
|
|
2021-08-09 06:50:46 +02:00
|
|
|
context.cacheDir.listFiles()?.forEach {
|
2020-06-21 18:51:22 +02:00
|
|
|
it.delete()
|
|
|
|
}
|
2021-08-09 06:50:46 +02:00
|
|
|
context.cacheDir.resolve("picasso-cache").deleteRecursively()
|
2020-06-21 18:06:38 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
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()
|
2020-06-10 23:59:09 +02:00
|
|
|
|
2021-07-21 09:11:07 +02:00
|
|
|
while (true) {
|
|
|
|
builder.appendLine(reader.readLine() ?: break)
|
|
|
|
}
|
2020-06-10 23:59:09 +02:00
|
|
|
|
2021-07-21 09:11:07 +02:00
|
|
|
builder.appendLine(e.toString())
|
2020-06-10 23:59:09 +02:00
|
|
|
|
2022-06-12 14:48:32 +02:00
|
|
|
FFACache.set(this@FFA, "crashdump", builder.toString())
|
2021-07-21 09:11:07 +02:00
|
|
|
}
|
2020-06-10 23:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defaultExceptionHandler?.uncaughtException(t, e)
|
|
|
|
}
|
|
|
|
}
|
2021-07-02 13:55:49 +02:00
|
|
|
}
|