-
Notifications
You must be signed in to change notification settings - Fork 24.1k
/
NetInfoModule.java
174 lines (146 loc) · 5.7 KB
/
NetInfoModule.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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.modules.netinfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.net.ConnectivityManagerCompat;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import static com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
/**
* Module that monitors and provides information about the connectivity state of the device.
*/
public class NetInfoModule extends ReactContextBaseJavaModule
implements LifecycleEventListener {
private static final String CONNECTION_TYPE_NONE = "NONE";
private static final String CONNECTION_TYPE_UNKNOWN = "UNKNOWN";
private static final String MISSING_PERMISSION_MESSAGE =
"To use NetInfo on Android, add the following to your AndroidManifest.xml:\n" +
"<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" />";
private static final String ERROR_MISSING_PERMISSION = "E_MISSING_PERMISSION";
private final ConnectivityManager mConnectivityManager;
private final ConnectivityBroadcastReceiver mConnectivityBroadcastReceiver;
private boolean mNoNetworkPermission = false;
private String mConnectivity = "";
public NetInfoModule(ReactApplicationContext reactContext) {
super(reactContext);
mConnectivityManager =
(ConnectivityManager) reactContext.getSystemService(Context.CONNECTIVITY_SERVICE);
mConnectivityBroadcastReceiver = new ConnectivityBroadcastReceiver();
}
@Override
public void onHostResume() {
registerReceiver();
}
@Override
public void onHostPause() {
unregisterReceiver();
}
@Override
public void onHostDestroy() {
}
@Override
public void initialize() {
getReactApplicationContext().addLifecycleEventListener(this);
}
@Override
public String getName() {
return "NetInfo";
}
@ReactMethod
public void getCurrentConnectivity(Promise promise) {
if (mNoNetworkPermission) {
promise.reject(ERROR_MISSING_PERMISSION, MISSING_PERMISSION_MESSAGE, null);
return;
}
promise.resolve(createConnectivityEventMap());
}
@ReactMethod
public void isConnectionMetered(Promise promise) {
if (mNoNetworkPermission) {
promise.reject(ERROR_MISSING_PERMISSION, MISSING_PERMISSION_MESSAGE, null);
return;
}
promise.resolve(ConnectivityManagerCompat.isActiveNetworkMetered(mConnectivityManager));
}
private void registerReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
getReactApplicationContext().registerReceiver(mConnectivityBroadcastReceiver, filter);
mConnectivityBroadcastReceiver.setRegistered(true);
}
private void unregisterReceiver() {
if (mConnectivityBroadcastReceiver.isRegistered()) {
getReactApplicationContext().unregisterReceiver(mConnectivityBroadcastReceiver);
mConnectivityBroadcastReceiver.setRegistered(false);
}
}
private void updateAndSendConnectionType() {
String currentConnectivity = getCurrentConnectionType();
// It is possible to get multiple broadcasts for the same connectivity change, so we only
// update and send an event when the connectivity has indeed changed.
if (!currentConnectivity.equalsIgnoreCase(mConnectivity)) {
mConnectivity = currentConnectivity;
sendConnectivityChangedEvent();
}
}
private String getCurrentConnectionType() {
try {
NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
return CONNECTION_TYPE_NONE;
} else if (ConnectivityManager.isNetworkTypeValid(networkInfo.getType())) {
return networkInfo.getTypeName().toUpperCase();
} else {
return CONNECTION_TYPE_UNKNOWN;
}
} catch (SecurityException e) {
mNoNetworkPermission = true;
return CONNECTION_TYPE_UNKNOWN;
}
}
private void sendConnectivityChangedEvent() {
getReactApplicationContext().getJSModule(RCTDeviceEventEmitter.class)
.emit("networkStatusDidChange", createConnectivityEventMap());
}
private WritableMap createConnectivityEventMap() {
WritableMap event = new WritableNativeMap();
event.putString("network_info", mConnectivity);
return event;
}
/**
* Class that receives intents whenever the connection type changes.
* NB: It is possible on some devices to receive certain connection type changes multiple times.
*/
private class ConnectivityBroadcastReceiver extends BroadcastReceiver {
//TODO: Remove registered check when source of crash is found. t9846865
private boolean isRegistered = false;
public void setRegistered(boolean registered) {
isRegistered = registered;
}
public boolean isRegistered() {
return isRegistered;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
updateAndSendConnectionType();
}
}
}
}