Apparence-io / CamerAwesome

📸 Embedding a camera experience within your own app shouldn't be that hard. A flutter plugin to integrate awesome Android / iOS camera experience.
https://ApparenceKit.dev
MIT License
906 stars 199 forks source link

After requesting not granted permissions, the result should be dispatched on main thread #450

Open korzonkiee opened 4 months ago

korzonkiee commented 4 months ago

Problem

Seeing the following crash reported multiple times in Sentry:

java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: DefaultDispatcher-worker-1
    at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:37)
    at io.flutter.embedding.engine.FlutterJNI.dispatchPlatformMessage(FlutterJNI.java:1)
    at mb.c.d(DartMessenger.java:79)
    at mb.a$c.c(DartExecutor.java:4)
    at mb.a.c(DartExecutor.java:3)
    at yb.d$c$a.a(EventChannel.java:50)
    at y1.j1.e(CameraPermissions.kt:166)
    at io.flutter.embedding.engine.c$c.i(FlutterEngineConnectionRegistry.java:21)
    at io.flutter.embedding.engine.c.e(FlutterEngineConnectionRegistry.java:14)
    at io.flutter.embedding.android.f.y(FlutterActivityAndFragmentDelegate.java:60)
    at io.flutter.embedding.android.e.onRequestPermissionsResult(FlutterActivity.java:11)
    at android.app.Activity.requestPermissions(Activity.java:5633)
    at androidx.core.app.b$b.b(ActivityCompat.java:1)
    at androidx.core.app.b.r(ActivityCompat.java:140)
    at y1.j1.g(CameraPermissions.kt:100)
    at y1.j1$b.invokeSuspend(CameraPermissions.kt:48)
    at kotlin.coroutines.jvm.internal.a.resumeWith(ContinuationImpl.kt:12)
    at nd.r0.run(DispatchedTask.kt:124)
    at sd.o$a.run(LimitedDispatcher.kt:4)
    at ud.k.run(Tasks.kt:3)
    at ud.a.z(CoroutineScheduler.kt:1)
    at ud.a$c.d(CoroutineScheduler.kt:15)
    at ud.a$c.p(CoroutineScheduler.kt:29)
    at ud.a$c.run(CoroutineScheduler.kt:1)

After some investigation, it turned out that after requesting the not-granted permissions, the callback function is invoked from the IO thread, which then leads to the java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. In order to fix it, we must dispatch the callback invocation to the main thread, similarily as in the case of requesting audio permission.

g-apparence commented 3 months ago

Yes I ended using permission package that does this way better. I am thinking about removing this from the plugin and encourage using it.