Skip to content

Commit 9c034a9

Browse files
author
Martin Konicek
committed
Make the NetInfoModule not crash when permission is missing
1 parent b0a6010 commit 9c034a9

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/netinfo/ConnectivityModule.java renamed to ReactAndroid/src/main/java/com/facebook/react/modules/netinfo/NetInfoModule.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
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+
*/
29

310
package com.facebook.react.modules.netinfo;
411

@@ -10,32 +17,40 @@
1017
import android.net.NetworkInfo;
1118
import android.support.v4.net.ConnectivityManagerCompat;
1219

20+
import com.facebook.common.logging.FLog;
1321
import com.facebook.react.bridge.Callback;
1422
import com.facebook.react.bridge.LifecycleEventListener;
1523
import com.facebook.react.bridge.ReactApplicationContext;
1624
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1725
import com.facebook.react.bridge.ReactMethod;
1826
import com.facebook.react.bridge.WritableMap;
1927
import com.facebook.react.bridge.WritableNativeMap;
28+
import com.facebook.react.common.ReactConstants;
2029

2130
import static com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;
2231

2332
/**
2433
* Module that monitors and provides information about the connectivity state of the device.
2534
*/
26-
public class ConnectivityModule extends ReactContextBaseJavaModule
35+
public class NetInfoModule extends ReactContextBaseJavaModule
2736
implements LifecycleEventListener {
2837

2938
private static final String CONNECTION_TYPE_NONE = "NONE";
3039
private static final String CONNECTION_TYPE_UNKNOWN = "UNKNOWN";
3140

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+
3245
private final ConnectivityManager mConnectivityManager;
3346
private final ConnectivityManagerCompat mConnectivityManagerCompat;
3447
private final ConnectivityBroadcastReceiver mConnectivityBroadcastReceiver;
3548

49+
private boolean mNoNetworkPermission = false;
50+
3651
private String mConnectivity = "";
3752

38-
public ConnectivityModule(ReactApplicationContext reactContext) {
53+
public NetInfoModule(ReactApplicationContext reactContext) {
3954
super(reactContext);
4055
mConnectivityManager =
4156
(ConnectivityManager) reactContext.getSystemService(Context.CONNECTIVITY_SERVICE);
@@ -69,11 +84,23 @@ public String getName() {
6984

7085
@ReactMethod
7186
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+
}
7295
successCallback.invoke(createConnectivityEventMap());
7396
}
7497

7598
@ReactMethod
7699
public void isConnectionMetered(Callback successCallback) {
100+
if (mNoNetworkPermission) {
101+
FLog.e(ReactConstants.TAG, MISSING_PERMISSION_MESSAGE);
102+
return;
103+
}
77104
successCallback.invoke(mConnectivityManagerCompat.isActiveNetworkMetered(mConnectivityManager));
78105
}
79106

@@ -98,15 +125,21 @@ private void updateAndSendConnectionType() {
98125
}
99126

100127
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;
107139
return CONNECTION_TYPE_UNKNOWN;
108140
}
109141
}
142+
110143
private void sendConnectivityChangedEvent() {
111144
getReactApplicationContext().getJSModule(RCTDeviceEventEmitter.class)
112145
.emit("networkStatusDidChange", createConnectivityEventMap());

ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.facebook.react.modules.fresco.FrescoModule;
2121
import com.facebook.react.modules.intent.IntentModule;
2222
import com.facebook.react.modules.location.LocationModule;
23-
import com.facebook.react.modules.netinfo.ConnectivityModule;
23+
import com.facebook.react.modules.netinfo.NetInfoModule;
2424
import com.facebook.react.modules.network.NetworkingModule;
2525
import com.facebook.react.modules.storage.AsyncStorageModule;
2626
import com.facebook.react.modules.toast.ToastModule;
@@ -57,7 +57,7 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
5757
new IntentModule(reactContext),
5858
new LocationModule(reactContext),
5959
new NetworkingModule(reactContext),
60-
new ConnectivityModule(reactContext),
60+
new NetInfoModule(reactContext),
6161
new WebSocketModule(reactContext),
6262
new ToastModule(reactContext));
6363
}

0 commit comments

Comments
 (0)