-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Connectivity] migrate to the new android embedding #2142
Changes from 2 commits
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
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package io.flutter.plugins.connectivity; | ||
|
|
||
| import android.content.BroadcastReceiver; | ||
| import androidx.annotation.NonNull; | ||
| import io.flutter.plugin.common.EventChannel; | ||
|
|
||
| /** Responsible for constructing a BroadcastReceiver as well as registering and unregistering it. */ | ||
| public interface BroadcastReceiverRegistrar { | ||
|
|
||
| /** | ||
| * Triggered when it is ready for the BroadcastReceiver to be registered. Register the receiver in | ||
|
||
| * this method body. | ||
| * | ||
| * @param receiver the receiver is going to be registered. | ||
| */ | ||
| void readyToRegisterBroadcastReceiver(@NonNull BroadcastReceiver receiver); | ||
|
|
||
| /** | ||
| * Triggered when it is ready for the BroadcastReceiver to be unregistered. Unregister the | ||
|
||
| * receiver in this method body. | ||
| * | ||
| * @param receiver the receiver is going to be unregistered. | ||
| */ | ||
| void readyToUnregisterBroadcastReceiver(@NonNull BroadcastReceiver receiver); | ||
|
|
||
| /** | ||
| * Creates a Broadcast receiver. | ||
|
||
| * | ||
| * @param events The events helps the broadcast receiver to dump information to the event channel. | ||
| * @return A BroadcastReceiver. | ||
| */ | ||
| @NonNull | ||
| BroadcastReceiver createReceiver(@NonNull final EventChannel.EventSink events); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| 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 BroadcastReceiverRegistrar used for the plugin. */ | ||
|
||
| public class BroadcastReceiverRegistrarImpl implements BroadcastReceiverRegistrar { | ||
| private Context context; | ||
| private Connectivity connectivity; | ||
|
|
||
| /** | ||
| * @param context used to register and unregister the broadcastReceiver. | ||
|
||
| * @param connectivity used to check connectivity information. | ||
| */ | ||
| public BroadcastReceiverRegistrarImpl( | ||
| @NonNull Context context, @NonNull Connectivity connectivity) { | ||
| this.context = context; | ||
| this.connectivity = connectivity; | ||
| } | ||
|
|
||
| @Override | ||
| public void readyToRegisterBroadcastReceiver(BroadcastReceiver receiver) { | ||
|
||
| context.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); | ||
| } | ||
|
|
||
| @Override | ||
| public void readyToUnregisterBroadcastReceiver(BroadcastReceiver receiver) { | ||
| context.unregisterReceiver(receiver); | ||
| } | ||
|
|
||
| @Override | ||
| public BroadcastReceiver createReceiver(final EventChannel.EventSink events) { | ||
| return new BroadcastReceiver() { | ||
| @Override | ||
| public void onReceive(Context context, Intent intent) { | ||
| events.success(connectivity.checkNetworkType()); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| 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; | ||
|
|
||
|
|
||
| /** Responsible for checking connectivity information. */ | ||
|
||
| public class Connectivity { | ||
| private ConnectivityManager connectivityManager; | ||
| private WifiManager wifiManager; | ||
|
|
||
| /** | ||
| * Constructs a ConnectivityChecker | ||
|
||
| * | ||
| * @param connectivityManager used to check connectivity information. | ||
| * @param wifiManager used to check wifi information. | ||
| */ | ||
| public Connectivity(ConnectivityManager connectivityManager, WifiManager wifiManager) { | ||
| this.connectivityManager = connectivityManager; | ||
| this.wifiManager = wifiManager; | ||
| } | ||
|
|
||
| @NonNull | ||
| String checkNetworkType() { | ||
|
||
| return getNetworkType(); | ||
| } | ||
|
|
||
| @Nullable | ||
| String getWifiName() { | ||
|
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. Should all of these methods being with "/* package */"? @mklim for style input
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. It's one of those totally subjective style things, like annotating primitive arguments with |
||
| 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(); | ||
| } | ||
|
|
||
| private 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(); | ||
| } | ||
|
|
||
| @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"; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package io.flutter.plugins.connectivity; | ||
|
|
||
| import android.content.BroadcastReceiver; | ||
| import androidx.annotation.NonNull; | ||
| import io.flutter.plugin.common.EventChannel; | ||
|
|
||
| /** Handles the event channel for the plugin. */ | ||
|
||
| public class ConnectivityEventChannelHandler implements EventChannel.StreamHandler { | ||
| private final BroadcastReceiverRegistrar broadcastReceiverRegistrar; | ||
| private BroadcastReceiver broadcastReceiver; | ||
|
|
||
| /** | ||
| * Constructs a ConnectivityEventChannelHandler | ||
|
||
| * | ||
| * @param broadcastReceiverRegistrar handling registration of the broadcastReceiver. | ||
| */ | ||
| public ConnectivityEventChannelHandler( | ||
| @NonNull BroadcastReceiverRegistrar broadcastReceiverRegistrar) { | ||
| this.broadcastReceiverRegistrar = broadcastReceiverRegistrar; | ||
| } | ||
|
|
||
| @Override | ||
| public void onListen(Object arguments, EventChannel.EventSink events) { | ||
| broadcastReceiver = broadcastReceiverRegistrar.createReceiver(events); | ||
| broadcastReceiverRegistrar.readyToRegisterBroadcastReceiver(broadcastReceiver); | ||
| } | ||
|
|
||
| @Override | ||
| public void onCancel(Object arguments) { | ||
| broadcastReceiverRegistrar.readyToUnregisterBroadcastReceiver(broadcastReceiver); | ||
| broadcastReceiver = null; | ||
| } | ||
| } | ||
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.
While this statement is true, it may not be useful for developers. When a developer reads this doc, what kinds of questions might that developer be attempting to answer?
Flutter style guide: Writing prompts for good documentation
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#writing-prompts-for-good-documentation