Skip to content
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
5 changes: 5 additions & 0 deletions packages/sensors_plus/sensors_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.3.0

- Android: Migrate to Kotlin
- Android: Update dependencies, build config updates

## 1.2.2

- Fix example embedding issues
Expand Down
15 changes: 4 additions & 11 deletions packages/sensors_plus/sensors_plus/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# sensors
# Sensors Plus

[![Flutter Community: sensors_plus](https://fluttercommunity.dev/_github/header/sensors_plus)](https://github.com/fluttercommunity/community)

[![pub package](https://img.shields.io/pub/v/sensors_plus.svg)](https://pub.dev/packages/sensors_plus)
[![sensors_plus](https://github.com/fluttercommunity/plus_plugins/actions/workflows/sensors_plus.yaml/badge.svg)](https://github.com/fluttercommunity/plus_plugins/actions/workflows/sensors_plus.yaml)
[![pub package](https://img.shields.io/pub/v/battery_plus.svg)](https://pub.dev/packages/battery_plus)

<p class="center">
<center><a href="https://flutter.dev/docs/development/packages-and-plugins/favorites" target="_blank" rel="noreferrer noopener"><img src="../../website/static/img/flutter-favorite-badge.png" width="100" alt="build"></a></center>
</p>
<a href="https://flutter.dev/docs/development/packages-and-plugins/favorites" target="_blank" rel="noreferrer noopener"><img src="../../../website/static/img/flutter-favorite-badge.png" width="100" alt="build"></a>

A Flutter plugin to access the accelerometer, gyroscope, and magnetometer
sensors.
Expand Down Expand Up @@ -70,9 +69,3 @@ Also see the `example` subdirectory for an example application that uses the
sensor data.

Check out our website to learn more: [Plus Plugins documentation](https://plus.fluttercommunity.dev/docs/overview)

**Important:** As of January 2021, the Flutter team is no longer accepting
non-critical PRs for the original set of plugins in `flutter/plugins`, and
instead they should be submitted in this project.
[You can read more about this announcement here](https://github.com/flutter/plugins/blob/master/CONTRIBUTING.md#important-note)
as well as [in the Flutter 2 announcement blog post](https://medium.com/flutter/whats-new-in-flutter-2-0-fe8e95ecc65).
9 changes: 8 additions & 1 deletion packages/sensors_plus/sensors_plus/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ group 'io.flutter.plugins.sensors'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.6.10'
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.2'
classpath 'com.android.tools.build:gradle:7.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

Expand All @@ -20,6 +22,7 @@ rootProject.allprojects {
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 31
Expand All @@ -31,4 +34,8 @@ android {
lintOptions {
disable 'InvalidPackage'
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<manifest package="dev.fluttercommunity.plus.sensors">
</manifest>
<manifest package="dev.fluttercommunity.plus.sensors" />

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dev.fluttercommunity.plus.sensors

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorManager
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

/** SensorsPlugin */
class SensorsPlugin : FlutterPlugin {
private lateinit var accelerometerChannel: EventChannel
private lateinit var userAccelChannel: EventChannel
private lateinit var gyroscopeChannel: EventChannel
private lateinit var magnetometerChannel: EventChannel

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

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

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

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

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

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

magnetometerChannel = EventChannel(messenger, MAGNETOMETER_CHANNEL_NAME)
val magnetometerStreamHandler = StreamHandlerImpl(
sensorsManager,
Sensor.TYPE_MAGNETIC_FIELD
)
magnetometerChannel.setStreamHandler(magnetometerStreamHandler)
}

private fun teardownEventChannels() {
accelerometerChannel.setStreamHandler(null)
userAccelChannel.setStreamHandler(null)
gyroscopeChannel.setStreamHandler(null)
magnetometerChannel.setStreamHandler(null)
}

companion object {
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 USER_ACCELEROMETER_CHANNEL_NAME = "dev.fluttercommunity.plus/sensors/user_accel"
private const val MAGNETOMETER_CHANNEL_NAME = "dev.fluttercommunity.plus/sensors/magnetometer"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dev.fluttercommunity.plus.sensors

import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.EventChannel.EventSink

internal class StreamHandlerImpl(
private val sensorManager: SensorManager,
sensorType: Int
) : EventChannel.StreamHandler {
private lateinit var sensorEventListener: SensorEventListener

private val sensor: Sensor by lazy {
sensorManager.getDefaultSensor(sensorType)
}

override fun onListen(arguments: Any?, events: EventSink) {
sensorEventListener = createSensorEventListener(events)
sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_NORMAL)
}

override fun onCancel(arguments: Any?) {
sensorManager.unregisterListener(sensorEventListener)
}

private fun createSensorEventListener(events: EventSink): SensorEventListener {
return object : SensorEventListener {
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) {}

override fun onSensorChanged(event: SensorEvent) {
val sensorValues = DoubleArray(event.values.size)
event.values.forEachIndexed { index, value ->
sensorValues[index] = value.toDouble()
}
events.success(sensorValues)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Tue Oct 05 15:47:01 CEST 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
2 changes: 1 addition & 1 deletion packages/sensors_plus/sensors_plus/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: sensors_plus
description: >
Flutter plugin for accessing accelerometer, gyroscope, and magnetometer
sensors.
version: 1.2.2
version: 1.3.0
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/

Expand Down