mirror of
https://github.com/VibedByKaKi/alexandroid.git
synced 2026-11-13 09:27:16 +01:00
243 lines
11 KiB
Java
243 lines
11 KiB
Java
package com.reactnativecommunity.netinfo;
|
|
|
|
import android.net.ConnectivityManager;
|
|
import android.net.wifi.WifiInfo;
|
|
import android.net.wifi.WifiManager;
|
|
import android.telephony.TelephonyManager;
|
|
import androidx.autofill.HintConstants;
|
|
import androidx.room.util.TableInfo$$ExternalSyntheticOutline0;
|
|
import com.amazon.alexa.audiopersonalization.constants.EventBusConstants;
|
|
import com.amazon.alexa.handsfree.devices.AMPDInformationProvider;
|
|
import com.amazon.rtcsc.service.deviceconfiguration.util.DeviceConfigConstants;
|
|
import com.facebook.react.bridge.Arguments;
|
|
import com.facebook.react.bridge.Promise;
|
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
import com.facebook.react.bridge.WritableMap;
|
|
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.Inet4Address;
|
|
import java.net.InetAddress;
|
|
import java.net.InterfaceAddress;
|
|
import java.net.NetworkInterface;
|
|
import java.net.SocketException;
|
|
import java.util.Enumeration;
|
|
import java.util.Iterator;
|
|
import java.util.Locale;
|
|
import javax.annotation.Nonnull;
|
|
import javax.annotation.Nullable;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
/* loaded from: classes9.dex */
|
|
public abstract class ConnectivityReceiver {
|
|
private final ConnectivityManager mConnectivityManager;
|
|
private Boolean mIsInternetReachableOverride;
|
|
private final ReactApplicationContext mReactContext;
|
|
private final TelephonyManager mTelephonyManager;
|
|
private final WifiManager mWifiManager;
|
|
public boolean hasListener = false;
|
|
|
|
@Nonnull
|
|
private ConnectionType mConnectionType = ConnectionType.UNKNOWN;
|
|
|
|
@Nullable
|
|
private CellularGeneration mCellularGeneration = null;
|
|
private boolean mIsInternetReachable = false;
|
|
|
|
public ConnectivityReceiver(ReactApplicationContext reactApplicationContext) {
|
|
this.mReactContext = reactApplicationContext;
|
|
this.mConnectivityManager = (ConnectivityManager) reactApplicationContext.getSystemService("connectivity");
|
|
this.mWifiManager = (WifiManager) reactApplicationContext.getApplicationContext().getSystemService("wifi");
|
|
this.mTelephonyManager = (TelephonyManager) reactApplicationContext.getSystemService(HintConstants.AUTOFILL_HINT_PHONE);
|
|
}
|
|
|
|
/* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
|
|
private WritableMap createDetailsMap(@Nonnull String str) {
|
|
WritableMap createMap;
|
|
WifiManager wifiManager;
|
|
WifiInfo connectionInfo;
|
|
createMap = Arguments.createMap();
|
|
str.getClass();
|
|
switch (str) {
|
|
case "ethernet":
|
|
try {
|
|
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
|
|
while (networkInterfaces.hasMoreElements()) {
|
|
Enumeration<InetAddress> inetAddresses = networkInterfaces.nextElement().getInetAddresses();
|
|
while (inetAddresses.hasMoreElements()) {
|
|
InetAddress nextElement = inetAddresses.nextElement();
|
|
if (!nextElement.isLoopbackAddress() && (nextElement instanceof Inet4Address)) {
|
|
createMap.putString("ipAddress", nextElement.getHostAddress());
|
|
createMap.putString("subnet", getSubnet(nextElement));
|
|
return createMap;
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return createMap;
|
|
case "cellular":
|
|
CellularGeneration cellularGeneration = this.mCellularGeneration;
|
|
if (cellularGeneration != null) {
|
|
createMap.putString("cellularGeneration", cellularGeneration.label);
|
|
}
|
|
String networkOperatorName = this.mTelephonyManager.getNetworkOperatorName();
|
|
if (networkOperatorName != null) {
|
|
createMap.putString(AMPDInformationProvider.DEVICE_CARRIER_KEY, networkOperatorName);
|
|
return createMap;
|
|
}
|
|
return createMap;
|
|
case "wifi":
|
|
if (NetInfoUtils.isAccessWifiStatePermissionGranted(getReactContext()) && (wifiManager = this.mWifiManager) != null && (connectionInfo = wifiManager.getConnectionInfo()) != null) {
|
|
try {
|
|
String ssid = connectionInfo.getSSID();
|
|
if (ssid != null && !ssid.contains("<unknown ssid>")) {
|
|
createMap.putString("ssid", ssid.replace(DeviceConfigConstants.DOUBLE_QUOTE, ""));
|
|
}
|
|
} catch (Exception unused) {
|
|
}
|
|
try {
|
|
String bssid = connectionInfo.getBSSID();
|
|
if (bssid != null) {
|
|
createMap.putString("bssid", bssid);
|
|
}
|
|
} catch (Exception unused2) {
|
|
}
|
|
try {
|
|
createMap.putInt("strength", WifiManager.calculateSignalLevel(connectionInfo.getRssi(), 100));
|
|
} catch (Exception unused3) {
|
|
}
|
|
try {
|
|
createMap.putInt(EventBusConstants.FREQUENCY_KEY, connectionInfo.getFrequency());
|
|
} catch (Exception unused4) {
|
|
}
|
|
try {
|
|
byte[] byteArray = BigInteger.valueOf(connectionInfo.getIpAddress()).toByteArray();
|
|
NetInfoUtils.reverseByteArray(byteArray);
|
|
createMap.putString("ipAddress", InetAddress.getByAddress(byteArray).getHostAddress());
|
|
} catch (Exception unused5) {
|
|
}
|
|
try {
|
|
byte[] byteArray2 = BigInteger.valueOf(connectionInfo.getIpAddress()).toByteArray();
|
|
NetInfoUtils.reverseByteArray(byteArray2);
|
|
createMap.putString("subnet", getSubnet(InetAddress.getByAddress(byteArray2)));
|
|
} catch (Exception unused6) {
|
|
}
|
|
try {
|
|
createMap.putInt("linkSpeed", connectionInfo.getLinkSpeed());
|
|
} catch (Exception unused7) {
|
|
}
|
|
try {
|
|
createMap.putInt("rxLinkSpeed", connectionInfo.getRxLinkSpeedMbps());
|
|
} catch (Exception unused8) {
|
|
}
|
|
try {
|
|
createMap.putInt("txLinkSpeed", connectionInfo.getTxLinkSpeedMbps());
|
|
} catch (Exception unused9) {
|
|
}
|
|
}
|
|
return createMap;
|
|
default:
|
|
return createMap;
|
|
}
|
|
}
|
|
|
|
private static String getSubnet(InetAddress inetAddress) throws SocketException {
|
|
short s;
|
|
Iterator<InterfaceAddress> it2 = NetworkInterface.getByInetAddress(inetAddress).getInterfaceAddresses().iterator();
|
|
while (true) {
|
|
if (!it2.hasNext()) {
|
|
s = 0;
|
|
break;
|
|
}
|
|
InterfaceAddress next = it2.next();
|
|
if (next.getAddress().getAddress().length == 4) {
|
|
s = next.getNetworkPrefixLength();
|
|
break;
|
|
}
|
|
}
|
|
int i = (-1) << (32 - s);
|
|
Locale locale = Locale.US;
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append((i >> 24) & 255);
|
|
sb.append(".");
|
|
sb.append((i >> 16) & 255);
|
|
sb.append(".");
|
|
sb.append((i >> 8) & 255);
|
|
return TableInfo$$ExternalSyntheticOutline0.m(".", i & 255, sb);
|
|
}
|
|
|
|
public void clearIsInternetReachableOverride() {
|
|
this.mIsInternetReachableOverride = null;
|
|
}
|
|
|
|
public WritableMap createConnectivityEventMap(@Nullable String str) {
|
|
WritableMap createMap = Arguments.createMap();
|
|
boolean z = false;
|
|
if (NetInfoUtils.isAccessWifiStatePermissionGranted(getReactContext())) {
|
|
WifiManager wifiManager = this.mWifiManager;
|
|
createMap.putBoolean("isWifiEnabled", wifiManager != null ? wifiManager.isWifiEnabled() : false);
|
|
}
|
|
createMap.putString("type", str != null ? str : this.mConnectionType.label);
|
|
boolean z2 = (this.mConnectionType.equals(ConnectionType.NONE) || this.mConnectionType.equals(ConnectionType.UNKNOWN)) ? false : true;
|
|
createMap.putBoolean("isConnected", z2);
|
|
if (this.mIsInternetReachable && (str == null || str.equals(this.mConnectionType.label))) {
|
|
z = true;
|
|
}
|
|
createMap.putBoolean("isInternetReachable", z);
|
|
if (str == null) {
|
|
str = this.mConnectionType.label;
|
|
}
|
|
WritableMap createDetailsMap = createDetailsMap(str);
|
|
if (z2) {
|
|
createDetailsMap.putBoolean("isConnectionExpensive", getConnectivityManager() != null ? getConnectivityManager().isActiveNetworkMetered() : true);
|
|
}
|
|
createMap.putMap("details", createDetailsMap);
|
|
return createMap;
|
|
}
|
|
|
|
public ConnectivityManager getConnectivityManager() {
|
|
return this.mConnectivityManager;
|
|
}
|
|
|
|
public void getCurrentState(@Nullable String str, Promise promise) {
|
|
promise.resolve(createConnectivityEventMap(str));
|
|
}
|
|
|
|
public ReactApplicationContext getReactContext() {
|
|
return this.mReactContext;
|
|
}
|
|
|
|
public abstract void register();
|
|
|
|
public void sendConnectivityChangedEvent() {
|
|
((DeviceEventManagerModule.RCTDeviceEventEmitter) getReactContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)).emit("netInfo.networkStatusDidChange", createConnectivityEventMap(null));
|
|
}
|
|
|
|
public void setIsInternetReachableOverride(boolean z) {
|
|
this.mIsInternetReachableOverride = Boolean.valueOf(z);
|
|
updateConnectivity(this.mConnectionType, this.mCellularGeneration, this.mIsInternetReachable);
|
|
}
|
|
|
|
public abstract void unregister();
|
|
|
|
public void updateConnectivity(@Nonnull ConnectionType connectionType, @Nullable CellularGeneration cellularGeneration, boolean z) {
|
|
Boolean bool = this.mIsInternetReachableOverride;
|
|
if (bool != null) {
|
|
z = bool.booleanValue();
|
|
}
|
|
boolean z2 = connectionType != this.mConnectionType;
|
|
boolean z3 = cellularGeneration != this.mCellularGeneration;
|
|
boolean z4 = z != this.mIsInternetReachable;
|
|
if (z2 || z3 || z4) {
|
|
this.mConnectionType = connectionType;
|
|
this.mCellularGeneration = cellularGeneration;
|
|
this.mIsInternetReachable = z;
|
|
if (this.hasListener) {
|
|
sendConnectivityChangedEvent();
|
|
}
|
|
}
|
|
}
|
|
}
|