Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[sensor] Support v2 android embedder. #2164

Merged
merged 26 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from 14 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
4 changes: 3 additions & 1 deletion packages/connectivity/ios/Classes/ConnectivityPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ - (NSString*)convertCLAuthorizationStatusToString:(CLAuthorizationStatus)status
case kCLAuthorizationStatusAuthorizedWhenInUse: {
return @"authorizedWhenInUse";
}
default: { return @"unknown"; }
default: {
return @"unknown";
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions packages/sensors/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,27 @@ android {
disable 'InvalidPackage'
}
}

afterEvaluate {
def containsEmbeddingDependencies = false
for (def configuration : configurations.all) {
for (def dependency : configuration.dependencies) {
if (dependency.group == 'io.flutter' &&
dependency.name.startsWith('flutter_embedding') &&
dependency.isTransitive())
{
containsEmbeddingDependencies = true
break
}
}
}
if (!containsEmbeddingDependencies) {
android {
dependencies {
def lifecycle_version = "2.1.0"
api "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
api "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
final 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommendation: If you've defined a setupEventChannels() that contains setup behavior, consider also defining a teardownEventChannels() that does the opposite, rather than directly implementing that behavior in onDetachedFromEngine().

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
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @cyanglaz @amirh @mklim @collinjackson @bparrishMines @kroikie I don't think we require the singleTop launch mode any more. If you discover the opposite, please let me know, but it is probably best not to include that in the example unless it is actually required. From an add-to-app perspective, single top is a problem.

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"/>
Expand Down
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());
}
}
3 changes: 3 additions & 0 deletions packages/sensors/example/android/gradle.properties
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