-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
NetworkCallbackConnectivityReceiver.java
194 lines (169 loc) · 7.66 KB
/
NetworkCallbackConnectivityReceiver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package versioned.host.exp.exponent.modules.api.netinfo;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import com.facebook.react.bridge.ReactApplicationContext;
import versioned.host.exp.exponent.modules.api.netinfo.types.CellularGeneration;
import versioned.host.exp.exponent.modules.api.netinfo.types.ConnectionType;
/**
* This gets the connectivity status using a NetworkCallback on the system default network. This
* method was added into Android from API level 24 (N) and we use it for all devices which support
* it.
*/
@TargetApi(Build.VERSION_CODES.N)
public class NetworkCallbackConnectivityReceiver extends ConnectivityReceiver {
private static final int DELAY_MS = 250;
private final ConnectivityNetworkCallback mNetworkCallback;
// from the docs:
// "Do NOT call ConnectivityManager.getNetworkCapabilities(android.net.Network)
// or ConnectivityManager.getLinkProperties(android.net.Network) or other
// synchronous ConnectivityManager methods in this callback as this is
// prone to race conditions ; calling these methods while in a callback
// may return an outdated or even a null object."
// https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback
// For this reason, we will fetch these values when not provided by the callback
// on an asynchronous thread.
private Network mNetwork = null;
private NetworkCapabilities mCapabilities = null;
public NetworkCallbackConnectivityReceiver(ReactApplicationContext reactContext) {
super(reactContext);
mNetworkCallback = new ConnectivityNetworkCallback();
}
@Override
@SuppressLint("MissingPermission")
public void register() {
try {
// Similar to BroadcastReceiver implementation, we need to force
// an initial callback call in order to get the current network state,
// otherwise, an app started without any network connection will
// always be reported as "unknown".
mNetwork = getConnectivityManager().getActiveNetwork();
asyncUpdateAndSend(0);
getConnectivityManager().registerDefaultNetworkCallback(mNetworkCallback);
} catch (SecurityException e) {
// TODO: Display a yellow box about this
}
}
@Override
public void unregister() {
try {
getConnectivityManager().unregisterNetworkCallback(mNetworkCallback);
} catch (SecurityException e) {
// TODO: Display a yellow box about this
} catch (IllegalArgumentException e) {
// ignore this, it is expected when the callback was not registered successfully
}
}
@SuppressLint("MissingPermission")
void updateAndSend() {
ConnectionType connectionType = ConnectionType.UNKNOWN;
CellularGeneration cellularGeneration = null;
NetworkInfo networkInfo = null;
boolean isInternetReachable = false;
boolean isInternetSuspended = false;
final Network network = mNetwork;
final NetworkCapabilities capabilities = mCapabilities;
if (capabilities != null) {
// Get the connection type
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) {
connectionType = ConnectionType.BLUETOOTH;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
connectionType = ConnectionType.CELLULAR;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
connectionType = ConnectionType.ETHERNET;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
connectionType = ConnectionType.WIFI;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
connectionType = ConnectionType.VPN;
}
if (network != null) {
// This may return null per API docs, and is deprecated, but for older APIs (< VERSION_CODES.P)
// we need it to test for suspended internet
networkInfo = getConnectivityManager().getNetworkInfo(network);
}
// Check to see if the network is temporarily unavailable or if airplane mode is toggled on
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
isInternetSuspended = !capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED);
} else {
if (network != null && networkInfo != null) {
NetworkInfo.DetailedState detailedConnectionState = networkInfo.getDetailedState();
if (!detailedConnectionState.equals(NetworkInfo.DetailedState.CONNECTED)) {
isInternetSuspended = true;
}
}
}
isInternetReachable =
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
&& capabilities.hasCapability(
NetworkCapabilities.NET_CAPABILITY_VALIDATED)
&& !isInternetSuspended;
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
isInternetReachable = isInternetReachable && capabilities.getLinkDownstreamBandwidthKbps() != 0;
}
// Get the cellular network type
if (network != null && connectionType == ConnectionType.CELLULAR && isInternetReachable) {
cellularGeneration = CellularGeneration.fromNetworkInfo(networkInfo);
}
} else {
connectionType = ConnectionType.NONE;
}
updateConnectivity(connectionType, cellularGeneration, isInternetReachable);
}
private void asyncUpdateAndSend(int delay) {
new Handler(Looper.getMainLooper()).postDelayed(() -> {
mCapabilities = getConnectivityManager().getNetworkCapabilities(mNetwork);
updateAndSend();
}, delay);
}
private class ConnectivityNetworkCallback extends ConnectivityManager.NetworkCallback {
@Override
public void onAvailable(Network network) {
mNetwork = network;
asyncUpdateAndSend(DELAY_MS);
}
@Override
public void onLosing(Network network, int maxMsToLive) {
mNetwork = network;
updateAndSend();
}
@Override
public void onLost(Network network) {
mNetwork = null;
mCapabilities = null;
updateAndSend();
}
@Override
public void onUnavailable() {
mNetwork = null;
mCapabilities = null;
updateAndSend();
}
@Override
public void onCapabilitiesChanged(
Network network, NetworkCapabilities networkCapabilities) {
mNetwork = network;
mCapabilities = networkCapabilities;
updateAndSend();
}
@Override
public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
if (mNetwork != null) {
mNetwork = network;
}
asyncUpdateAndSend(DELAY_MS);
}
}
}