-
Notifications
You must be signed in to change notification settings - Fork 271
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
22c5403
commit 4f77665
Showing
32 changed files
with
1,072 additions
and
895 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
7 changes: 7 additions & 0 deletions
7
android/src/main/kotlin/com/simform/flutter_credit_card/Constants.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,7 @@ | ||
object Constants { | ||
const val GYROSCOPE_CHANNEL_NAME: String = "com.simform.flutter_credit_card/gyroscope" | ||
const val METHOD_CHANNEL_NAME: String = "com.simform.flutter_credit_card" | ||
const val INITIATE_EVENTS: String = "initiateEvents" | ||
const val IS_GYRO_AVAILABLE: String = "isGyroscopeAvailable" | ||
const val CANCEL_EVENTS: String = "cancelEvents" | ||
} |
95 changes: 45 additions & 50 deletions
95
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,63 @@ | ||
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 com.simform.flutter_credit_card.gyroscope.GyroscopeChannelImpl | ||
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.MethodChannel | ||
|
||
/** FlutterCreditCardPlugin */ | ||
class FlutterCreditCardPlugin: FlutterPlugin { | ||
private lateinit var gyroscopeChannel: EventChannel | ||
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.R) | ||
private val isAtLeastOsR: Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R | ||
|
||
private lateinit var methodChannel: MethodChannel | ||
/** FlutterCreditCardPlugin */ | ||
class FlutterCreditCardPlugin : FlutterPlugin, ActivityAware { | ||
private var gyroscopeChannel: GyroscopeChannelImpl? = null | ||
private var activity: Activity? = null | ||
|
||
private lateinit var gyroScopeStreamHandler: StreamHandlerImpl | ||
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) = | ||
initializeChannels(binding.applicationContext, binding.binaryMessenger) | ||
|
||
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { | ||
setupEventChannels(binding.applicationContext, binding.binaryMessenger) | ||
setupMethodChannel(binding.applicationContext, binding.binaryMessenger) | ||
} | ||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) = | ||
disposeChannels() | ||
|
||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { | ||
teardownEventChannels() | ||
teardownMethodChannel() | ||
} | ||
override fun onAttachedToActivity(binding: ActivityPluginBinding) { | ||
activity = if (isAtLeastOsR) binding.activity else null | ||
} | ||
|
||
private fun setupEventChannels(context: Context, messenger: BinaryMessenger) { | ||
val sensorsManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager | ||
override fun onDetachedFromActivity() { | ||
activity = null | ||
} | ||
|
||
gyroscopeChannel = EventChannel(messenger, GYROSCOPE_CHANNEL_NAME) | ||
gyroScopeStreamHandler = com.simform.flutter_credit_card.StreamHandlerImpl( | ||
sensorsManager, | ||
Sensor.TYPE_GYROSCOPE, | ||
) | ||
gyroscopeChannel.setStreamHandler(gyroScopeStreamHandler) | ||
} | ||
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { | ||
activity = if (isAtLeastOsR) binding.activity else null | ||
} | ||
|
||
private fun teardownEventChannels() { | ||
gyroscopeChannel.setStreamHandler(null) | ||
gyroScopeStreamHandler.onCancel(null) | ||
} | ||
override fun onDetachedFromActivityForConfigChanges() { | ||
activity = 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) | ||
} | ||
private fun initializeChannels(context: Context, messenger: BinaryMessenger) { | ||
gyroscopeChannel = GyroscopeChannelImpl(context, messenger) { getViewDisplay(context) } | ||
} | ||
} | ||
|
||
private fun teardownMethodChannel() { | ||
methodChannel.setMethodCallHandler(null) | ||
} | ||
private fun getViewDisplay(context: Context): 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 disposeChannels() { | ||
gyroscopeChannel?.dispose() | ||
gyroscopeChannel = null | ||
activity = null | ||
} | ||
} |
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.
74 changes: 74 additions & 0 deletions
74
android/src/main/kotlin/com/simform/flutter_credit_card/gyroscope/GyroscopeChannelImpl.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,74 @@ | ||
package com.simform.flutter_credit_card.gyroscope | ||
|
||
import Constants | ||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import android.hardware.SensorManager | ||
import android.view.Display | ||
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 | ||
|
||
internal class GyroscopeChannelImpl( | ||
private val context: Context, | ||
private val messenger: BinaryMessenger, | ||
private val getDisplay: () -> Display?, | ||
) : MethodCallHandler { | ||
private val methodChannel: MethodChannel | ||
private var eventChannel: EventChannel? = null | ||
private var streamHandler: GyroscopeStreamHandler? = null | ||
|
||
init { | ||
eventStreamSetup() | ||
methodChannel = MethodChannel(messenger, Constants.METHOD_CHANNEL_NAME) | ||
methodChannel.setMethodCallHandler(this) | ||
} | ||
|
||
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { | ||
when (call.method) { | ||
Constants.INITIATE_EVENTS -> { | ||
eventStreamSetup() | ||
result.success(null) | ||
} | ||
|
||
Constants.IS_GYRO_AVAILABLE -> result.success(hasGyroAvailability()) | ||
|
||
Constants.CANCEL_EVENTS -> { | ||
eventStreamDisposal() | ||
result.success(null) | ||
} | ||
|
||
else -> result.notImplemented() | ||
} | ||
} | ||
|
||
fun dispose() { | ||
eventStreamDisposal() | ||
methodChannel.setMethodCallHandler(null) | ||
} | ||
|
||
private fun eventStreamSetup() { | ||
if (!hasGyroAvailability()) return | ||
|
||
val sensorsManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager | ||
|
||
eventChannel = if (eventChannel == null) EventChannel( | ||
messenger, | ||
Constants.GYROSCOPE_CHANNEL_NAME, | ||
) else eventChannel | ||
|
||
streamHandler = GyroscopeStreamHandler(getDisplay(), sensorsManager) | ||
eventChannel!!.setStreamHandler(streamHandler) | ||
} | ||
|
||
private fun eventStreamDisposal() { | ||
streamHandler?.onCancel(null) | ||
eventChannel?.setStreamHandler(null) | ||
eventChannel = null | ||
} | ||
|
||
private fun hasGyroAvailability(): Boolean = | ||
context.packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_GYROSCOPE) | ||
} |
Oops, something went wrong.