-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[sensor] Support v2 android embedder. #2164
Changes from 9 commits
2e9cf9f
360484c
623b10b
992115f
c2db84c
63cff27
74a3097
5641b07
493fbca
958ac3c
c2e4152
b0280ee
4fc4882
4a1e1ca
d4f17a8
fd07b5d
9a66c2d
e32b5b5
f8c5e2a
2ee7ea6
096c6b5
8d44c5e
77340b2
6777439
4fb6d02
b656059
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| org.gradle.jvmargs=-Xmx1536M | ||
| android.enableR8=true | ||
| android.useAndroidX=true | ||
| android.enableJetifier=true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,71 +6,63 @@ | |
|
|
||
| import android.content.Context; | ||
| import android.hardware.Sensor; | ||
| import android.hardware.SensorEvent; | ||
| import android.hardware.SensorEventListener; | ||
| import android.hardware.SensorManager; | ||
| import io.flutter.embedding.engine.plugins.FlutterPlugin; | ||
| import io.flutter.plugin.common.BinaryMessenger; | ||
| import io.flutter.plugin.common.EventChannel; | ||
| import io.flutter.plugin.common.PluginRegistry.Registrar; | ||
|
|
||
| /** SensorsPlugin */ | ||
| public class SensorsPlugin implements EventChannel.StreamHandler { | ||
| public class SensorsPlugin implements FlutterPlugin { | ||
| private static final String ACCELEROMETER_CHANNEL_NAME = | ||
| "plugins.flutter.io/sensors/accelerometer"; | ||
| private static final String GYROSCOPE_CHANNEL_NAME = "plugins.flutter.io/sensors/gyroscope"; | ||
| private static final String USER_ACCELEROMETER_CHANNEL_NAME = | ||
| "plugins.flutter.io/sensors/user_accel"; | ||
|
|
||
| private EventChannel accelerometerChannel; | ||
| private EventChannel userAccelChannel; | ||
| private EventChannel gyroscopeChannel; | ||
|
|
||
| /** Plugin registration. */ | ||
| public static void registerWith(Registrar registrar) { | ||
| final EventChannel accelerometerChannel = | ||
| new EventChannel(registrar.messenger(), ACCELEROMETER_CHANNEL_NAME); | ||
| accelerometerChannel.setStreamHandler( | ||
| new SensorsPlugin(registrar.context(), Sensor.TYPE_ACCELEROMETER)); | ||
|
|
||
| final EventChannel userAccelChannel = | ||
| new EventChannel(registrar.messenger(), USER_ACCELEROMETER_CHANNEL_NAME); | ||
| userAccelChannel.setStreamHandler( | ||
| new SensorsPlugin(registrar.context(), Sensor.TYPE_LINEAR_ACCELERATION)); | ||
|
|
||
| final EventChannel gyroscopeChannel = | ||
| new EventChannel(registrar.messenger(), GYROSCOPE_CHANNEL_NAME); | ||
| gyroscopeChannel.setStreamHandler( | ||
| new SensorsPlugin(registrar.context(), Sensor.TYPE_GYROSCOPE)); | ||
| } | ||
|
|
||
| private SensorEventListener sensorEventListener; | ||
| private final SensorManager sensorManager; | ||
| private final Sensor sensor; | ||
|
|
||
| private SensorsPlugin(Context context, int sensorType) { | ||
| sensorManager = (SensorManager) context.getSystemService(context.SENSOR_SERVICE); | ||
| sensor = sensorManager.getDefaultSensor(sensorType); | ||
| SensorsPlugin plugin = new SensorsPlugin(); | ||
| plugin.setupEventChannels(registrar.context(), registrar.messenger()); | ||
| } | ||
|
|
||
| @Override | ||
| public void onListen(Object arguments, EventChannel.EventSink events) { | ||
| sensorEventListener = createSensorEventListener(events); | ||
| sensorManager.registerListener(sensorEventListener, sensor, sensorManager.SENSOR_DELAY_NORMAL); | ||
| public void onAttachedToEngine(FlutterPluginBinding binding) { | ||
| Context context = binding.getApplicationContext(); | ||
| setupEventChannels(context, binding.getFlutterEngine().getDartExecutor()); | ||
| } | ||
|
|
||
| @Override | ||
| public void onCancel(Object arguments) { | ||
| sensorManager.unregisterListener(sensorEventListener); | ||
| public void onDetachedFromEngine(FlutterPluginBinding binding) { | ||
| accelerometerChannel.setStreamHandler(null); | ||
|
||
| userAccelChannel.setStreamHandler(null); | ||
| gyroscopeChannel.setStreamHandler(null); | ||
| } | ||
|
|
||
| SensorEventListener createSensorEventListener(final EventChannel.EventSink events) { | ||
| return new SensorEventListener() { | ||
| @Override | ||
| public void onAccuracyChanged(Sensor sensor, int accuracy) {} | ||
| private void setupEventChannels(Context context, BinaryMessenger messenger) { | ||
| accelerometerChannel = new EventChannel(messenger, ACCELEROMETER_CHANNEL_NAME); | ||
| final StreamHandlerImpl accelerationStreamHandler = | ||
| new StreamHandlerImpl( | ||
| (SensorManager) context.getSystemService(context.SENSOR_SERVICE), | ||
| Sensor.TYPE_ACCELEROMETER); | ||
| accelerometerChannel.setStreamHandler(accelerationStreamHandler); | ||
|
|
||
| userAccelChannel = new EventChannel(messenger, USER_ACCELEROMETER_CHANNEL_NAME); | ||
| final StreamHandlerImpl linearAccelerationStreamHandler = | ||
| new StreamHandlerImpl( | ||
| (SensorManager) context.getSystemService(context.SENSOR_SERVICE), | ||
| Sensor.TYPE_LINEAR_ACCELERATION); | ||
| userAccelChannel.setStreamHandler(linearAccelerationStreamHandler); | ||
|
|
||
| @Override | ||
| public void onSensorChanged(SensorEvent event) { | ||
| double[] sensorValues = new double[event.values.length]; | ||
| for (int i = 0; i < event.values.length; i++) { | ||
| sensorValues[i] = event.values[i]; | ||
| } | ||
| events.success(sensorValues); | ||
| } | ||
| }; | ||
| gyroscopeChannel = new EventChannel(messenger, GYROSCOPE_CHANNEL_NAME); | ||
| final StreamHandlerImpl gyroScopeStreamHandler = | ||
| new StreamHandlerImpl( | ||
| (SensorManager) context.getSystemService(context.SENSOR_SERVICE), | ||
| Sensor.TYPE_GYROSCOPE); | ||
| gyroscopeChannel.setStreamHandler(gyroScopeStreamHandler); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright 2019 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. | ||
|
|
||
| package io.flutter.plugins.sensors; | ||
|
|
||
| import android.hardware.Sensor; | ||
| import android.hardware.SensorEvent; | ||
| import android.hardware.SensorEventListener; | ||
| import android.hardware.SensorManager; | ||
| import io.flutter.plugin.common.EventChannel; | ||
|
|
||
| class StreamHandlerImpl implements EventChannel.StreamHandler { | ||
|
|
||
| private SensorEventListener sensorEventListener; | ||
| private final SensorManager sensorManager; | ||
| private final Sensor sensor; | ||
|
|
||
| StreamHandlerImpl(SensorManager sensorManager, int sensorType) { | ||
| this.sensorManager = sensorManager; | ||
| sensor = sensorManager.getDefaultSensor(sensorType); | ||
| } | ||
|
|
||
| @Override | ||
| public void onListen(Object arguments, EventChannel.EventSink events) { | ||
| sensorEventListener = createSensorEventListener(events); | ||
| sensorManager.registerListener(sensorEventListener, sensor, sensorManager.SENSOR_DELAY_NORMAL); | ||
| } | ||
|
|
||
| @Override | ||
| public void onCancel(Object arguments) { | ||
| sensorManager.unregisterListener(sensorEventListener); | ||
| } | ||
|
|
||
| SensorEventListener createSensorEventListener(final EventChannel.EventSink events) { | ||
| return new SensorEventListener() { | ||
| @Override | ||
| public void onAccuracyChanged(Sensor sensor, int accuracy) {} | ||
|
|
||
| @Override | ||
| public void onSensorChanged(SensorEvent event) { | ||
| double[] sensorValues = new double[event.values.length]; | ||
| for (int i = 0; i < event.values.length; i++) { | ||
| sensorValues[i] = event.values[i]; | ||
| } | ||
| events.success(sensorValues); | ||
| } | ||
| }; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,19 @@ | |
| <uses-permission android:name="android.permission.INTERNET"/> | ||
|
|
||
| <application android:name="io.flutter.app.FlutterApplication" android:label="sensors_example" android:icon="@mipmap/ic_launcher"> | ||
| <activity android:name=".MainActivity" | ||
| <activity android:name=".EmbeddingV1Activity" | ||
| android:launchMode="singleTop" | ||
| android:theme="@android:style/Theme.Black.NoTitleBar" | ||
| android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection" | ||
| android:hardwareAccelerated="true" | ||
| android:windowSoftInputMode="adjustResize"> | ||
| </activity> | ||
| <activity android:name=".MainActivity" | ||
| android:launchMode="singleTop" | ||
|
||
| android:theme="@android:style/Theme.Black.NoTitleBar" | ||
| android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection" | ||
| android:hardwareAccelerated="true" | ||
| android:windowSoftInputMode="adjustResize"> | ||
| <intent-filter> | ||
| <action android:name="android.intent.action.MAIN"/> | ||
| <category android:name="android.intent.category.LAUNCHER"/> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright 2019 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. | ||
|
|
||
| package io.flutter.plugins.sensorsexample; | ||
|
|
||
| import android.os.Bundle; | ||
| import io.flutter.app.FlutterActivity; | ||
| import io.flutter.plugins.GeneratedPluginRegistrant; | ||
|
|
||
| public class EmbeddingV1Activity extends FlutterActivity { | ||
| @Override | ||
| protected void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| GeneratedPluginRegistrant.registerWith(this); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,18 @@ | ||
| // Copyright 2019 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. | ||
|
|
||
| package io.flutter.plugins.sensorsexample; | ||
|
|
||
| import android.os.Bundle; | ||
| import io.flutter.app.FlutterActivity; | ||
| import io.flutter.plugins.GeneratedPluginRegistrant; | ||
| import io.flutter.embedding.android.FlutterActivity; | ||
| import io.flutter.embedding.engine.FlutterEngine; | ||
| import io.flutter.plugins.sensors.SensorsPlugin; | ||
|
|
||
| public class MainActivity extends FlutterActivity { | ||
|
|
||
| @Override | ||
| protected void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| GeneratedPluginRegistrant.registerWith(this); | ||
| public void configureFlutterEngine(FlutterEngine flutterEngine) { | ||
| super.configureFlutterEngine(flutterEngine); | ||
| flutterEngine.getPlugins().add(new SensorsPlugin()); | ||
cyanglaz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| org.gradle.jvmargs=-Xmx1536M | ||
| android.enableR8=true | ||
| android.useAndroidX=true | ||
| android.enableJetifier=true |
Uh oh!
There was an error while loading. Please reload this page.