Skip to content

Commit

Permalink
feat(android): Make the ACCESS_WIFI_STATE permission optional (#328 by
Browse files Browse the repository at this point in the history
…@sweggersen)

Wifi state will now only be included if the ACCESS_WIFI_STATE permission is in the AndroidManifext.xml. This allows developers to reduce the amount of permissions and data they collect, if they are not using it.
  • Loading branch information
sweggersen committed Mar 30, 2020
1 parent 742c79a commit ba16e0a
Showing 1 changed file with 66 additions and 58 deletions.
Expand Up @@ -6,12 +6,15 @@
*/
package com.reactnativecommunity.netinfo;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

import androidx.core.content.ContextCompat;
import androidx.core.net.ConnectivityManagerCompat;

import com.facebook.react.bridge.Arguments;
Expand All @@ -21,10 +24,12 @@
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.reactnativecommunity.netinfo.types.CellularGeneration;
import com.reactnativecommunity.netinfo.types.ConnectionType;

import java.math.BigInteger;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Locale;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -106,7 +111,7 @@ private void sendConnectivityChangedEvent() {

private WritableMap createConnectivityEventMap(@Nullable final String requestedInterface) {
WritableMap event = Arguments.createMap();

// Add if WiFi is ON or OFF
boolean isEnabled = mWifiManager.isWifiEnabled();
event.putBoolean("isWifiEnabled", isEnabled);
Expand Down Expand Up @@ -154,67 +159,70 @@ private WritableMap createDetailsMap(@Nonnull String detailsInterface) {
}
break;
case "wifi":
WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
if (wifiInfo != null) {
// Get the SSID
try {
String initialSSID = wifiInfo.getSSID();
if (initialSSID != null && !initialSSID.contains("<unknown ssid>")) {
// Strip the quotes, if any
String ssid = initialSSID.replace("\"", "");
details.putString("ssid", ssid);
if (ContextCompat.checkSelfPermission(getReactContext(),
Manifest.permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED) {
WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
if (wifiInfo != null) {
// Get the SSID
try {
String initialSSID = wifiInfo.getSSID();
if (initialSSID != null && !initialSSID.contains("<unknown ssid>")) {
// Strip the quotes, if any
String ssid = initialSSID.replace("\"", "");
details.putString("ssid", ssid);
}
} catch (Exception e) {
// Ignore errors
}
} catch (Exception e) {
// Ignore errors
}

// Get/parse the wifi signal strength
try {
int signalStrength =
WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 100);
details.putInt("strength", signalStrength);
} catch (Exception e) {
// Ignore errors
}
// Get/parse the wifi signal strength
try {
int signalStrength =
WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 100);
details.putInt("strength", signalStrength);
} catch (Exception e) {
// Ignore errors
}

// Get the IP address
try {
byte[] ipAddressByteArray =
BigInteger.valueOf(wifiInfo.getIpAddress()).toByteArray();
NetInfoUtils.reverseByteArray(ipAddressByteArray);
InetAddress inetAddress = InetAddress.getByAddress(ipAddressByteArray);
String ipAddress = inetAddress.getHostAddress();
details.putString("ipAddress", ipAddress);
} catch (Exception e) {
// Ignore errors
}
// Get the IP address
try {
byte[] ipAddressByteArray =
BigInteger.valueOf(wifiInfo.getIpAddress()).toByteArray();
NetInfoUtils.reverseByteArray(ipAddressByteArray);
InetAddress inetAddress = InetAddress.getByAddress(ipAddressByteArray);
String ipAddress = inetAddress.getHostAddress();
details.putString("ipAddress", ipAddress);
} catch (Exception e) {
// Ignore errors
}

// Get the subnet mask
try {
byte[] ipAddressByteArray =
BigInteger.valueOf(wifiInfo.getIpAddress()).toByteArray();
NetInfoUtils.reverseByteArray(ipAddressByteArray);
InetAddress inetAddress = InetAddress.getByAddress(ipAddressByteArray);
NetworkInterface netAddress =
NetworkInterface.getByInetAddress(inetAddress);
int mask =
0xffffffff
<< (32
- netAddress
.getInterfaceAddresses()
.get(1)
.getNetworkPrefixLength());
String subnet =
String.format(
Locale.US,
"%d.%d.%d.%d",
(mask >> 24 & 0xff),
(mask >> 16 & 0xff),
(mask >> 8 & 0xff),
(mask & 0xff));
details.putString("subnet", subnet);
} catch (Exception e) {
// Ignore errors
// Get the subnet mask
try {
byte[] ipAddressByteArray =
BigInteger.valueOf(wifiInfo.getIpAddress()).toByteArray();
NetInfoUtils.reverseByteArray(ipAddressByteArray);
InetAddress inetAddress = InetAddress.getByAddress(ipAddressByteArray);
NetworkInterface netAddress =
NetworkInterface.getByInetAddress(inetAddress);
int mask =
0xffffffff
<< (32
- netAddress
.getInterfaceAddresses()
.get(1)
.getNetworkPrefixLength());
String subnet =
String.format(
Locale.US,
"%d.%d.%d.%d",
(mask >> 24 & 0xff),
(mask >> 16 & 0xff),
(mask >> 8 & 0xff),
(mask & 0xff));
details.putString("subnet", subnet);
} catch (Exception e) {
// Ignore errors
}
}
}
break;
Expand Down

0 comments on commit ba16e0a

Please sign in to comment.