Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sensors_plus): Configurable sample rate #2248

Merged
merged 7 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,78 @@ import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodChannel

/** SensorsPlugin */
class SensorsPlugin : FlutterPlugin {
private lateinit var methodChannel: MethodChannel

private lateinit var accelerometerChannel: EventChannel
private lateinit var userAccelChannel: EventChannel
private lateinit var gyroscopeChannel: EventChannel
private lateinit var magnetometerChannel: EventChannel

private lateinit var accelerationStreamHandler: StreamHandlerImpl
private lateinit var linearAccelerationStreamHandler: StreamHandlerImpl
private lateinit var gyroScopeStreamHandler: StreamHandlerImpl
private lateinit var accelerometerStreamHandler: StreamHandlerImpl
private lateinit var userAccelStreamHandler: StreamHandlerImpl
private lateinit var gyroscopeStreamHandler: StreamHandlerImpl
private lateinit var magnetometerStreamHandler: StreamHandlerImpl

override fun onAttachedToEngine(binding: FlutterPluginBinding) {
setupMethodChannel(binding.binaryMessenger)
setupEventChannels(binding.applicationContext, binding.binaryMessenger)
}

override fun onDetachedFromEngine(binding: FlutterPluginBinding) {
teardownMethodChannel()
teardownEventChannels()
}

private fun setupMethodChannel(messenger: BinaryMessenger) {
methodChannel = MethodChannel(messenger, METHOD_CHANNEL_NAME)
methodChannel.setMethodCallHandler { call, result ->
val streamHandler = when (call.method) {
"setAccelerationSamplingPeriod" -> accelerometerStreamHandler
"setUserAccelerometerSamplingPeriod" -> userAccelStreamHandler
"setGyroscopeSamplingPeriod" -> gyroscopeStreamHandler
"setMagnetometerSamplingPeriod" -> magnetometerStreamHandler
else -> null
}
streamHandler?.samplingPeriod = call.arguments as Int
if (streamHandler != null) {
result.success(null)
} else {
result.notImplemented()
}
}
}

private fun teardownMethodChannel() {
methodChannel.setMethodCallHandler(null)
}

private fun setupEventChannels(context: Context, messenger: BinaryMessenger) {
val sensorsManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager

accelerometerChannel = EventChannel(messenger, ACCELEROMETER_CHANNEL_NAME)
accelerationStreamHandler = StreamHandlerImpl(
accelerometerStreamHandler = StreamHandlerImpl(
sensorsManager,
Sensor.TYPE_ACCELEROMETER
)
accelerometerChannel.setStreamHandler(accelerationStreamHandler)
accelerometerChannel.setStreamHandler(accelerometerStreamHandler)

userAccelChannel = EventChannel(messenger, USER_ACCELEROMETER_CHANNEL_NAME)
linearAccelerationStreamHandler = StreamHandlerImpl(
userAccelStreamHandler = StreamHandlerImpl(
sensorsManager,
Sensor.TYPE_LINEAR_ACCELERATION
)
userAccelChannel.setStreamHandler(linearAccelerationStreamHandler)
userAccelChannel.setStreamHandler(userAccelStreamHandler)

gyroscopeChannel = EventChannel(messenger, GYROSCOPE_CHANNEL_NAME)
gyroScopeStreamHandler = StreamHandlerImpl(
gyroscopeStreamHandler = StreamHandlerImpl(
sensorsManager,
Sensor.TYPE_GYROSCOPE
)
gyroscopeChannel.setStreamHandler(gyroScopeStreamHandler)
gyroscopeChannel.setStreamHandler(gyroscopeStreamHandler)

magnetometerChannel = EventChannel(messenger, MAGNETOMETER_CHANNEL_NAME)
magnetometerStreamHandler = StreamHandlerImpl(
Expand All @@ -66,16 +94,19 @@ class SensorsPlugin : FlutterPlugin {
gyroscopeChannel.setStreamHandler(null)
magnetometerChannel.setStreamHandler(null)

accelerationStreamHandler.onCancel(null)
linearAccelerationStreamHandler.onCancel(null)
gyroScopeStreamHandler.onCancel(null)
accelerometerStreamHandler.onCancel(null)
userAccelStreamHandler.onCancel(null)
gyroscopeStreamHandler.onCancel(null)
magnetometerStreamHandler.onCancel(null)
}

companion object {
private const val METHOD_CHANNEL_NAME =
"dev.fluttercommunity.plus/sensors/method"
private const val ACCELEROMETER_CHANNEL_NAME =
"dev.fluttercommunity.plus/sensors/accelerometer"
private const val GYROSCOPE_CHANNEL_NAME = "dev.fluttercommunity.plus/sensors/gyroscope"
private const val GYROSCOPE_CHANNEL_NAME =
"dev.fluttercommunity.plus/sensors/gyroscope"
private const val USER_ACCELEROMETER_CHANNEL_NAME =
"dev.fluttercommunity.plus/sensors/user_accel"
private const val MAGNETOMETER_CHANNEL_NAME =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ internal class StreamHandlerImpl(

private var sensor: Sensor? = null

var samplingPeriod = 200000
set(value) {
field = value
updateRegistration()
}

override fun onListen(arguments: Any?, events: EventSink) {
sensor = sensorManager.getDefaultSensor(sensorType)
if (sensor != null) {
sensorEventListener = createSensorEventListener(events)
sensorManager.registerListener(
sensorEventListener,
sensor,
SensorManager.SENSOR_DELAY_NORMAL
)
sensorManager.registerListener(sensorEventListener, sensor, samplingPeriod)
} else {
events.error(
"NO_SENSOR",
Expand All @@ -36,6 +38,14 @@ internal class StreamHandlerImpl(
override fun onCancel(arguments: Any?) {
if (sensor != null) {
sensorManager.unregisterListener(sensorEventListener)
sensorEventListener = null
}
}

private fun updateRegistration() {
if (sensorEventListener != null) {
sensorManager.unregisterListener(sensorEventListener)
sensorManager.registerListener(sensorEventListener, sensor, samplingPeriod)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ void main() {
(WidgetTester tester) async {
final completer = Completer<AccelerometerEvent>();
late StreamSubscription<AccelerometerEvent> subscription;
subscription = accelerometerEvents.listen((AccelerometerEvent event) {
subscription =
accelerometerEventStream().listen((AccelerometerEvent event) {
completer.complete(event);
subscription.cancel();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
18B7D72A2ABD9B4F006A0D55 /* Recovered References */ = {
isa = PBXGroup;
children = (
);
name = "Recovered References";
sourceTree = "<group>";
};
331C8082294A63A400263BE5 /* RunnerTests */ = {
isa = PBXGroup;
children = (
Expand All @@ -105,7 +112,6 @@
178BE122EE4A7CE24B05D8AC /* Pods-RunnerTests.release.xcconfig */,
101AB93329D02192F269FF15 /* Pods-RunnerTests.profile.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
Expand All @@ -129,6 +135,7 @@
331C8082294A63A400263BE5 /* RunnerTests */,
9643F7DAEAB1D555F9F2E496 /* Pods */,
E9DC5051924EC6D7963378C9 /* Frameworks */,
18B7D72A2ABD9B4F006A0D55 /* Recovered References */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -215,7 +222,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1300;
LastUpgradeCheck = 1430;
ORGANIZATIONNAME = "";
TargetAttributes = {
331C8080294A63A400263BE5 = {
Expand Down Expand Up @@ -452,6 +459,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
INFOPLIST_KEY_NSMotionUsageDescription = "";
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
Expand All @@ -470,6 +478,7 @@
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -490,6 +499,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = io.flutter.plugins.sensorsplus.example.RunnerTests;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -508,6 +518,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = io.flutter.plugins.sensorsplus.example.RunnerTests;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -524,6 +535,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = io.flutter.plugins.sensorsplus.example.RunnerTests;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down Expand Up @@ -579,6 +591,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
INFOPLIST_KEY_NSMotionUsageDescription = "";
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -628,6 +641,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
INFOPLIST_KEY_NSMotionUsageDescription = "";
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
Expand All @@ -648,6 +662,7 @@
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -670,6 +685,7 @@
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Loading
Loading