1
- // Copyright 2004-present Facebook. All Rights Reserved.
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ */
2
9
3
10
package com .facebook .react .modules .netinfo ;
4
11
10
17
import android .net .NetworkInfo ;
11
18
import android .support .v4 .net .ConnectivityManagerCompat ;
12
19
20
+ import com .facebook .common .logging .FLog ;
13
21
import com .facebook .react .bridge .Callback ;
14
22
import com .facebook .react .bridge .LifecycleEventListener ;
15
23
import com .facebook .react .bridge .ReactApplicationContext ;
16
24
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
17
25
import com .facebook .react .bridge .ReactMethod ;
18
26
import com .facebook .react .bridge .WritableMap ;
19
27
import com .facebook .react .bridge .WritableNativeMap ;
28
+ import com .facebook .react .common .ReactConstants ;
20
29
21
30
import static com .facebook .react .modules .core .DeviceEventManagerModule .RCTDeviceEventEmitter ;
22
31
23
32
/**
24
33
* Module that monitors and provides information about the connectivity state of the device.
25
34
*/
26
- public class ConnectivityModule extends ReactContextBaseJavaModule
35
+ public class NetInfoModule extends ReactContextBaseJavaModule
27
36
implements LifecycleEventListener {
28
37
29
38
private static final String CONNECTION_TYPE_NONE = "NONE" ;
30
39
private static final String CONNECTION_TYPE_UNKNOWN = "UNKNOWN" ;
31
40
41
+ private static final String MISSING_PERMISSION_MESSAGE =
42
+ "To use NetInfo on Android, add the following to your AndroidManifest.xml:\n " +
43
+ "<uses-permission android:name=\" android.permission.ACCESS_NETWORK_STATE\" />" ;
44
+
32
45
private final ConnectivityManager mConnectivityManager ;
33
46
private final ConnectivityManagerCompat mConnectivityManagerCompat ;
34
47
private final ConnectivityBroadcastReceiver mConnectivityBroadcastReceiver ;
35
48
49
+ private boolean mNoNetworkPermission = false ;
50
+
36
51
private String mConnectivity = "" ;
37
52
38
- public ConnectivityModule (ReactApplicationContext reactContext ) {
53
+ public NetInfoModule (ReactApplicationContext reactContext ) {
39
54
super (reactContext );
40
55
mConnectivityManager =
41
56
(ConnectivityManager ) reactContext .getSystemService (Context .CONNECTIVITY_SERVICE );
@@ -69,11 +84,23 @@ public String getName() {
69
84
70
85
@ ReactMethod
71
86
public void getCurrentConnectivity (Callback successCallback , Callback errorCallback ) {
87
+ if (mNoNetworkPermission ) {
88
+ if (errorCallback == null ) {
89
+ FLog .e (ReactConstants .TAG , MISSING_PERMISSION_MESSAGE );
90
+ return ;
91
+ }
92
+ errorCallback .invoke (MISSING_PERMISSION_MESSAGE );
93
+ return ;
94
+ }
72
95
successCallback .invoke (createConnectivityEventMap ());
73
96
}
74
97
75
98
@ ReactMethod
76
99
public void isConnectionMetered (Callback successCallback ) {
100
+ if (mNoNetworkPermission ) {
101
+ FLog .e (ReactConstants .TAG , MISSING_PERMISSION_MESSAGE );
102
+ return ;
103
+ }
77
104
successCallback .invoke (mConnectivityManagerCompat .isActiveNetworkMetered (mConnectivityManager ));
78
105
}
79
106
@@ -98,15 +125,21 @@ private void updateAndSendConnectionType() {
98
125
}
99
126
100
127
private String getCurrentConnectionType () {
101
- NetworkInfo networkInfo = mConnectivityManager .getActiveNetworkInfo ();
102
- if (networkInfo == null || !networkInfo .isConnected ()) {
103
- return CONNECTION_TYPE_NONE ;
104
- } else if (ConnectivityManager .isNetworkTypeValid (networkInfo .getType ())) {
105
- return networkInfo .getTypeName ().toUpperCase ();
106
- } else {
128
+ try {
129
+ NetworkInfo networkInfo = mConnectivityManager .getActiveNetworkInfo ();
130
+ if (networkInfo == null || !networkInfo .isConnected ()) {
131
+ return CONNECTION_TYPE_NONE ;
132
+ } else if (ConnectivityManager .isNetworkTypeValid (networkInfo .getType ())) {
133
+ return networkInfo .getTypeName ().toUpperCase ();
134
+ } else {
135
+ return CONNECTION_TYPE_UNKNOWN ;
136
+ }
137
+ } catch (SecurityException e ) {
138
+ mNoNetworkPermission = true ;
107
139
return CONNECTION_TYPE_UNKNOWN ;
108
140
}
109
141
}
142
+
110
143
private void sendConnectivityChangedEvent () {
111
144
getReactApplicationContext ().getJSModule (RCTDeviceEventEmitter .class )
112
145
.emit ("networkStatusDidChange" , createConnectivityEventMap ());
0 commit comments