Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 15 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
1 change: 1 addition & 0 deletions packages/connectivity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* [Android] Updated logic to retrieve network info.


## 0.4.3+3

* Support for TYPE_MOBILE_HIPRI on Android.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.EventChannel.EventSink;
import io.flutter.plugin.common.EventChannel.StreamHandler;
Expand Down Expand Up @@ -76,6 +77,60 @@ private static String getNetworkType(int type) {
return "none";
}
}

private static String getNetworkSubType(int type, int subType) {
switch (type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think if we are going to get the network subtype, we should separate the return value for each subtype. For example, for ConnectivityManager.TYPE_ETHERNET we should just return "TYPE_ETHERNET", and we also should match both Android and iOS to give a main type (e.g. TYPE_MOBILE_HIPRI) and a sub type (e.g. NETWORK_TYPE_1xRTT)
Since we are adding a new method now, you are free to not limit the return type to just a String, it can be an object type for example:

class ConnectionType {
   final String type;
   final String subType;
}

I hope this makes sense.

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:
switch (subType) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
return "2G"; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_CDMA:
return "2G"; // ~ 14-64 kbps
case TelephonyManager.NETWORK_TYPE_EDGE:
return "2G"; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return "3G"; // ~ 400-1000 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return "3G"; // ~ 600-1400 kbps
case TelephonyManager.NETWORK_TYPE_GPRS:
return "2G"; // ~ 100 kbps
case TelephonyManager.NETWORK_TYPE_HSDPA:
return "3G"; // ~ 2-14 Mbps
case TelephonyManager.NETWORK_TYPE_HSPA:
return "3G"; // ~ 700-1700 kbps
case TelephonyManager.NETWORK_TYPE_HSUPA:
return "3G"; // ~ 1-23 Mbps
case TelephonyManager.NETWORK_TYPE_UMTS:
return "3G"; // ~ 400-7000 kbps
/*
* Above API level 7, make sure to set android:targetSdkVersion
* to appropriate level to use these
*/
case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
return "3G"; // ~ 1-2 Mbps
case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
return "3G"; // ~ 5 Mbps
case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
return "3G"; // ~ 10-20 Mbps
case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
return "2G"; // ~25 kbps
case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
return "4G"; // ~ 10+ Mbps
// Unknown
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
default:
return "none";
}
default:
return "none";
}
}

@Override
public void onMethodCall(MethodCall call, Result result) {
Expand All @@ -92,12 +147,24 @@ public void onMethodCall(MethodCall call, Result result) {
case "wifiIPAddress":
handleWifiIPAddress(call, result);
break;
case "getMobileConnectionType"
handleMobileConnectionType(call, result);
break;
default:
result.notImplemented();
break;
}
}

private void handleMobileConnectionType(MethodCall call, final Result result) {
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
result.success(getNetworkSubType(info.getType(), info.getSubtype()));
} else {
result.success("none");
}
}

private void handleCheck(MethodCall call, final Result result) {
result.success(checkNetworkType());
}
Expand Down Expand Up @@ -158,6 +225,16 @@ private BroadcastReceiver createReceiver(final EventSink events) {
return new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean isLost = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

It is not clear to me what does these code do. Could you explain? It doesn't seem to be related to the intention of this PR. I might be missing something though

if (isLost) {
events.success("none");
return;
}

int type = intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, -1);
TelephonyManager mTelephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int subType = mTelephonyManager.getNetworkType();
events.success(checkNetworkType());
}
};
Expand Down
42 changes: 41 additions & 1 deletion packages/connectivity/ios/Classes/ConnectivityPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ @interface FLTConnectivityPlugin () <FlutterStreamHandler>
@implementation FLTConnectivityPlugin {
FlutterEventSink _eventSink;
}
-

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FLTConnectivityPlugin* instance = [[FLTConnectivityPlugin alloc] init];

FlutterMethodChannel* channel =
Expand Down Expand Up @@ -85,6 +86,43 @@ - (NSString*)getWifiIP {
return address;
}

- (NSString*)getMobileConnectionType:(Reachability*)reachability {
NetworkStatus status = [reachability currentReachabilityStatus];
switch (status) {
case NotReachable:
return @"none";
case ReachableViaWiFi:
return @"wifi";
case ReachableViaWWAN:
CTTelephonyNetworkInfo* netinfo = [[CTTelephonyNetworkInfo alloc] init];
NSString* carrierType = netinfo.currentRadioAccessTechnology;
if ([carrierType isEqualToString:CTRadioAccessTechnologyGPRS]) {
return @"2G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyEdge]) {
return @"2G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyWCDMA]) {
return @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyHSDPA]) {
return @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyHSUPA]) {
return @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
return @"2G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
return @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
return @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
return @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyeHRPD]) {
return @"3G";
} else if ([carrierType isEqualToString:CTRadioAccessTechnologyLTE]) {
return @"4G";
}
return @"none";
}
}

- (NSString*)statusFromReachability:(Reachability*)reachability {
NetworkStatus status = [reachability currentReachabilityStatus];
switch (status) {
Expand All @@ -111,6 +149,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
result([self getBSSID]);
} else if ([call.method isEqualToString:@"wifiIPAddress"]) {
result([self getWifiIP]);
} else if ([call.method isEqualToString:@"getMobileConnectionType"]) {
result([self getMobileConnectionType]);
} else {
result(FlutterMethodNotImplemented);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/connectivity/lib/connectivity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ class Connectivity {
Future<String> getWifiIP() async {
return await methodChannel.invokeMethod<String>('wifiIPAddress');
}

Future<String> getMobileConnectionType() async {
return await methodChannel.invokeMethod<String>('getMobileConnectionType');
}
}

ConnectivityResult _parseConnectivityResult(String state) {
Expand Down
2 changes: 2 additions & 0 deletions packages/connectivity/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ description: Flutter plugin for discovering the state of the network (WiFi &
mobile/cellular) connectivity on Android and iOS.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity

Copy link
Contributor

Choose a reason for hiding this comment

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

revert

Copy link
Contributor

Choose a reason for hiding this comment

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

extra line here, could you remove it?

version: 0.4.3+4
Copy link
Contributor

Choose a reason for hiding this comment

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

please remember to update the pubspec and changelog to a new version after everything is done.


Copy link
Contributor

Choose a reason for hiding this comment

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

another extra line here.


Copy link
Contributor

Choose a reason for hiding this comment

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

revert

flutter:
plugin:
androidPackage: io.flutter.plugins.connectivity
Expand Down