-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2146540
commit b81424f
Showing
26 changed files
with
932 additions
and
874 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 100 additions & 52 deletions
152
android/src/main/kotlin/com/simform/flutter_credit_card/FlutterCreditCardPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,116 @@ | ||
package com.simform.flutter_credit_card | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import android.hardware.Sensor | ||
import android.hardware.SensorManager | ||
import android.os.Build | ||
import android.view.Display | ||
import android.view.WindowManager | ||
import androidx.annotation.ChecksSdkIntAtLeast | ||
|
||
import io.flutter.embedding.engine.plugins.FlutterPlugin | ||
import io.flutter.embedding.engine.plugins.activity.ActivityAware | ||
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding | ||
import io.flutter.plugin.common.BinaryMessenger | ||
import io.flutter.plugin.common.EventChannel | ||
import io.flutter.plugin.common.MethodCall | ||
import io.flutter.plugin.common.MethodChannel | ||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler | ||
|
||
private const val GYROSCOPE_CHANNEL_NAME = "com.simform.flutter_credit_card/gyroscope" | ||
private const val METHOD_CHANNEL_NAME = "com.simform.flutter_credit_card" | ||
|
||
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.R) | ||
private val isAtLeastOsR: Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R | ||
|
||
/** FlutterCreditCardPlugin */ | ||
class FlutterCreditCardPlugin: FlutterPlugin { | ||
private lateinit var gyroscopeChannel: EventChannel | ||
|
||
private lateinit var methodChannel: MethodChannel | ||
|
||
private lateinit var gyroScopeStreamHandler: StreamHandlerImpl | ||
|
||
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { | ||
setupEventChannels(binding.applicationContext, binding.binaryMessenger) | ||
setupMethodChannel(binding.applicationContext, binding.binaryMessenger) | ||
} | ||
|
||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { | ||
teardownEventChannels() | ||
teardownMethodChannel() | ||
} | ||
|
||
private fun setupEventChannels(context: Context, messenger: BinaryMessenger) { | ||
val sensorsManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager | ||
|
||
gyroscopeChannel = EventChannel(messenger, GYROSCOPE_CHANNEL_NAME) | ||
gyroScopeStreamHandler = com.simform.flutter_credit_card.StreamHandlerImpl( | ||
sensorsManager, | ||
Sensor.TYPE_GYROSCOPE, | ||
) | ||
gyroscopeChannel.setStreamHandler(gyroScopeStreamHandler) | ||
} | ||
|
||
private fun teardownEventChannels() { | ||
gyroscopeChannel.setStreamHandler(null) | ||
gyroScopeStreamHandler.onCancel(null) | ||
} | ||
|
||
private fun setupMethodChannel(context: Context, messenger: BinaryMessenger) { | ||
methodChannel = MethodChannel(messenger, METHOD_CHANNEL_NAME) | ||
methodChannel.setMethodCallHandler { call, result -> | ||
if (call.method == "isGyroscopeAvailable") { | ||
val packageManager: PackageManager = context.packageManager | ||
val gyroExists = | ||
packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_GYROSCOPE) | ||
result.success(gyroExists) | ||
} | ||
class FlutterCreditCardPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { | ||
private var activity: Activity? = null | ||
private lateinit var context: Context | ||
|
||
private lateinit var gyroscopeChannel: EventChannel | ||
private lateinit var methodChannel: MethodChannel | ||
private lateinit var gyroscopeStreamHandler: GyroscopeStreamHandler | ||
|
||
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { | ||
context = binding.applicationContext | ||
initializeChannels(binding.binaryMessenger) | ||
} | ||
} | ||
|
||
private fun teardownMethodChannel() { | ||
methodChannel.setMethodCallHandler(null) | ||
} | ||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) = | ||
disposeChannels() | ||
|
||
override fun onAttachedToActivity(binding: ActivityPluginBinding) { | ||
activity = if (isAtLeastOsR) binding.activity else null | ||
} | ||
|
||
override fun onDetachedFromActivity() { | ||
activity = null | ||
} | ||
|
||
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { | ||
activity = if (isAtLeastOsR) binding.activity else null | ||
} | ||
|
||
override fun onDetachedFromActivityForConfigChanges() { | ||
activity = null | ||
} | ||
|
||
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { | ||
when (call.method) { | ||
"initiateEvents" -> { | ||
gyroEventChannelSetup() | ||
result.success(null) | ||
} | ||
|
||
"isGyroscopeAvailable" -> result.success(hasGyroAvailability()) | ||
|
||
"cancelEvents" -> { | ||
gyroEventChannelDisposal() | ||
result.success(null) | ||
} | ||
|
||
else -> result.notImplemented() | ||
} | ||
} | ||
|
||
private fun initializeChannels(messenger: BinaryMessenger) { | ||
gyroscopeChannel = EventChannel(messenger, GYROSCOPE_CHANNEL_NAME) | ||
gyroEventChannelSetup() | ||
methodChannel = MethodChannel(messenger, METHOD_CHANNEL_NAME) | ||
methodChannel.setMethodCallHandler(this) | ||
} | ||
|
||
private fun gyroEventChannelSetup() { | ||
if (!hasGyroAvailability()) return | ||
|
||
val sensorsManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager | ||
|
||
gyroscopeStreamHandler = GyroscopeStreamHandler(getViewDisplay(), sensorsManager) | ||
gyroscopeChannel.setStreamHandler(gyroscopeStreamHandler) | ||
} | ||
|
||
private fun gyroEventChannelDisposal() { | ||
gyroscopeStreamHandler.onCancel(null) | ||
gyroscopeChannel.setStreamHandler(null) | ||
} | ||
|
||
private fun disposeChannels() { | ||
gyroEventChannelDisposal() | ||
methodChannel.setMethodCallHandler(null) | ||
activity = null | ||
} | ||
|
||
private fun getViewDisplay(): Display? { | ||
if (isAtLeastOsR) { | ||
return activity?.display | ||
} else { | ||
@Suppress("DEPRECATION") | ||
return (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay | ||
} | ||
} | ||
|
||
companion object { | ||
private const val GYROSCOPE_CHANNEL_NAME = "com.simform.flutter_credit_card/gyroscope" | ||
private const val METHOD_CHANNEL_NAME = "com.simform.flutter_credit_card" | ||
private const val DEFAULT_UPDATE_INTERVAL = SensorManager.SENSOR_DELAY_NORMAL | ||
} | ||
private fun hasGyroAvailability(): Boolean = | ||
context.packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_GYROSCOPE) | ||
} |
73 changes: 73 additions & 0 deletions
73
android/src/main/kotlin/com/simform/flutter_credit_card/GyroscopeStreamHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.simform.flutter_credit_card | ||
|
||
import android.hardware.Sensor | ||
import android.hardware.SensorEvent | ||
import android.hardware.SensorEventListener | ||
import android.hardware.SensorManager | ||
import android.view.Display | ||
import android.view.Surface | ||
import io.flutter.plugin.common.EventChannel | ||
import io.flutter.plugin.common.EventChannel.EventSink | ||
|
||
internal class GyroscopeStreamHandler( | ||
private val display: Display?, | ||
private val sensorManager: SensorManager, | ||
) : EventChannel.StreamHandler { | ||
private var sensorEventListener: SensorEventListener? = null | ||
|
||
private val sensor: Sensor by lazy { | ||
sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) | ||
} | ||
|
||
override fun onListen(arguments: Any?, events: EventSink) { | ||
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) | ||
|
||
private fun createSensorEventListener(events: EventSink): SensorEventListener { | ||
return object : SensorEventListener { | ||
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {} | ||
|
||
override fun onSensorChanged(event: SensorEvent) = | ||
events.success(processForOrientation(event.values)) | ||
} | ||
} | ||
|
||
private fun processForOrientation(values: FloatArray): DoubleArray { | ||
if (display == null) { | ||
return values.map { it.toDouble() }.toDoubleArray() | ||
} else { | ||
val arr = DoubleArray(3) | ||
|
||
val x = values[0].toDouble() | ||
val y = values[1].toDouble() | ||
val z = values[2].toDouble() | ||
|
||
when (display.rotation) { | ||
Surface.ROTATION_0, | ||
Surface.ROTATION_180 -> { | ||
arr[0] = x | ||
arr[1] = y | ||
arr[2] = z | ||
} | ||
|
||
Surface.ROTATION_270 -> { | ||
arr[0] = y | ||
arr[1] = -x | ||
arr[2] = z | ||
} | ||
|
||
Surface.ROTATION_90 -> { | ||
arr[0] = -y | ||
arr[1] = x | ||
arr[2] = z | ||
} | ||
} | ||
|
||
return arr | ||
} | ||
} | ||
} |
42 changes: 0 additions & 42 deletions
42
android/src/main/kotlin/com/simform/flutter_credit_card/StreamHandlerImpl.kt
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
android/src/test/kotlin/com/simform/flutter_credit_card/FlutterCreditCardPluginTest.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.