Files
textpipe/app/src/main/java/sh/sar/textpipe/TextpipeApplication.kt

43 lines
1.4 KiB
Kotlin

package sh.sar.textpipe
import android.app.Application
import android.util.Log
import sh.sar.textpipe.data.db.AppDatabase
import sh.sar.textpipe.sim.SimManager
import java.net.BindException
class TextpipeApplication : Application() {
companion object {
private const val TAG = "TextpipeApplication"
}
lateinit var database: AppDatabase
private set
lateinit var simManager: SimManager
private set
private var defaultHandler: Thread.UncaughtExceptionHandler? = null
override fun onCreate() {
super.onCreate()
database = AppDatabase.getInstance(this)
simManager = SimManager(this)
// Set up global uncaught exception handler to prevent crashes from Ktor binding issues
defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
if (throwable is BindException ||
(throwable.cause is BindException) ||
throwable.message?.contains("Address already in use") == true) {
// Log but don't crash for bind exceptions
Log.e(TAG, "Caught BindException in thread ${thread.name}, ignoring to prevent crash", throwable)
} else {
// For other exceptions, use the default handler
defaultHandler?.uncaughtException(thread, throwable)
}
}
}
}