|
| 1 | +// Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package io.flutter.plugins.connectivity; |
| 6 | + |
| 7 | +import android.net.ConnectivityManager; |
| 8 | +import android.net.Network; |
| 9 | +import android.net.NetworkCapabilities; |
| 10 | +import android.net.NetworkInfo; |
| 11 | +import android.net.wifi.WifiInfo; |
| 12 | +import android.net.wifi.WifiManager; |
| 13 | +import android.os.Build; |
| 14 | +import androidx.annotation.NonNull; |
| 15 | +import androidx.annotation.Nullable; |
| 16 | + |
| 17 | +/** Reports connectivity related information such as connectivity type and wifi information. */ |
| 18 | +class Connectivity { |
| 19 | + private ConnectivityManager connectivityManager; |
| 20 | + private WifiManager wifiManager; |
| 21 | + |
| 22 | + Connectivity(ConnectivityManager connectivityManager, WifiManager wifiManager) { |
| 23 | + this.connectivityManager = connectivityManager; |
| 24 | + this.wifiManager = wifiManager; |
| 25 | + } |
| 26 | + |
| 27 | + @NonNull |
| 28 | + String getNetworkType() { |
| 29 | + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 30 | + Network network = connectivityManager.getActiveNetwork(); |
| 31 | + NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network); |
| 32 | + if (capabilities == null) { |
| 33 | + return "none"; |
| 34 | + } |
| 35 | + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) |
| 36 | + || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { |
| 37 | + return "wifi"; |
| 38 | + } |
| 39 | + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { |
| 40 | + return "mobile"; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return getNetworkTypeLegacy(); |
| 45 | + } |
| 46 | + |
| 47 | + @Nullable |
| 48 | + String getWifiName() { |
| 49 | + WifiInfo wifiInfo = getWifiInfo(); |
| 50 | + String ssid = null; |
| 51 | + if (wifiInfo != null) ssid = wifiInfo.getSSID(); |
| 52 | + if (ssid != null) ssid = ssid.replaceAll("\"", ""); // Android returns "SSID" |
| 53 | + return ssid; |
| 54 | + } |
| 55 | + |
| 56 | + @Nullable |
| 57 | + String getWifiBSSID() { |
| 58 | + WifiInfo wifiInfo = getWifiInfo(); |
| 59 | + String bssid = null; |
| 60 | + if (wifiInfo != null) { |
| 61 | + bssid = wifiInfo.getBSSID(); |
| 62 | + } |
| 63 | + return bssid; |
| 64 | + } |
| 65 | + |
| 66 | + @Nullable |
| 67 | + String getWifiIPAddress() { |
| 68 | + WifiInfo wifiInfo = null; |
| 69 | + if (wifiManager != null) wifiInfo = wifiManager.getConnectionInfo(); |
| 70 | + |
| 71 | + String ip = null; |
| 72 | + int i_ip = 0; |
| 73 | + if (wifiInfo != null) i_ip = wifiInfo.getIpAddress(); |
| 74 | + |
| 75 | + if (i_ip != 0) |
| 76 | + ip = |
| 77 | + String.format( |
| 78 | + "%d.%d.%d.%d", |
| 79 | + (i_ip & 0xff), (i_ip >> 8 & 0xff), (i_ip >> 16 & 0xff), (i_ip >> 24 & 0xff)); |
| 80 | + |
| 81 | + return ip; |
| 82 | + } |
| 83 | + |
| 84 | + @Nullable |
| 85 | + private WifiInfo getWifiInfo() { |
| 86 | + return wifiManager == null ? null : wifiManager.getConnectionInfo(); |
| 87 | + } |
| 88 | + |
| 89 | + @SuppressWarnings("deprecation") |
| 90 | + private String getNetworkTypeLegacy() { |
| 91 | + // handle type for Android versions less than Android 9 |
| 92 | + NetworkInfo info = connectivityManager.getActiveNetworkInfo(); |
| 93 | + if (info == null || !info.isConnected()) { |
| 94 | + return "none"; |
| 95 | + } |
| 96 | + int type = info.getType(); |
| 97 | + switch (type) { |
| 98 | + case ConnectivityManager.TYPE_ETHERNET: |
| 99 | + case ConnectivityManager.TYPE_WIFI: |
| 100 | + case ConnectivityManager.TYPE_WIMAX: |
| 101 | + return "wifi"; |
| 102 | + case ConnectivityManager.TYPE_MOBILE: |
| 103 | + case ConnectivityManager.TYPE_MOBILE_DUN: |
| 104 | + case ConnectivityManager.TYPE_MOBILE_HIPRI: |
| 105 | + return "mobile"; |
| 106 | + default: |
| 107 | + return "none"; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments