Skip to content

Commit

Permalink
fix: CannotDeliverBroadcastException 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
easyhz committed Apr 19, 2024
1 parent 82fc764 commit 866f7bb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ android {
applicationId = "com.easyhz.picly"
minSdk = 26
targetSdk = 34
versionCode = 13
versionCode = 14
versionName = "2.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -64,6 +64,9 @@ android {
"proguard-rules.pro"
)
signingConfig = signingConfigs.getByName("release")
ndk {
debugSymbolLevel = "FULL"
}
}
}
compileOptions {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/easyhz/picly/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.easyhz.picly.data.repository.user.UserManager
import com.easyhz.picly.databinding.ActivityMainBinding
import com.easyhz.picly.domain.model.album.IncomingImages
import com.easyhz.picly.util.BlueSnackBar
import com.easyhz.picly.util.exception.PiclyUncaughtExceptionHandler
import com.easyhz.picly.view.navigation.NavControllerManager
import dagger.hilt.android.AndroidEntryPoint

Expand All @@ -20,6 +21,7 @@ class MainActivity : AppCompatActivity() {
binding = ActivityMainBinding.inflate(layoutInflater)
getFirstRun()
setContentView(binding.root)
uncaughtException()
}

private fun getFirstRun() {
Expand Down Expand Up @@ -47,4 +49,11 @@ class MainActivity : AppCompatActivity() {
NavControllerManager.navigateMainToUploadWithIncoming(IncomingImages().apply { addAll(incomingImages) })
}

private fun uncaughtException() {
val defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler(
defaultUncaughtExceptionHandler?.let { PiclyUncaughtExceptionHandler(it) }
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.easyhz.picly.util.exception

class PiclyUncaughtExceptionHandler(
private val uncaughtExceptionHandler: Thread.UncaughtExceptionHandler
) : Thread.UncaughtExceptionHandler {

override fun uncaughtException(thread: Thread, exception: Throwable) {
if (shouldAbsorb(exception)) {
return
}
uncaughtExceptionHandler.uncaughtException(thread, exception)
}

/**
* Evaluate whether to silently absorb uncaught crashes such that they
* don't crash the app. We generally want to avoid this practice - we would
* rather know about them. However in some cases there's nothing we can do
* about the crash (e.g. it is an OS fault) and we would rather not have them
* pollute our reliability stats.
*/
private fun shouldAbsorb(exception: Throwable): Boolean {
return when (exception::class.simpleName) {
"CannotDeliverBroadcastException" -> true
else -> false
}
}

}

0 comments on commit 866f7bb

Please sign in to comment.