diff --git a/packages/light/CHANGELOG.md b/packages/light/CHANGELOG.md index d5872acc9..df03a3375 100644 --- a/packages/light/CHANGELOG.md +++ b/packages/light/CHANGELOG.md @@ -1,27 +1,35 @@ +## 4.0.0 + +* Upgrade of APK (issue [[#1116](https://github.com/cph-cachet/flutter-plugins/issues/1116)]) +* Added linter to the code +* Removed marked support for iOS in pubspec file +* Refactor of error handling (removing the `LightException`) +* Refactor of example app code + ## 3.0.1 -- Updated pubspec.yaml +* Updated pubspec.yaml ## 3.0.0 -- `Light()` implemented as singleton. -- Updates Kotlin plugin and AGP. -- Upgrade of `compileSdkVersion` to 33. -- Upgrade to Dart 3. +* `Light()` implemented as singleton. +* Updates Kotlin plugin and AGP. +* Upgrade of `compileSdkVersion` to 33. +* Upgrade to Dart 3. ## 2.1.0 -- `lightSensorStream` is not returning `null` values. -- update to example app +* `lightSensorStream` is not returning `null` values. +* update to example app ## 2.0.0 -- Migration to null safety +* Migration to null safety ## 1.0.1 -- fix: issue [#139](https://github.com/cph-cachet/carp.sensing-flutter/issues/139) +* fix: issue [#139](https://github.com/cph-cachet/carp.sensing-flutter/issues/139) ## 1.0.0 -- Updated version of the old plugin. +* Updated version of the old plugin. diff --git a/packages/light/README.md b/packages/light/README.md index b12414e6d..fb1615710 100644 --- a/packages/light/README.md +++ b/packages/light/README.md @@ -2,7 +2,7 @@ [![pub package](https://img.shields.io/pub/v/light.svg)](https://pub.dartlang.org/packages/light) -A Flutter plugin for collecting data from the [ambient light sensor on Android](https://developer.android.com/guide/topics/sensors/sensors_environment#java). +A Flutter plugin for collecting ambient light data from the [Android Environment Sensors](https://developer.android.com/develop/sensors-and-location/sensors/sensors_environment). ## Install @@ -12,27 +12,23 @@ For help on adding as a dependency, view the [documentation](https://flutter.io/ ## Usage -Instantiate an instance of the `Light()` plugin and listen on the `lightSensorStream` stream. +Use the singleton `Light()` to listen on the `lightSensorStream` stream. ```dart - Light? _light; - StreamSubscription? _subscription; - - void onData(int luxValue) async { - print("Lux value: $luxValue"); - } - + StreamSubscription? _lightEvents; void startListening() { - _light = Light(); try { - _subscription = _light?.lightSensorStream.listen(onData); - } on LightException catch (exception) { + _lightEvents = + Light().lightSensorStream.listen((luxValue) => setState(() { + // Do something with the lux value + })); + } catch (exception) { print(exception); } } void stopListening() { - _subscription?.cancel(); + _lightEvents?.cancel(); } ``` diff --git a/packages/light/analysis_options.yaml b/packages/light/analysis_options.yaml new file mode 100644 index 000000000..b449163ea --- /dev/null +++ b/packages/light/analysis_options.yaml @@ -0,0 +1,18 @@ +# Additional information about this file can be found at +# https://dart.dev/guides/language/analysis-options + +include: package:lints/recommended.yaml + +analyzer: + exclude: [build/**] + language: + strict-casts: true + strict-inference: true + strict-raw-types: true + +linter: + rules: + cancel_subscriptions: true + constant_identifier_names: false + depend_on_referenced_packages: false + use_string_in_part_of_directives: false diff --git a/packages/light/android/build.gradle b/packages/light/android/build.gradle index d256b7247..8dc13c694 100644 --- a/packages/light/android/build.gradle +++ b/packages/light/android/build.gradle @@ -1,16 +1,16 @@ -group 'dk.cachet.light' -version '1.0-SNAPSHOT' +group = "dk.cachet.light" +version = "1.0-SNAPSHOT" buildscript { - ext.kotlin_version = '1.7.10' + ext.kotlin_version = "1.8.22" repositories { google() mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:7.3.0' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath("com.android.tools.build:gradle:8.1.0") + classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") } } @@ -21,34 +21,46 @@ allprojects { } } -apply plugin: 'com.android.library' -apply plugin: 'kotlin-android' +apply plugin: "com.android.library" +apply plugin: "kotlin-android" android { - compileSdkVersion 31 - namespace 'dk.cachet.light' + namespace = "dk.cachet.light" + + compileSdk = 35 compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } + + kotlinOptions { + jvmTarget = JavaVersion.VERSION_11 } sourceSets { - main.java.srcDirs += 'src/main/kotlin' - test.java.srcDirs += 'src/test/kotlin' + main.java.srcDirs += "src/main/kotlin" + test.java.srcDirs += "src/test/kotlin" } defaultConfig { - minSdkVersion 16 + minSdk = 21 } - dependencies { - testImplementation 'org.jetbrains.kotlin:kotlin-test' - testImplementation 'org.mockito:mockito-core:5.0.0' + def flutterRoot = { + testImplementation("org.jetbrains.kotlin:kotlin-test") + testImplementation("org.mockito:mockito-core:5.0.0") } - lintOptions { - disable 'InvalidPackage' + testOptions { + unitTests.all { + useJUnitPlatform() + + testLogging { + events "passed", "skipped", "failed", "standardOut", "standardError" + outputs.upToDateWhen {false} + showStandardStreams = true + } + } } - namespace "dk.cachet.light" } diff --git a/packages/light/android/gradle/wrapper/gradle-wrapper.properties b/packages/light/android/gradle/wrapper/gradle-wrapper.properties index 01a286e96..09523c0e5 100644 --- a/packages/light/android/gradle/wrapper/gradle-wrapper.properties +++ b/packages/light/android/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip +networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip diff --git a/packages/light/example/android/app/build.gradle b/packages/light/example/android/app/build.gradle index e25e15a13..911fded7c 100644 --- a/packages/light/example/android/app/build.gradle +++ b/packages/light/example/android/app/build.gradle @@ -1,3 +1,9 @@ +plugins { + id "com.android.application" + id "kotlin-android" + id "dev.flutter.flutter-gradle-plugin" +} + def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { @@ -6,11 +12,6 @@ if (localPropertiesFile.exists()) { } } -def flutterRoot = localProperties.getProperty('flutter.sdk') -if (flutterRoot == null) { - throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") -} - def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' @@ -21,9 +22,6 @@ if (flutterVersionName == null) { flutterVersionName = '1.0' } -apply plugin: 'com.android.application' -apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" - android { namespace "dk.cachet.light_example" compileSdkVersion flutter.compileSdkVersion diff --git a/packages/light/example/android/build.gradle b/packages/light/example/android/build.gradle index f7eb7f63c..d2ffbffa4 100644 --- a/packages/light/example/android/build.gradle +++ b/packages/light/example/android/build.gradle @@ -1,16 +1,3 @@ -buildscript { - ext.kotlin_version = '1.7.10' - repositories { - google() - mavenCentral() - } - - dependencies { - classpath 'com.android.tools.build:gradle:7.3.0' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" - } -} - allprojects { repositories { google() @@ -18,12 +5,12 @@ allprojects { } } -rootProject.buildDir = '../build' +rootProject.buildDir = "../build" subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { - project.evaluationDependsOn(':app') + project.evaluationDependsOn(":app") } tasks.register("clean", Delete) { diff --git a/packages/light/example/android/gradle.properties b/packages/light/example/android/gradle.properties index 38c8d4544..259717082 100644 --- a/packages/light/example/android/gradle.properties +++ b/packages/light/example/android/gradle.properties @@ -1,4 +1,3 @@ -org.gradle.jvmargs=-Xmx1536M -android.enableR8=true +org.gradle.jvmargs=-Xmx4G -XX:MaxMetaspaceSize=2G -XX:+HeapDumpOnOutOfMemoryError android.useAndroidX=true android.enableJetifier=true diff --git a/packages/light/example/android/gradle/wrapper/gradle-wrapper.properties b/packages/light/example/android/gradle/wrapper/gradle-wrapper.properties index 6b665338b..7bb2df6ba 100644 --- a/packages/light/example/android/gradle/wrapper/gradle-wrapper.properties +++ b/packages/light/example/android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,5 @@ -#Fri Jun 23 08:50:38 CEST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip diff --git a/packages/light/example/android/settings.gradle b/packages/light/example/android/settings.gradle index d3b6a4013..b9e43bd37 100644 --- a/packages/light/example/android/settings.gradle +++ b/packages/light/example/android/settings.gradle @@ -1,15 +1,25 @@ -// Copyright 2014 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. +pluginManagement { + def flutterSdkPath = { + def properties = new Properties() + file("local.properties").withInputStream { properties.load(it) } + def flutterSdkPath = properties.getProperty("flutter.sdk") + assert flutterSdkPath != null, "flutter.sdk not set in local.properties" + return flutterSdkPath + }() -include ':app' + includeBuild("$flutterSdkPath/packages/flutter_tools/gradle") -def localPropertiesFile = new File(rootProject.projectDir, "local.properties") -def properties = new Properties() + repositories { + google() + mavenCentral() + gradlePluginPortal() + } +} -assert localPropertiesFile.exists() -localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) } +plugins { + id "dev.flutter.flutter-plugin-loader" version "1.0.0" + id "com.android.application" version "8.1.0" apply false + id "org.jetbrains.kotlin.android" version "1.8.22" apply false +} -def flutterSdkPath = properties.getProperty("flutter.sdk") -assert flutterSdkPath != null, "flutter.sdk not set in local.properties" -apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle" +include ":app" diff --git a/packages/light/example/lib/main.dart b/packages/light/example/lib/main.dart index 90a3bb339..ccffbe029 100644 --- a/packages/light/example/lib/main.dart +++ b/packages/light/example/lib/main.dart @@ -2,38 +2,32 @@ import 'package:flutter/material.dart'; import 'dart:async'; import 'package:light/light.dart'; -void main() => runApp(new MyApp()); +void main() => runApp(LightApp()); -class MyApp extends StatefulWidget { +class LightApp extends StatefulWidget { @override - _MyAppState createState() => new _MyAppState(); + LightAppState createState() => LightAppState(); } -class _MyAppState extends State { +class LightAppState extends State { String _luxString = 'Unknown'; - Light? _light; - StreamSubscription? _subscription; - - void onData(int luxValue) async { - print("Lux value: $luxValue"); - setState(() { - _luxString = "$luxValue"; - }); - } - - void stopListening() { - _subscription?.cancel(); - } + StreamSubscription? _lightEvents; void startListening() { - _light = Light(); try { - _subscription = _light?.lightSensorStream.listen(onData); - } on LightException catch (exception) { + _lightEvents = + Light().lightSensorStream.listen((luxValue) => setState(() { + _luxString = "$luxValue"; + })); + } catch (exception) { print(exception); } } + void stopListening() { + _lightEvents?.cancel(); + } + @override void initState() { super.initState(); @@ -42,15 +36,10 @@ class _MyAppState extends State { @override Widget build(BuildContext context) { - return new MaterialApp( - home: new Scaffold( - appBar: new AppBar( - title: const Text('Light Example App'), - ), - body: new Center( - child: new Text('Lux value: $_luxString\n'), - ), - ), - ); + return MaterialApp( + home: Scaffold( + appBar: AppBar(title: const Text('Light Example App')), + body: Center(child: Text('Lux value: $_luxString\n')), + )); } } diff --git a/packages/light/example/test/widget_test.dart b/packages/light/example/test/widget_test.dart index 421225d18..874d742d6 100644 --- a/packages/light/example/test/widget_test.dart +++ b/packages/light/example/test/widget_test.dart @@ -13,7 +13,7 @@ import 'package:light_example/main.dart'; void main() { testWidgets('Verify Platform version', (WidgetTester tester) async { // Build our app and trigger a frame. - await tester.pumpWidget(MyApp()); + await tester.pumpWidget(LightApp()); // Verify that platform version is retrieved. expect( diff --git a/packages/light/lib/light.dart b/packages/light/lib/light.dart index 2931d38a0..276aa8aa2 100644 --- a/packages/light/lib/light.dart +++ b/packages/light/lib/light.dart @@ -3,36 +3,32 @@ import 'dart:async'; import 'package:flutter/services.dart'; import 'dart:io' show Platform; -/// Custom Exception for the plugin, -/// thrown whenever the plugin is used on platforms other than Android -class LightException implements Exception { - String cause; - LightException(this.cause); - @override - String toString() => "$runtimeType - $cause"; -} - class Light { - static Light? _singleton; - static const EventChannel _eventChannel = - const EventChannel("light.eventChannel"); + static Light? _instance; + static const EventChannel _eventChannel = EventChannel("light.eventChannel"); /// Constructs a singleton instance of [Light]. /// /// [Light] is designed to work as a singleton. - factory Light() => _singleton ??= Light._(); + factory Light() => _instance ??= Light._(); Light._(); Stream? _lightSensorStream; /// The stream of light events. - /// Throws a [LightException] if device isn't on Android. + /// + /// Return an empty Stream if this device isn't Android or if the accessing + /// the light sensor fails. Stream get lightSensorStream { - if (!Platform.isAndroid) - throw LightException('Light sensor API only available on Android.'); - - return _lightSensorStream ??= - _eventChannel.receiveBroadcastStream().map((lux) => lux); + try { + return (Platform.isAndroid) + ? _lightSensorStream ??= _eventChannel + .receiveBroadcastStream() + .map((lux) => int.tryParse(lux.toString()) ?? -1) + : Stream.empty(); + } catch (_) { + return Stream.empty(); + } } } diff --git a/packages/light/pubspec.yaml b/packages/light/pubspec.yaml index 00bbc875f..155f9bf4c 100644 --- a/packages/light/pubspec.yaml +++ b/packages/light/pubspec.yaml @@ -1,21 +1,23 @@ name: light description: Plugin for collecting data from the ambient light sensor on Android. -version: 3.0.1 +version: 4.0.0 homepage: https://github.com/cph-cachet/flutter-plugins/tree/master environment: - sdk: ">=2.17.0 <4.0.0" - flutter: ">=3.0.0" + sdk: ^3.6.0 + flutter: '>=3.3.0' dependencies: flutter: sdk: flutter +dev_dependencies: + test: any + flutter_lints: any + flutter: plugin: platforms: android: package: dk.cachet.light pluginClass: LightPlugin - ios: - pluginClass: LightPlugin