diff --git a/packages/connectivity/CHANGELOG.md b/packages/connectivity/CHANGELOG.md index e28fcd744805..3165ea93afab 100644 --- a/packages/connectivity/CHANGELOG.md +++ b/packages/connectivity/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.4.5 + +* Support the v2 Android embedder. + ## 0.4.4+1 * Update and migrate iOS example project. diff --git a/packages/connectivity/android/build.gradle b/packages/connectivity/android/build.gradle index 681eb0438b75..3012844d8778 100644 --- a/packages/connectivity/android/build.gradle +++ b/packages/connectivity/android/build.gradle @@ -45,3 +45,28 @@ android { disable 'InvalidPackage' } } + +// TODO(amirh): Remove this hack once androidx.lifecycle is included on stable. https://github.com/flutter/flutter/issues/42348 +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" + } + } + } +} diff --git a/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/Connectivity.java b/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/Connectivity.java new file mode 100644 index 000000000000..605acdb73948 --- /dev/null +++ b/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/Connectivity.java @@ -0,0 +1,110 @@ +// 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.connectivity; + +import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkCapabilities; +import android.net.NetworkInfo; +import android.net.wifi.WifiInfo; +import android.net.wifi.WifiManager; +import android.os.Build; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +/** Reports connectivity related information such as connectivity type and wifi information. */ +class Connectivity { + private ConnectivityManager connectivityManager; + private WifiManager wifiManager; + + Connectivity(ConnectivityManager connectivityManager, WifiManager wifiManager) { + this.connectivityManager = connectivityManager; + this.wifiManager = wifiManager; + } + + @NonNull + String getNetworkType() { + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + Network network = connectivityManager.getActiveNetwork(); + NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network); + if (capabilities == null) { + return "none"; + } + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) + || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { + return "wifi"; + } + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { + return "mobile"; + } + } + + return getNetworkTypeLegacy(); + } + + @Nullable + String getWifiName() { + WifiInfo wifiInfo = getWifiInfo(); + String ssid = null; + if (wifiInfo != null) ssid = wifiInfo.getSSID(); + if (ssid != null) ssid = ssid.replaceAll("\"", ""); // Android returns "SSID" + return ssid; + } + + @Nullable + String getWifiBSSID() { + WifiInfo wifiInfo = getWifiInfo(); + String bssid = null; + if (wifiInfo != null) { + bssid = wifiInfo.getBSSID(); + } + return bssid; + } + + @Nullable + String getWifiIPAddress() { + WifiInfo wifiInfo = null; + if (wifiManager != null) wifiInfo = wifiManager.getConnectionInfo(); + + String ip = null; + int i_ip = 0; + if (wifiInfo != null) i_ip = wifiInfo.getIpAddress(); + + if (i_ip != 0) + ip = + String.format( + "%d.%d.%d.%d", + (i_ip & 0xff), (i_ip >> 8 & 0xff), (i_ip >> 16 & 0xff), (i_ip >> 24 & 0xff)); + + return ip; + } + + @Nullable + private WifiInfo getWifiInfo() { + return wifiManager == null ? null : wifiManager.getConnectionInfo(); + } + + @SuppressWarnings("deprecation") + private String getNetworkTypeLegacy() { + // handle type for Android versions less than Android 9 + NetworkInfo info = connectivityManager.getActiveNetworkInfo(); + if (info == null || !info.isConnected()) { + return "none"; + } + int type = info.getType(); + switch (type) { + case ConnectivityManager.TYPE_ETHERNET: + case ConnectivityManager.TYPE_WIFI: + case ConnectivityManager.TYPE_WIMAX: + return "wifi"; + case ConnectivityManager.TYPE_MOBILE: + case ConnectivityManager.TYPE_MOBILE_DUN: + case ConnectivityManager.TYPE_MOBILE_HIPRI: + return "mobile"; + default: + return "none"; + } + } +} diff --git a/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityBroadcastReceiver.java b/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityBroadcastReceiver.java new file mode 100644 index 000000000000..d046eceb0fa6 --- /dev/null +++ b/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityBroadcastReceiver.java @@ -0,0 +1,51 @@ +// 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.connectivity; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.net.ConnectivityManager; +import androidx.annotation.NonNull; +import io.flutter.plugin.common.EventChannel; + +/** + * The ConnectivityBroadcastReceiver receives the connectivity updates and send them to the UIThread + * through an {@link EventChannel.EventSink} + * + *
Use {@link
+ * io.flutter.plugin.common.EventChannel#setStreamHandler(io.flutter.plugin.common.EventChannel.StreamHandler)}
+ * to set up the receiver.
+ */
+class ConnectivityBroadcastReceiver extends BroadcastReceiver
+ implements EventChannel.StreamHandler {
+ private Context context;
+ private Connectivity connectivity;
+ private EventChannel.EventSink events;
+
+ ConnectivityBroadcastReceiver(@NonNull Context context, @NonNull Connectivity connectivity) {
+ this.context = context;
+ this.connectivity = connectivity;
+ }
+
+ @Override
+ public void onListen(Object arguments, EventChannel.EventSink events) {
+ this.events = events;
+ context.registerReceiver(this, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
+ }
+
+ @Override
+ public void onCancel(Object arguments) {
+ context.unregisterReceiver(this);
+ }
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (events != null) {
+ events.success(connectivity.getNetworkType());
+ }
+ }
+}
diff --git a/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityMethodChannelHandler.java b/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityMethodChannelHandler.java
new file mode 100644
index 000000000000..488c8efdd15f
--- /dev/null
+++ b/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityMethodChannelHandler.java
@@ -0,0 +1,49 @@
+// 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.connectivity;
+
+import androidx.annotation.NonNull;
+import io.flutter.plugin.common.MethodCall;
+import io.flutter.plugin.common.MethodChannel;
+
+/**
+ * The handler receives {@link MethodCall}s from the UIThread, gets the related information from
+ * a @{@link Connectivity}, and then send the result back to the UIThread through the {@link
+ * MethodChannel.Result}.
+ */
+class ConnectivityMethodChannelHandler implements MethodChannel.MethodCallHandler {
+
+ private Connectivity connectivity;
+
+ /**
+ * Construct the ConnectivityMethodChannelHandler with a {@code connectivity}. The {@code
+ * connectivity} must not be null.
+ */
+ ConnectivityMethodChannelHandler(@NonNull Connectivity connectivity) {
+ assert (connectivity != null);
+ this.connectivity = connectivity;
+ }
+
+ @Override
+ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
+ switch (call.method) {
+ case "check":
+ result.success(connectivity.getNetworkType());
+ break;
+ case "wifiName":
+ result.success(connectivity.getWifiName());
+ break;
+ case "wifiBSSID":
+ result.success(connectivity.getWifiBSSID());
+ break;
+ case "wifiIPAddress":
+ result.success(connectivity.getWifiIPAddress());
+ break;
+ default:
+ result.notImplemented();
+ break;
+ }
+ }
+}
diff --git a/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityPlugin.java b/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityPlugin.java
index dac720b0450c..ef8f7861d8e0 100644
--- a/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityPlugin.java
+++ b/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityPlugin.java
@@ -4,186 +4,60 @@
package io.flutter.plugins.connectivity;
-import android.content.BroadcastReceiver;
import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
import android.net.ConnectivityManager;
-import android.net.Network;
-import android.net.NetworkCapabilities;
-import android.net.NetworkInfo;
-import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
-import android.os.Build;
+import io.flutter.embedding.engine.plugins.FlutterPlugin;
+import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
-import io.flutter.plugin.common.EventChannel.EventSink;
-import io.flutter.plugin.common.EventChannel.StreamHandler;
-import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
-import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
-import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
/** ConnectivityPlugin */
-public class ConnectivityPlugin implements MethodCallHandler, StreamHandler {
- private final Registrar registrar;
- private final ConnectivityManager manager;
- private BroadcastReceiver receiver;
+public class ConnectivityPlugin implements FlutterPlugin {
+
+ private MethodChannel methodChannel;
+ private EventChannel eventChannel;
/** Plugin registration. */
public static void registerWith(Registrar registrar) {
- final MethodChannel channel =
- new MethodChannel(registrar.messenger(), "plugins.flutter.io/connectivity");
- final EventChannel eventChannel =
- new EventChannel(registrar.messenger(), "plugins.flutter.io/connectivity_status");
- ConnectivityPlugin instance = new ConnectivityPlugin(registrar);
- channel.setMethodCallHandler(instance);
- eventChannel.setStreamHandler(instance);
- }
- private ConnectivityPlugin(Registrar registrar) {
- this.registrar = registrar;
- this.manager =
- (ConnectivityManager)
- registrar
- .context()
- .getApplicationContext()
- .getSystemService(Context.CONNECTIVITY_SERVICE);
+ ConnectivityPlugin plugin = new ConnectivityPlugin();
+ plugin.setupChannels(registrar.messenger(), registrar.context());
}
@Override
- public void onListen(Object arguments, EventSink events) {
- receiver = createReceiver(events);
- registrar
- .context()
- .registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
+ public void onAttachedToEngine(FlutterPluginBinding binding) {
+ setupChannels(binding.getFlutterEngine().getDartExecutor(), binding.getApplicationContext());
}
@Override
- public void onCancel(Object arguments) {
- registrar.context().unregisterReceiver(receiver);
- receiver = null;
- }
-
- private String getNetworkType(ConnectivityManager manager) {
- if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- Network network = manager.getActiveNetwork();
- NetworkCapabilities capabilities = manager.getNetworkCapabilities(network);
- if (capabilities == null) {
- return "none";
- }
- if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
- || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
- return "wifi";
- }
- if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
- return "mobile";
- }
- }
-
- return getNetworkTypeLegacy(manager);
+ public void onDetachedFromEngine(FlutterPluginBinding binding) {
+ teardownChannels();
}
- @SuppressWarnings("deprecation")
- private String getNetworkTypeLegacy(ConnectivityManager manager) {
- // handle type for Android versions less than Android 9
- NetworkInfo info = manager.getActiveNetworkInfo();
- if (info == null || !info.isConnected()) {
- return "none";
- }
- int type = info.getType();
- switch (type) {
- case ConnectivityManager.TYPE_ETHERNET:
- case ConnectivityManager.TYPE_WIFI:
- case ConnectivityManager.TYPE_WIMAX:
- return "wifi";
- case ConnectivityManager.TYPE_MOBILE:
- case ConnectivityManager.TYPE_MOBILE_DUN:
- case ConnectivityManager.TYPE_MOBILE_HIPRI:
- return "mobile";
- default:
- return "none";
- }
- }
-
- @Override
- public void onMethodCall(MethodCall call, Result result) {
- switch (call.method) {
- case "check":
- handleCheck(call, result);
- break;
- case "wifiName":
- handleWifiName(call, result);
- break;
- case "wifiBSSID":
- handleBSSID(call, result);
- break;
- case "wifiIPAddress":
- handleWifiIPAddress(call, result);
- break;
- default:
- result.notImplemented();
- break;
- }
- }
-
- private void handleCheck(MethodCall call, final Result result) {
- result.success(checkNetworkType());
- }
-
- private String checkNetworkType() {
- return getNetworkType(manager);
- }
-
- private WifiInfo getWifiInfo() {
- WifiManager wifiManager =
- (WifiManager)
- registrar.context().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
- return wifiManager == null ? null : wifiManager.getConnectionInfo();
- }
-
- private void handleWifiName(MethodCall call, final Result result) {
- WifiInfo wifiInfo = getWifiInfo();
- String ssid = null;
- if (wifiInfo != null) ssid = wifiInfo.getSSID();
- if (ssid != null) ssid = ssid.replaceAll("\"", ""); // Android returns "SSID"
- result.success(ssid);
- }
-
- private void handleBSSID(MethodCall call, MethodChannel.Result result) {
- WifiInfo wifiInfo = getWifiInfo();
- String bssid = null;
- if (wifiInfo != null) bssid = wifiInfo.getBSSID();
- result.success(bssid);
- }
-
- private void handleWifiIPAddress(MethodCall call, final Result result) {
- WifiManager wifiManager =
- (WifiManager)
- registrar.context().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
-
- WifiInfo wifiInfo = null;
- if (wifiManager != null) wifiInfo = wifiManager.getConnectionInfo();
+ private void setupChannels(BinaryMessenger messenger, Context context) {
+ methodChannel = new MethodChannel(messenger, "plugins.flutter.io/connectivity");
+ eventChannel = new EventChannel(messenger, "plugins.flutter.io/connectivity_status");
+ ConnectivityManager connectivityManager =
+ (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
+ WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
- String ip = null;
- int i_ip = 0;
- if (wifiInfo != null) i_ip = wifiInfo.getIpAddress();
+ Connectivity connectivity = new Connectivity(connectivityManager, wifiManager);
- if (i_ip != 0)
- ip =
- String.format(
- "%d.%d.%d.%d",
- (i_ip & 0xff), (i_ip >> 8 & 0xff), (i_ip >> 16 & 0xff), (i_ip >> 24 & 0xff));
+ ConnectivityMethodChannelHandler methodChannelHandler =
+ new ConnectivityMethodChannelHandler(connectivity);
+ ConnectivityBroadcastReceiver receiver =
+ new ConnectivityBroadcastReceiver(context, connectivity);
- result.success(ip);
+ methodChannel.setMethodCallHandler(methodChannelHandler);
+ eventChannel.setStreamHandler(receiver);
}
- private BroadcastReceiver createReceiver(final EventSink events) {
- return new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- events.success(checkNetworkType());
- }
- };
+ private void teardownChannels() {
+ methodChannel.setMethodCallHandler(null);
+ eventChannel.setStreamHandler(null);
+ methodChannel = null;
+ eventChannel = null;
}
}
diff --git a/packages/connectivity/example/android/app/src/main/AndroidManifest.xml b/packages/connectivity/example/android/app/src/main/AndroidManifest.xml
index bf36efe1a689..3bf2ca03bc33 100644
--- a/packages/connectivity/example/android/app/src/main/AndroidManifest.xml
+++ b/packages/connectivity/example/android/app/src/main/AndroidManifest.xml
@@ -4,12 +4,20 @@