diff --git a/example/aurora/main.cpp b/example/aurora/main.cpp index 6677776..383acc5 100644 --- a/example/aurora/main.cpp +++ b/example/aurora/main.cpp @@ -3,10 +3,12 @@ * SPDX-License-Identifier: BSD-3-Clause */ #include +#include #include "generated_plugin_registrant.h" int main(int argc, char *argv[]) { Application::Initialize(argc, argv); + EnableQtCompatibility(); RegisterPlugins(); Application::Launch(); return 0; diff --git a/example/lib/packages/sensors_plus/page.dart b/example/lib/packages/sensors_plus/page.dart index 8a1c6ee..427bb33 100644 --- a/example/lib/packages/sensors_plus/page.dart +++ b/example/lib/packages/sensors_plus/page.dart @@ -8,6 +8,7 @@ import 'package:flutter_example_packages/widgets/blocks/block_info_package.dart' import 'package:flutter_example_packages/widgets/layouts/block_layout.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:sensors_plus/sensors_plus.dart'; +import 'package:sensors_plus_aurora/orientation_event.dart'; import 'package:sensors_plus_aurora/sensors_plus_aurora.dart'; import 'model.dart'; @@ -25,22 +26,42 @@ class SensorsPlusPage extends AppStatefulWidget { } class _SensorsPlusPageState extends AppState { + OrientationEvent orientation = OrientationEvent.undefined; + AccelerometerEvent accelerometer = AccelerometerEvent(0, 0, 0); @override void initState() { super.initState(); - /// Listen change - // accelerometerEvents.listen((AccelerometerEvent event) { - // debugPrint(event.toString()); - // }, - // onError: (error) { - // debugPrint(error.toString()); - // }, - // cancelOnError: true, - // ); + /// Accelerometer change + accelerometerEvents.listen( + (AccelerometerEvent event) { + if (mounted) { + setState(() { + accelerometer = event; + }); + } + }, + onError: (error) { + debugPrint(error.toString()); + }, + cancelOnError: true, + ); - SensorsPlusAurora().listenSensor().then((value) => debugPrint('yes')); + /// Only Aurora OS orientation change + orientationEvents?.listen( + (OrientationEvent event) { + if (mounted) { + setState(() { + orientation = event; + }); + } + }, + onError: (error) { + debugPrint(error.toString()); + }, + cancelOnError: true, + ); } @override @@ -60,9 +81,13 @@ class _SensorsPlusPageState extends AppState { crossAxisAlignment: CrossAxisAlignment.start, children: [ BlockInfoPackage(widget.package), - const Center( - child: Text('YES'), - ) + Center( + child: Text('Orientation: $orientation'), + ), + const SizedBox(height: 20,), + Center( + child: Text('Accelerometer: $accelerometer'), + ), ], ), ), diff --git a/example/pubspec.lock b/example/pubspec.lock index a27313f..e32084a 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -683,7 +683,7 @@ packages: name: path_provider_android url: "https://pub.dartlang.org" source: hosted - version: "2.0.27" + version: "2.1.0" path_provider_aurora: dependency: "direct main" description: @@ -699,14 +699,14 @@ packages: name: path_provider_foundation url: "https://pub.dartlang.org" source: hosted - version: "2.2.4" + version: "2.3.0" path_provider_linux: dependency: transitive description: name: path_provider_linux url: "https://pub.dartlang.org" source: hosted - version: "2.1.11" + version: "2.2.0" path_provider_platform_interface: dependency: transitive description: @@ -720,7 +720,7 @@ packages: name: path_provider_windows url: "https://pub.dartlang.org" source: hosted - version: "2.1.7" + version: "2.2.0" petitparser: dependency: transitive description: diff --git a/packages/sensors_plus/sensors_plus_aurora/aurora/include/sensors_plus_aurora/sensors_plus_aurora_plugin.h b/packages/sensors_plus/sensors_plus_aurora/aurora/include/sensors_plus_aurora/sensors_plus_aurora_plugin.h index 0403b1c..2e121ab 100644 --- a/packages/sensors_plus/sensors_plus_aurora/aurora/include/sensors_plus_aurora/sensors_plus_aurora_plugin.h +++ b/packages/sensors_plus/sensors_plus_aurora/aurora/include/sensors_plus_aurora/sensors_plus_aurora_plugin.h @@ -13,6 +13,7 @@ #include #include +#include class PLUGIN_EXPORT SensorsPlusAuroraPlugin final : public QObject @@ -24,14 +25,18 @@ public: void RegisterWithRegistrar(PluginRegistrar ®istrar) override; public slots: - void receivedData(const Unsigned &data); + void eventSensorOrientation(const Unsigned &data); + void eventSensorAccelerometer(const XYZ& data); private: void onMethodCall(const MethodCall &call); - void onListenSensor(const MethodCall &call); + void onListenSensorOrientation(); + void onListenSensorAccelerometer(); void unimplemented(const MethodCall &call); private: + bool m_isSendSensorOrientation = false; + bool m_isSendSensorAccelerometer = false; AbstractSensorChannelInterface *m_iface = nullptr; }; diff --git a/packages/sensors_plus/sensors_plus_aurora/aurora/sensors_plus_aurora_plugin.cpp b/packages/sensors_plus/sensors_plus_aurora/aurora/sensors_plus_aurora_plugin.cpp index 89e7d23..8aced7e 100644 --- a/packages/sensors_plus/sensors_plus_aurora/aurora/sensors_plus_aurora_plugin.cpp +++ b/packages/sensors_plus/sensors_plus_aurora/aurora/sensors_plus_aurora_plugin.cpp @@ -9,8 +9,10 @@ #include #include +#include -static AbstractSensorChannelInterface *iface; +static AbstractSensorChannelInterface *ifaceOrientation; +static AbstractSensorChannelInterface *ifaceAccelerometer; namespace { @@ -23,6 +25,36 @@ void SensorsPlusAuroraPlugin::RegisterWithRegistrar(PluginRegistrar ®istrar) registrar.RegisterMethodChannel("sensors_plus_aurora", MethodCodecType::Standard, [this](const MethodCall &call) { this->onMethodCall(call); }); + + registrar.RegisterEventChannel( + "sensors_aurora_orientation", + MethodCodecType::Standard, + [this](const Encodable &) + { + this->m_isSendSensorOrientation = true; + onListenSensorOrientation(); + return EventResponse(); + }, + [this](const Encodable &) + { + this->m_isSendSensorOrientation = false; + return EventResponse(); + }); + + registrar.RegisterEventChannel( + "sensors_aurora_accelerometer", + MethodCodecType::Standard, + [this](const Encodable &) + { + this->m_isSendSensorAccelerometer = true; + onListenSensorAccelerometer(); + return EventResponse(); + }, + [this](const Encodable &) + { + this->m_isSendSensorAccelerometer = false; + return EventResponse(); + }); } void SensorsPlusAuroraPlugin::onMethodCall(const MethodCall &call) @@ -30,17 +62,16 @@ void SensorsPlusAuroraPlugin::onMethodCall(const MethodCall &call) const auto &method = call.GetMethod(); if (method == "listenSensor") { - onListenSensor(call); + // onListenSensor(); return; } unimplemented(call); } -void SensorsPlusAuroraPlugin::onListenSensor(const MethodCall &call) +void SensorsPlusAuroraPlugin::onListenSensorOrientation() { - if (iface != nullptr) { - unimplemented(call); + if (ifaceOrientation != nullptr) { return; } @@ -58,28 +89,80 @@ void SensorsPlusAuroraPlugin::onListenSensor(const MethodCall &call) sm.registerSensorInterface("orientationsensor"); - iface = OrientationSensorChannelInterface::interface("orientationsensor"); - qDebug() << "iface -> " << iface->thread(); + ifaceOrientation = OrientationSensorChannelInterface::interface("orientationsensor"); - if (!iface) { + if (!ifaceOrientation) { qDebug() << "Cannot get OrientationSensorChannelInterface"; exit(0); } - connect(iface, SIGNAL(orientationChanged(const Unsigned&)), - this, SLOT(receivedData(const Unsigned&))); + connect(ifaceOrientation, + SIGNAL(orientationChanged(const Unsigned&)), + this, + SLOT(eventSensorOrientation(const Unsigned&)) + ); - qDebug() << "Created sensor: " << iface->description(); - iface->start(); + ifaceOrientation->start(); +} - qDebug() << "Received data start"; +void SensorsPlusAuroraPlugin::eventSensorOrientation(const Unsigned &data) +{ + if (this->m_isSendSensorOrientation) + { + EventChannel("sensors_aurora_orientation", MethodCodecType::Standard) + .SendEvent(data.x()); + } +} - unimplemented(call); +void SensorsPlusAuroraPlugin::onListenSensorAccelerometer() +{ + if (ifaceAccelerometer != nullptr) { + return; + } + + SensorManagerInterface &sm = SensorManagerInterface::instance(); + if (!sm.isValid()) { + qDebug() << "Failed to create SensorManagerInterface"; + exit(0); + } + + QDBusReply reply(sm.loadPlugin("accelerometersensor")); + if (!reply.isValid() || !reply.value()) { + qDebug() << "Failed to load plugin"; + exit(0); + } + + sm.registerSensorInterface("accelerometersensor"); + + ifaceAccelerometer = AccelerometerSensorChannelInterface::interface("accelerometersensor"); + + if (!ifaceAccelerometer) { + qDebug() << "Cannot get AccelerometerSensorChannelInterface"; + exit(0); + } + + connect(ifaceAccelerometer, + SIGNAL(dataAvailable(const XYZ&)), + this, + SLOT(eventSensorAccelerometer(const XYZ&)) + ); + + ifaceAccelerometer->start(); } -void SensorsPlusAuroraPlugin::receivedData(const Unsigned &data) +void SensorsPlusAuroraPlugin::eventSensorAccelerometer(const XYZ& data) { - qDebug() << "receivedData: " << data.x(); + if (this->m_isSendSensorAccelerometer) + { + std::vector result; + + result.push_back(data.x()); + result.push_back(data.y()); + result.push_back(data.z()); + + EventChannel("sensors_aurora_accelerometer", MethodCodecType::Standard) + .SendEvent(result); + } } void SensorsPlusAuroraPlugin::unimplemented(const MethodCall &call) diff --git a/packages/sensors_plus/sensors_plus_aurora/lib/orientation_event.dart b/packages/sensors_plus/sensors_plus_aurora/lib/orientation_event.dart new file mode 100644 index 0000000..8ba764f --- /dev/null +++ b/packages/sensors_plus/sensors_plus_aurora/lib/orientation_event.dart @@ -0,0 +1,19 @@ +// SPDX-FileCopyrightText: Copyright 2023 Open Mobile Platform LLC +// SPDX-License-Identifier: BSD-3-Clause + +enum OrientationEvent { + /// The orientation is unknown. + undefined, + /// The Top edge of the device is pointing up. + topUp, + /// The Top edge of the device is pointing down. + topDown, + /// The Left edge of the device is pointing up. + leftUp, + /// The Right edge of the device is pointing up. + rightUp, + /// The Face of the device is pointing up. + faceUp, + /// The Face of the device is pointing down. + faceDown +} diff --git a/packages/sensors_plus/sensors_plus_aurora/lib/sensors_plus_aurora.dart b/packages/sensors_plus/sensors_plus_aurora/lib/sensors_plus_aurora.dart index 4c2003a..98dd33f 100644 --- a/packages/sensors_plus/sensors_plus_aurora/lib/sensors_plus_aurora.dart +++ b/packages/sensors_plus/sensors_plus_aurora/lib/sensors_plus_aurora.dart @@ -1,8 +1,29 @@ +// SPDX-FileCopyrightText: Copyright 2023 Open Mobile Platform LLC +// SPDX-License-Identifier: BSD-3-Clause + +import 'package:flutter/foundation.dart'; +import 'package:sensors_plus_aurora/orientation_event.dart'; +import 'package:sensors_plus_platform_interface/sensors_plus_platform_interface.dart'; import 'sensors_plus_aurora_platform_interface.dart'; -class SensorsPlusAurora { - Future listenSensor() { - return SensorsPlusAuroraPlatform.instance.listenSensor(); +/// A broadcast stream of events from the Aurora OS device orientation. +Stream? get orientationEvents { + if (TargetPlatform.aurora == defaultTargetPlatform) { + return SensorsPlusAurora().onChangeOrientation; + } + return null; +} + +class SensorsPlusAurora extends SensorsPlatform { + static void registerWith() { + SensorsPlatform.instance = SensorsPlusAurora(); } + + Stream get onChangeOrientation => + SensorsPlusAuroraPlatform.instance.onChangeOrientation(); + + @override + Stream get accelerometerEvents => + SensorsPlusAuroraPlatform.instance.onChangeAccelerometer(); } diff --git a/packages/sensors_plus/sensors_plus_aurora/lib/sensors_plus_aurora_method_channel.dart b/packages/sensors_plus/sensors_plus_aurora/lib/sensors_plus_aurora_method_channel.dart index dbd3a52..c1cf24a 100644 --- a/packages/sensors_plus/sensors_plus_aurora/lib/sensors_plus_aurora_method_channel.dart +++ b/packages/sensors_plus/sensors_plus_aurora/lib/sensors_plus_aurora_method_channel.dart @@ -1,5 +1,10 @@ +// SPDX-FileCopyrightText: Copyright 2023 Open Mobile Platform LLC +// SPDX-License-Identifier: BSD-3-Clause + import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; +import 'package:sensors_plus_aurora/orientation_event.dart'; +import 'package:sensors_plus_platform_interface/sensors_plus_platform_interface.dart'; import 'sensors_plus_aurora_platform_interface.dart'; @@ -10,8 +15,45 @@ class MethodChannelSensorsPlusAurora extends SensorsPlusAuroraPlatform { final methodChannel = const MethodChannel('sensors_plus_aurora'); @override - Future listenSensor() async { - final version = await methodChannel.invokeMethod('listenSensor'); - return version; + Stream onChangeOrientation() async* { + await for (final event in const EventChannel('sensors_aurora_orientation') + .receiveBroadcastStream()) { + int result = (event as int?) ?? 0; + switch (result) { + case 1: + yield OrientationEvent.rightUp; + break; + case 2: + yield OrientationEvent.leftUp; + break; + case 3: + yield OrientationEvent.topDown; + break; + case 4: + yield OrientationEvent.topUp; + break; + case 5: + yield OrientationEvent.faceDown; + break; + case 6: + yield OrientationEvent.faceUp; + break; + default: + yield OrientationEvent.undefined; + } + } + } + + @override + Stream onChangeAccelerometer() async* { + await for (final event in const EventChannel('sensors_aurora_accelerometer') + .receiveBroadcastStream()) { + final list = (event as List); + yield AccelerometerEvent( + (list[0] as int?)?.toDouble() ?? 0, + (list[1] as int?)?.toDouble() ?? 0, + (list[2] as int?)?.toDouble() ?? 0, + ); + } } } diff --git a/packages/sensors_plus/sensors_plus_aurora/lib/sensors_plus_aurora_platform_interface.dart b/packages/sensors_plus/sensors_plus_aurora/lib/sensors_plus_aurora_platform_interface.dart index 728a052..9ccb897 100644 --- a/packages/sensors_plus/sensors_plus_aurora/lib/sensors_plus_aurora_platform_interface.dart +++ b/packages/sensors_plus/sensors_plus_aurora/lib/sensors_plus_aurora_platform_interface.dart @@ -1,4 +1,9 @@ +// SPDX-FileCopyrightText: Copyright 2023 Open Mobile Platform LLC +// SPDX-License-Identifier: BSD-3-Clause + import 'package:plugin_platform_interface/plugin_platform_interface.dart'; +import 'package:sensors_plus_aurora/orientation_event.dart'; +import 'package:sensors_plus_platform_interface/sensors_plus_platform_interface.dart'; import 'sensors_plus_aurora_method_channel.dart'; @@ -23,7 +28,11 @@ abstract class SensorsPlusAuroraPlatform extends PlatformInterface { _instance = instance; } - Future listenSensor() { - throw UnimplementedError('listenSensor() has not been implemented.'); + Stream onChangeOrientation() { + throw UnimplementedError('onChangeOrientation() has not been implemented.'); + } + + Stream onChangeAccelerometer() { + throw UnimplementedError('onChangeAccelerometer() has not been implemented.'); } } diff --git a/packages/sensors_plus/sensors_plus_aurora/pubspec.yaml b/packages/sensors_plus/sensors_plus_aurora/pubspec.yaml index f78c456..fad8b75 100644 --- a/packages/sensors_plus/sensors_plus_aurora/pubspec.yaml +++ b/packages/sensors_plus/sensors_plus_aurora/pubspec.yaml @@ -13,6 +13,7 @@ dependencies: flutter: sdk: flutter plugin_platform_interface: ^2.0.2 + sensors_plus_platform_interface: ^1.1.3 dev_dependencies: flutter_test: @@ -24,3 +25,4 @@ flutter: platforms: aurora: pluginClass: SensorsPlusAuroraPlugin + dartPluginClass: SensorsPlusAurora diff --git a/packages/sensors_plus/sensors_plus_aurora/test/sensors_plus_aurora_method_channel_test.dart b/packages/sensors_plus/sensors_plus_aurora/test/sensors_plus_aurora_method_channel_test.dart deleted file mode 100644 index 8b01cf9..0000000 --- a/packages/sensors_plus/sensors_plus_aurora/test/sensors_plus_aurora_method_channel_test.dart +++ /dev/null @@ -1,27 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Open Mobile Platform LLC -// SPDX-License-Identifier: BSD-3-Clause - -import 'package:flutter/services.dart'; -import 'package:flutter_test/flutter_test.dart'; -import 'package:sensors_plus_aurora/sensors_plus_aurora_method_channel.dart'; - -void main() { - MethodChannelSensorsPlusAurora platform = MethodChannelSensorsPlusAurora(); - const MethodChannel channel = MethodChannel('sensors_plus_aurora'); - - TestWidgetsFlutterBinding.ensureInitialized(); - - setUp(() { - channel.setMockMethodCallHandler((MethodCall methodCall) async { - return '42'; - }); - }); - - tearDown(() { - channel.setMockMethodCallHandler(null); - }); - - test('getPlatformVersion', () async { - expect(await platform.getPlatformVersion(), '42'); - }); -} diff --git a/packages/sensors_plus/sensors_plus_aurora/test/sensors_plus_aurora_test.dart b/packages/sensors_plus/sensors_plus_aurora/test/sensors_plus_aurora_test.dart deleted file mode 100644 index 59beaa1..0000000 --- a/packages/sensors_plus/sensors_plus_aurora/test/sensors_plus_aurora_test.dart +++ /dev/null @@ -1,32 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Open Mobile Platform LLC -// SPDX-License-Identifier: BSD-3-Clause - -import 'package:flutter_test/flutter_test.dart'; -import 'package:sensors_plus_aurora/sensors_plus_aurora.dart'; -import 'package:sensors_plus_aurora/sensors_plus_aurora_platform_interface.dart'; -import 'package:sensors_plus_aurora/sensors_plus_aurora_method_channel.dart'; -import 'package:plugin_platform_interface/plugin_platform_interface.dart'; - -class MockSensorsPlusAuroraPlatform - with MockPlatformInterfaceMixin - implements SensorsPlusAuroraPlatform { - - @override - Future getPlatformVersion() => Future.value('42'); -} - -void main() { - final SensorsPlusAuroraPlatform initialPlatform = SensorsPlusAuroraPlatform.instance; - - test('$MethodChannelSensorsPlusAurora is the default instance', () { - expect(initialPlatform, isInstanceOf()); - }); - - test('getPlatformVersion', () async { - SensorsPlusAurora sensorsPlusAuroraPlugin = SensorsPlusAurora(); - MockSensorsPlusAuroraPlatform fakePlatform = MockSensorsPlusAuroraPlatform(); - SensorsPlusAuroraPlatform.instance = fakePlatform; - - expect(await sensorsPlusAuroraPlugin.getPlatformVersion(), '42'); - }); -}