-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Connectivity] migrate to the new android embedding #2142
Changes from 1 commit
ae42d12
67f4f70
6aebaf7
f682eb7
8922d5b
b24cfc9
7267b11
4926557
60dde9e
24d949a
13c60dd
1516b81
b3abbe4
a212fc9
dfcd8c9
98c2d09
a58e305
ba9a2fb
43bfaea
ba0bb22
ef9762e
e118ec6
5194df3
17213fd
1ee15d8
114435d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,41 +4,35 @@ | |
|
|
||
| package dev.flutter.plugins.connectivity; | ||
|
|
||
| import android.hardware.camera2.CameraAccessException; | ||
| import android.os.Build; | ||
|
|
||
| //import androidx.annotation.NonNull; | ||
|
|
||
| import io.flutter.embedding.engine.plugins.FlutterPlugin; | ||
| import io.flutter.embedding.engine.plugins.activity.ActivityAware; | ||
| import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; | ||
| import io.flutter.plugin.common.EventChannel; | ||
| 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; | ||
| import io.flutter.plugins.connectivity.ConnectivityMethodChannelHandler; | ||
|
|
||
| public class ConnectivityPlugin implements FlutterPlugin { | ||
|
|
||
| private FlutterPluginBinding pluginBinding; | ||
|
|
||
| @Override | ||
| public void onAttachedToEngine(FlutterPluginBinding binding) { | ||
| this.pluginBinding = binding; | ||
| final MethodChannel channel = | ||
| new MethodChannel(pluginBinding.getFlutterEngine().getDartExecutor(), "plugins.flutter.io/connectivity"); | ||
| final EventChannel eventChannel = new EventChannel(pluginBinding.getFlutterEngine().getDartExecutor(), "plugins.flutter.io/connectivity_status"); | ||
|
|
||
| ConnectivityMethodChannelHandler handler = new ConnectivityMethodChannelHandler(pluginBinding.getApplicationContext()); | ||
|
|
||
| channel.setMethodCallHandler(handler); | ||
| eventChannel.setStreamHandler(handler); | ||
| } | ||
|
|
||
| @Override | ||
| public void onDetachedFromEngine(FlutterPluginBinding binding) { | ||
| this.pluginBinding = null; | ||
| } | ||
| private FlutterPluginBinding pluginBinding; | ||
|
|
||
| @Override | ||
| public void onAttachedToEngine(FlutterPluginBinding binding) { | ||
| this.pluginBinding = binding; | ||
|
||
| final MethodChannel channel = | ||
| new MethodChannel( | ||
| pluginBinding.getFlutterEngine().getDartExecutor(), "plugins.flutter.io/connectivity"); | ||
| final EventChannel eventChannel = | ||
| new EventChannel( | ||
| pluginBinding.getFlutterEngine().getDartExecutor(), | ||
| "plugins.flutter.io/connectivity_status"); | ||
|
|
||
| ConnectivityMethodChannelHandler handler = | ||
| new ConnectivityMethodChannelHandler(pluginBinding.getApplicationContext()); | ||
|
|
||
| channel.setMethodCallHandler(handler); | ||
| eventChannel.setStreamHandler(handler); | ||
| } | ||
|
|
||
| @Override | ||
| public void onDetachedFromEngine(FlutterPluginBinding binding) { | ||
| this.pluginBinding = null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,155 +11,153 @@ | |
| import android.net.wifi.WifiInfo; | ||
| import android.net.wifi.WifiManager; | ||
| import android.os.Build; | ||
|
|
||
| import io.flutter.plugin.common.EventChannel; | ||
| import io.flutter.plugin.common.MethodCall; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
| import io.flutter.plugin.common.PluginRegistry; | ||
| import io.flutter.view.TextureRegistry; | ||
|
|
||
| public class ConnectivityMethodChannelHandler implements MethodChannel.MethodCallHandler, EventChannel.StreamHandler { | ||
|
|
||
| private final Context context; | ||
| private ConnectivityManager manager; | ||
| private BroadcastReceiver receiver; | ||
|
|
||
| public ConnectivityMethodChannelHandler(Context context) { | ||
| assert(context != null); | ||
| this.context = context; | ||
| this.manager = (ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); | ||
| } | ||
|
|
||
| @Override | ||
| public void onListen(Object arguments, EventChannel.EventSink events) { | ||
| context.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); | ||
| } | ||
|
|
||
| @Override | ||
| public void onCancel(Object arguments) { | ||
| context.unregisterReceiver(receiver); | ||
| receiver = null; | ||
| } | ||
|
|
||
| @Override | ||
| public void onMethodCall(MethodCall call, MethodChannel.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 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); | ||
| } | ||
|
|
||
| @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"; | ||
| } | ||
| public class ConnectivityMethodChannelHandler | ||
| implements MethodChannel.MethodCallHandler, EventChannel.StreamHandler { | ||
|
|
||
| private final Context context; | ||
| private ConnectivityManager manager; | ||
| private BroadcastReceiver receiver; | ||
|
|
||
| public ConnectivityMethodChannelHandler(Context context) { | ||
|
||
| assert (context != null); | ||
| this.context = context; | ||
| this.manager = | ||
| (ConnectivityManager) | ||
| context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); | ||
| } | ||
|
|
||
| @Override | ||
| public void onListen(Object arguments, EventChannel.EventSink events) { | ||
|
||
| context.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); | ||
| } | ||
|
|
||
| @Override | ||
| public void onCancel(Object arguments) { | ||
| context.unregisterReceiver(receiver); | ||
| receiver = null; | ||
| } | ||
|
|
||
| @Override | ||
| public void onMethodCall(MethodCall call, MethodChannel.Result result) { | ||
|
Contributor
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. Would you mind adding unit tests for this class to ensure that incoming messages are correctly parsed, and outgoing messages are correctly serialized?
Contributor
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. I think that Dart e2e integration tests would be acceptable as an alternative to unit tests. |
||
| 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 BroadcastReceiver createReceiver(final EventChannel.EventSink events) { | ||
| return new BroadcastReceiver() { | ||
| @Override | ||
| public void onReceive(Context context, Intent intent) { | ||
| events.success(checkNetworkType()); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private String checkNetworkType() { | ||
| return getNetworkType(manager); | ||
| } | ||
|
|
||
| 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"; | ||
| } | ||
| } | ||
|
|
||
| private void handleCheck(MethodCall call, final MethodChannel.Result result) { | ||
| result.success(checkNetworkType()); | ||
| } | ||
| return getNetworkTypeLegacy(manager); | ||
| } | ||
|
|
||
| private WifiInfo getWifiInfo() { | ||
| WifiManager wifiManager = | ||
| (WifiManager) | ||
| context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); | ||
| return wifiManager == null ? null : wifiManager.getConnectionInfo(); | ||
| @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"; | ||
| } | ||
|
|
||
| private void handleWifiName(MethodCall call, final MethodChannel.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 MethodChannel.Result result) { | ||
| WifiManager wifiManager = | ||
| (WifiManager) | ||
| context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); | ||
|
|
||
| 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)); | ||
|
|
||
| result.success(ip); | ||
| 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"; | ||
| } | ||
| } | ||
|
|
||
| private BroadcastReceiver createReceiver(final EventChannel.EventSink events) { | ||
|
||
| return new BroadcastReceiver() { | ||
| @Override | ||
| public void onReceive(Context context, Intent intent) { | ||
| events.success(checkNetworkType()); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private String checkNetworkType() { | ||
| return getNetworkType(manager); | ||
| } | ||
|
|
||
| private void handleCheck(MethodCall call, final MethodChannel.Result result) { | ||
| result.success(checkNetworkType()); | ||
| } | ||
|
|
||
| private WifiInfo getWifiInfo() { | ||
| WifiManager wifiManager = | ||
| (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); | ||
| return wifiManager == null ? null : wifiManager.getConnectionInfo(); | ||
| } | ||
|
|
||
| private void handleWifiName(MethodCall call, final MethodChannel.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 MethodChannel.Result result) { | ||
| WifiManager wifiManager = | ||
|
||
| (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); | ||
|
|
||
| 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)); | ||
|
|
||
| result.success(ip); | ||
| } | ||
| } | ||
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.
Would you mind add Javadocs to all public, non-trivial classes/methods/fields?
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.
New classes members/methods added should definitely be added with Javadocs.
Regarding all existing code - this is definitely tech debt we should be paying, and is welcomed if the author is interested in doing it as part of this PR, though it shouldn't block it. For the purpose of supporting the v2 embedder in all plugins I suggest we focus PRs and reviews on that goal. Please do leave any improvement suggestions of existing code as non-blocking nits.
I think this fits with the spirit of the Flutter code review guidelines: