Skip to content

Commit 607886e

Browse files
committed
refactor
1 parent 90ffcc4 commit 607886e

4 files changed

Lines changed: 40 additions & 16 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
99

1010
<application
11+
android:name=".Application"
1112
android:allowBackup="true"
1213
android:dataExtractionRules="@xml/data_extraction_rules"
1314
android:fullBackupContent="@xml/backup_rules"
File renamed without changes.
Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,50 @@
11
package rs.clash.android
22

3-
import android.app.Application
4-
import kotlinx.coroutines.CoroutineScope
5-
import kotlinx.coroutines.Dispatchers
6-
import kotlinx.coroutines.cancel
3+
import android.util.Log
74
import kotlinx.coroutines.flow.MutableStateFlow
5+
import uniffi.clash_android_ffi.EyreException
6+
import uniffi.clash_android_ffi.formatEyreError
7+
import android.app.Application as AndroidApplication
88

9-
object Global : CoroutineScope by CoroutineScope(Dispatchers.IO) {
10-
val application: Application
11-
get() = applicationInstance
12-
13-
private lateinit var applicationInstance: Application
9+
class Application : AndroidApplication() {
10+
override fun onCreate() {
11+
super.onCreate()
12+
// Setup uncaught exception handler
13+
setupUncaughtExceptionHandler()
14+
Global.application = this
15+
}
16+
}
1417

18+
object Global {
1519
var profilePath: String = ""
16-
1720
val isServiceRunning = MutableStateFlow(false)
21+
lateinit var application: Application
22+
}
1823

19-
fun init(application: Application) {
20-
this.applicationInstance = application
21-
}
24+
private fun setupUncaughtExceptionHandler() {
25+
val defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
26+
27+
Thread.setDefaultUncaughtExceptionHandler { thread, throwable ->
28+
try {
29+
// Check if the exception is EyreException
30+
if (throwable is EyreException) {
31+
val errorMessage = formatEyreError(throwable)
32+
Log.e("Clash", "Uncaught EyreException on thread ${thread.name}:")
33+
Log.e("Clash", errorMessage)
2234

23-
fun destroy() {
24-
cancel()
35+
// Also print to stderr
36+
System.err.println("Uncaught EyreException on thread ${thread.name}:")
37+
System.err.println(errorMessage)
38+
} else {
39+
// For other exceptions, log normally
40+
Log.e("Clash", "Uncaught exception on thread ${thread.name}:", throwable)
41+
}
42+
} catch (e: Exception) {
43+
// If something goes wrong in our handler, log it
44+
Log.e("Clash", "Error in exception handler:", e)
45+
} finally {
46+
// Call the default handler to let the system handle the crash
47+
defaultHandler?.uncaughtException(thread, throwable)
48+
}
2549
}
2650
}

app/src/main/java/rs/clash/android/MainActivity.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class MainActivity : ComponentActivity() {
3434
super.onCreate(savedInstanceState)
3535
System.loadLibrary("clash_android_ffi")
3636
javaInit()
37-
Global.init(application)
3837

3938
// Apply language preference
4039
applyLanguagePreference()

0 commit comments

Comments
 (0)