From daab043928cd42c26abee31cb972d4adb140ad6b Mon Sep 17 00:00:00 2001 From: Boram Bae Date: Tue, 1 Dec 2020 14:35:40 +0900 Subject: [PATCH 1/3] [sensors] Add sensors_tizen package --- packages/sensors/.gitignore | 7 + packages/sensors/CHANGELOG.md | 3 + packages/sensors/LICENSE | 26 ++ packages/sensors/README.md | 27 +++ packages/sensors/example/.gitignore | 41 ++++ packages/sensors/example/README.md | 7 + packages/sensors/example/lib/main.dart | 144 +++++++++++ packages/sensors/example/lib/snake.dart | 130 ++++++++++ packages/sensors/example/pubspec.yaml | 24 ++ .../test_driver/test/integration_test.dart | 17 ++ packages/sensors/example/tizen/.gitignore | 5 + packages/sensors/example/tizen/App.cs | 20 ++ packages/sensors/example/tizen/NuGet.Config | 7 + packages/sensors/example/tizen/Runner.csproj | 26 ++ .../example/tizen/shared/res/ic_launcher.png | Bin 0 -> 1443 bytes .../sensors/example/tizen/tizen-manifest.xml | 10 + .../integration_test/sensors_test.dart | 24 ++ packages/sensors/pubspec.yaml | 29 +++ packages/sensors/tizen/.gitignore | 5 + packages/sensors/tizen/inc/sensors_plugin.h | 23 ++ packages/sensors/tizen/project_def.prop | 30 +++ packages/sensors/tizen/src/log.h | 20 ++ packages/sensors/tizen/src/sensors_plugin.cc | 229 ++++++++++++++++++ 23 files changed, 854 insertions(+) create mode 100644 packages/sensors/.gitignore create mode 100644 packages/sensors/CHANGELOG.md create mode 100644 packages/sensors/LICENSE create mode 100644 packages/sensors/README.md create mode 100644 packages/sensors/example/.gitignore create mode 100644 packages/sensors/example/README.md create mode 100644 packages/sensors/example/lib/main.dart create mode 100644 packages/sensors/example/lib/snake.dart create mode 100644 packages/sensors/example/pubspec.yaml create mode 100644 packages/sensors/example/test_driver/test/integration_test.dart create mode 100644 packages/sensors/example/tizen/.gitignore create mode 100644 packages/sensors/example/tizen/App.cs create mode 100644 packages/sensors/example/tizen/NuGet.Config create mode 100644 packages/sensors/example/tizen/Runner.csproj create mode 100644 packages/sensors/example/tizen/shared/res/ic_launcher.png create mode 100644 packages/sensors/example/tizen/tizen-manifest.xml create mode 100644 packages/sensors/integration_test/sensors_test.dart create mode 100644 packages/sensors/pubspec.yaml create mode 100644 packages/sensors/tizen/.gitignore create mode 100644 packages/sensors/tizen/inc/sensors_plugin.h create mode 100644 packages/sensors/tizen/project_def.prop create mode 100644 packages/sensors/tizen/src/log.h create mode 100644 packages/sensors/tizen/src/sensors_plugin.cc diff --git a/packages/sensors/.gitignore b/packages/sensors/.gitignore new file mode 100644 index 000000000..e9dc58d3d --- /dev/null +++ b/packages/sensors/.gitignore @@ -0,0 +1,7 @@ +.DS_Store +.dart_tool/ + +.packages +.pub/ + +build/ diff --git a/packages/sensors/CHANGELOG.md b/packages/sensors/CHANGELOG.md new file mode 100644 index 000000000..0d8803f93 --- /dev/null +++ b/packages/sensors/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +* Initial release. diff --git a/packages/sensors/LICENSE b/packages/sensors/LICENSE new file mode 100644 index 000000000..4e5cfe14e --- /dev/null +++ b/packages/sensors/LICENSE @@ -0,0 +1,26 @@ +Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved. +Copyright (c) 2017 The Chromium Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the names of the copyright holders nor the names of the + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/packages/sensors/README.md b/packages/sensors/README.md new file mode 100644 index 000000000..4894ad9a4 --- /dev/null +++ b/packages/sensors/README.md @@ -0,0 +1,27 @@ +# sensors_tizen + +The Tizen implementation of [`sensors`](https://github.com/flutter/plugins/tree/master/packages/sensors). + +## Usage + +This package is not an _endorsed_ implementation of `sensors`. Therefore, you have to include `sensors_tizen` alongside `sensors` as dependencies in your `pubspec.yaml` file. + +```yaml +dependencies: + sensors: ^0.4.0 + sensors_tizen: ^1.0.0 +``` + +Then you can import `sensors` in your Dart code: + +```dart +import 'package:sensors/sensors.dart'; +``` + +For detailed usage, see https://github.com/flutter/plugins/tree/master/packages/sensors#usage. + +## Supported devices + +This plugin is available for use only on these types of devices: + +- Galaxy Watch (running Tizen 5.5 or later) diff --git a/packages/sensors/example/.gitignore b/packages/sensors/example/.gitignore new file mode 100644 index 000000000..9d532b18a --- /dev/null +++ b/packages/sensors/example/.gitignore @@ -0,0 +1,41 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +**/doc/api/ +**/ios/Flutter/.last_build_id +.dart_tool/ +.flutter-plugins +.flutter-plugins-dependencies +.packages +.pub-cache/ +.pub/ +/build/ + +# Web related +lib/generated_plugin_registrant.dart + +# Symbolication related +app.*.symbols + +# Obfuscation related +app.*.map.json diff --git a/packages/sensors/example/README.md b/packages/sensors/example/README.md new file mode 100644 index 000000000..b58d0ec33 --- /dev/null +++ b/packages/sensors/example/README.md @@ -0,0 +1,7 @@ +# sensors_example + +Demonstrates how to use the sensors plugin. + +## Getting Started + +To run this app on your Tizen device, use [flutter-tizen](https://github.com/flutter-tizen/flutter-tizen). diff --git a/packages/sensors/example/lib/main.dart b/packages/sensors/example/lib/main.dart new file mode 100644 index 000000000..575e04937 --- /dev/null +++ b/packages/sensors/example/lib/main.dart @@ -0,0 +1,144 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// ignore_for_file: public_member_api_docs + +import 'dart:async'; +import 'package:flutter/material.dart'; +import 'package:sensors/sensors.dart'; + +import 'snake.dart'; + +void main() { + runApp(MyApp()); +} + +class MyApp extends StatelessWidget { + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'Sensors Demo', + theme: ThemeData( + primarySwatch: Colors.blue, + ), + home: MyHomePage(title: 'Flutter Demo Home Page'), + ); + } +} + +class MyHomePage extends StatefulWidget { + MyHomePage({Key key, this.title}) : super(key: key); + + final String title; + + @override + _MyHomePageState createState() => _MyHomePageState(); +} + +class _MyHomePageState extends State { + static const int _snakeRows = 20; + static const int _snakeColumns = 20; + static const double _snakeCellSize = 10.0; + + List _accelerometerValues; + List _userAccelerometerValues; + List _gyroscopeValues; + List> _streamSubscriptions = + >[]; + + @override + Widget build(BuildContext context) { + final List accelerometer = + _accelerometerValues?.map((double v) => v.toStringAsFixed(1))?.toList(); + final List gyroscope = + _gyroscopeValues?.map((double v) => v.toStringAsFixed(1))?.toList(); + final List userAccelerometer = _userAccelerometerValues + ?.map((double v) => v.toStringAsFixed(1)) + ?.toList(); + + return Scaffold( + appBar: AppBar( + title: const Text('Sensor Example'), + ), + body: Column( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + Center( + child: DecoratedBox( + decoration: BoxDecoration( + border: Border.all(width: 1.0, color: Colors.black38), + ), + child: SizedBox( + height: _snakeRows * _snakeCellSize, + width: _snakeColumns * _snakeCellSize, + child: Snake( + rows: _snakeRows, + columns: _snakeColumns, + cellSize: _snakeCellSize, + ), + ), + ), + ), + Padding( + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text('Accelerometer: $accelerometer'), + ], + ), + padding: const EdgeInsets.all(16.0), + ), + Padding( + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text('UserAccelerometer: $userAccelerometer'), + ], + ), + padding: const EdgeInsets.all(16.0), + ), + Padding( + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text('Gyroscope: $gyroscope'), + ], + ), + padding: const EdgeInsets.all(16.0), + ), + ], + ), + ); + } + + @override + void dispose() { + super.dispose(); + for (StreamSubscription subscription in _streamSubscriptions) { + subscription.cancel(); + } + } + + @override + void initState() { + super.initState(); + _streamSubscriptions + .add(accelerometerEvents.listen((AccelerometerEvent event) { + setState(() { + _accelerometerValues = [event.x, event.y, event.z]; + }); + })); + _streamSubscriptions.add(gyroscopeEvents.listen((GyroscopeEvent event) { + setState(() { + _gyroscopeValues = [event.x, event.y, event.z]; + }); + })); + _streamSubscriptions + .add(userAccelerometerEvents.listen((UserAccelerometerEvent event) { + setState(() { + _userAccelerometerValues = [event.x, event.y, event.z]; + }); + })); + } +} diff --git a/packages/sensors/example/lib/snake.dart b/packages/sensors/example/lib/snake.dart new file mode 100644 index 000000000..d6b2f9b48 --- /dev/null +++ b/packages/sensors/example/lib/snake.dart @@ -0,0 +1,130 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// ignore_for_file: public_member_api_docs + +import 'dart:async'; +import 'dart:math' as math; + +import 'package:flutter/material.dart'; +import 'package:sensors/sensors.dart'; + +class Snake extends StatefulWidget { + Snake({this.rows = 20, this.columns = 20, this.cellSize = 10.0}) { + assert(10 <= rows); + assert(10 <= columns); + assert(5.0 <= cellSize); + } + + final int rows; + final int columns; + final double cellSize; + + @override + State createState() => SnakeState(rows, columns, cellSize); +} + +class SnakeBoardPainter extends CustomPainter { + SnakeBoardPainter(this.state, this.cellSize); + + GameState state; + double cellSize; + + @override + void paint(Canvas canvas, Size size) { + final Paint blackLine = Paint()..color = Colors.black; + final Paint blackFilled = Paint() + ..color = Colors.black + ..style = PaintingStyle.fill; + canvas.drawRect( + Rect.fromPoints(Offset.zero, size.bottomLeft(Offset.zero)), + blackLine, + ); + for (math.Point p in state.body) { + final Offset a = Offset(cellSize * p.x, cellSize * p.y); + final Offset b = Offset(cellSize * (p.x + 1), cellSize * (p.y + 1)); + + canvas.drawRect(Rect.fromPoints(a, b), blackFilled); + } + } + + @override + bool shouldRepaint(CustomPainter oldDelegate) { + return true; + } +} + +class SnakeState extends State { + SnakeState(int rows, int columns, this.cellSize) { + state = GameState(rows, columns); + } + + double cellSize; + GameState state; + AccelerometerEvent acceleration; + StreamSubscription _streamSubscription; + Timer _timer; + + @override + Widget build(BuildContext context) { + return CustomPaint(painter: SnakeBoardPainter(state, cellSize)); + } + + @override + void dispose() { + super.dispose(); + _streamSubscription.cancel(); + _timer.cancel(); + } + + @override + void initState() { + super.initState(); + _streamSubscription = + accelerometerEvents.listen((AccelerometerEvent event) { + setState(() { + acceleration = event; + }); + }); + + _timer = Timer.periodic(const Duration(milliseconds: 200), (_) { + setState(() { + _step(); + }); + }); + } + + void _step() { + final math.Point newDirection = acceleration == null + ? null + : acceleration.x.abs() < 1.0 && acceleration.y.abs() < 1.0 + ? null + : (acceleration.x.abs() < acceleration.y.abs()) + ? math.Point(0, acceleration.y.sign.toInt()) + : math.Point(-acceleration.x.sign.toInt(), 0); + state.step(newDirection); + } +} + +class GameState { + GameState(this.rows, this.columns) { + snakeLength = math.min(rows, columns) - 5; + } + + int rows; + int columns; + int snakeLength; + + List> body = >[const math.Point(0, 0)]; + math.Point direction = const math.Point(1, 0); + + void step(math.Point newDirection) { + math.Point next = body.last + direction; + next = math.Point(next.x % columns, next.y % rows); + + body.add(next); + if (body.length > snakeLength) body.removeAt(0); + direction = newDirection ?? direction; + } +} diff --git a/packages/sensors/example/pubspec.yaml b/packages/sensors/example/pubspec.yaml new file mode 100644 index 000000000..e56c76be7 --- /dev/null +++ b/packages/sensors/example/pubspec.yaml @@ -0,0 +1,24 @@ +name: sensors_example +description: Demonstrates how to use the sensors plugin. + +dependencies: + flutter: + sdk: flutter + sensors: ^0.4.0 + sensors_tizen: + path: ../ + +dev_dependencies: + flutter_driver: + sdk: flutter + integration_test: ^0.9.2 + integration_test_tizen: + path: ../../integration_test/ + pedantic: ^1.8.0 + +flutter: + uses-material-design: true + +environment: + sdk: ">=2.0.0-dev.28.0 <3.0.0" + flutter: ">=1.9.1+hotfix.2 <2.0.0" diff --git a/packages/sensors/example/test_driver/test/integration_test.dart b/packages/sensors/example/test_driver/test/integration_test.dart new file mode 100644 index 000000000..7a2c21338 --- /dev/null +++ b/packages/sensors/example/test_driver/test/integration_test.dart @@ -0,0 +1,17 @@ +// Copyright 2019, the Chromium project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; +import 'package:flutter_driver/flutter_driver.dart'; + +Future main() async { + final FlutterDriver driver = await FlutterDriver.connect(); + final String data = + await driver.requestData(null, timeout: const Duration(minutes: 1)); + await driver.close(); + final Map result = jsonDecode(data); + exit(result['result'] == 'true' ? 0 : 1); +} diff --git a/packages/sensors/example/tizen/.gitignore b/packages/sensors/example/tizen/.gitignore new file mode 100644 index 000000000..750f3af1b --- /dev/null +++ b/packages/sensors/example/tizen/.gitignore @@ -0,0 +1,5 @@ +flutter/ +.vs/ +*.user +bin/ +obj/ diff --git a/packages/sensors/example/tizen/App.cs b/packages/sensors/example/tizen/App.cs new file mode 100644 index 000000000..6dd4a6356 --- /dev/null +++ b/packages/sensors/example/tizen/App.cs @@ -0,0 +1,20 @@ +using Tizen.Flutter.Embedding; + +namespace Runner +{ + public class App : FlutterApplication + { + protected override void OnCreate() + { + base.OnCreate(); + + GeneratedPluginRegistrant.RegisterPlugins(this); + } + + static void Main(string[] args) + { + var app = new App(); + app.Run(args); + } + } +} diff --git a/packages/sensors/example/tizen/NuGet.Config b/packages/sensors/example/tizen/NuGet.Config new file mode 100644 index 000000000..c4ea70c17 --- /dev/null +++ b/packages/sensors/example/tizen/NuGet.Config @@ -0,0 +1,7 @@ + + + + + + + diff --git a/packages/sensors/example/tizen/Runner.csproj b/packages/sensors/example/tizen/Runner.csproj new file mode 100644 index 000000000..8ebc2abdf --- /dev/null +++ b/packages/sensors/example/tizen/Runner.csproj @@ -0,0 +1,26 @@ + + + + Exe + tizen60 + + + + portable + + + none + + + + + + + + + + %(RecursiveDir) + + + + diff --git a/packages/sensors/example/tizen/shared/res/ic_launcher.png b/packages/sensors/example/tizen/shared/res/ic_launcher.png new file mode 100644 index 0000000000000000000000000000000000000000..4d6372eebdb28e45604e46eeda8dd24651419bc0 GIT binary patch literal 1443 zcmb`G{WsKk6vsdJTdFg%tJav9_E4vzrOaqkWF|A724Nly!y+?N9`YV6wZ}5(X(D_N(?!*n3`|_r0Hc?=PQw&*vnU?QTFY zB_MsH|!j$PP;I}?dppoE_gA(4uc!jV&0!l7_;&p2^pxNo>PEcNJv za5_RT$o2Mf!<+r?&EbHH6nMoTsDOa;mN(wv8RNsHpG)`^ymG-S5By8=l9iVXzN_eG%Xg2@Xeq76tTZ*dGh~Lo9vl;Zfs+W#BydUw zCkZ$o1LqWQO$FC9aKlLl*7x9^0q%0}$OMlp@Kk_jHXOjofdePND+j!A{q!8~Jn+s3 z?~~w@4?egS02}8NuulUA=L~QQfm;MzCGd)XhiftT;+zFO&JVyp2mBww?;QByS_1w! zrQlx%{^cMj0|Bo1FjwY@Q8?Hx0cIPF*@-ZRFpPc#bBw{5@tD(5%sClzIfl8WU~V#u zm5Q;_F!wa$BSpqhN>W@2De?TKWR*!ujY;Yylk_X5#~V!L*Gw~;$%4Q8~Mad z@`-kG?yb$a9cHIApZDVZ^U6Xkp<*4rU82O7%}0jjHlK{id@?-wpN*fCHXyXh(bLt* zPc}H-x0e4E&nQ>y%B-(EL=9}RyC%MyX=upHuFhAk&MLbsF0LP-q`XnH78@fT+pKPW zu72MW`|?8ht^tz$iC}ZwLp4tB;Q49K!QCF3@!iB1qOI=?w z7In!}F~ij(18UYUjnbmC!qKhPo%24?8U1x{7o(+?^Zu0Hx81|FuS?bJ0jgBhEMzf< zCgUq7r2OCB(`XkKcN-TL>u5y#dD6D!)5W?`O5)V^>jb)P)GBdy%t$uUMpf$SNV31$ zb||OojAbvMP?T@$h_ZiFLFVHDmbyMhJF|-_)HX3%m=CDI+ID$0^C>kzxprBW)hw(v zr!Gmda);ICoQyhV_oP5+C%?jcG8v+D@9f?Dk*!BxY}dazmrT@64UrP3hlslANK)bq z$67n83eh}OeW&SV@HG95P|bjfqJ7gw$e+`Hxo!4cx`jdK1bJ>YDSpGKLPZ^1cv$ek zIB?0S<#tX?SJCLWdMd{-ME?$hc7A$zBOdIJ)4!KcAwb=VMov)nK;9z>x~rfT1>dS+ zZ6#`2v@`jgbqq)P22H)Tx2CpmM^o1$B+xT6`(v%5xJ(?j#>Q$+rx_R|7TzDZe{J6q zG1*EcU%tE?!kO%^M;3aM6JN*LAKUVb^xz8-Pxo#jR5(-KBeLJvA@-gxNHx0M-ZJLl z;#JwQoh~9V?`UVo#}{6ka@II>++D@%KqGpMdlQ}?9E*wFcf5(#XQnP$Dk5~%iX^>f z%$y;?M0BLp{O3a(-4A?ewryHrrD%cx#Q^%KY1H zNre$ve+vceSLZcNY4U(RBX&)oZn*Py()h)XkE?PL$!bNb{N5FVI2Y%LKEm%yvpyTP z(1P?z~7YxD~Rf<(a@_y` literal 0 HcmV?d00001 diff --git a/packages/sensors/example/tizen/tizen-manifest.xml b/packages/sensors/example/tizen/tizen-manifest.xml new file mode 100644 index 000000000..a1a802221 --- /dev/null +++ b/packages/sensors/example/tizen/tizen-manifest.xml @@ -0,0 +1,10 @@ + + + + + + ic_launcher.png + + + + diff --git a/packages/sensors/integration_test/sensors_test.dart b/packages/sensors/integration_test/sensors_test.dart new file mode 100644 index 000000000..ea1db0375 --- /dev/null +++ b/packages/sensors/integration_test/sensors_test.dart @@ -0,0 +1,24 @@ +// Copyright 2019, the Chromium project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'dart:async'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:sensors/sensors.dart'; +import 'package:integration_test/integration_test.dart'; + +void main() { + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); + + testWidgets('Can subscript to accelerometerEvents and get non-null events', + (WidgetTester tester) async { + final Completer completer = + Completer(); + StreamSubscription subscription; + subscription = accelerometerEvents.listen((AccelerometerEvent event) { + completer.complete(event); + subscription.cancel(); + }); + expect(await completer.future, isNotNull); + }); +} diff --git a/packages/sensors/pubspec.yaml b/packages/sensors/pubspec.yaml new file mode 100644 index 000000000..e2abc41cf --- /dev/null +++ b/packages/sensors/pubspec.yaml @@ -0,0 +1,29 @@ +name: sensors_tizen +description: Tizen implementation of the sensors plugin +homepage: https://github.com/flutter-tizen/plugins +version: 1.0.0 + +flutter: + plugin: + platforms: + tizen: + pluginClass: SensorsPlugin + fileName: sensors_plugin.h + +dependencies: + flutter: + sdk: flutter + +dev_dependencies: + test: ^1.3.0 + flutter_test: + sdk: flutter + integration_test: ^0.9.2 + integration_test_tizen: + path: ../integration_test + pedantic: ^1.8.0 + sensors: ^0.4.0 + +environment: + sdk: ">=2.1.0<3.0.0" + flutter: ">=1.12.13+hotfix.5 <2.0.0" diff --git a/packages/sensors/tizen/.gitignore b/packages/sensors/tizen/.gitignore new file mode 100644 index 000000000..a2a7d62b1 --- /dev/null +++ b/packages/sensors/tizen/.gitignore @@ -0,0 +1,5 @@ +.cproject +.sign +crash-info/ +Debug/ +Release/ diff --git a/packages/sensors/tizen/inc/sensors_plugin.h b/packages/sensors/tizen/inc/sensors_plugin.h new file mode 100644 index 000000000..251ad9bd0 --- /dev/null +++ b/packages/sensors/tizen/inc/sensors_plugin.h @@ -0,0 +1,23 @@ +#ifndef FLUTTER_PLUGIN_SENSORS_PLUGIN_H_ +#define FLUTTER_PLUGIN_SENSORS_PLUGIN_H_ + +#include + +#ifdef FLUTTER_PLUGIN_IMPL +#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default"))) +#else +#define FLUTTER_PLUGIN_EXPORT +#endif + +#if defined(__cplusplus) +extern "C" { +#endif + +FLUTTER_PLUGIN_EXPORT void SensorsPluginRegisterWithRegistrar( + FlutterDesktopPluginRegistrarRef registrar); + +#if defined(__cplusplus) +} // extern "C" +#endif + +#endif // FLUTTER_PLUGIN_SENSORS_PLUGIN_H_ diff --git a/packages/sensors/tizen/project_def.prop b/packages/sensors/tizen/project_def.prop new file mode 100644 index 000000000..791cda79b --- /dev/null +++ b/packages/sensors/tizen/project_def.prop @@ -0,0 +1,30 @@ +# See https://docs.tizen.org/application/tizen-studio/native-tools/project-conversion +# for details. + +APPNAME = sensors_plugin +type = sharedLib +profile = common-5.5 + +# Source files +USER_SRCS += src/sensors_plugin.cc + +# User defines +USER_DEFS = +USER_UNDEFS = +USER_CPP_DEFS = TIZEN_DEPRECATION DEPRECATION_WARNING FLUTTER_PLUGIN_IMPL +USER_CPP_UNDEFS = + +# Compiler/linker flags +USER_CFLAGS_MISC = +USER_CPPFLAGS_MISC = -c -fmessage-length=0 +USER_LFLAGS = + +# Libraries and objects +USER_LIB_DIRS = lib +USER_LIBS = +USER_OBJS = + +# User includes +USER_INC_DIRS = inc src +USER_INC_FILES = +USER_CPP_INC_FILES = diff --git a/packages/sensors/tizen/src/log.h b/packages/sensors/tizen/src/log.h new file mode 100644 index 000000000..155a994f9 --- /dev/null +++ b/packages/sensors/tizen/src/log.h @@ -0,0 +1,20 @@ +#ifndef __LOG_H__ +#define __LOG_H__ + +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "SensorsPlugin" + +#define LOG(prio, fmt, arg...) \ + dlog_print(prio, LOG_TAG, "%s: %s(%d) > " fmt, __FILE__, __func__, __LINE__, \ + ##arg) + +#define LOG_DEBUG(fmt, args...) LOG(DLOG_DEBUG, fmt, ##args) +#define LOG_INFO(fmt, args...) LOG(DLOG_INFO, fmt, ##args) +#define LOG_WARN(fmt, args...) LOG(DLOG_WARN, fmt, ##args) +#define LOG_ERROR(fmt, args...) LOG(DLOG_ERROR, fmt, ##args) + +#endif // __LOG_H__ diff --git a/packages/sensors/tizen/src/sensors_plugin.cc b/packages/sensors/tizen/src/sensors_plugin.cc new file mode 100644 index 000000000..25c00cf40 --- /dev/null +++ b/packages/sensors/tizen/src/sensors_plugin.cc @@ -0,0 +1,229 @@ +#include "sensors_plugin.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "log.h" + +#define ACCELEROMETER_CHANNEL_NAME "plugins.flutter.io/sensors/accelerometer" +#define GYROSCOPE_CHANNEL_NAME "plugins.flutter.io/sensors/gyroscope" +#define USER_ACCELEROMETER_CHANNEL_NAME "plugins.flutter.io/sensors/user_accel" + +class Listener { + public: + Listener( + std::unique_ptr> &&event_sink) + : event_sink_(std::move(event_sink)) {} + bool Init(sensor_type_e type) { + sensor_h sensor; + int ret = sensor_get_default_sensor(type, &sensor); + if (ret != SENSOR_ERROR_NONE) { + LOG_ERROR("%s", get_error_message(ret)); + return false; + } + ret = sensor_create_listener(sensor, &listener_); + if (ret != SENSOR_ERROR_NONE) { + LOG_ERROR("%s", get_error_message(ret)); + return false; + } + return true; + } + + void Listen() { + if (is_listening_) { + LOG_ERROR("Already listening!"); + return; + } + + int ret = sensor_listener_set_event_cb( + listener_, 1000, + [](sensor_h sensor, sensor_event_s *event, void *user_data) { + Listener *s = (Listener *)user_data; + std::vector list; + for (int i = 0; i < event->value_count; i++) { + list.push_back(event->values[i]); + } + flutter::EncodableValue value(list); + s->event_sink_->Success(value); + }, + this); + + if (ret != SENSOR_ERROR_NONE) { + LOG_ERROR("%s", get_error_message(ret)); + return; + } + ret = sensor_listener_start(listener_); + if (ret != SENSOR_ERROR_NONE) { + LOG_ERROR("%s", get_error_message(ret)); + return; + } + + is_listening_ = true; + } + + void Cancel() { + if (!is_listening_) { + LOG_ERROR("Already canceled!"); + return; + } + int ret = sensor_listener_stop(listener_); + if (ret != SENSOR_ERROR_NONE) { + LOG_ERROR("%s", get_error_message(ret)); + return; + } + + ret = sensor_listener_unset_event_cb(listener_); + if (ret != SENSOR_ERROR_NONE) { + LOG_ERROR("%s", get_error_message(ret)); + return; + } + is_listening_ = false; + } + + virtual ~Listener() { + if (is_listening_) { + Cancel(); + } + if (listener_) { + int ret = sensor_destroy_listener(listener_); + if (ret != SENSOR_ERROR_NONE) { + LOG_ERROR("%s", get_error_message(ret)); + } + } + } + + protected: + std::unique_ptr> event_sink_; + sensor_listener_h listener_{nullptr}; + bool is_listening_{false}; +}; +class SensorsPlugin : public flutter::Plugin { + public: + static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) { + LOG_DEBUG("RegisterWithRegistrar"); + auto plugin = std::make_unique(); + plugin->setupEventChannels(registrar); + registrar->AddPlugin(std::move(plugin)); + } + + SensorsPlugin() { LOG_DEBUG("Create"); } + + virtual ~SensorsPlugin() { LOG_DEBUG("Destroy"); } + + private: + void setupEventChannels(flutter::PluginRegistrar *registrar) { + accelerometer_channel_ = + std::make_unique>( + registrar->messenger(), ACCELEROMETER_CHANNEL_NAME, + &flutter::StandardMethodCodec::GetInstance()); + auto accelerometer_channel_handler = + std::make_unique>( + [this](const flutter::EncodableValue *arguments, + std::unique_ptr> &&events) + -> std::unique_ptr> { + LOG_DEBUG("OnListen"); + accelerometer_listener_ = + std::make_unique(std::move(events)); + if (accelerometer_listener_->Init(SENSOR_ACCELEROMETER)) { + accelerometer_listener_->Listen(); + } + return nullptr; + }, + [this](const flutter::EncodableValue *arguments) + -> std::unique_ptr> { + LOG_DEBUG("OnCancel"); + if (accelerometer_listener_) { + accelerometer_listener_->Cancel(); + accelerometer_listener_ = nullptr; + } + return nullptr; + }); + accelerometer_channel_->SetStreamHandler( + std::move(accelerometer_channel_handler)); + + gyroscope_channel_ = + std::make_unique>( + registrar->messenger(), GYROSCOPE_CHANNEL_NAME, + &flutter::StandardMethodCodec::GetInstance()); + auto gyroscope_channel_handler = + std::make_unique>( + [this](const flutter::EncodableValue *arguments, + std::unique_ptr> &&events) + -> std::unique_ptr> { + LOG_DEBUG("OnListen"); + gyroscope_listener_ = + std::make_unique(std::move(events)); + if (gyroscope_listener_->Init(SENSOR_GYROSCOPE)) { + gyroscope_listener_->Listen(); + } + return nullptr; + }, + [this](const flutter::EncodableValue *arguments) + -> std::unique_ptr> { + LOG_DEBUG("OnCancel"); + if (gyroscope_listener_) { + gyroscope_listener_->Cancel(); + gyroscope_listener_ = nullptr; + } + return nullptr; + }); + gyroscope_channel_->SetStreamHandler(std::move(gyroscope_channel_handler)); + + user_accel_channel_ = + std::make_unique>( + registrar->messenger(), USER_ACCELEROMETER_CHANNEL_NAME, + &flutter::StandardMethodCodec::GetInstance()); + auto user_accel_handler = + std::make_unique>( + [this](const flutter::EncodableValue *arguments, + std::unique_ptr> &&events) + -> std::unique_ptr> { + LOG_DEBUG("OnListen"); + user_accel_listener_ = + std::make_unique(std::move(events)); + if (user_accel_listener_->Init(SENSOR_LINEAR_ACCELERATION)) { + user_accel_listener_->Listen(); + } + return nullptr; + }, + [this](const flutter::EncodableValue *arguments) + -> std::unique_ptr> { + LOG_DEBUG("OnCancel"); + if (user_accel_listener_) { + user_accel_listener_->Cancel(); + user_accel_listener_ = nullptr; + } + return nullptr; + }); + user_accel_channel_->SetStreamHandler(std::move(user_accel_handler)); + } + + std::unique_ptr> + accelerometer_channel_; + std::unique_ptr accelerometer_listener_; + std::unique_ptr> + gyroscope_channel_; + std::unique_ptr gyroscope_listener_; + std::unique_ptr> + user_accel_channel_; + std::unique_ptr user_accel_listener_; +}; + +void SensorsPluginRegisterWithRegistrar( + FlutterDesktopPluginRegistrarRef registrar) { + SensorsPlugin::RegisterWithRegistrar( + flutter::PluginRegistrarManager::GetInstance() + ->GetRegistrar(registrar)); +} From e6a82eb8f5be8234667bf0ea7dfe4f6629ee472b Mon Sep 17 00:00:00 2001 From: Swift Kim Date: Wed, 2 Dec 2020 10:45:59 +0900 Subject: [PATCH 2/3] Update copyright notice --- .../sensors/example/test_driver/test/integration_test.dart | 6 +++--- packages/sensors/integration_test/sensors_test.dart | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/sensors/example/test_driver/test/integration_test.dart b/packages/sensors/example/test_driver/test/integration_test.dart index 7a2c21338..4d87d3940 100644 --- a/packages/sensors/example/test_driver/test/integration_test.dart +++ b/packages/sensors/example/test_driver/test/integration_test.dart @@ -1,6 +1,6 @@ -// Copyright 2019, the Chromium project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. +// Copyright 2019 the Chromium project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. import 'dart:async'; import 'dart:convert'; diff --git a/packages/sensors/integration_test/sensors_test.dart b/packages/sensors/integration_test/sensors_test.dart index ea1db0375..f38107175 100644 --- a/packages/sensors/integration_test/sensors_test.dart +++ b/packages/sensors/integration_test/sensors_test.dart @@ -1,6 +1,6 @@ -// Copyright 2019, the Chromium project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. +// Copyright 2019 the Chromium project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; From 291b28a6cf514504b04b024758100f62f3fd2a02 Mon Sep 17 00:00:00 2001 From: Swift Kim Date: Wed, 2 Dec 2020 12:10:25 +0900 Subject: [PATCH 3/3] Replace integration_test.dart --- packages/sensors/README.md | 2 +- .../example/test_driver/integration_test.dart | 3 +++ .../test_driver/test/integration_test.dart | 17 ----------------- packages/sensors/tizen/src/sensors_plugin.cc | 1 + 4 files changed, 5 insertions(+), 18 deletions(-) create mode 100644 packages/sensors/example/test_driver/integration_test.dart delete mode 100644 packages/sensors/example/test_driver/test/integration_test.dart diff --git a/packages/sensors/README.md b/packages/sensors/README.md index 4894ad9a4..9625cb029 100644 --- a/packages/sensors/README.md +++ b/packages/sensors/README.md @@ -22,6 +22,6 @@ For detailed usage, see https://github.com/flutter/plugins/tree/master/packages/ ## Supported devices -This plugin is available for use only on these types of devices: +This plugin is supported on these types of devices: - Galaxy Watch (running Tizen 5.5 or later) diff --git a/packages/sensors/example/test_driver/integration_test.dart b/packages/sensors/example/test_driver/integration_test.dart new file mode 100644 index 000000000..b38629cca --- /dev/null +++ b/packages/sensors/example/test_driver/integration_test.dart @@ -0,0 +1,3 @@ +import 'package:integration_test/integration_test_driver.dart'; + +Future main() => integrationDriver(); diff --git a/packages/sensors/example/test_driver/test/integration_test.dart b/packages/sensors/example/test_driver/test/integration_test.dart deleted file mode 100644 index 4d87d3940..000000000 --- a/packages/sensors/example/test_driver/test/integration_test.dart +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2019 the Chromium project authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -import 'dart:async'; -import 'dart:convert'; -import 'dart:io'; -import 'package:flutter_driver/flutter_driver.dart'; - -Future main() async { - final FlutterDriver driver = await FlutterDriver.connect(); - final String data = - await driver.requestData(null, timeout: const Duration(minutes: 1)); - await driver.close(); - final Map result = jsonDecode(data); - exit(result['result'] == 'true' ? 0 : 1); -} diff --git a/packages/sensors/tizen/src/sensors_plugin.cc b/packages/sensors/tizen/src/sensors_plugin.cc index 25c00cf40..4c27927ac 100644 --- a/packages/sensors/tizen/src/sensors_plugin.cc +++ b/packages/sensors/tizen/src/sensors_plugin.cc @@ -109,6 +109,7 @@ class Listener { sensor_listener_h listener_{nullptr}; bool is_listening_{false}; }; + class SensorsPlugin : public flutter::Plugin { public: static void RegisterWithRegistrar(flutter::PluginRegistrar *registrar) {