diff --git a/CHANGELOG.md b/CHANGELOG.md index 022fefc..94ebb3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # [4.0.2](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/tree/4.0.2) [UNRELEASED] - Fixed floating event stream bad state exception [#157](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/issues/157). +- Fixed Gyroscope initialization issue [#173](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/issues/173). +- Fixed Namespace Not Found issue [#176](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/issues/176). # [4.0.1](https://github.com/SimformSolutionsPvtLtd/flutter_credit_card/tree/4.0.1) diff --git a/android/src/main/kotlin/com/simform/flutter_credit_card/gyroscope/GyroscopeStreamHandler.kt b/android/src/main/kotlin/com/simform/flutter_credit_card/gyroscope/GyroscopeStreamHandler.kt index 42bcce4..df3bb2d 100644 --- a/android/src/main/kotlin/com/simform/flutter_credit_card/gyroscope/GyroscopeStreamHandler.kt +++ b/android/src/main/kotlin/com/simform/flutter_credit_card/gyroscope/GyroscopeStreamHandler.kt @@ -15,17 +15,26 @@ internal class GyroscopeStreamHandler( ) : EventChannel.StreamHandler { private var sensorEventListener: SensorEventListener? = null - private val sensor: Sensor by lazy { + private val sensor: Sensor? by lazy { sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) } override fun onListen(arguments: Any?, events: EventSink) { + if (sensor == null) { + events.error("SENSOR_UNAVAILABLE", "Gyroscope sensor is not available on this device.", null) + return + } sensorEventListener = createSensorEventListener(events) // Gyroscope Event sample period set at 60 fps, specified in microseconds. sensorManager.registerListener(sensorEventListener, sensor, 16666) } - override fun onCancel(arguments: Any?) = sensorManager.unregisterListener(sensorEventListener) + override fun onCancel(arguments: Any?) { + if (sensorEventListener != null) { + sensorManager.unregisterListener(sensorEventListener) + sensorEventListener = null + } + } private fun createSensorEventListener(events: EventSink): SensorEventListener { return object : SensorEventListener { diff --git a/lib/src/plugin/flutter_credit_card_method_channel.dart b/lib/src/plugin/flutter_credit_card_method_channel.dart index e99338b..a749cf1 100644 --- a/lib/src/plugin/flutter_credit_card_method_channel.dart +++ b/lib/src/plugin/flutter_credit_card_method_channel.dart @@ -51,7 +51,6 @@ class MethodChannelFlutterCreditCard extends FlutterCreditCardPlatform { ); if (Platform.isIOS || Platform.isAndroid) { - await initiateEvents(); _isGyroscopeAvailable = await _methodChannel!.invokeMethod( AppConstants.isGyroAvailableMethod, ) ?? @@ -60,6 +59,10 @@ class MethodChannelFlutterCreditCard extends FlutterCreditCardPlatform { // Other platforms should not use the gyroscope events. _isGyroscopeAvailable = false; } + // We will only initialize event if gyroScope is available. + if (_isGyroscopeAvailable) { + await initiateEvents(); + } } @override