Skip to content
Snippets Groups Projects
Commit 406c1490 authored by Daniel Schwietert's avatar Daniel Schwietert
Browse files

Update sensor view, add more sensors, add app usage

parent 73adbfbe
No related branches found
No related tags found
1 merge request!1added diary, emoji, image, sensor data, stress scalar
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "stress_detection",
"request": "launch",
"type": "dart"
}
]
}
\ No newline at end of file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flutter_app">
package="com.example.flutter_app"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions"/>
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
......
......@@ -2,201 +2,206 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_sensors/flutter_sensors.dart';
import 'package:app_usage/app_usage.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
class SensorInput extends StatefulWidget {
@override
_SensorInputState createState() => _SensorInputState();
}
class _Sensor {
String name;
String unit;
int type;
bool available = false;
StreamSubscription stream = null;
List<double> data = null;
_Sensor(name, unit, type) {
this.name = name;
this.unit = unit;
this.type = type;
}
}
class _SensorInputState extends State<SensorInput> {
bool _accelAvailable = false;
bool _gyroAvailable = false;
List<double> _accelData = List.filled(3, 0.0);
List<double> _gyroData = List.filled(3, 0.0);
StreamSubscription _accelSubscription;
StreamSubscription _gyroSubscription;
List<AppUsageInfo> _infos = [];
List<_Sensor> _sensors = [
_Sensor("Accelerometer", "m/s2", 1),
_Sensor("Gyroscope", "rad/s", 4),
_Sensor("Proximity", "cm", 8),
_Sensor("Motion", "m/s2", 30),
_Sensor("Humidity", "%", 12),
_Sensor("Pressure", "hPa", 6),
_Sensor("Light", "lux", 5),
_Sensor("Temperature", "°C", 13),
_Sensor("Heartrate", "bpm", 21),
_Sensor("Stepcounter", "#", 19)
];
bool _measuring = false;
@override
void initState() {
_checkAccelerometerStatus();
_checkGyroscopeStatus();
_checkSensors();
super.initState();
}
@override
void dispose() {
_stopAccelerometer();
_stopGyroscope();
_stopSensors();
super.dispose();
}
void _checkAccelerometerStatus() async {
await SensorManager()
.isSensorAvailable(Sensors.ACCELEROMETER)
.then((result) {
setState(() {
_accelAvailable = result;
});
});
}
void _startSensors() async {
_measuring = true;
getUsageStats();
Future<void> _startAccelerometer() async {
if (_accelSubscription != null) return;
if (_accelAvailable) {
for (var sensor in _sensors) {
if (sensor.stream != null) return;
if (sensor.available) {
final stream = await SensorManager().sensorUpdates(
sensorId: Sensors.ACCELEROMETER,
sensorId: sensor.type,
interval: Sensors.SENSOR_DELAY_FASTEST,
);
_accelSubscription = stream.listen((sensorEvent) {
sensor.stream = stream.listen((sensorEvent) {
setState(() {
_accelData = sensorEvent.data;
sensor.data = sensorEvent.data;
});
});
}
}
}
void _stopAccelerometer() {
if (_accelSubscription == null) return;
_accelSubscription.cancel();
_accelSubscription = null;
void _stopSensors() async {
_measuring = false;
for (var sensor in _sensors) {
if (sensor.stream == null) return;
sensor.stream.cancel();
sensor.stream = null;
}
;
}
void _checkGyroscopeStatus() async {
await SensorManager().isSensorAvailable(Sensors.GYROSCOPE).then((result) {
void _checkSensors() async {
for (var sensor in _sensors) {
await SensorManager().isSensorAvailable(sensor.type).then((result) {
setState(() {
_gyroAvailable = result;
sensor.available = result;
});
});
}
}
Future<void> _startGyroscope() async {
if (_gyroSubscription != null) return;
if (_gyroAvailable) {
final stream =
await SensorManager().sensorUpdates(sensorId: Sensors.GYROSCOPE);
_gyroSubscription = stream.listen((sensorEvent) {
void getUsageStats() async {
try {
DateTime endDate = new DateTime.now();
DateTime startDate = endDate.subtract(Duration(hours: 1));
List<AppUsageInfo> infoList =
await AppUsage.getAppUsage(startDate, endDate);
setState(() {
_gyroData = sensorEvent.data;
});
_infos = infoList;
});
for (var info in infoList) {
print(info.toString());
}
} on AppUsageException catch (exception) {
print(exception);
}
void _stopGyroscope() {
if (_gyroSubscription == null) return;
_gyroSubscription.cancel();
_gyroSubscription = null;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Sensor Data'),
),
body: Container(
padding: EdgeInsets.all(16.0),
alignment: AlignmentDirectional.topCenter,
child: Column(
children: <Widget>[
Text(
"Accelerometer Test",
textAlign: TextAlign.center,
),
Text(
"Accelerometer Enabled: $_accelAvailable",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Text(
"[0](X) = ${_accelData[0]}",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Text(
"[1](Y) = ${_accelData[1]}",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Text(
"[2](Z) = ${_accelData[2]}",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
List<Widget> getSensorWidgets() {
List<Widget> list = new List<Widget>();
list.add(
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
MaterialButton(
child: Text("Start"),
color: Colors.green,
onPressed: _accelAvailable != null
? () => _startAccelerometer()
: null,
child: Text(_measuring ? "Stop measure" : "Start measure"),
color: _measuring ? Colors.red.shade200 : Colors.green.shade200,
onPressed:
_measuring ? () => _stopSensors() : () => _startSensors(),
),
Padding(
padding: EdgeInsets.all(8.0),
),
MaterialButton(
child: Text("Stop"),
color: Colors.red,
onPressed: _accelAvailable != null
? () => _stopAccelerometer()
: null,
Visibility(
child: SpinKitPulse(
color: Colors.accents.first,
size: 30.0,
),
visible: _measuring,
maintainState: true,
maintainAnimation: true,
maintainSize: true,
)
],
),
Padding(padding: EdgeInsets.only(top: 16.0)),
);
for (var sensor in _sensors)
list.add(Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(padding: EdgeInsets.only(top: 20.0)),
Text(
"Gyroscope Test",
sensor.name,
textAlign: TextAlign.center,
style: TextStyle(
color: sensor.available ? Colors.black : Colors.red.shade300,
fontSize: 20,
),
Text(
"Gyroscope Enabled: $_gyroAvailable",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
if (sensor.data != null)
for (var i = 0; i < sensor.data.length; i++)
Text(
"[0](X) = ${_gyroData[0]}",
"${sensor.unit} ($i) = ${sensor.data[i].toStringAsFixed(2)}",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Text(
"[1](Y) = ${_gyroData[1]}",
]));
list.add(Padding(padding: EdgeInsets.only(top: 20.0)));
list.add(Text("App Usage",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Text(
"[2](Z) = ${_gyroData[2]}",
style: TextStyle(
color: Colors.black,
fontSize: 20,
)));
list.add(Padding(padding: EdgeInsets.only(top: 16.0)));
for (var item in _infos) {
list.add(ListTile(
title: Text(
item.appName,
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
MaterialButton(
child: Text("Start"),
color: Colors.green,
onPressed:
_gyroAvailable != null ? () => _startGyroscope() : null,
),
Padding(
padding: EdgeInsets.all(8.0),
),
MaterialButton(
child: Text("Stop"),
color: Colors.red,
onPressed:
_gyroAvailable != null ? () => _stopGyroscope() : null,
),
],
),
],
trailing: Text(item.usage.inMinutes.toString())));
}
return list;
}
// ### View ###
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Sensor Data'),
),
body: Container(
padding: EdgeInsets.all(16.0),
alignment: AlignmentDirectional.topCenter,
child: ListView(children: getSensorWidgets()),
),
),
);
}
}
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
app_usage:
dependency: "direct main"
description:
name: app_usage
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0-nullsafety.1"
version: "2.5.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0-nullsafety.1"
version: "2.1.0"
cached_network_image:
dependency: "direct dev"
description:
......@@ -28,21 +35,21 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.3"
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0-nullsafety.1"
version: "1.2.0"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.1"
version: "1.1.0"
cloud_firestore:
dependency: "direct main"
description:
......@@ -70,7 +77,7 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0-nullsafety.3"
version: "1.15.0"
convert:
dependency: transitive
description:
......@@ -119,7 +126,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0-nullsafety.1"
version: "1.2.0"
ffi:
dependency: transitive
description:
......@@ -216,6 +223,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.6"
flutter_spinkit:
dependency: "direct main"
description:
name: flutter_spinkit
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.2+1"
flutter_test:
dependency: "direct dev"
description: flutter
......@@ -295,21 +309,21 @@ packages:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.2"
version: "0.6.3"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10-nullsafety.1"
version: "0.12.10"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0-nullsafety.3"
version: "1.3.0"
octo_image:
dependency: transitive
description:
......@@ -323,7 +337,7 @@ packages:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0-nullsafety.1"
version: "1.8.0"
path_provider:
dependency: "direct main"
description:
......@@ -461,7 +475,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0-nullsafety.2"
version: "1.8.0"
sqflite:
dependency: "direct main"
description:
......@@ -482,21 +496,21 @@ packages:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.10.0-nullsafety.1"
version: "1.10.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0-nullsafety.1"
version: "2.1.0"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.1"
version: "1.1.0"
synchronized:
dependency: transitive
description:
......@@ -510,21 +524,21 @@ packages:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0-nullsafety.1"
version: "1.2.0"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19-nullsafety.2"
version: "0.2.19"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0-nullsafety.3"
version: "1.3.0"
uuid:
dependency: transitive
description:
......@@ -538,7 +552,7 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0-nullsafety.3"
version: "2.1.0"
win32:
dependency: transitive
description:
......@@ -554,5 +568,5 @@ packages:
source: hosted
version: "0.1.2"
sdks:
dart: ">=2.10.2 <2.11.0"
flutter: ">=1.22.2 <2.0.0"
dart: ">=2.12.0-0.0 <3.0.0"
flutter: ">=1.22.2"
......@@ -39,6 +39,8 @@ dependencies:
firebase_database: 4.4.0
firebase_core: ^0.5.0
flutter_sensors: ^0.1.6
app_usage: ^1.2.0
flutter_spinkit: "^4.1.2"
dev_dependencies:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment