Skip to content

Commit 3cf8ce7

Browse files
committed
let MobileScanner manage Method Channel internally
1 parent 4902a22 commit 3cf8ce7

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ import dev.steenbakker.mobile_scanner.objects.MobileScannerStartParameters
4444
import dev.steenbakker.mobile_scanner.utils.invertBitmapColors
4545
import dev.steenbakker.mobile_scanner.utils.rotateBitmap
4646
import 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
4750
import io.flutter.view.TextureRegistry
4851
import kotlinx.coroutines.CoroutineScope
4952
import kotlinx.coroutines.Dispatchers
@@ -55,16 +58,37 @@ import kotlin.math.roundToInt
5558

5659
@ExperimentalGetImage
5760
class 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
}

android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScannerHandler.kt

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import io.flutter.plugin.common.MethodCall
2121
import io.flutter.plugin.common.MethodChannel
2222
import io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener
2323
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
24-
import io.flutter.plugin.common.EventChannel
2524
import io.flutter.view.TextureRegistry
2625
import java.io.File
2726
import com.google.mlkit.vision.barcode.ZoomSuggestionOptions
@@ -79,9 +78,6 @@ class MobileScannerHandler(
7978
barcodeHandler.publishError(MobileScannerErrorCodes.BARCODE_ERROR, error, null)
8079
}
8180

82-
private var methodChannel: MethodChannel? = null
83-
private var deviceOrientationChannel: EventChannel? = null
84-
8581
private var mobileScanner: MobileScanner? = null
8682

8783
private val torchStateCallback: TorchStateCallback = {state: Int ->
@@ -94,25 +90,17 @@ class MobileScannerHandler(
9490
}
9591

9692
init {
97-
methodChannel = MethodChannel(binaryMessenger,
98-
"dev.steenbakker.mobile_scanner/scanner/method")
99-
methodChannel!!.setMethodCallHandler(this)
100-
101-
val deviceOrientationListener = DeviceOrientationListener(activity)
102-
103-
deviceOrientationChannel = EventChannel(binaryMessenger,
104-
"dev.steenbakker.mobile_scanner/scanner/deviceOrientation")
105-
deviceOrientationChannel!!.setStreamHandler(deviceOrientationListener)
106-
10793
mobileScanner = MobileScanner(
108-
activity, textureRegistry, callback, errorCallback, deviceOrientationListener)
94+
binaryMessenger,
95+
this,
96+
activity,
97+
textureRegistry,
98+
callback,
99+
errorCallback
100+
)
109101
}
110102

111103
fun dispose(activityPluginBinding: ActivityPluginBinding) {
112-
methodChannel?.setMethodCallHandler(null)
113-
methodChannel = null
114-
deviceOrientationChannel?.setStreamHandler(null)
115-
deviceOrientationChannel = null
116104
barcodeHandler.dispose()
117105
mobileScanner?.dispose()
118106
mobileScanner = null

0 commit comments

Comments
 (0)