@@ -44,6 +44,9 @@ import dev.steenbakker.mobile_scanner.objects.MobileScannerStartParameters
4444import dev.steenbakker.mobile_scanner.utils.invertBitmapColors
4545import dev.steenbakker.mobile_scanner.utils.rotateBitmap
4646import dev.steenbakker.mobile_scanner.utils.serialize
47+ import io.flutter.plugin.common.BinaryMessenger
48+ import io.flutter.plugin.common.EventChannel
49+ import io.flutter.plugin.common.MethodChannel
4750import io.flutter.view.TextureRegistry
4851import kotlinx.coroutines.CoroutineScope
4952import kotlinx.coroutines.Dispatchers
@@ -55,16 +58,37 @@ import kotlin.math.roundToInt
5558
5659@ExperimentalGetImage
5760class MobileScanner (
61+ binaryMessenger : BinaryMessenger ,
62+ methodCallHandler : MethodChannel .MethodCallHandler ,
5863 private val activity : Activity ,
5964 private val textureRegistry : TextureRegistry ,
6065 private val mobileScannerCallback : MobileScannerCallback ,
6166 private val mobileScannerErrorCallback : MobileScannerErrorCallback ,
62- private val deviceOrientationListener : DeviceOrientationListener ,
6367 private val barcodeScannerFactory : (options: BarcodeScannerOptions ? ) -> BarcodeScanner = ::defaultBarcodeScannerFactory,
6468) {
6569
70+ private val methodChannel: MethodChannel
71+ private val deviceOrientationChannel: EventChannel
72+
6673 init {
6774 configureCameraProcessProvider()
75+ methodChannel = MethodChannel (binaryMessenger,
76+ " dev.steenbakker.mobile_scanner/scanner/method" )
77+ methodChannel.setMethodCallHandler(methodCallHandler)
78+ deviceOrientationChannel = EventChannel (binaryMessenger,
79+ " dev.steenbakker.mobile_scanner/scanner/deviceOrientation" )
80+ }
81+
82+ // The device orientation listener is a lazy property,
83+ // because eagerly initializing it during `init {}`
84+ // leads to privacy warnings with more restrictive Play Store vendors.
85+ private val deviceOrientationListener by lazy {
86+ val listener = DeviceOrientationListener (activity)
87+
88+ // Only attach the listener to the event channel when it actually exists.
89+ deviceOrientationChannel.setStreamHandler(listener)
90+
91+ return @lazy listener
6892 }
6993
7094 // / Internal variables
@@ -718,6 +742,9 @@ class MobileScanner(
718742 * Dispose of this scanner instance.
719743 */
720744 fun dispose () {
745+ methodChannel.setMethodCallHandler(null )
746+ deviceOrientationChannel.setStreamHandler(null )
747+
721748 if (isStopped()) {
722749 return
723750 }
0 commit comments