-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[platform_channels] adds EventChannel Demo #462
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
476f5ca
adds EventChannel demo
AyushBherwani1998 ab48b29
adds test
AyushBherwani1998 6b0b155
improves formatting
AyushBherwani1998 ae8714a
updates demo list
AyushBherwani1998 426a3e3
improves formatting in main.dart
AyushBherwani1998 89badf1
improves formatting of .kt files
AyushBherwani1998 4d59bd3
improves documentation
AyushBherwani1998 42ec132
improves test
AyushBherwani1998 659831a
worked on the reviews
AyushBherwani1998 d0d0c54
updates test
AyushBherwani1998 a2066e2
updates Accelerometer documentation
AyushBherwani1998 f231ce9
revert the UI changes
AyushBherwani1998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...s/android/app/src/main/kotlin/dev/flutter/platform_channels/AccelerometerStreamHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2020 The Flutter team. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package dev.flutter.platform_channels | ||
|
||
import android.hardware.Sensor | ||
import android.hardware.SensorEvent | ||
import android.hardware.SensorEventListener | ||
import android.hardware.SensorManager | ||
import io.flutter.plugin.common.EventChannel | ||
|
||
class AccelerometerStreamHandler(sManager: SensorManager, s: Sensor) : EventChannel.StreamHandler, SensorEventListener { | ||
private val sensorManager: SensorManager = sManager | ||
private val accelerometerSensor: Sensor = s | ||
private lateinit var eventSink: EventChannel.EventSink | ||
|
||
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) { | ||
if (events != null) { | ||
eventSink = events | ||
sensorManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_UI) | ||
} | ||
} | ||
|
||
override fun onCancel(arguments: Any?) { | ||
sensorManager.unregisterListener(this) | ||
} | ||
|
||
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {} | ||
|
||
override fun onSensorChanged(sensorEvent: SensorEvent?) { | ||
if (sensorEvent != null) { | ||
val axisValues = listOf(sensorEvent.values[0], sensorEvent.values[1], sensorEvent.values[2]) | ||
eventSink.success(axisValues) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
platform_channels/lib/src/accelerometer_event_channel.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2020 The Flutter team. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/services.dart'; | ||
|
||
/// This class includes the implementation for [EventChannel] to listen to value | ||
/// changes from the Accelerometer sensor from native side. It has a [readings] | ||
/// getter to provide a stream of [AccelerometerReadings]. | ||
class Accelerometer { | ||
static final _eventChannel = const EventChannel('eventChannelDemo'); | ||
|
||
/// Method responsible for providing a stream of [AccelerometerReadings] to listen | ||
/// to value changes from the Accelerometer sensor. | ||
static Stream<AccelerometerReadings> get readings { | ||
return _eventChannel.receiveBroadcastStream().map( | ||
(dynamic event) => AccelerometerReadings( | ||
event[0] as double, | ||
event[1] as double, | ||
event[2] as double, | ||
), | ||
); | ||
} | ||
} | ||
|
||
class AccelerometerReadings { | ||
/// Acceleration force along the x-axis. | ||
final double x; | ||
|
||
/// Acceleration force along the y-axis. | ||
final double y; | ||
|
||
/// Acceleration force along the z-axis. | ||
final double z; | ||
|
||
AccelerometerReadings(this.x, this.y, this.z); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2020 The Flutter team. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:platform_channels/src/accelerometer_event_channel.dart'; | ||
|
||
/// Demonstrates how to use [EventChannel] to listen continuous values | ||
/// of Accelerometer Sensor from platform. | ||
/// | ||
/// The widget uses a [StreamBuilder] to rebuild it's descendant whenever it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, "the widget" is fine. It's just for the initial sentence that the noun is typically dropped. |
||
/// listens a new value from the [Accelerometer.readings] stream. It has three | ||
/// [Text] widgets to display the value of [AccelerometerReadings.x], | ||
/// [AccelerometerReadings.y], and [AccelerometerReadings.z] respectively. | ||
class EventChannelDemo extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
final textStyle = Theme.of(context).textTheme.headline5; | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: Text('EventChannel Demo'), | ||
), | ||
body: Center( | ||
child: StreamBuilder<AccelerometerReadings>( | ||
stream: Accelerometer.readings, | ||
builder: (context, snapshot) { | ||
if (snapshot.hasError) { | ||
return Text(snapshot.error.toString()); | ||
} else if (snapshot.hasData) { | ||
return Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: [ | ||
Text( | ||
'x axis: ' + snapshot.data.x.toStringAsFixed(3), | ||
style: textStyle, | ||
), | ||
Text( | ||
'y axis: ' + snapshot.data.y.toStringAsFixed(3), | ||
style: textStyle, | ||
), | ||
Text( | ||
'z axis: ' + snapshot.data.z.toStringAsFixed(3), | ||
style: textStyle, | ||
) | ||
], | ||
); | ||
} | ||
|
||
return Text( | ||
'No Data Available', | ||
style: textStyle, | ||
); | ||
}, | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2020 The Flutter team. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:platform_channels/src/event_channel_demo.dart'; | ||
|
||
void main() { | ||
group('EventChannel Demo tests', () { | ||
final sensorValues = [1.3556, 2.3, -0.12]; | ||
setUpAll(() { | ||
// By default EventChannel uses StandardMethodCodec to communicate with | ||
// platform. | ||
const standardMethod = StandardMethodCodec(); | ||
|
||
// This function handles the incoming messages from the platform. It | ||
// calls the BinaryMessenger.setMessageHandler registered for the EventChannel | ||
// and add the incoming message to the StreamController used by the EventChannel | ||
// after decoding the message with codec used by the EventChannel. | ||
void emitValues(ByteData event) { | ||
ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage( | ||
'eventChannelDemo', | ||
event, | ||
(reply) {}, | ||
); | ||
} | ||
|
||
// Register a mock for EventChannel. EventChannel under the hood uses | ||
// MethodChannel to listen and cancel the created stream. | ||
ServicesBinding.instance.defaultBinaryMessenger | ||
.setMockMessageHandler('eventChannelDemo', (message) async { | ||
// Decode the message into MethodCallHandler. | ||
final methodCall = standardMethod.decodeMethodCall(message); | ||
|
||
if (methodCall.method == 'listen') { | ||
// Emit new sensor values. | ||
emitValues(standardMethod.encodeSuccessEnvelope(sensorValues)); | ||
emitValues(null); | ||
return standardMethod.encodeSuccessEnvelope(null); | ||
} else if (methodCall.method == 'cancel') { | ||
return standardMethod.encodeSuccessEnvelope(null); | ||
} else { | ||
fail('Expected listen or cancel'); | ||
} | ||
}); | ||
}); | ||
|
||
testWidgets('EventChannel AccelerometerReadings Stream test', | ||
(tester) async { | ||
await tester.pumpWidget(MaterialApp( | ||
home: EventChannelDemo(), | ||
)); | ||
|
||
await tester.pumpAndSettle(); | ||
|
||
// Check the values of axis. The value is rounded to 3 decimal places. | ||
expect( | ||
find.text('x axis: ' + sensorValues[0].toStringAsFixed(3)), | ||
findsOneWidget, | ||
); | ||
expect( | ||
find.text('y axis: ' + sensorValues[1].toStringAsFixed(3)), | ||
findsOneWidget, | ||
); | ||
expect( | ||
find.text('z axis: ' + sensorValues[2].toStringAsFixed(3)), | ||
findsOneWidget, | ||
); | ||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional suggestion, but you could make this a const constructor.