-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[connectivity] - add network speed info #1727
Changes from 15 commits
f08e866
3e9b146
68509b6
b222a5a
c32222b
9ccc51c
16a9a71
596e5e4
8ff7d9c
1525456
df989a3
97636ef
8f0c1cd
56dc958
ba74b91
8ddf11f
843f316
12e2a4b
8f34cd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| import android.net.NetworkInfo; | ||
| import android.net.wifi.WifiInfo; | ||
| import android.net.wifi.WifiManager; | ||
| import android.telephony.TelephonyManager; | ||
| import io.flutter.plugin.common.EventChannel; | ||
| import io.flutter.plugin.common.EventChannel.EventSink; | ||
| import io.flutter.plugin.common.EventChannel.StreamHandler; | ||
|
|
@@ -76,6 +77,60 @@ private static String getNetworkType(int type) { | |
| return "none"; | ||
| } | ||
| } | ||
|
|
||
| private static String getNetworkSubType(int type, int subType) { | ||
| 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: | ||
| switch (subType) { | ||
| case TelephonyManager.NETWORK_TYPE_1xRTT: | ||
| return "2G"; // ~ 50-100 kbps | ||
| case TelephonyManager.NETWORK_TYPE_CDMA: | ||
| return "2G"; // ~ 14-64 kbps | ||
| case TelephonyManager.NETWORK_TYPE_EDGE: | ||
| return "2G"; // ~ 50-100 kbps | ||
| case TelephonyManager.NETWORK_TYPE_EVDO_0: | ||
| return "3G"; // ~ 400-1000 kbps | ||
| case TelephonyManager.NETWORK_TYPE_EVDO_A: | ||
| return "3G"; // ~ 600-1400 kbps | ||
| case TelephonyManager.NETWORK_TYPE_GPRS: | ||
| return "2G"; // ~ 100 kbps | ||
| case TelephonyManager.NETWORK_TYPE_HSDPA: | ||
| return "3G"; // ~ 2-14 Mbps | ||
| case TelephonyManager.NETWORK_TYPE_HSPA: | ||
| return "3G"; // ~ 700-1700 kbps | ||
| case TelephonyManager.NETWORK_TYPE_HSUPA: | ||
| return "3G"; // ~ 1-23 Mbps | ||
| case TelephonyManager.NETWORK_TYPE_UMTS: | ||
| return "3G"; // ~ 400-7000 kbps | ||
| /* | ||
| * Above API level 7, make sure to set android:targetSdkVersion | ||
| * to appropriate level to use these | ||
| */ | ||
| case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11 | ||
| return "3G"; // ~ 1-2 Mbps | ||
| case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9 | ||
| return "3G"; // ~ 5 Mbps | ||
| case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13 | ||
| return "3G"; // ~ 10-20 Mbps | ||
| case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8 | ||
| return "2G"; // ~25 kbps | ||
| case TelephonyManager.NETWORK_TYPE_LTE: // API level 11 | ||
| return "4G"; // ~ 10+ Mbps | ||
| // Unknown | ||
| case TelephonyManager.NETWORK_TYPE_UNKNOWN: | ||
| default: | ||
| return "none"; | ||
| } | ||
| default: | ||
| return "none"; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onMethodCall(MethodCall call, Result result) { | ||
|
|
@@ -92,12 +147,24 @@ public void onMethodCall(MethodCall call, Result result) { | |
| case "wifiIPAddress": | ||
| handleWifiIPAddress(call, result); | ||
| break; | ||
| case "getMobileConnectionType" | ||
| handleMobileConnectionType(call, result); | ||
| break; | ||
| default: | ||
| result.notImplemented(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| private void handleMobileConnectionType(MethodCall call, final Result result) { | ||
| NetworkInfo info = manager.getActiveNetworkInfo(); | ||
| if (info != null && info.isConnected()) { | ||
| result.success(getNetworkSubType(info.getType(), info.getSubtype())); | ||
| } else { | ||
| result.success("none"); | ||
| } | ||
| } | ||
|
|
||
| private void handleCheck(MethodCall call, final Result result) { | ||
| result.success(checkNetworkType()); | ||
| } | ||
|
|
@@ -158,6 +225,16 @@ private BroadcastReceiver createReceiver(final EventSink events) { | |
| return new BroadcastReceiver() { | ||
| @Override | ||
| public void onReceive(Context context, Intent intent) { | ||
| boolean isLost = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); | ||
|
||
| if (isLost) { | ||
| events.success("none"); | ||
| return; | ||
| } | ||
|
|
||
| int type = intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, -1); | ||
| TelephonyManager mTelephonyManager = | ||
| (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); | ||
| int subType = mTelephonyManager.getNetworkType(); | ||
| events.success(checkNetworkType()); | ||
| } | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,10 @@ description: Flutter plugin for discovering the state of the network (WiFi & | |
| mobile/cellular) connectivity on Android and iOS. | ||
| author: Flutter Team <flutter-dev@googlegroups.com> | ||
| homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra line here, could you remove it? |
||
| version: 0.4.3+4 | ||
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another extra line here. |
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. revert |
||
| flutter: | ||
| plugin: | ||
| androidPackage: io.flutter.plugins.connectivity | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.