From 3974342842d0ab57016e1480cdd99e0434bc99b4 Mon Sep 17 00:00:00 2001 From: Sander Roest Date: Thu, 21 Jan 2021 19:09:06 +0100 Subject: [PATCH] [camera] Ensure that channel.invokeMethod runs on the main thread (#3444) --- packages/camera/camera/CHANGELOG.md | 4 +-- .../flutter/plugins/camera/DartMessenger.java | 31 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/packages/camera/camera/CHANGELOG.md b/packages/camera/camera/CHANGELOG.md index 0ba77e5ee1d5..7e3128395934 100644 --- a/packages/camera/camera/CHANGELOG.md +++ b/packages/camera/camera/CHANGELOG.md @@ -15,7 +15,7 @@ ## 0.6.4+2 -* Set ImageStreamReader listener to null to prevent stale images when streaming images. +* Set ImageStreamReader listener to null to prevent stale images when streaming images. ## 0.6.4+1 @@ -75,7 +75,7 @@ As part of implementing federated architecture and making the interface compatib Method changes in `CameraController`: - The `takePicture` method no longer accepts the `path` parameter, but instead returns the captured image as an instance of the `XFile` class; -- The `startVideoRecording` method no longer accepts the `filePath`. Instead the recorded video is now returned as a `XFile` instance when the `stopVideoRecording` method completes; +- The `startVideoRecording` method no longer accepts the `filePath`. Instead the recorded video is now returned as a `XFile` instance when the `stopVideoRecording` method completes; - The `stopVideoRecording` method now returns the captured video when it completes; - Added the `buildPreview` method which is now used to implement the CameraPreview widget. diff --git a/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/DartMessenger.java b/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/DartMessenger.java index 2fee13816b51..0c3487defc9f 100644 --- a/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/DartMessenger.java +++ b/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/DartMessenger.java @@ -1,5 +1,7 @@ package io.flutter.plugins.camera; +import android.os.Handler; +import android.os.Looper; import android.text.TextUtils; import androidx.annotation.Nullable; import io.flutter.plugin.common.BinaryMessenger; @@ -56,7 +58,25 @@ void sendCameraErrorEvent(@Nullable String description) { }); } - void send(EventType eventType) { + void send(CameraEventType eventType) { + send(eventType, new HashMap<>()); + } + + void send(CameraEventType eventType, Map args) { + if (cameraChannel == null) { + return; + } + new Handler(Looper.getMainLooper()) + .post( + new Runnable() { + @Override + public void run() { + cameraChannel.invokeMethod(eventType.method, args); + } + }); + } + + void send(DeviceEventType eventType) { send(eventType, new HashMap<>()); } @@ -64,6 +84,13 @@ void send(EventType eventType, Map args) { if (channel == null) { return; } - channel.invokeMethod(eventType.toString().toLowerCase(), args); + new Handler(Looper.getMainLooper()) + .post( + new Runnable() { + @Override + public void run() { + deviceChannel.invokeMethod(eventType.method, args); + } + }); } }