From ebca508839eab6bf36c19cacc8d71af0a94bdd0e Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Mon, 5 Aug 2019 13:42:54 +0000 Subject: [PATCH 01/25] added mobile connection subtype information(android,ios) --- .../connectivity/ConnectivityPlugin.java | 69 ++++++++++++++++++- .../ios/Runner.xcodeproj/project.pbxproj | 9 +-- packages/connectivity/example/lib/main.dart | 10 ++- .../example/test_driver/connectivity.dart | 2 +- .../ios/Classes/ConnectivityPlugin.m | 44 +++++++++++- packages/connectivity/lib/connectivity.dart | 67 +++++++++++++++--- 6 files changed, 178 insertions(+), 23 deletions(-) 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..08703d3a22f9 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 @@ -15,6 +15,8 @@ import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Build; +import android.telephony.TelephonyManager; + import io.flutter.plugin.common.EventChannel; import io.flutter.plugin.common.EventChannel.EventSink; import io.flutter.plugin.common.EventChannel.StreamHandler; @@ -24,13 +26,17 @@ import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; -/** ConnectivityPlugin */ +/** + * ConnectivityPlugin + */ public class ConnectivityPlugin implements MethodCallHandler, StreamHandler { private final Registrar registrar; private final ConnectivityManager manager; private BroadcastReceiver receiver; - /** Plugin registration. */ + /** + * Plugin registration. + */ public static void registerWith(Registrar registrar) { final MethodChannel channel = new MethodChannel(registrar.messenger(), "plugins.flutter.io/connectivity"); @@ -106,6 +112,56 @@ private String getNetworkTypeLegacy(ConnectivityManager manager) { } } + private static String getNetworkSubType(ConnectivityManager manager) { + NetworkInfo info = manager.getActiveNetworkInfo(); + + if (info == null || !info.isConnected()) + return "none"; + + switch (info.getSubtype()) { + 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: + return "unknown"; // is connected but cannot tell the speed + default: + return "none"; + } + } + + @Override public void onMethodCall(MethodCall call, Result result) { switch (call.method) { @@ -121,6 +177,9 @@ public void onMethodCall(MethodCall call, Result result) { case "wifiIPAddress": handleWifiIPAddress(call, result); break; + case "subtype": + handleNetworkSubType(call, result); + break; default: result.notImplemented(); break; @@ -131,8 +190,12 @@ private void handleCheck(MethodCall call, final Result result) { result.success(checkNetworkType()); } + private void handleNetworkSubType(MethodCall call, final Result result) { + result.success(getNetworkSubType(manager)); + } + private String checkNetworkType() { - return getNetworkType(manager); + return getNetworkType(manager) + "," + getNetworkSubType(manager); } private WifiInfo getWifiInfo() { diff --git a/packages/connectivity/example/ios/Runner.xcodeproj/project.pbxproj b/packages/connectivity/example/ios/Runner.xcodeproj/project.pbxproj index 78c4f7c8f116..2cdec60747b0 100644 --- a/packages/connectivity/example/ios/Runner.xcodeproj/project.pbxproj +++ b/packages/connectivity/example/ios/Runner.xcodeproj/project.pbxproj @@ -39,10 +39,12 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 0B3C39CAD8BDF66703B2A5F7 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = ""; }; 1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = ""; }; 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = ""; }; 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = ""; }; 3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = ""; }; + 3DFD50681F014E6A560C2D40 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = ""; }; 7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = ""; }; 7AFFD8ED1D35381100E5BB4D /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; @@ -83,6 +85,8 @@ 8ECC1C323F60D5498EEC2315 /* Pods */ = { isa = PBXGroup; children = ( + 0B3C39CAD8BDF66703B2A5F7 /* Pods-Runner.debug.xcconfig */, + 3DFD50681F014E6A560C2D40 /* Pods-Runner.release.xcconfig */, ); name = Pods; sourceTree = ""; @@ -256,16 +260,13 @@ files = ( ); inputPaths = ( - "${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh", - "${PODS_ROOT}/../.symlinks/flutter/ios/Flutter.framework", ); name = "[CP] Embed Pods Frameworks"; outputPaths = ( - "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n"; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n"; showEnvVarsInLog = 0; }; 9740EEB61CF901F6004384FC /* Run Script */ = { diff --git a/packages/connectivity/example/lib/main.dart b/packages/connectivity/example/lib/main.dart index 3784a22fc241..4f9e460556d2 100644 --- a/packages/connectivity/example/lib/main.dart +++ b/packages/connectivity/example/lib/main.dart @@ -44,8 +44,7 @@ class _MyHomePageState extends State { void initState() { super.initState(); initConnectivity(); - _connectivitySubscription = - _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); + _connectivitySubscription = _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); } @override @@ -85,7 +84,7 @@ class _MyHomePageState extends State { } Future _updateConnectionStatus(ConnectivityResult result) async { - switch (result) { + switch (result.type) { case ConnectivityResult.wifi: String wifiName, wifiBSSID, wifiIP; @@ -118,6 +117,11 @@ class _MyHomePageState extends State { }); break; case ConnectivityResult.mobile: + setState(() { + _connectionStatus = '$result\n' + 'Mobile Connection Type: ${result.subtype}\n'; + }); + break; case ConnectivityResult.none: setState(() => _connectionStatus = result.toString()); break; diff --git a/packages/connectivity/example/test_driver/connectivity.dart b/packages/connectivity/example/test_driver/connectivity.dart index 7efba5e4ab50..188e7bf8c775 100644 --- a/packages/connectivity/example/test_driver/connectivity.dart +++ b/packages/connectivity/example/test_driver/connectivity.dart @@ -18,7 +18,7 @@ void main() { test('test connectivity result', () async { final ConnectivityResult result = await _connectivity.checkConnectivity(); expect(result, isNotNull); - switch (result) { + switch (result.type) { case ConnectivityResult.wifi: expect(_connectivity.getWifiName(), completes); expect(_connectivity.getWifiBSSID(), completes); diff --git a/packages/connectivity/ios/Classes/ConnectivityPlugin.m b/packages/connectivity/ios/Classes/ConnectivityPlugin.m index e92fff220a2e..cc2b0dc1c5de 100644 --- a/packages/connectivity/ios/Classes/ConnectivityPlugin.m +++ b/packages/connectivity/ios/Classes/ConnectivityPlugin.m @@ -12,6 +12,8 @@ #include +#import + @interface FLTConnectivityPlugin () @end @@ -85,15 +87,48 @@ - (NSString*)getWifiIP { return address; } + +- (NSString*)getConnectionSubtype:(Reachability*)reachability { + + 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]; + NSString *subtype = [self getConnectionSubtype:[Reachability reachabilityForInternetConnection]]; switch (status) { case NotReachable: - return @"none"; + return [NSString stringWithFormat:@"%@,%@", @"none" , subtype]; case ReachableViaWiFi: - return @"wifi"; + return [NSString stringWithFormat:@"%@,%@", @"wifi" , subtype]; case ReachableViaWWAN: - return @"mobile"; + return [NSString stringWithFormat:@"%@,%@", @"mobile" , subtype]; } } @@ -111,6 +146,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { result([self getBSSID]); } else if ([call.method isEqualToString:@"wifiIPAddress"]) { result([self getWifiIP]); + }else if ([call.method isEqualToString:@"subtype"]) { + result([self getConnectionSubtype:[Reachability reachabilityForInternetConnection]]); + } else { result(FlutterMethodNotImplemented); } diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index a1fd21cb1668..8fe3f7af91e3 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -12,7 +12,19 @@ import 'package:meta/meta.dart'; /// WiFi: Device connected via Wi-Fi /// Mobile: Device connected to cellular network /// None: Device not connected to any network -enum ConnectivityResult { wifi, mobile, none } +enum ConnectionType { wifi, mobile, none } +enum ConnectionSubtype { _2G, _3G, _4G, none, unknown } + +class ConnectivityResult { + static const ConnectionType wifi = ConnectionType.wifi; + static const ConnectionType mobile = ConnectionType.mobile; + static const ConnectionType none = ConnectionType.none; + + final ConnectionType type; + final ConnectionSubtype subtype; + + ConnectivityResult(this.type, this.subtype); +} class Connectivity { /// Constructs a singleton instance of [Connectivity]. @@ -47,9 +59,8 @@ class Connectivity { /// Fires whenever the connectivity state changes. Stream get onConnectivityChanged { if (_onConnectivityChanged == null) { - _onConnectivityChanged = eventChannel - .receiveBroadcastStream() - .map((dynamic event) => _parseConnectivityResult(event)); + _onConnectivityChanged = + eventChannel.receiveBroadcastStream().map((dynamic event) => _parseConnectivityResult(event)); } return _onConnectivityChanged; } @@ -60,11 +71,24 @@ class Connectivity { /// make a network request. It only gives you the radio status. /// /// Instead listen for connectivity changes via [onConnectivityChanged] stream. - Future checkConnectivity() async { + Future checkConnectivity({bool checkSubtype = false}) async { final String result = await methodChannel.invokeMethod('check'); + String subtype; + if (checkSubtype) subtype = await getNetworkSubtype(); return _parseConnectivityResult(result); } + /// Checks the network mobile connection subtype of the device. + /// Return 2G, 3G, 4G depending on the connection of the mobile connection + /// if it is connected. + /// + /// Return none if there is no connections + /// + /// Return unknown if it is connected but there is not connection subtype info. eg. Wifi + Future getNetworkSubtype() async { + return await methodChannel.invokeMethod('subtype'); + } + /// Obtains the wifi name (SSID) of the connected network /// /// Please note that it DOESN'T WORK on emulators (returns null). @@ -96,13 +120,38 @@ class Connectivity { } ConnectivityResult _parseConnectivityResult(String state) { - switch (state) { + ConnectionType type = ConnectionType.none; + ConnectionSubtype subType = ConnectionSubtype.unknown; + + final List split = state.split(","); + + switch (split[0]) { case 'wifi': - return ConnectivityResult.wifi; + type = ConnectionType.wifi; + break; case 'mobile': - return ConnectivityResult.mobile; + type = ConnectionType.mobile; + break; + case 'none': + default: + type = ConnectionType.none; + } + switch (split[1]) { + case '2G': + subType = ConnectionSubtype._2G; + break; + case '3G': + subType = ConnectionSubtype._3G; + break; + case '4G': + subType = ConnectionSubtype._4G; + break; + case 'unknown': + subType = ConnectionSubtype.unknown; + break; case 'none': default: - return ConnectivityResult.none; + subType = ConnectionSubtype.none; } + return ConnectivityResult(type, subType); } From 137ed3ba4ed549055806c50dc501f78c01c5bc3d Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Mon, 5 Aug 2019 13:59:15 +0000 Subject: [PATCH 02/25] refactored existing tests --- packages/connectivity/lib/connectivity.dart | 9 +++++---- packages/connectivity/test/connectivity_test.dart | 12 ++++++++---- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index 8fe3f7af91e3..7dae28b6c21f 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -13,7 +13,7 @@ import 'package:meta/meta.dart'; /// Mobile: Device connected to cellular network /// None: Device not connected to any network enum ConnectionType { wifi, mobile, none } -enum ConnectionSubtype { _2G, _3G, _4G, none, unknown } +enum ConnectionSubtype { EDGE, HSDPA, LTE, none, unknown } class ConnectivityResult { static const ConnectionType wifi = ConnectionType.wifi; @@ -24,6 +24,7 @@ class ConnectivityResult { final ConnectionSubtype subtype; ConnectivityResult(this.type, this.subtype); + } class Connectivity { @@ -138,13 +139,13 @@ ConnectivityResult _parseConnectivityResult(String state) { } switch (split[1]) { case '2G': - subType = ConnectionSubtype._2G; + subType = ConnectionSubtype.EDGE; break; case '3G': - subType = ConnectionSubtype._3G; + subType = ConnectionSubtype.HSDPA; break; case '4G': - subType = ConnectionSubtype._4G; + subType = ConnectionSubtype.LTE; break; case 'unknown': subType = ConnectionSubtype.unknown; diff --git a/packages/connectivity/test/connectivity_test.dart b/packages/connectivity/test/connectivity_test.dart index 4159130b3190..2fdefb987101 100644 --- a/packages/connectivity/test/connectivity_test.dart +++ b/packages/connectivity/test/connectivity_test.dart @@ -12,7 +12,9 @@ void main() { log.add(methodCall); switch (methodCall.method) { case 'check': - return 'wifi'; + return 'wifi,unknown'; + case 'subtype': + return '3G'; case 'wifiName': return '1337wifi'; case 'wifiBSSID': @@ -33,7 +35,7 @@ void main() { // ignore: deprecated_member_use await BinaryMessages.handlePlatformMessage( Connectivity.eventChannel.name, - Connectivity.eventChannel.codec.encodeSuccessEnvelope('wifi'), + Connectivity.eventChannel.codec.encodeSuccessEnvelope('mobile,2G'), (_) {}, ); break; @@ -47,7 +49,8 @@ void main() { test('onConnectivityChanged', () async { final ConnectivityResult result = await Connectivity().onConnectivityChanged.first; - expect(result, ConnectivityResult.wifi); + expect(result.type, ConnectivityResult.mobile); + expect(result.subtype, ConnectionSubtype.EDGE); }); test('getWifiName', () async { @@ -95,7 +98,8 @@ void main() { test('checkConnectivity', () async { final ConnectivityResult result = await Connectivity().checkConnectivity(); - expect(result, ConnectivityResult.wifi); + expect(result.type, ConnectivityResult.wifi); + expect(result.subtype, ConnectionSubtype.unknown); expect( log, [ From 658417431c9b5426883f30e3d42e398d6717cc6f Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Mon, 5 Aug 2019 14:05:50 +0000 Subject: [PATCH 03/25] refactored existing tests --- packages/connectivity/lib/connectivity.dart | 44 +++++++++---------- .../connectivity/test/connectivity_test.dart | 16 +++++++ 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index 7dae28b6c21f..a9be9d6a3261 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -24,7 +24,6 @@ class ConnectivityResult { final ConnectionSubtype subtype; ConnectivityResult(this.type, this.subtype); - } class Connectivity { @@ -74,20 +73,19 @@ class Connectivity { /// Instead listen for connectivity changes via [onConnectivityChanged] stream. Future checkConnectivity({bool checkSubtype = false}) async { final String result = await methodChannel.invokeMethod('check'); - String subtype; - if (checkSubtype) subtype = await getNetworkSubtype(); return _parseConnectivityResult(result); } /// Checks the network mobile connection subtype of the device. - /// Return 2G, 3G, 4G depending on the connection of the mobile connection + /// Return EDGE for 2G, HSDPA for 3G and LTE for 4G depending on the connection of the mobile connection /// if it is connected. /// /// Return none if there is no connections /// /// Return unknown if it is connected but there is not connection subtype info. eg. Wifi - Future getNetworkSubtype() async { - return await methodChannel.invokeMethod('subtype'); + Future getNetworkSubtype() async { + final String result = await methodChannel.invokeMethod('subtype'); + return _parseConnectionSubtype(result); } /// Obtains the wifi name (SSID) of the connected network @@ -120,6 +118,22 @@ class Connectivity { } } +ConnectionSubtype _parseConnectionSubtype(String state) { + switch (state) { + case '2G': + return ConnectionSubtype.EDGE; + case '3G': + return ConnectionSubtype.HSDPA; + case '4G': + return ConnectionSubtype.LTE; + case 'unknown': + return ConnectionSubtype.unknown; + case 'none': + default: + return ConnectionSubtype.none; + } +} + ConnectivityResult _parseConnectivityResult(String state) { ConnectionType type = ConnectionType.none; ConnectionSubtype subType = ConnectionSubtype.unknown; @@ -137,22 +151,6 @@ ConnectivityResult _parseConnectivityResult(String state) { default: type = ConnectionType.none; } - switch (split[1]) { - case '2G': - subType = ConnectionSubtype.EDGE; - break; - case '3G': - subType = ConnectionSubtype.HSDPA; - break; - case '4G': - subType = ConnectionSubtype.LTE; - break; - case 'unknown': - subType = ConnectionSubtype.unknown; - break; - case 'none': - default: - subType = ConnectionSubtype.none; - } + subType = _parseConnectionSubtype(split[1]); return ConnectivityResult(type, subType); } diff --git a/packages/connectivity/test/connectivity_test.dart b/packages/connectivity/test/connectivity_test.dart index 2fdefb987101..cc9d9ee57bad 100644 --- a/packages/connectivity/test/connectivity_test.dart +++ b/packages/connectivity/test/connectivity_test.dart @@ -110,5 +110,21 @@ void main() { ], ); }); + + test('subtype', () async { + final ConnectivityResult result = + await Connectivity().checkConnectivity(); + expect(result.type, ConnectivityResult.wifi); + expect(result.subtype, ConnectionSubtype.unknown); + expect( + log, + [ + isMethodCall( + 'subtype', + arguments: null, + ), + ], + ); + }); }); } From c3892d0d44e21a650c91b36085e0c9eb88009923 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Mon, 5 Aug 2019 14:07:28 +0000 Subject: [PATCH 04/25] test written for connection subtype --- packages/connectivity/test/connectivity_test.dart | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/connectivity/test/connectivity_test.dart b/packages/connectivity/test/connectivity_test.dart index cc9d9ee57bad..83edbf94e3b2 100644 --- a/packages/connectivity/test/connectivity_test.dart +++ b/packages/connectivity/test/connectivity_test.dart @@ -112,10 +112,9 @@ void main() { }); test('subtype', () async { - final ConnectivityResult result = - await Connectivity().checkConnectivity(); - expect(result.type, ConnectivityResult.wifi); - expect(result.subtype, ConnectionSubtype.unknown); + final ConnectionSubtype result = + await Connectivity().getNetworkSubtype(); + expect(result, ConnectionSubtype.HSDPA); expect( log, [ From 72e6c48c439289722dbb46c3de8c3256094ade71 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Mon, 5 Aug 2019 14:19:48 +0000 Subject: [PATCH 05/25] updated read me --- packages/connectivity/README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/connectivity/README.md b/packages/connectivity/README.md index b1c2b8b356fe..7b0150a3acaf 100644 --- a/packages/connectivity/README.md +++ b/packages/connectivity/README.md @@ -13,13 +13,32 @@ Sample usage to check current status: import 'package:connectivity/connectivity.dart'; var connectivityResult = await (Connectivity().checkConnectivity()); -if (connectivityResult == ConnectivityResult.mobile) { +if (connectivityResult.type == ConnectivityResult.mobile) { // I am connected to a mobile network. + if(connectivityResult.subtype == ConnectionSubtype.HSDPA){ + // I am on a 3G network + } } else if (connectivityResult == ConnectivityResult.wifi) { // I am connected to a wifi network. } ``` +You can also check for a connection type when you are on mobile: + +```dart +import 'package:connectivity/connectivity.dart'; + +var connectivityResult = await (Connectivity().getNetworkSubtype()); + +if(connectivityResult.subtype == ConnectionSubtype.EDGE){ + // I am on a 2G network +} else if(connectivityResult.subtype == ConnectionSubtype.HSDPA){ + // I am on a 3G network +} else if(connectivityResult.subtype == ConnectionSubtype.LTE){ + // I am on a 4G network +} +``` + > Note that you should not be using the current network status for deciding whether you can reliably make a network connection. Always guard your app code against timeouts and errors that might come from the network layer. From db287e7cc6f7256d8e40ce5eec2e6127ddc283e8 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Mon, 5 Aug 2019 14:59:22 +0000 Subject: [PATCH 06/25] Updated Changelog and pubsec --- packages/connectivity/CHANGELOG.md | 5 +++++ packages/connectivity/pubspec.yaml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/connectivity/CHANGELOG.md b/packages/connectivity/CHANGELOG.md index 6bc9f4a24be6..494250d5878d 100644 --- a/packages/connectivity/CHANGELOG.md +++ b/packages/connectivity/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.5.0 + +* Change `ConnectionResult` type from enum to class with 2 fields. `type:ConnectionType` and `subtype:ConnectionSubtype` +* Add `getNetworkSubtype` to get mobile connection subtype. + ## 0.4.3+6 * [Android] Fix the invalid suppression check (it should be "deprecation" not "deprecated"). diff --git a/packages/connectivity/pubspec.yaml b/packages/connectivity/pubspec.yaml index 33c8e4d94595..05b2f62ac85d 100644 --- a/packages/connectivity/pubspec.yaml +++ b/packages/connectivity/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for discovering the state of the network (WiFi & mobile/cellular) connectivity on Android and iOS. author: Flutter Team homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity -version: 0.4.3+6 +version: 0.5.0 flutter: plugin: From 8a4d6fb19ea0b8be9588a4aba4d16a5bff1411d4 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Mon, 5 Aug 2019 15:26:49 +0000 Subject: [PATCH 07/25] overide == operator to prevent breaking changes --- packages/connectivity/README.md | 2 +- packages/connectivity/lib/connectivity.dart | 9 +++++++++ packages/connectivity/test/connectivity_test.dart | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/connectivity/README.md b/packages/connectivity/README.md index 7b0150a3acaf..d9495d40e667 100644 --- a/packages/connectivity/README.md +++ b/packages/connectivity/README.md @@ -13,7 +13,7 @@ Sample usage to check current status: import 'package:connectivity/connectivity.dart'; var connectivityResult = await (Connectivity().checkConnectivity()); -if (connectivityResult.type == ConnectivityResult.mobile) { +if (connectivityResult == ConnectivityResult.mobile) { // I am connected to a mobile network. if(connectivityResult.subtype == ConnectionSubtype.HSDPA){ // I am on a 3G network diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index a9be9d6a3261..1ee8db71df20 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -24,6 +24,15 @@ class ConnectivityResult { final ConnectionSubtype subtype; ConnectivityResult(this.type, this.subtype); + + @override + bool operator ==(Object object) { + if (!(object is ConnectionType)) return false; + return type == object; + } + + @override + int get hashCode => type.hashCode; } class Connectivity { diff --git a/packages/connectivity/test/connectivity_test.dart b/packages/connectivity/test/connectivity_test.dart index 83edbf94e3b2..9a9486d4668b 100644 --- a/packages/connectivity/test/connectivity_test.dart +++ b/packages/connectivity/test/connectivity_test.dart @@ -1,6 +1,7 @@ import 'package:flutter/services.dart'; import 'package:connectivity/connectivity.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter_test/flutter_test.dart' as prefix0; void main() { group('$Connectivity', () { @@ -98,6 +99,7 @@ void main() { test('checkConnectivity', () async { final ConnectivityResult result = await Connectivity().checkConnectivity(); + expect(result == ConnectionType.wifi, true); expect(result.type, ConnectivityResult.wifi); expect(result.subtype, ConnectionSubtype.unknown); expect( From 9ee62745a8ed57f0ba0fc8b2436225202554b7e0 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Mon, 5 Aug 2019 15:30:32 +0000 Subject: [PATCH 08/25] updated changelog --- packages/connectivity/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/connectivity/CHANGELOG.md b/packages/connectivity/CHANGELOG.md index 494250d5878d..c23c31e8717f 100644 --- a/packages/connectivity/CHANGELOG.md +++ b/packages/connectivity/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.5.0 +## 0.4.4 * Change `ConnectionResult` type from enum to class with 2 fields. `type:ConnectionType` and `subtype:ConnectionSubtype` * Add `getNetworkSubtype` to get mobile connection subtype. From f03a29122d3949ccde3453a635030eced2038cf4 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Mon, 5 Aug 2019 15:32:51 +0000 Subject: [PATCH 09/25] updated version in pubsec --- packages/connectivity/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/connectivity/pubspec.yaml b/packages/connectivity/pubspec.yaml index 05b2f62ac85d..7ec5d31bcc42 100644 --- a/packages/connectivity/pubspec.yaml +++ b/packages/connectivity/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for discovering the state of the network (WiFi & mobile/cellular) connectivity on Android and iOS. author: Flutter Team homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity -version: 0.5.0 +version: 0.4.4 flutter: plugin: From bbcbfa6334380eeddc3910e0a87db83aee3f6da7 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Mon, 5 Aug 2019 19:23:57 +0000 Subject: [PATCH 10/25] fix errors from CI --- packages/connectivity/lib/connectivity.dart | 9 +++++---- packages/connectivity/test/connectivity_test.dart | 1 - 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index 1ee8db71df20..78f8cd26ae7f 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -16,14 +16,15 @@ enum ConnectionType { wifi, mobile, none } enum ConnectionSubtype { EDGE, HSDPA, LTE, none, unknown } class ConnectivityResult { - static const ConnectionType wifi = ConnectionType.wifi; - static const ConnectionType mobile = ConnectionType.mobile; - static const ConnectionType none = ConnectionType.none; + + ConnectivityResult(this.type, this.subtype); final ConnectionType type; final ConnectionSubtype subtype; - ConnectivityResult(this.type, this.subtype); + static const ConnectionType wifi = ConnectionType.wifi; + static const ConnectionType mobile = ConnectionType.mobile; + static const ConnectionType none = ConnectionType.none; @override bool operator ==(Object object) { diff --git a/packages/connectivity/test/connectivity_test.dart b/packages/connectivity/test/connectivity_test.dart index 9a9486d4668b..c5eaa58afbc0 100644 --- a/packages/connectivity/test/connectivity_test.dart +++ b/packages/connectivity/test/connectivity_test.dart @@ -1,7 +1,6 @@ import 'package:flutter/services.dart'; import 'package:connectivity/connectivity.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:flutter_test/flutter_test.dart' as prefix0; void main() { group('$Connectivity', () { From 47c98694201a43cac3523527f902b465de01100b Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Mon, 5 Aug 2019 19:27:26 +0000 Subject: [PATCH 11/25] suppress unrelated_type_equality_check --- .../connectivity/test/connectivity_test.dart | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/connectivity/test/connectivity_test.dart b/packages/connectivity/test/connectivity_test.dart index c5eaa58afbc0..269c7bcccd9e 100644 --- a/packages/connectivity/test/connectivity_test.dart +++ b/packages/connectivity/test/connectivity_test.dart @@ -7,8 +7,7 @@ void main() { final List log = []; setUp(() async { - Connectivity.methodChannel - .setMockMethodCallHandler((MethodCall methodCall) async { + Connectivity.methodChannel.setMockMethodCallHandler((MethodCall methodCall) async { log.add(methodCall); switch (methodCall.method) { case 'check': @@ -26,8 +25,7 @@ void main() { } }); log.clear(); - MethodChannel(Connectivity.eventChannel.name) - .setMockMethodCallHandler((MethodCall methodCall) async { + MethodChannel(Connectivity.eventChannel.name).setMockMethodCallHandler((MethodCall methodCall) async { switch (methodCall.method) { case 'listen': // TODO(hterkelsen): Remove this when defaultBinaryMessages is in stable. @@ -47,8 +45,7 @@ void main() { }); test('onConnectivityChanged', () async { - final ConnectivityResult result = - await Connectivity().onConnectivityChanged.first; + final ConnectivityResult result = await Connectivity().onConnectivityChanged.first; expect(result.type, ConnectivityResult.mobile); expect(result.subtype, ConnectionSubtype.EDGE); }); @@ -96,8 +93,8 @@ void main() { }); test('checkConnectivity', () async { - final ConnectivityResult result = - await Connectivity().checkConnectivity(); + final ConnectivityResult result = await Connectivity().checkConnectivity(); + // ignore: unrelated_type_equality_checks expect(result == ConnectionType.wifi, true); expect(result.type, ConnectivityResult.wifi); expect(result.subtype, ConnectionSubtype.unknown); @@ -113,8 +110,7 @@ void main() { }); test('subtype', () async { - final ConnectionSubtype result = - await Connectivity().getNetworkSubtype(); + final ConnectionSubtype result = await Connectivity().getNetworkSubtype(); expect(result, ConnectionSubtype.HSDPA); expect( log, From 31e4b3bb9aebeedb5ae1feaaa7aeef2d3d658d2a Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Tue, 6 Aug 2019 09:18:23 +0000 Subject: [PATCH 12/25] formatted files with flutter plugin tools --- .../connectivity/ConnectivityPlugin.java | 23 +++++++------------ packages/connectivity/example/lib/main.dart | 3 ++- .../ios/Classes/ConnectivityPlugin.m | 22 ++++++++---------- packages/connectivity/lib/connectivity.dart | 9 ++++---- .../connectivity/test/connectivity_test.dart | 15 ++++++++---- 5 files changed, 35 insertions(+), 37 deletions(-) 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 08703d3a22f9..dce8765f11ec 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 @@ -16,7 +16,6 @@ import android.net.wifi.WifiManager; import android.os.Build; import android.telephony.TelephonyManager; - import io.flutter.plugin.common.EventChannel; import io.flutter.plugin.common.EventChannel.EventSink; import io.flutter.plugin.common.EventChannel.StreamHandler; @@ -26,17 +25,13 @@ import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; -/** - * ConnectivityPlugin - */ +/** ConnectivityPlugin */ public class ConnectivityPlugin implements MethodCallHandler, StreamHandler { private final Registrar registrar; private final ConnectivityManager manager; private BroadcastReceiver receiver; - /** - * Plugin registration. - */ + /** Plugin registration. */ public static void registerWith(Registrar registrar) { final MethodChannel channel = new MethodChannel(registrar.messenger(), "plugins.flutter.io/connectivity"); @@ -115,8 +110,7 @@ private String getNetworkTypeLegacy(ConnectivityManager manager) { private static String getNetworkSubType(ConnectivityManager manager) { NetworkInfo info = manager.getActiveNetworkInfo(); - if (info == null || !info.isConnected()) - return "none"; + if (info == null || !info.isConnected()) return "none"; switch (info.getSubtype()) { case TelephonyManager.NETWORK_TYPE_1xRTT: @@ -139,10 +133,10 @@ private static String getNetworkSubType(ConnectivityManager manager) { 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 - */ + /* + * 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 @@ -153,7 +147,7 @@ private static String getNetworkSubType(ConnectivityManager manager) { return "2G"; // ~25 kbps case TelephonyManager.NETWORK_TYPE_LTE: // API level 11 return "4G"; // ~ 10+ Mbps - // Unknown + // Unknown case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "unknown"; // is connected but cannot tell the speed default: @@ -161,7 +155,6 @@ private static String getNetworkSubType(ConnectivityManager manager) { } } - @Override public void onMethodCall(MethodCall call, Result result) { switch (call.method) { diff --git a/packages/connectivity/example/lib/main.dart b/packages/connectivity/example/lib/main.dart index 4f9e460556d2..a54afeb85525 100644 --- a/packages/connectivity/example/lib/main.dart +++ b/packages/connectivity/example/lib/main.dart @@ -44,7 +44,8 @@ class _MyHomePageState extends State { void initState() { super.initState(); initConnectivity(); - _connectivitySubscription = _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); + _connectivitySubscription = + _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); } @override diff --git a/packages/connectivity/ios/Classes/ConnectivityPlugin.m b/packages/connectivity/ios/Classes/ConnectivityPlugin.m index cc2b0dc1c5de..e331a4a8a366 100644 --- a/packages/connectivity/ios/Classes/ConnectivityPlugin.m +++ b/packages/connectivity/ios/Classes/ConnectivityPlugin.m @@ -12,7 +12,7 @@ #include -#import +#import @interface FLTConnectivityPlugin () @end @@ -87,12 +87,10 @@ - (NSString*)getWifiIP { return address; } - - (NSString*)getConnectionSubtype:(Reachability*)reachability { - - CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init]; - NSString *carrierType = netinfo.currentRadioAccessTechnology; - + CTTelephonyNetworkInfo* netinfo = [[CTTelephonyNetworkInfo alloc] init]; + NSString* carrierType = netinfo.currentRadioAccessTechnology; + if ([carrierType isEqualToString:CTRadioAccessTechnologyGPRS]) { return @"2G"; } else if ([carrierType isEqualToString:CTRadioAccessTechnologyEdge]) { @@ -121,14 +119,14 @@ - (NSString*)getConnectionSubtype:(Reachability*)reachability { - (NSString*)statusFromReachability:(Reachability*)reachability { NetworkStatus status = [reachability currentReachabilityStatus]; - NSString *subtype = [self getConnectionSubtype:[Reachability reachabilityForInternetConnection]]; + NSString* subtype = [self getConnectionSubtype:[Reachability reachabilityForInternetConnection]]; switch (status) { case NotReachable: - return [NSString stringWithFormat:@"%@,%@", @"none" , subtype]; + return [NSString stringWithFormat:@"%@,%@", @"none", subtype]; case ReachableViaWiFi: - return [NSString stringWithFormat:@"%@,%@", @"wifi" , subtype]; + return [NSString stringWithFormat:@"%@,%@", @"wifi", subtype]; case ReachableViaWWAN: - return [NSString stringWithFormat:@"%@,%@", @"mobile" , subtype]; + return [NSString stringWithFormat:@"%@,%@", @"mobile", subtype]; } } @@ -146,9 +144,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { result([self getBSSID]); } else if ([call.method isEqualToString:@"wifiIPAddress"]) { result([self getWifiIP]); - }else if ([call.method isEqualToString:@"subtype"]) { + } else if ([call.method isEqualToString:@"subtype"]) { result([self getConnectionSubtype:[Reachability reachabilityForInternetConnection]]); - + } else { result(FlutterMethodNotImplemented); } diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index 78f8cd26ae7f..6b14827e16e7 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -16,7 +16,6 @@ enum ConnectionType { wifi, mobile, none } enum ConnectionSubtype { EDGE, HSDPA, LTE, none, unknown } class ConnectivityResult { - ConnectivityResult(this.type, this.subtype); final ConnectionType type; @@ -69,8 +68,9 @@ class Connectivity { /// Fires whenever the connectivity state changes. Stream get onConnectivityChanged { if (_onConnectivityChanged == null) { - _onConnectivityChanged = - eventChannel.receiveBroadcastStream().map((dynamic event) => _parseConnectivityResult(event)); + _onConnectivityChanged = eventChannel + .receiveBroadcastStream() + .map((dynamic event) => _parseConnectivityResult(event)); } return _onConnectivityChanged; } @@ -81,7 +81,8 @@ class Connectivity { /// make a network request. It only gives you the radio status. /// /// Instead listen for connectivity changes via [onConnectivityChanged] stream. - Future checkConnectivity({bool checkSubtype = false}) async { + Future checkConnectivity( + {bool checkSubtype = false}) async { final String result = await methodChannel.invokeMethod('check'); return _parseConnectivityResult(result); } diff --git a/packages/connectivity/test/connectivity_test.dart b/packages/connectivity/test/connectivity_test.dart index 269c7bcccd9e..22ab6e2f39d4 100644 --- a/packages/connectivity/test/connectivity_test.dart +++ b/packages/connectivity/test/connectivity_test.dart @@ -7,7 +7,8 @@ void main() { final List log = []; setUp(() async { - Connectivity.methodChannel.setMockMethodCallHandler((MethodCall methodCall) async { + Connectivity.methodChannel + .setMockMethodCallHandler((MethodCall methodCall) async { log.add(methodCall); switch (methodCall.method) { case 'check': @@ -25,7 +26,8 @@ void main() { } }); log.clear(); - MethodChannel(Connectivity.eventChannel.name).setMockMethodCallHandler((MethodCall methodCall) async { + MethodChannel(Connectivity.eventChannel.name) + .setMockMethodCallHandler((MethodCall methodCall) async { switch (methodCall.method) { case 'listen': // TODO(hterkelsen): Remove this when defaultBinaryMessages is in stable. @@ -33,7 +35,8 @@ void main() { // ignore: deprecated_member_use await BinaryMessages.handlePlatformMessage( Connectivity.eventChannel.name, - Connectivity.eventChannel.codec.encodeSuccessEnvelope('mobile,2G'), + Connectivity.eventChannel.codec + .encodeSuccessEnvelope('mobile,2G'), (_) {}, ); break; @@ -45,7 +48,8 @@ void main() { }); test('onConnectivityChanged', () async { - final ConnectivityResult result = await Connectivity().onConnectivityChanged.first; + final ConnectivityResult result = + await Connectivity().onConnectivityChanged.first; expect(result.type, ConnectivityResult.mobile); expect(result.subtype, ConnectionSubtype.EDGE); }); @@ -93,7 +97,8 @@ void main() { }); test('checkConnectivity', () async { - final ConnectivityResult result = await Connectivity().checkConnectivity(); + final ConnectivityResult result = + await Connectivity().checkConnectivity(); // ignore: unrelated_type_equality_checks expect(result == ConnectionType.wifi, true); expect(result.type, ConnectivityResult.wifi); From c548c278f6295e8ee280efbef5f03bd7e092930b Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Tue, 6 Aug 2019 17:56:01 +0000 Subject: [PATCH 13/25] added mobile broadband speed links --- .../connectivity/ConnectivityPlugin.java | 55 +++++++------------ 1 file changed, 20 insertions(+), 35 deletions(-) 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 dce8765f11ec..8fd119aece5a 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 @@ -112,42 +112,27 @@ private static String getNetworkSubType(ConnectivityManager manager) { if (info == null || !info.isConnected()) return "none"; + /// Telephony Manager documentation https://developer.android.com/reference/android/telephony/TelephonyManager + /// Information about mobile broadband - https://en.wikipedia.org/wiki/Mobile_broadband#Generations switch (info.getSubtype()) { - 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_1xRTT: // ~ 50-100 kbps + case TelephonyManager.NETWORK_TYPE_CDMA: // ~ 14-64 kbps + case TelephonyManager.NETWORK_TYPE_EDGE: // ~ 50-100 kbps + case TelephonyManager.NETWORK_TYPE_GPRS: // ~ 100 kbps + case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8 ~25 kbps + return "2G"; + case TelephonyManager.NETWORK_TYPE_EVDO_0: // ~ 400-1000 kbps + case TelephonyManager.NETWORK_TYPE_EVDO_A: // ~ 600-1400 kbps + case TelephonyManager.NETWORK_TYPE_HSDPA: // ~ 2-14 Mbps + case TelephonyManager.NETWORK_TYPE_HSPA: // ~ 700-1700 kbps + case TelephonyManager.NETWORK_TYPE_HSUPA: // ~ 1-23 Mbps + case TelephonyManager.NETWORK_TYPE_UMTS: // ~ 400-7000 kbps + case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11 ~ 1-2 Mbps + case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9 ~ 5 Mbps + case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13 ~ 10-20 Mbps + return "3G"; + case TelephonyManager.NETWORK_TYPE_LTE: // API level 11 ~ 10+ Mbps + return "4G"; case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "unknown"; // is connected but cannot tell the speed default: From 7e1c52f8d54b5168f73ae71a0b6563f4f4a20f22 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Tue, 6 Aug 2019 19:06:05 +0000 Subject: [PATCH 14/25] break down mobile subtype into detailed category --- .../connectivity/ConnectivityPlugin.java | 54 ++++++++++------ .../ios/Classes/ConnectivityPlugin.m | 29 +++++---- packages/connectivity/lib/connectivity.dart | 62 +++++++++++++------ 3 files changed, 96 insertions(+), 49 deletions(-) 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 8fd119aece5a..74b82ef6d1d9 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 @@ -114,25 +114,43 @@ private static String getNetworkSubType(ConnectivityManager manager) { /// Telephony Manager documentation https://developer.android.com/reference/android/telephony/TelephonyManager /// Information about mobile broadband - https://en.wikipedia.org/wiki/Mobile_broadband#Generations + switch (info.getSubtype()) { - case TelephonyManager.NETWORK_TYPE_1xRTT: // ~ 50-100 kbps - case TelephonyManager.NETWORK_TYPE_CDMA: // ~ 14-64 kbps - case TelephonyManager.NETWORK_TYPE_EDGE: // ~ 50-100 kbps - case TelephonyManager.NETWORK_TYPE_GPRS: // ~ 100 kbps - case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8 ~25 kbps - return "2G"; - case TelephonyManager.NETWORK_TYPE_EVDO_0: // ~ 400-1000 kbps - case TelephonyManager.NETWORK_TYPE_EVDO_A: // ~ 600-1400 kbps - case TelephonyManager.NETWORK_TYPE_HSDPA: // ~ 2-14 Mbps - case TelephonyManager.NETWORK_TYPE_HSPA: // ~ 700-1700 kbps - case TelephonyManager.NETWORK_TYPE_HSUPA: // ~ 1-23 Mbps - case TelephonyManager.NETWORK_TYPE_UMTS: // ~ 400-7000 kbps - case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11 ~ 1-2 Mbps - case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9 ~ 5 Mbps - case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13 ~ 10-20 Mbps - return "3G"; - case TelephonyManager.NETWORK_TYPE_LTE: // API level 11 ~ 10+ Mbps - return "4G"; + case TelephonyManager.NETWORK_TYPE_1xRTT: + return "1xRTT"; // ~ 50-100 kbps + case TelephonyManager.NETWORK_TYPE_CDMA: + return "cdma"; // ~ 14-64 kbps + case TelephonyManager.NETWORK_TYPE_EDGE: + return "edge"; // ~ 50-100 kbps + case TelephonyManager.NETWORK_TYPE_EVDO_0: + return "evdo_0"; // ~ 400-1000 kbps + case TelephonyManager.NETWORK_TYPE_EVDO_A: + return "evdo_a"; // ~ 600-1400 kbps + case TelephonyManager.NETWORK_TYPE_GPRS: + return "gprs"; // ~ 100 kbps + case TelephonyManager.NETWORK_TYPE_HSDPA: + return "hsdpa"; // ~ 2-14 Mbps + case TelephonyManager.NETWORK_TYPE_HSPA: + return "hspa"; // ~ 700-1700 kbps + case TelephonyManager.NETWORK_TYPE_HSUPA: + return "hsupa"; // ~ 1-23 Mbps + case TelephonyManager.NETWORK_TYPE_UMTS: + return "umts"; // ~ 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 "ehrpd"; // ~ 1-2 Mbps + case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9 + return "evdo_b"; // ~ 5 Mbps + case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13 + return "hspap"; // ~ 10-20 Mbps + case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8 + return "iden"; // ~25 kbps + case TelephonyManager.NETWORK_TYPE_LTE: // API level 11 + return "lte"; // ~ 10+ Mbps + // Unknown case TelephonyManager.NETWORK_TYPE_UNKNOWN: return "unknown"; // is connected but cannot tell the speed default: diff --git a/packages/connectivity/ios/Classes/ConnectivityPlugin.m b/packages/connectivity/ios/Classes/ConnectivityPlugin.m index e331a4a8a366..39b36624f8d8 100644 --- a/packages/connectivity/ios/Classes/ConnectivityPlugin.m +++ b/packages/connectivity/ios/Classes/ConnectivityPlugin.m @@ -88,33 +88,38 @@ - (NSString*)getWifiIP { } - (NSString*)getConnectionSubtype:(Reachability*)reachability { + + if([reachability currentReachabilityStatus] == NotReachable){ + return @"none"; + } + CTTelephonyNetworkInfo* netinfo = [[CTTelephonyNetworkInfo alloc] init]; NSString* carrierType = netinfo.currentRadioAccessTechnology; if ([carrierType isEqualToString:CTRadioAccessTechnologyGPRS]) { - return @"2G"; + return @"gprs"; } else if ([carrierType isEqualToString:CTRadioAccessTechnologyEdge]) { - return @"2G"; + return @"edge"; } else if ([carrierType isEqualToString:CTRadioAccessTechnologyWCDMA]) { - return @"3G"; + return @"cdma"; } else if ([carrierType isEqualToString:CTRadioAccessTechnologyHSDPA]) { - return @"3G"; + return @"hsdpa"; } else if ([carrierType isEqualToString:CTRadioAccessTechnologyHSUPA]) { - return @"3G"; + return @"hsupa"; } else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMA1x]) { - return @"2G"; + return @"cdma"; } else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) { - return @"3G"; + return @"evdo_0"; } else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) { - return @"3G"; + return @"evdo_a"; } else if ([carrierType isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) { - return @"3G"; + return @"evdo_b"; } else if ([carrierType isEqualToString:CTRadioAccessTechnologyeHRPD]) { - return @"3G"; + return @"ehrpd"; } else if ([carrierType isEqualToString:CTRadioAccessTechnologyLTE]) { - return @"4G"; + return @"lte"; } - return @"none"; + return @"unknown"; } - (NSString*)statusFromReachability:(Reachability*)reachability { diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index 6b14827e16e7..4bbe301ecbb5 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -13,7 +13,45 @@ import 'package:meta/meta.dart'; /// Mobile: Device connected to cellular network /// None: Device not connected to any network enum ConnectionType { wifi, mobile, none } -enum ConnectionSubtype { EDGE, HSDPA, LTE, none, unknown } +enum ConnectionSubtype { + none, + unknown, + m1xRTT, // ~ 50-100 kbps + cdma, // ~ 14-64 kbps + edge, // ~ 50-100 kbps + evdo_0, // ~ 400-1000 kbps + evdo_a, // ~ 600-1400 kbps + gprs, // ~ 100 kbps + hsdpa, // ~ 2-14 Mbps + hspa, // ~ 700-1700 kbps + hsupa, // ~ 1-23 Mbps + umts, // ~ 400-7000 kbps + ehrpd, // ~ 1-2 Mbps + evdo_b, // ~ 5 Mbps + hspap, // ~ 10-20 Mbps + iden, // ~25 kbps + lte, // ~ 10+ Mbps +} + +Map connectionTypeMap = { + "1xRTT": ConnectionSubtype.m1xRTT, // ~ 50-100 kbps + "cdma": ConnectionSubtype.cdma, // ~ 14-64 kbps + "edge": ConnectionSubtype.edge, // ~ 50-100 kbps + "evdo_0": ConnectionSubtype.evdo_0, // ~ 400-1000 kbps + "evdo_a": ConnectionSubtype.evdo_a, // ~ 600-1400 kbps + "gprs": ConnectionSubtype.gprs, // ~ 100 kbps + "hsdpa": ConnectionSubtype.hsdpa, // ~ 2-14 Mbps + "hspa": ConnectionSubtype.hspa, // ~ 700-1700 kbps + "hsupa": ConnectionSubtype.hsupa, // ~ 1-23 Mbps + "umts": ConnectionSubtype.umts, // ~ 400-7000 kbps + "ehrpd": ConnectionSubtype.ehrpd, // ~ 1-2 Mbps + "evdo_b": ConnectionSubtype.evdo_b, // ~ 5 Mbps + "hspap": ConnectionSubtype.hspap, // ~ 10-20 Mbps + "iden": ConnectionSubtype.iden, // ~25 kbps + "lte": ConnectionSubtype.lte, // ~ 10+ Mbps + "unknown": ConnectionSubtype.none, // is connected but cannot tell the speed + "none": ConnectionSubtype.none +}; class ConnectivityResult { ConnectivityResult(this.type, this.subtype); @@ -68,9 +106,8 @@ class Connectivity { /// Fires whenever the connectivity state changes. Stream get onConnectivityChanged { if (_onConnectivityChanged == null) { - _onConnectivityChanged = eventChannel - .receiveBroadcastStream() - .map((dynamic event) => _parseConnectivityResult(event)); + _onConnectivityChanged = + eventChannel.receiveBroadcastStream().map((dynamic event) => _parseConnectivityResult(event)); } return _onConnectivityChanged; } @@ -81,8 +118,7 @@ class Connectivity { /// make a network request. It only gives you the radio status. /// /// Instead listen for connectivity changes via [onConnectivityChanged] stream. - Future checkConnectivity( - {bool checkSubtype = false}) async { + Future checkConnectivity({bool checkSubtype = false}) async { final String result = await methodChannel.invokeMethod('check'); return _parseConnectivityResult(result); } @@ -130,19 +166,7 @@ class Connectivity { } ConnectionSubtype _parseConnectionSubtype(String state) { - switch (state) { - case '2G': - return ConnectionSubtype.EDGE; - case '3G': - return ConnectionSubtype.HSDPA; - case '4G': - return ConnectionSubtype.LTE; - case 'unknown': - return ConnectionSubtype.unknown; - case 'none': - default: - return ConnectionSubtype.none; - } + return connectionTypeMap[state]; } ConnectivityResult _parseConnectivityResult(String state) { From f50263e8c3567a8cca446bedea6cd9144e5f2a33 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Tue, 6 Aug 2019 19:12:16 +0000 Subject: [PATCH 15/25] passing tests --- packages/connectivity/lib/connectivity.dart | 2 +- packages/connectivity/test/connectivity_test.dart | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index 4bbe301ecbb5..69c90dda5c87 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -49,7 +49,7 @@ Map connectionTypeMap = { "hspap": ConnectionSubtype.hspap, // ~ 10-20 Mbps "iden": ConnectionSubtype.iden, // ~25 kbps "lte": ConnectionSubtype.lte, // ~ 10+ Mbps - "unknown": ConnectionSubtype.none, // is connected but cannot tell the speed + "unknown": ConnectionSubtype.unknown, // is connected but cannot tell the speed "none": ConnectionSubtype.none }; diff --git a/packages/connectivity/test/connectivity_test.dart b/packages/connectivity/test/connectivity_test.dart index 22ab6e2f39d4..bb999cb24b1b 100644 --- a/packages/connectivity/test/connectivity_test.dart +++ b/packages/connectivity/test/connectivity_test.dart @@ -14,7 +14,7 @@ void main() { case 'check': return 'wifi,unknown'; case 'subtype': - return '3G'; + return 'evdo_b'; case 'wifiName': return '1337wifi'; case 'wifiBSSID': @@ -36,7 +36,7 @@ void main() { await BinaryMessages.handlePlatformMessage( Connectivity.eventChannel.name, Connectivity.eventChannel.codec - .encodeSuccessEnvelope('mobile,2G'), + .encodeSuccessEnvelope('mobile,hsdpa'), (_) {}, ); break; @@ -51,7 +51,7 @@ void main() { final ConnectivityResult result = await Connectivity().onConnectivityChanged.first; expect(result.type, ConnectivityResult.mobile); - expect(result.subtype, ConnectionSubtype.EDGE); + expect(result.subtype, ConnectionSubtype.hsdpa); }); test('getWifiName', () async { @@ -116,7 +116,7 @@ void main() { test('subtype', () async { final ConnectionSubtype result = await Connectivity().getNetworkSubtype(); - expect(result, ConnectionSubtype.HSDPA); + expect(result, ConnectionSubtype.evdo_b); expect( log, [ From c59a5879456881bf95f7fb2ca2b7a244a775facb Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Tue, 6 Aug 2019 20:07:31 +0000 Subject: [PATCH 16/25] added more documenations to connectivity.dart --- .../connectivity/ConnectivityPlugin.java | 64 +++++++++++++------ packages/connectivity/lib/connectivity.dart | 18 ++++-- 2 files changed, 56 insertions(+), 26 deletions(-) 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 74b82ef6d1d9..b5fe8671fc58 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 @@ -16,6 +16,7 @@ import android.net.wifi.WifiManager; import android.os.Build; import android.telephony.TelephonyManager; + import io.flutter.plugin.common.EventChannel; import io.flutter.plugin.common.EventChannel.EventSink; import io.flutter.plugin.common.EventChannel.StreamHandler; @@ -25,13 +26,17 @@ import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; -/** ConnectivityPlugin */ +/** + * ConnectivityPlugin + */ public class ConnectivityPlugin implements MethodCallHandler, StreamHandler { private final Registrar registrar; private final ConnectivityManager manager; private BroadcastReceiver receiver; - /** Plugin registration. */ + /** + * Plugin registration. + */ public static void registerWith(Registrar registrar) { final MethodChannel channel = new MethodChannel(registrar.messenger(), "plugins.flutter.io/connectivity"); @@ -110,51 +115,70 @@ private String getNetworkTypeLegacy(ConnectivityManager manager) { private static String getNetworkSubType(ConnectivityManager manager) { NetworkInfo info = manager.getActiveNetworkInfo(); - if (info == null || !info.isConnected()) return "none"; + if (info == null || !info.isConnected()) { + return "none"; + } /// Telephony Manager documentation https://developer.android.com/reference/android/telephony/TelephonyManager /// Information about mobile broadband - https://en.wikipedia.org/wiki/Mobile_broadband#Generations switch (info.getSubtype()) { - case TelephonyManager.NETWORK_TYPE_1xRTT: + case TelephonyManager.NETWORK_TYPE_1xRTT: { return "1xRTT"; // ~ 50-100 kbps - case TelephonyManager.NETWORK_TYPE_CDMA: + } + case TelephonyManager.NETWORK_TYPE_CDMA: { return "cdma"; // ~ 14-64 kbps - case TelephonyManager.NETWORK_TYPE_EDGE: + } + case TelephonyManager.NETWORK_TYPE_EDGE: { return "edge"; // ~ 50-100 kbps - case TelephonyManager.NETWORK_TYPE_EVDO_0: + } + case TelephonyManager.NETWORK_TYPE_EVDO_0: { return "evdo_0"; // ~ 400-1000 kbps - case TelephonyManager.NETWORK_TYPE_EVDO_A: + } + case TelephonyManager.NETWORK_TYPE_EVDO_A: { return "evdo_a"; // ~ 600-1400 kbps - case TelephonyManager.NETWORK_TYPE_GPRS: + } + case TelephonyManager.NETWORK_TYPE_GPRS: { return "gprs"; // ~ 100 kbps - case TelephonyManager.NETWORK_TYPE_HSDPA: + } + case TelephonyManager.NETWORK_TYPE_HSDPA: { return "hsdpa"; // ~ 2-14 Mbps - case TelephonyManager.NETWORK_TYPE_HSPA: + } + case TelephonyManager.NETWORK_TYPE_HSPA: { return "hspa"; // ~ 700-1700 kbps - case TelephonyManager.NETWORK_TYPE_HSUPA: + } + case TelephonyManager.NETWORK_TYPE_HSUPA: { return "hsupa"; // ~ 1-23 Mbps - case TelephonyManager.NETWORK_TYPE_UMTS: + } + case TelephonyManager.NETWORK_TYPE_UMTS: { return "umts"; // ~ 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 + case TelephonyManager.NETWORK_TYPE_EHRPD: { // API level 11 return "ehrpd"; // ~ 1-2 Mbps - case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9 + } + case TelephonyManager.NETWORK_TYPE_EVDO_B: { // API level 9 return "evdo_b"; // ~ 5 Mbps - case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13 + } + case TelephonyManager.NETWORK_TYPE_HSPAP: {// API level 13 return "hspap"; // ~ 10-20 Mbps - case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8 + } + case TelephonyManager.NETWORK_TYPE_IDEN: { // API level 8 return "iden"; // ~25 kbps - case TelephonyManager.NETWORK_TYPE_LTE: // API level 11 + } + case TelephonyManager.NETWORK_TYPE_LTE: { // API level 11 return "lte"; // ~ 10+ Mbps + } // Unknown - case TelephonyManager.NETWORK_TYPE_UNKNOWN: + case TelephonyManager.NETWORK_TYPE_UNKNOWN: { return "unknown"; // is connected but cannot tell the speed - default: + } + default: { return "none"; + } } } diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index 69c90dda5c87..0e25e24653c8 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -65,7 +65,9 @@ class ConnectivityResult { @override bool operator ==(Object object) { - if (!(object is ConnectionType)) return false; + if (!(object is ConnectionType)) { + return false; + } return type == object; } @@ -118,18 +120,22 @@ class Connectivity { /// make a network request. It only gives you the radio status. /// /// Instead listen for connectivity changes via [onConnectivityChanged] stream. - Future checkConnectivity({bool checkSubtype = false}) async { + /// + /// You can also check the mobile broadband connectivity subtype via [getNetworkSubtype] + Future checkConnectivity() async { final String result = await methodChannel.invokeMethod('check'); return _parseConnectivityResult(result); } /// Checks the network mobile connection subtype of the device. - /// Return EDGE for 2G, HSDPA for 3G and LTE for 4G depending on the connection of the mobile connection - /// if it is connected. + /// Returns the appropriate mobile connectivity subtype enum [ConnectionSubtype] such + /// as gprs, edge, hsdpa etc. /// - /// Return none if there is no connections + /// More information on mobile connectivity types can be found at + /// https://en.wikipedia.org/wiki/Mobile_broadband#Generations /// - /// Return unknown if it is connected but there is not connection subtype info. eg. Wifi + /// Return [ConnectionSubtype.unknown] if it is connected but there is not connection subtype info. eg. Wifi + /// Returns [ConnectionSubtype.none] if there is no connection Future getNetworkSubtype() async { final String result = await methodChannel.invokeMethod('subtype'); return _parseConnectionSubtype(result); From 0fcc46fb8d280bf27c42f2e520de1874d3956315 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Tue, 6 Aug 2019 20:18:30 +0000 Subject: [PATCH 17/25] revert change in connectivity/example/io/Runner.xcodeproj/project.pbsproj to a822896 --- .../example/ios/Runner.xcodeproj/project.pbxproj | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/connectivity/example/ios/Runner.xcodeproj/project.pbxproj b/packages/connectivity/example/ios/Runner.xcodeproj/project.pbxproj index 2cdec60747b0..78c4f7c8f116 100644 --- a/packages/connectivity/example/ios/Runner.xcodeproj/project.pbxproj +++ b/packages/connectivity/example/ios/Runner.xcodeproj/project.pbxproj @@ -39,12 +39,10 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - 0B3C39CAD8BDF66703B2A5F7 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = ""; }; 1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = ""; }; 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = ""; }; 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = ""; }; 3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = ""; }; - 3DFD50681F014E6A560C2D40 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = ""; }; 7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = ""; }; 7AFFD8ED1D35381100E5BB4D /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; @@ -85,8 +83,6 @@ 8ECC1C323F60D5498EEC2315 /* Pods */ = { isa = PBXGroup; children = ( - 0B3C39CAD8BDF66703B2A5F7 /* Pods-Runner.debug.xcconfig */, - 3DFD50681F014E6A560C2D40 /* Pods-Runner.release.xcconfig */, ); name = Pods; sourceTree = ""; @@ -260,13 +256,16 @@ files = ( ); inputPaths = ( + "${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh", + "${PODS_ROOT}/../.symlinks/flutter/ios/Flutter.framework", ); name = "[CP] Embed Pods Frameworks"; outputPaths = ( + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n"; + shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n"; showEnvVarsInLog = 0; }; 9740EEB61CF901F6004384FC /* Run Script */ = { From 26180ce636a60d5a53d84a83bc1d659ecca4801a Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Tue, 6 Aug 2019 21:11:45 +0000 Subject: [PATCH 18/25] new api and tests --- packages/connectivity/example/lib/main.dart | 4 +- .../example/test_driver/connectivity.dart | 2 +- packages/connectivity/lib/connectivity.dart | 57 ++++++++++--------- .../connectivity/test/connectivity_test.dart | 45 +++++++++------ 4 files changed, 61 insertions(+), 47 deletions(-) diff --git a/packages/connectivity/example/lib/main.dart b/packages/connectivity/example/lib/main.dart index a54afeb85525..0c2a981cf6fb 100644 --- a/packages/connectivity/example/lib/main.dart +++ b/packages/connectivity/example/lib/main.dart @@ -85,7 +85,7 @@ class _MyHomePageState extends State { } Future _updateConnectionStatus(ConnectivityResult result) async { - switch (result.type) { + switch (result) { case ConnectivityResult.wifi: String wifiName, wifiBSSID, wifiIP; @@ -120,7 +120,7 @@ class _MyHomePageState extends State { case ConnectivityResult.mobile: setState(() { _connectionStatus = '$result\n' - 'Mobile Connection Type: ${result.subtype}\n'; + 'Mobile Connection Type: $result\n'; }); break; case ConnectivityResult.none: diff --git a/packages/connectivity/example/test_driver/connectivity.dart b/packages/connectivity/example/test_driver/connectivity.dart index 188e7bf8c775..7efba5e4ab50 100644 --- a/packages/connectivity/example/test_driver/connectivity.dart +++ b/packages/connectivity/example/test_driver/connectivity.dart @@ -18,7 +18,7 @@ void main() { test('test connectivity result', () async { final ConnectivityResult result = await _connectivity.checkConnectivity(); expect(result, isNotNull); - switch (result.type) { + switch (result) { case ConnectivityResult.wifi: expect(_connectivity.getWifiName(), completes); expect(_connectivity.getWifiBSSID(), completes); diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index 0e25e24653c8..b222dadf82f0 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -12,7 +12,7 @@ import 'package:meta/meta.dart'; /// WiFi: Device connected via Wi-Fi /// Mobile: Device connected to cellular network /// None: Device not connected to any network -enum ConnectionType { wifi, mobile, none } +enum ConnectivityResult { wifi, mobile, none } enum ConnectionSubtype { none, unknown, @@ -33,7 +33,7 @@ enum ConnectionSubtype { lte, // ~ 10+ Mbps } -Map connectionTypeMap = { +Map connectionTypeMap = { "1xRTT": ConnectionSubtype.m1xRTT, // ~ 50-100 kbps "cdma": ConnectionSubtype.cdma, // ~ 14-64 kbps "edge": ConnectionSubtype.edge, // ~ 50-100 kbps @@ -53,26 +53,11 @@ Map connectionTypeMap = { "none": ConnectionSubtype.none }; -class ConnectivityResult { - ConnectivityResult(this.type, this.subtype); +class ConnectivityInfo { + ConnectivityInfo(this.result, this.subtype); - final ConnectionType type; + final ConnectivityResult result; final ConnectionSubtype subtype; - - static const ConnectionType wifi = ConnectionType.wifi; - static const ConnectionType mobile = ConnectionType.mobile; - static const ConnectionType none = ConnectionType.none; - - @override - bool operator ==(Object object) { - if (!(object is ConnectionType)) { - return false; - } - return type == object; - } - - @override - int get hashCode => type.hashCode; } class Connectivity { @@ -94,6 +79,7 @@ class Connectivity { static Connectivity _singleton; Stream _onConnectivityChanged; + Stream _onConnectivityInfoChanged; @visibleForTesting static const MethodChannel methodChannel = MethodChannel( @@ -105,15 +91,24 @@ class Connectivity { 'plugins.flutter.io/connectivity_status', ); - /// Fires whenever the connectivity state changes. + /// Fires whenever the connectivity state changes. Returns stream of [ConnectivityResult] Stream get onConnectivityChanged { if (_onConnectivityChanged == null) { _onConnectivityChanged = - eventChannel.receiveBroadcastStream().map((dynamic event) => _parseConnectivityResult(event)); + eventChannel.receiveBroadcastStream().map((dynamic event) => _parseConnectivityResult(event).result); } return _onConnectivityChanged; } + /// Fires whenever the connectivity state changes. Return stream of [ConnectivityInfo] + Stream get onConnectivityInfoChanged { + if (_onConnectivityInfoChanged == null) { + _onConnectivityInfoChanged = + eventChannel.receiveBroadcastStream().map((dynamic event) => _parseConnectivityResult(event)); + } + return _onConnectivityInfoChanged; + } + /// Checks the connection status of the device. /// /// Do not use the result of this function to decide whether you can reliably @@ -123,6 +118,12 @@ class Connectivity { /// /// You can also check the mobile broadband connectivity subtype via [getNetworkSubtype] Future checkConnectivity() async { + final String result = await methodChannel.invokeMethod('check'); + return _parseConnectivityResult(result).result; + } + + /// Checks connectivity info, [ConnectivityInfo] + Future checkConnectivityInfo() async { final String result = await methodChannel.invokeMethod('check'); return _parseConnectivityResult(result); } @@ -175,23 +176,23 @@ ConnectionSubtype _parseConnectionSubtype(String state) { return connectionTypeMap[state]; } -ConnectivityResult _parseConnectivityResult(String state) { - ConnectionType type = ConnectionType.none; +ConnectivityInfo _parseConnectivityResult(String state) { + ConnectivityResult type = ConnectivityResult.none; ConnectionSubtype subType = ConnectionSubtype.unknown; final List split = state.split(","); switch (split[0]) { case 'wifi': - type = ConnectionType.wifi; + type = ConnectivityResult.wifi; break; case 'mobile': - type = ConnectionType.mobile; + type = ConnectivityResult.mobile; break; case 'none': default: - type = ConnectionType.none; + type = ConnectivityResult.none; } subType = _parseConnectionSubtype(split[1]); - return ConnectivityResult(type, subType); + return ConnectivityInfo(type, subType); } diff --git a/packages/connectivity/test/connectivity_test.dart b/packages/connectivity/test/connectivity_test.dart index bb999cb24b1b..590c7d2dae22 100644 --- a/packages/connectivity/test/connectivity_test.dart +++ b/packages/connectivity/test/connectivity_test.dart @@ -7,8 +7,7 @@ void main() { final List log = []; setUp(() async { - Connectivity.methodChannel - .setMockMethodCallHandler((MethodCall methodCall) async { + Connectivity.methodChannel.setMockMethodCallHandler((MethodCall methodCall) async { log.add(methodCall); switch (methodCall.method) { case 'check': @@ -26,8 +25,7 @@ void main() { } }); log.clear(); - MethodChannel(Connectivity.eventChannel.name) - .setMockMethodCallHandler((MethodCall methodCall) async { + MethodChannel(Connectivity.eventChannel.name).setMockMethodCallHandler((MethodCall methodCall) async { switch (methodCall.method) { case 'listen': // TODO(hterkelsen): Remove this when defaultBinaryMessages is in stable. @@ -35,8 +33,7 @@ void main() { // ignore: deprecated_member_use await BinaryMessages.handlePlatformMessage( Connectivity.eventChannel.name, - Connectivity.eventChannel.codec - .encodeSuccessEnvelope('mobile,hsdpa'), + Connectivity.eventChannel.codec.encodeSuccessEnvelope('mobile,hsdpa'), (_) {}, ); break; @@ -48,10 +45,14 @@ void main() { }); test('onConnectivityChanged', () async { - final ConnectivityResult result = - await Connectivity().onConnectivityChanged.first; - expect(result.type, ConnectivityResult.mobile); - expect(result.subtype, ConnectionSubtype.hsdpa); + final ConnectivityResult result = await Connectivity().onConnectivityChanged.first; + expect(result, ConnectivityResult.mobile); + }); + + test('onConnectivityInfoChanged', () async { + final ConnectivityInfo info = await Connectivity().onConnectivityInfoChanged.first; + expect(info.result, ConnectivityResult.mobile); + expect(info.subtype, ConnectionSubtype.hsdpa); }); test('getWifiName', () async { @@ -97,12 +98,24 @@ void main() { }); test('checkConnectivity', () async { - final ConnectivityResult result = - await Connectivity().checkConnectivity(); - // ignore: unrelated_type_equality_checks - expect(result == ConnectionType.wifi, true); - expect(result.type, ConnectivityResult.wifi); - expect(result.subtype, ConnectionSubtype.unknown); + final ConnectivityResult result = await Connectivity().checkConnectivity(); + expect(result, ConnectivityResult.wifi); + expect( + log, + [ + isMethodCall( + 'check', + arguments: null, + ), + ], + ); + }); + + + test('checkConnectivityInfo', () async { + final ConnectivityInfo info = await Connectivity().checkConnectivityInfo(); + expect(info.result, ConnectivityResult.wifi); + expect(info.subtype, ConnectionSubtype.unknown); expect( log, [ From 5716218344e5b6df37e6d942eeeca3388ddd6393 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Tue, 6 Aug 2019 21:19:56 +0000 Subject: [PATCH 19/25] updates tests in connectivity driver tests --- .../connectivity/example/test_driver/connectivity.dart | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/connectivity/example/test_driver/connectivity.dart b/packages/connectivity/example/test_driver/connectivity.dart index 7efba5e4ab50..25c4ffa7a46e 100644 --- a/packages/connectivity/example/test_driver/connectivity.dart +++ b/packages/connectivity/example/test_driver/connectivity.dart @@ -15,10 +15,11 @@ void main() { _connectivity = Connectivity(); }); - test('test connectivity result', () async { - final ConnectivityResult result = await _connectivity.checkConnectivity(); - expect(result, isNotNull); - switch (result) { + test('test connectivity info', () async { + final ConnectivityInfo info = await _connectivity.checkConnectivityInfo(); + expect(info, isNotNull); + expect(info.subtype, ConnectionSubtype.unknown); + switch (info.result) { case ConnectivityResult.wifi: expect(_connectivity.getWifiName(), completes); expect(_connectivity.getWifiBSSID(), completes); From f8a381c8a45a3008f3ac0a0d3c1487b9c2004c54 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Tue, 6 Aug 2019 21:29:08 +0000 Subject: [PATCH 20/25] Readme updated with new apis] --- packages/connectivity/README.md | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/connectivity/README.md b/packages/connectivity/README.md index d9495d40e667..e4e1dae458ac 100644 --- a/packages/connectivity/README.md +++ b/packages/connectivity/README.md @@ -12,13 +12,13 @@ Sample usage to check current status: ```dart import 'package:connectivity/connectivity.dart'; -var connectivityResult = await (Connectivity().checkConnectivity()); -if (connectivityResult == ConnectivityResult.mobile) { +var connectivityInfo = await (Connectivity().checkConnectivityInfo()); +if (connectivityInfo.result == ConnectivityResult.mobile) { // I am connected to a mobile network. - if(connectivityResult.subtype == ConnectionSubtype.HSDPA){ - // I am on a 3G network + if(connectivityInfo.subtype == ConnectionSubtype.HSDPA){ + // I am on an HSDPA network } -} else if (connectivityResult == ConnectivityResult.wifi) { +} else if (connectivityInfo.result == ConnectivityResult.wifi) { // I am connected to a wifi network. } ``` @@ -30,12 +30,12 @@ import 'package:connectivity/connectivity.dart'; var connectivityResult = await (Connectivity().getNetworkSubtype()); -if(connectivityResult.subtype == ConnectionSubtype.EDGE){ - // I am on a 2G network -} else if(connectivityResult.subtype == ConnectionSubtype.HSDPA){ - // I am on a 3G network -} else if(connectivityResult.subtype == ConnectionSubtype.LTE){ - // I am on a 4G network +if(connectivityResult.subtype == ConnectionSubtype.edge){ + // I am on an edge network +} else if(connectivityResult.subtype == ConnectionSubtype.hsdpa){ + // I am on a hsdpa network +} else if(connectivityResult.subtype == ConnectionSubtype.lte){ + // I am on an lte network } ``` @@ -53,9 +53,11 @@ import 'package:connectivity/connectivity.dart'; initState() { super.initState(); - subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) { + subscription = Connectivity().onConnectivityInfoChanged.listen((ConnectionInfo info) { // Got a new connectivity status! - }) + info.result = // ConnectivityResult info + info.subtype = // Mobile Connectivity Subtype info + }); } // Be sure to cancel subscription after you are done From 49c81744b6941aa49b981ebfc03c745115fb9960 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Tue, 6 Aug 2019 21:31:12 +0000 Subject: [PATCH 21/25] formatter run --- .../connectivity/ConnectivityPlugin.java | 138 ++++++++++-------- .../ios/Classes/ConnectivityPlugin.m | 5 +- packages/connectivity/lib/connectivity.dart | 13 +- .../connectivity/test/connectivity_test.dart | 22 ++- 4 files changed, 99 insertions(+), 79 deletions(-) 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 b5fe8671fc58..6cab9edbcfac 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 @@ -16,7 +16,6 @@ import android.net.wifi.WifiManager; import android.os.Build; import android.telephony.TelephonyManager; - import io.flutter.plugin.common.EventChannel; import io.flutter.plugin.common.EventChannel.EventSink; import io.flutter.plugin.common.EventChannel.StreamHandler; @@ -26,17 +25,13 @@ import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; -/** - * ConnectivityPlugin - */ +/** ConnectivityPlugin */ public class ConnectivityPlugin implements MethodCallHandler, StreamHandler { private final Registrar registrar; private final ConnectivityManager manager; private BroadcastReceiver receiver; - /** - * Plugin registration. - */ + /** Plugin registration. */ public static void registerWith(Registrar registrar) { final MethodChannel channel = new MethodChannel(registrar.messenger(), "plugins.flutter.io/connectivity"); @@ -123,62 +118,79 @@ private static String getNetworkSubType(ConnectivityManager manager) { /// Information about mobile broadband - https://en.wikipedia.org/wiki/Mobile_broadband#Generations switch (info.getSubtype()) { - case TelephonyManager.NETWORK_TYPE_1xRTT: { - return "1xRTT"; // ~ 50-100 kbps - } - case TelephonyManager.NETWORK_TYPE_CDMA: { - return "cdma"; // ~ 14-64 kbps - } - case TelephonyManager.NETWORK_TYPE_EDGE: { - return "edge"; // ~ 50-100 kbps - } - case TelephonyManager.NETWORK_TYPE_EVDO_0: { - return "evdo_0"; // ~ 400-1000 kbps - } - case TelephonyManager.NETWORK_TYPE_EVDO_A: { - return "evdo_a"; // ~ 600-1400 kbps - } - case TelephonyManager.NETWORK_TYPE_GPRS: { - return "gprs"; // ~ 100 kbps - } - case TelephonyManager.NETWORK_TYPE_HSDPA: { - return "hsdpa"; // ~ 2-14 Mbps - } - case TelephonyManager.NETWORK_TYPE_HSPA: { - return "hspa"; // ~ 700-1700 kbps - } - case TelephonyManager.NETWORK_TYPE_HSUPA: { - return "hsupa"; // ~ 1-23 Mbps - } - case TelephonyManager.NETWORK_TYPE_UMTS: { - return "umts"; // ~ 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 "ehrpd"; // ~ 1-2 Mbps - } - case TelephonyManager.NETWORK_TYPE_EVDO_B: { // API level 9 - return "evdo_b"; // ~ 5 Mbps - } - case TelephonyManager.NETWORK_TYPE_HSPAP: {// API level 13 - return "hspap"; // ~ 10-20 Mbps - } - case TelephonyManager.NETWORK_TYPE_IDEN: { // API level 8 - return "iden"; // ~25 kbps - } - case TelephonyManager.NETWORK_TYPE_LTE: { // API level 11 - return "lte"; // ~ 10+ Mbps - } - // Unknown - case TelephonyManager.NETWORK_TYPE_UNKNOWN: { - return "unknown"; // is connected but cannot tell the speed - } - default: { - return "none"; - } + case TelephonyManager.NETWORK_TYPE_1xRTT: + { + return "1xRTT"; // ~ 50-100 kbps + } + case TelephonyManager.NETWORK_TYPE_CDMA: + { + return "cdma"; // ~ 14-64 kbps + } + case TelephonyManager.NETWORK_TYPE_EDGE: + { + return "edge"; // ~ 50-100 kbps + } + case TelephonyManager.NETWORK_TYPE_EVDO_0: + { + return "evdo_0"; // ~ 400-1000 kbps + } + case TelephonyManager.NETWORK_TYPE_EVDO_A: + { + return "evdo_a"; // ~ 600-1400 kbps + } + case TelephonyManager.NETWORK_TYPE_GPRS: + { + return "gprs"; // ~ 100 kbps + } + case TelephonyManager.NETWORK_TYPE_HSDPA: + { + return "hsdpa"; // ~ 2-14 Mbps + } + case TelephonyManager.NETWORK_TYPE_HSPA: + { + return "hspa"; // ~ 700-1700 kbps + } + case TelephonyManager.NETWORK_TYPE_HSUPA: + { + return "hsupa"; // ~ 1-23 Mbps + } + case TelephonyManager.NETWORK_TYPE_UMTS: + { + return "umts"; // ~ 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 "ehrpd"; // ~ 1-2 Mbps + } + case TelephonyManager.NETWORK_TYPE_EVDO_B: + { // API level 9 + return "evdo_b"; // ~ 5 Mbps + } + case TelephonyManager.NETWORK_TYPE_HSPAP: + { // API level 13 + return "hspap"; // ~ 10-20 Mbps + } + case TelephonyManager.NETWORK_TYPE_IDEN: + { // API level 8 + return "iden"; // ~25 kbps + } + case TelephonyManager.NETWORK_TYPE_LTE: + { // API level 11 + return "lte"; // ~ 10+ Mbps + } + // Unknown + case TelephonyManager.NETWORK_TYPE_UNKNOWN: + { + return "unknown"; // is connected but cannot tell the speed + } + default: + { + return "none"; + } } } diff --git a/packages/connectivity/ios/Classes/ConnectivityPlugin.m b/packages/connectivity/ios/Classes/ConnectivityPlugin.m index 39b36624f8d8..c0062ade7a0d 100644 --- a/packages/connectivity/ios/Classes/ConnectivityPlugin.m +++ b/packages/connectivity/ios/Classes/ConnectivityPlugin.m @@ -88,11 +88,10 @@ - (NSString*)getWifiIP { } - (NSString*)getConnectionSubtype:(Reachability*)reachability { - - if([reachability currentReachabilityStatus] == NotReachable){ + if ([reachability currentReachabilityStatus] == NotReachable) { return @"none"; } - + CTTelephonyNetworkInfo* netinfo = [[CTTelephonyNetworkInfo alloc] init]; NSString* carrierType = netinfo.currentRadioAccessTechnology; diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index b222dadf82f0..0504d0170290 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -49,7 +49,8 @@ Map connectionTypeMap = { "hspap": ConnectionSubtype.hspap, // ~ 10-20 Mbps "iden": ConnectionSubtype.iden, // ~25 kbps "lte": ConnectionSubtype.lte, // ~ 10+ Mbps - "unknown": ConnectionSubtype.unknown, // is connected but cannot tell the speed + "unknown": + ConnectionSubtype.unknown, // is connected but cannot tell the speed "none": ConnectionSubtype.none }; @@ -94,8 +95,9 @@ class Connectivity { /// Fires whenever the connectivity state changes. Returns stream of [ConnectivityResult] Stream get onConnectivityChanged { if (_onConnectivityChanged == null) { - _onConnectivityChanged = - eventChannel.receiveBroadcastStream().map((dynamic event) => _parseConnectivityResult(event).result); + _onConnectivityChanged = eventChannel + .receiveBroadcastStream() + .map((dynamic event) => _parseConnectivityResult(event).result); } return _onConnectivityChanged; } @@ -103,8 +105,9 @@ class Connectivity { /// Fires whenever the connectivity state changes. Return stream of [ConnectivityInfo] Stream get onConnectivityInfoChanged { if (_onConnectivityInfoChanged == null) { - _onConnectivityInfoChanged = - eventChannel.receiveBroadcastStream().map((dynamic event) => _parseConnectivityResult(event)); + _onConnectivityInfoChanged = eventChannel + .receiveBroadcastStream() + .map((dynamic event) => _parseConnectivityResult(event)); } return _onConnectivityInfoChanged; } diff --git a/packages/connectivity/test/connectivity_test.dart b/packages/connectivity/test/connectivity_test.dart index 590c7d2dae22..0ebc9ab8a60e 100644 --- a/packages/connectivity/test/connectivity_test.dart +++ b/packages/connectivity/test/connectivity_test.dart @@ -7,7 +7,8 @@ void main() { final List log = []; setUp(() async { - Connectivity.methodChannel.setMockMethodCallHandler((MethodCall methodCall) async { + Connectivity.methodChannel + .setMockMethodCallHandler((MethodCall methodCall) async { log.add(methodCall); switch (methodCall.method) { case 'check': @@ -25,7 +26,8 @@ void main() { } }); log.clear(); - MethodChannel(Connectivity.eventChannel.name).setMockMethodCallHandler((MethodCall methodCall) async { + MethodChannel(Connectivity.eventChannel.name) + .setMockMethodCallHandler((MethodCall methodCall) async { switch (methodCall.method) { case 'listen': // TODO(hterkelsen): Remove this when defaultBinaryMessages is in stable. @@ -33,7 +35,8 @@ void main() { // ignore: deprecated_member_use await BinaryMessages.handlePlatformMessage( Connectivity.eventChannel.name, - Connectivity.eventChannel.codec.encodeSuccessEnvelope('mobile,hsdpa'), + Connectivity.eventChannel.codec + .encodeSuccessEnvelope('mobile,hsdpa'), (_) {}, ); break; @@ -45,12 +48,14 @@ void main() { }); test('onConnectivityChanged', () async { - final ConnectivityResult result = await Connectivity().onConnectivityChanged.first; + final ConnectivityResult result = + await Connectivity().onConnectivityChanged.first; expect(result, ConnectivityResult.mobile); }); test('onConnectivityInfoChanged', () async { - final ConnectivityInfo info = await Connectivity().onConnectivityInfoChanged.first; + final ConnectivityInfo info = + await Connectivity().onConnectivityInfoChanged.first; expect(info.result, ConnectivityResult.mobile); expect(info.subtype, ConnectionSubtype.hsdpa); }); @@ -98,7 +103,8 @@ void main() { }); test('checkConnectivity', () async { - final ConnectivityResult result = await Connectivity().checkConnectivity(); + final ConnectivityResult result = + await Connectivity().checkConnectivity(); expect(result, ConnectivityResult.wifi); expect( log, @@ -111,9 +117,9 @@ void main() { ); }); - test('checkConnectivityInfo', () async { - final ConnectivityInfo info = await Connectivity().checkConnectivityInfo(); + final ConnectivityInfo info = + await Connectivity().checkConnectivityInfo(); expect(info.result, ConnectivityResult.wifi); expect(info.subtype, ConnectionSubtype.unknown); expect( From ada8a1eb2bbcd38bd36c7637b93f2b0e8bc971c2 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Tue, 6 Aug 2019 23:17:32 +0000 Subject: [PATCH 22/25] feedback from review implemented --- .../example/test_driver/connectivity.dart | 2 +- packages/connectivity/lib/connectivity.dart | 63 +++++++++---------- .../connectivity/test/connectivity_test.dart | 4 +- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/packages/connectivity/example/test_driver/connectivity.dart b/packages/connectivity/example/test_driver/connectivity.dart index 25c4ffa7a46e..c811e0626cae 100644 --- a/packages/connectivity/example/test_driver/connectivity.dart +++ b/packages/connectivity/example/test_driver/connectivity.dart @@ -16,7 +16,7 @@ void main() { }); test('test connectivity info', () async { - final ConnectivityInfo info = await _connectivity.checkConnectivityInfo(); + final ConnectivityDetailedResult info = await _connectivity.checkConnectivityInfo(); expect(info, isNotNull); expect(info.subtype, ConnectionSubtype.unknown); switch (info.result) { diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index 0504d0170290..80f0d018a2c5 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -49,13 +49,15 @@ Map connectionTypeMap = { "hspap": ConnectionSubtype.hspap, // ~ 10-20 Mbps "iden": ConnectionSubtype.iden, // ~25 kbps "lte": ConnectionSubtype.lte, // ~ 10+ Mbps - "unknown": - ConnectionSubtype.unknown, // is connected but cannot tell the speed + "unknown": ConnectionSubtype.unknown, // is connected but cannot tell the speed "none": ConnectionSubtype.none }; -class ConnectivityInfo { - ConnectivityInfo(this.result, this.subtype); +class ConnectivityDetailedResult { + ConnectivityDetailedResult({ + this.result = ConnectivityResult.none, + this.subtype = ConnectionSubtype.none, + }); final ConnectivityResult result; final ConnectionSubtype subtype; @@ -80,7 +82,7 @@ class Connectivity { static Connectivity _singleton; Stream _onConnectivityChanged; - Stream _onConnectivityInfoChanged; + Stream _onConnectivityInfoChanged; @visibleForTesting static const MethodChannel methodChannel = MethodChannel( @@ -95,19 +97,17 @@ class Connectivity { /// Fires whenever the connectivity state changes. Returns stream of [ConnectivityResult] Stream get onConnectivityChanged { if (_onConnectivityChanged == null) { - _onConnectivityChanged = eventChannel - .receiveBroadcastStream() - .map((dynamic event) => _parseConnectivityResult(event).result); + _onConnectivityChanged = + eventChannel.receiveBroadcastStream().map((dynamic event) => _parseConnectivityDetailedResult(event).result); } return _onConnectivityChanged; } - /// Fires whenever the connectivity state changes. Return stream of [ConnectivityInfo] - Stream get onConnectivityInfoChanged { + /// Fires whenever the connectivity state changes. Return stream of [ConnectivityDetailedResult] + Stream get onConnectivityInfoChanged { if (_onConnectivityInfoChanged == null) { - _onConnectivityInfoChanged = eventChannel - .receiveBroadcastStream() - .map((dynamic event) => _parseConnectivityResult(event)); + _onConnectivityInfoChanged = + eventChannel.receiveBroadcastStream().map((dynamic event) => _parseConnectivityDetailedResult(event)); } return _onConnectivityInfoChanged; } @@ -122,13 +122,13 @@ class Connectivity { /// You can also check the mobile broadband connectivity subtype via [getNetworkSubtype] Future checkConnectivity() async { final String result = await methodChannel.invokeMethod('check'); - return _parseConnectivityResult(result).result; + return _parseConnectivityDetailedResult(result).result; } - /// Checks connectivity info, [ConnectivityInfo] - Future checkConnectivityInfo() async { + /// Checks connectivity info, [ConnectivityDetailedResult] + Future checkConnectivityInfo() async { final String result = await methodChannel.invokeMethod('check'); - return _parseConnectivityResult(result); + return _parseConnectivityDetailedResult(result); } /// Checks the network mobile connection subtype of the device. @@ -175,27 +175,26 @@ class Connectivity { } } -ConnectionSubtype _parseConnectionSubtype(String state) { - return connectionTypeMap[state]; +ConnectivityDetailedResult _parseConnectivityDetailedResult(String state) { + final List split = state.split(","); + return ConnectivityDetailedResult( + result: _parseConnectivityResult(split[0]), + subtype: _parseConnectionSubtype(split[1]), + ); } -ConnectivityInfo _parseConnectivityResult(String state) { - ConnectivityResult type = ConnectivityResult.none; - ConnectionSubtype subType = ConnectionSubtype.unknown; - - final List split = state.split(","); +ConnectionSubtype _parseConnectionSubtype(String state) { + return connectionTypeMap[state] ?? ConnectionSubtype.unknown; +} - switch (split[0]) { +ConnectivityResult _parseConnectivityResult(String state) { + switch (state) { case 'wifi': - type = ConnectivityResult.wifi; - break; + return ConnectivityResult.wifi; case 'mobile': - type = ConnectivityResult.mobile; - break; + return ConnectivityResult.mobile; case 'none': default: - type = ConnectivityResult.none; + return ConnectivityResult.none; } - subType = _parseConnectionSubtype(split[1]); - return ConnectivityInfo(type, subType); } diff --git a/packages/connectivity/test/connectivity_test.dart b/packages/connectivity/test/connectivity_test.dart index 0ebc9ab8a60e..33c4bb802e5e 100644 --- a/packages/connectivity/test/connectivity_test.dart +++ b/packages/connectivity/test/connectivity_test.dart @@ -54,7 +54,7 @@ void main() { }); test('onConnectivityInfoChanged', () async { - final ConnectivityInfo info = + final ConnectivityDetailedResult info = await Connectivity().onConnectivityInfoChanged.first; expect(info.result, ConnectivityResult.mobile); expect(info.subtype, ConnectionSubtype.hsdpa); @@ -118,7 +118,7 @@ void main() { }); test('checkConnectivityInfo', () async { - final ConnectivityInfo info = + final ConnectivityDetailedResult info = await Connectivity().checkConnectivityInfo(); expect(info.result, ConnectivityResult.wifi); expect(info.subtype, ConnectionSubtype.unknown); From 54dbe04d6cf5c5c4c08d029893d16f8678a7b2e8 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Tue, 6 Aug 2019 23:19:46 +0000 Subject: [PATCH 23/25] formatter run --- .../example/test_driver/connectivity.dart | 3 ++- packages/connectivity/lib/connectivity.dart | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/connectivity/example/test_driver/connectivity.dart b/packages/connectivity/example/test_driver/connectivity.dart index c811e0626cae..4ffc3e6b1091 100644 --- a/packages/connectivity/example/test_driver/connectivity.dart +++ b/packages/connectivity/example/test_driver/connectivity.dart @@ -16,7 +16,8 @@ void main() { }); test('test connectivity info', () async { - final ConnectivityDetailedResult info = await _connectivity.checkConnectivityInfo(); + final ConnectivityDetailedResult info = + await _connectivity.checkConnectivityInfo(); expect(info, isNotNull); expect(info.subtype, ConnectionSubtype.unknown); switch (info.result) { diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index 80f0d018a2c5..1382879b50e2 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -49,7 +49,8 @@ Map connectionTypeMap = { "hspap": ConnectionSubtype.hspap, // ~ 10-20 Mbps "iden": ConnectionSubtype.iden, // ~25 kbps "lte": ConnectionSubtype.lte, // ~ 10+ Mbps - "unknown": ConnectionSubtype.unknown, // is connected but cannot tell the speed + "unknown": + ConnectionSubtype.unknown, // is connected but cannot tell the speed "none": ConnectionSubtype.none }; @@ -97,8 +98,8 @@ class Connectivity { /// Fires whenever the connectivity state changes. Returns stream of [ConnectivityResult] Stream get onConnectivityChanged { if (_onConnectivityChanged == null) { - _onConnectivityChanged = - eventChannel.receiveBroadcastStream().map((dynamic event) => _parseConnectivityDetailedResult(event).result); + _onConnectivityChanged = eventChannel.receiveBroadcastStream().map( + (dynamic event) => _parseConnectivityDetailedResult(event).result); } return _onConnectivityChanged; } @@ -106,8 +107,9 @@ class Connectivity { /// Fires whenever the connectivity state changes. Return stream of [ConnectivityDetailedResult] Stream get onConnectivityInfoChanged { if (_onConnectivityInfoChanged == null) { - _onConnectivityInfoChanged = - eventChannel.receiveBroadcastStream().map((dynamic event) => _parseConnectivityDetailedResult(event)); + _onConnectivityInfoChanged = eventChannel + .receiveBroadcastStream() + .map((dynamic event) => _parseConnectivityDetailedResult(event)); } return _onConnectivityInfoChanged; } From 33a17a4cc7d07430fcd9680aa60f562d896707e3 Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Wed, 4 Sep 2019 10:56:08 +0000 Subject: [PATCH 24/25] update Changelog and bump connectivity package version --- packages/connectivity/CHANGELOG.md | 8 ++++---- packages/connectivity/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/connectivity/CHANGELOG.md b/packages/connectivity/CHANGELOG.md index d52538595e2d..5714035c44e9 100644 --- a/packages/connectivity/CHANGELOG.md +++ b/packages/connectivity/CHANGELOG.md @@ -1,13 +1,13 @@ -## 0.4.4 +## 0.4.5 -<<<<<<< HEAD * Change `ConnectionResult` type from enum to class with 2 fields. `type:ConnectionType` and `subtype:ConnectionSubtype` * Add `getNetworkSubtype` to get mobile connection subtype. -======= + +## 0.4.4 + * Add `requestLocationServiceAuthorization` to request location authorization on iOS. * Add `getLocationServiceAuthorization` to get location authorization status on iOS. * Update README: add more information on iOS 13 updates with CNCopyCurrentNetworkInfo. ->>>>>>> 0a7535d1cd7119767d8d2506b2c9e3742f585fa8 ## 0.4.3+7 diff --git a/packages/connectivity/pubspec.yaml b/packages/connectivity/pubspec.yaml index 7ec5d31bcc42..65316df82e49 100644 --- a/packages/connectivity/pubspec.yaml +++ b/packages/connectivity/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for discovering the state of the network (WiFi & mobile/cellular) connectivity on Android and iOS. author: Flutter Team homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity -version: 0.4.4 +version: 0.4.5 flutter: plugin: From ea6f5386020d14d1bed68402fc649ed6aff68a3f Mon Sep 17 00:00:00 2001 From: Bubunyo Nyavor Date: Thu, 17 Oct 2019 20:04:55 +0000 Subject: [PATCH 25/25] Refactor Connectivity to match new android embedding --- .../plugins/connectivity/Connectivity.java | 74 +++++++ .../ConnectivityMethodChannelHandler.java | 3 + .../connectivity/ConnectivityPlugin.java | 187 +----------------- .../connectivity/test/connectivity_e2e.dart | 6 +- 4 files changed, 84 insertions(+), 186 deletions(-) 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 index 605acdb73948..bf5c032478b9 100644 --- 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 @@ -11,6 +11,8 @@ import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Build; +import android.telephony.TelephonyManager; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -107,4 +109,76 @@ private String getNetworkTypeLegacy() { return "none"; } } + + @Nullable + String getNetworkSubType() { + + NetworkInfo info = this.connectivityManager.getActiveNetworkInfo(); + + if (info == null || !info.isConnected()) { + return null; + } + + /// Telephony Manager documentation https://developer.android.com/reference/android/telephony/TelephonyManager + /// Information about mobile broadband - https://en.wikipedia.org/wiki/Mobile_broadband#Generations + + switch (info.getSubtype()) { + case TelephonyManager.NETWORK_TYPE_1xRTT: { + return "1xRTT"; // ~ 50-100 kbps + } + case TelephonyManager.NETWORK_TYPE_CDMA: { + return "cdma"; // ~ 14-64 kbps + } + case TelephonyManager.NETWORK_TYPE_EDGE: { + return "edge"; // ~ 50-100 kbps + } + case TelephonyManager.NETWORK_TYPE_EVDO_0: { + return "evdo_0"; // ~ 400-1000 kbps + } + case TelephonyManager.NETWORK_TYPE_EVDO_A: { + return "evdo_a"; // ~ 600-1400 kbps + } + case TelephonyManager.NETWORK_TYPE_GPRS: { + return "gprs"; // ~ 100 kbps + } + case TelephonyManager.NETWORK_TYPE_HSDPA: { + return "hsdpa"; // ~ 2-14 Mbps + } + case TelephonyManager.NETWORK_TYPE_HSPA: { + return "hspa"; // ~ 700-1700 kbps + } + case TelephonyManager.NETWORK_TYPE_HSUPA: { + return "hsupa"; // ~ 1-23 Mbps + } + case TelephonyManager.NETWORK_TYPE_UMTS: { + return "umts"; // ~ 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 "ehrpd"; // ~ 1-2 Mbps + } + case TelephonyManager.NETWORK_TYPE_EVDO_B: { // API level 9 + return "evdo_b"; // ~ 5 Mbps + } + case TelephonyManager.NETWORK_TYPE_HSPAP: { // API level 13 + return "hspap"; // ~ 10-20 Mbps + } + case TelephonyManager.NETWORK_TYPE_IDEN: { // API level 8 + return "iden"; // ~25 kbps + } + case TelephonyManager.NETWORK_TYPE_LTE: { // API level 11 + return "lte"; // ~ 10+ Mbps + } + // Unknown + case TelephonyManager.NETWORK_TYPE_UNKNOWN: { + return "unknown"; // is connected but cannot tell the speed + } + default: { + return null; + } + } + } } 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 index 488c8efdd15f..9f0b04ad28d8 100644 --- 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 @@ -41,6 +41,9 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) { case "wifiIPAddress": result.success(connectivity.getWifiIPAddress()); break; + case "subtype": + result.success(connectivity.getNetworkSubType()); + 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 c10fb5642c13..6557c8fba0fc 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 @@ -7,13 +7,9 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.wifi.WifiManager; -<<<<<<< HEAD -import android.os.Build; -import android.telephony.TelephonyManager; -======= + import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.plugin.common.BinaryMessenger; ->>>>>>> dce7169d302f1c5c1327692620458c9afd4b73d7 import io.flutter.plugin.common.EventChannel; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.PluginRegistry.Registrar; @@ -41,189 +37,12 @@ public void onDetachedFromEngine(FlutterPluginBinding binding) { teardownChannels(); } -<<<<<<< HEAD - @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"; - } - } - - private static String getNetworkSubType(ConnectivityManager manager) { - NetworkInfo info = manager.getActiveNetworkInfo(); - - if (info == null || !info.isConnected()) { - return "none"; - } - - /// Telephony Manager documentation https://developer.android.com/reference/android/telephony/TelephonyManager - /// Information about mobile broadband - https://en.wikipedia.org/wiki/Mobile_broadband#Generations - - switch (info.getSubtype()) { - case TelephonyManager.NETWORK_TYPE_1xRTT: - { - return "1xRTT"; // ~ 50-100 kbps - } - case TelephonyManager.NETWORK_TYPE_CDMA: - { - return "cdma"; // ~ 14-64 kbps - } - case TelephonyManager.NETWORK_TYPE_EDGE: - { - return "edge"; // ~ 50-100 kbps - } - case TelephonyManager.NETWORK_TYPE_EVDO_0: - { - return "evdo_0"; // ~ 400-1000 kbps - } - case TelephonyManager.NETWORK_TYPE_EVDO_A: - { - return "evdo_a"; // ~ 600-1400 kbps - } - case TelephonyManager.NETWORK_TYPE_GPRS: - { - return "gprs"; // ~ 100 kbps - } - case TelephonyManager.NETWORK_TYPE_HSDPA: - { - return "hsdpa"; // ~ 2-14 Mbps - } - case TelephonyManager.NETWORK_TYPE_HSPA: - { - return "hspa"; // ~ 700-1700 kbps - } - case TelephonyManager.NETWORK_TYPE_HSUPA: - { - return "hsupa"; // ~ 1-23 Mbps - } - case TelephonyManager.NETWORK_TYPE_UMTS: - { - return "umts"; // ~ 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 "ehrpd"; // ~ 1-2 Mbps - } - case TelephonyManager.NETWORK_TYPE_EVDO_B: - { // API level 9 - return "evdo_b"; // ~ 5 Mbps - } - case TelephonyManager.NETWORK_TYPE_HSPAP: - { // API level 13 - return "hspap"; // ~ 10-20 Mbps - } - case TelephonyManager.NETWORK_TYPE_IDEN: - { // API level 8 - return "iden"; // ~25 kbps - } - case TelephonyManager.NETWORK_TYPE_LTE: - { // API level 11 - return "lte"; // ~ 10+ Mbps - } - // Unknown - case TelephonyManager.NETWORK_TYPE_UNKNOWN: - { - return "unknown"; // is connected but cannot tell the speed - } - 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; - case "subtype": - handleNetworkSubType(call, result); - break; - default: - result.notImplemented(); - break; - } - } - - private void handleCheck(MethodCall call, final Result result) { - result.success(checkNetworkType()); - } - - private void handleNetworkSubType(MethodCall call, final Result result) { - result.success(getNetworkSubType(manager)); - } - - private String checkNetworkType() { - return getNetworkType(manager) + "," + getNetworkSubType(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); ->>>>>>> dce7169d302f1c5c1327692620458c9afd4b73d7 + WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); Connectivity connectivity = new Connectivity(connectivityManager, wifiManager); @@ -242,4 +61,4 @@ private void teardownChannels() { methodChannel = null; eventChannel = null; } -} +} \ No newline at end of file diff --git a/packages/connectivity/test/connectivity_e2e.dart b/packages/connectivity/test/connectivity_e2e.dart index 8522e4ff28bb..54f43f96fc3b 100644 --- a/packages/connectivity/test/connectivity_e2e.dart +++ b/packages/connectivity/test/connectivity_e2e.dart @@ -18,7 +18,8 @@ void main() { }); test('test connectivity result', (WidgetTester tester) async { - final ConnectivityDetailedResult result = await _connectivity.checkConnectivityInfo(); + final ConnectivityDetailedResult result = + await _connectivity.checkConnectivityInfo(); expect(result, isNotNull); expect(result.subtype, ConnectionSubtype.unknown); switch (result.result) { @@ -34,7 +35,8 @@ void main() { testWidgets('test location methods, iOS only', (WidgetTester tester) async { if (Platform.isIOS) { - expect((await _connectivity.getLocationServiceAuthorization()), LocationAuthorizationStatus.notDetermined); + expect((await _connectivity.getLocationServiceAuthorization()), + LocationAuthorizationStatus.notDetermined); } }); });