discord-jadx/app/src/main/java/org/webrtc/NetworkMonitorAutoDetect.java

594 lines
27 KiB
Java

package org.webrtc;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.LinkAddress;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.net.wifi.WifiInfo;
import android.net.wifi.p2p.WifiP2pGroup;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Build;
import androidx.annotation.Nullable;
import c.d.b.a.a;
import h0.c.q;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.webrtc.NetworkChangeDetector;
public class NetworkMonitorAutoDetect extends BroadcastReceiver implements NetworkChangeDetector {
private static final long INVALID_NET_ID = -1;
private static final String TAG = "NetworkMonitorAutoDetect";
@Nullable
private final ConnectivityManager.NetworkCallback allNetworkCallback;
private NetworkChangeDetector.ConnectionType connectionType;
private ConnectivityManagerDelegate connectivityManagerDelegate;
private final Context context;
private final IntentFilter intentFilter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
private boolean isRegistered;
@Nullable
private final ConnectivityManager.NetworkCallback mobileNetworkCallback;
private final NetworkChangeDetector.Observer observer;
private WifiDirectManagerDelegate wifiDirectManagerDelegate;
private WifiManagerDelegate wifiManagerDelegate;
private String wifiSSID;
public static class ConnectivityManagerDelegate {
@Nullable
private final ConnectivityManager connectivityManager;
public ConnectivityManagerDelegate() {
this.connectivityManager = null;
}
public ConnectivityManagerDelegate(Context context) {
this.connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
}
public static /* synthetic */ NetworkChangeDetector.NetworkInformation access$300(ConnectivityManagerDelegate connectivityManagerDelegate, Network network) {
return connectivityManagerDelegate.networkToInfo(network);
}
private NetworkState getNetworkState(@Nullable NetworkInfo networkInfo) {
return (networkInfo == null || !networkInfo.isConnected()) ? new NetworkState(false, -1, -1, -1, -1) : new NetworkState(true, networkInfo.getType(), networkInfo.getSubtype(), -1, -1);
}
@Nullable
@SuppressLint({"NewApi"})
private NetworkChangeDetector.NetworkInformation networkToInfo(@Nullable Network network) {
ConnectivityManager connectivityManager;
if (network == null || (connectivityManager = this.connectivityManager) == null) {
return null;
}
LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
if (linkProperties == null) {
StringBuilder K = a.K("Detected unknown network: ");
K.append(network.toString());
Logging.w("NetworkMonitorAutoDetect", K.toString());
return null;
} else if (linkProperties.getInterfaceName() == null) {
StringBuilder K2 = a.K("Null interface name for network ");
K2.append(network.toString());
Logging.w("NetworkMonitorAutoDetect", K2.toString());
return null;
} else {
NetworkState networkState = getNetworkState(network);
NetworkChangeDetector.ConnectionType connectionType = NetworkMonitorAutoDetect.getConnectionType(networkState);
if (connectionType == NetworkChangeDetector.ConnectionType.CONNECTION_NONE) {
StringBuilder K3 = a.K("Network ");
K3.append(network.toString());
K3.append(" is disconnected");
Logging.d("NetworkMonitorAutoDetect", K3.toString());
return null;
}
if (connectionType == NetworkChangeDetector.ConnectionType.CONNECTION_UNKNOWN || connectionType == NetworkChangeDetector.ConnectionType.CONNECTION_UNKNOWN_CELLULAR) {
StringBuilder K4 = a.K("Network ");
K4.append(network.toString());
K4.append(" connection type is ");
K4.append(connectionType);
K4.append(" because it has type ");
K4.append(networkState.getNetworkType());
K4.append(" and subtype ");
K4.append(networkState.getNetworkSubType());
Logging.d("NetworkMonitorAutoDetect", K4.toString());
}
return new NetworkChangeDetector.NetworkInformation(linkProperties.getInterfaceName(), connectionType, NetworkMonitorAutoDetect.access$400(networkState), NetworkMonitorAutoDetect.access$000(network), getIPAddresses(linkProperties));
}
}
@Nullable
public List<NetworkChangeDetector.NetworkInformation> getActiveNetworkList() {
if (!supportNetworkCallback()) {
return null;
}
ArrayList arrayList = new ArrayList();
for (Network network : getAllNetworks()) {
NetworkChangeDetector.NetworkInformation networkToInfo = networkToInfo(network);
if (networkToInfo != null) {
arrayList.add(networkToInfo);
}
}
return arrayList;
}
@SuppressLint({"NewApi"})
public Network[] getAllNetworks() {
ConnectivityManager connectivityManager = this.connectivityManager;
return connectivityManager == null ? new Network[0] : connectivityManager.getAllNetworks();
}
@SuppressLint({"NewApi"})
public long getDefaultNetId() {
NetworkInfo activeNetworkInfo;
NetworkInfo networkInfo;
if (!supportNetworkCallback() || (activeNetworkInfo = this.connectivityManager.getActiveNetworkInfo()) == null) {
return -1;
}
Network[] allNetworks = getAllNetworks();
long j = -1;
for (Network network : allNetworks) {
if (hasInternetCapability(network) && (networkInfo = this.connectivityManager.getNetworkInfo(network)) != null && networkInfo.getType() == activeNetworkInfo.getType()) {
if (j == -1) {
j = NetworkMonitorAutoDetect.access$000(network);
} else {
throw new RuntimeException("Multiple connected networks of same type are not supported.");
}
}
}
return j;
}
@SuppressLint({"NewApi"})
public NetworkChangeDetector.IPAddress[] getIPAddresses(LinkProperties linkProperties) {
NetworkChangeDetector.IPAddress[] iPAddressArr = new NetworkChangeDetector.IPAddress[linkProperties.getLinkAddresses().size()];
int i = 0;
for (LinkAddress linkAddress : linkProperties.getLinkAddresses()) {
iPAddressArr[i] = new NetworkChangeDetector.IPAddress(linkAddress.getAddress().getAddress());
i++;
}
return iPAddressArr;
}
public NetworkState getNetworkState() {
ConnectivityManager connectivityManager = this.connectivityManager;
return connectivityManager == null ? new NetworkState(false, -1, -1, -1, -1) : getNetworkState(connectivityManager.getActiveNetworkInfo());
}
@SuppressLint({"NewApi"})
public NetworkState getNetworkState(@Nullable Network network) {
ConnectivityManager connectivityManager;
NetworkInfo activeNetworkInfo;
if (network == null || (connectivityManager = this.connectivityManager) == null) {
return new NetworkState(false, -1, -1, -1, -1);
}
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
if (networkInfo == null) {
StringBuilder K = a.K("Couldn't retrieve information from network ");
K.append(network.toString());
Logging.w("NetworkMonitorAutoDetect", K.toString());
return new NetworkState(false, -1, -1, -1, -1);
} else if (networkInfo.getType() == 17) {
return networkInfo.getType() == 17 ? (Build.VERSION.SDK_INT < 23 || !network.equals(this.connectivityManager.getActiveNetwork()) || (activeNetworkInfo = this.connectivityManager.getActiveNetworkInfo()) == null || activeNetworkInfo.getType() == 17) ? new NetworkState(networkInfo.isConnected(), 17, -1, -1, -1) : new NetworkState(networkInfo.isConnected(), 17, -1, activeNetworkInfo.getType(), activeNetworkInfo.getSubtype()) : getNetworkState(networkInfo);
} else {
NetworkCapabilities networkCapabilities = this.connectivityManager.getNetworkCapabilities(network);
return (networkCapabilities == null || !networkCapabilities.hasTransport(4)) ? getNetworkState(networkInfo) : new NetworkState(networkInfo.isConnected(), 17, -1, networkInfo.getType(), networkInfo.getSubtype());
}
}
@SuppressLint({"NewApi"})
public boolean hasInternetCapability(Network network) {
NetworkCapabilities networkCapabilities;
ConnectivityManager connectivityManager = this.connectivityManager;
return (connectivityManager == null || (networkCapabilities = connectivityManager.getNetworkCapabilities(network)) == null || !networkCapabilities.hasCapability(12)) ? false : true;
}
@SuppressLint({"NewApi"})
public void registerNetworkCallback(ConnectivityManager.NetworkCallback networkCallback) {
this.connectivityManager.registerNetworkCallback(new NetworkRequest.Builder().addCapability(12).build(), networkCallback);
}
@SuppressLint({"NewApi"})
public void releaseCallback(ConnectivityManager.NetworkCallback networkCallback) {
if (supportNetworkCallback()) {
Logging.d("NetworkMonitorAutoDetect", "Unregister network callback");
this.connectivityManager.unregisterNetworkCallback(networkCallback);
}
}
@SuppressLint({"NewApi"})
public void requestMobileNetwork(ConnectivityManager.NetworkCallback networkCallback) {
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(12).addTransportType(0);
this.connectivityManager.requestNetwork(builder.build(), networkCallback);
}
public boolean supportNetworkCallback() {
return this.connectivityManager != null;
}
}
public static class NetworkState {
private final boolean connected;
private final int subtype;
private final int type;
private final int underlyingNetworkSubtypeForVpn;
private final int underlyingNetworkTypeForVpn;
public NetworkState(boolean z2, int i, int i2, int i3, int i4) {
this.connected = z2;
this.type = i;
this.subtype = i2;
this.underlyingNetworkTypeForVpn = i3;
this.underlyingNetworkSubtypeForVpn = i4;
}
public int getNetworkSubType() {
return this.subtype;
}
public int getNetworkType() {
return this.type;
}
public int getUnderlyingNetworkSubtypeForVpn() {
return this.underlyingNetworkSubtypeForVpn;
}
public int getUnderlyingNetworkTypeForVpn() {
return this.underlyingNetworkTypeForVpn;
}
public boolean isConnected() {
return this.connected;
}
}
@SuppressLint({"NewApi"})
public class SimpleNetworkCallback extends ConnectivityManager.NetworkCallback {
private SimpleNetworkCallback() {
}
private void onNetworkChanged(Network network) {
NetworkChangeDetector.NetworkInformation access$300 = ConnectivityManagerDelegate.access$300(NetworkMonitorAutoDetect.access$200(NetworkMonitorAutoDetect.this), network);
if (access$300 != null) {
NetworkMonitorAutoDetect.access$100(NetworkMonitorAutoDetect.this).onNetworkConnect(access$300);
}
}
@Override // android.net.ConnectivityManager.NetworkCallback
public void onAvailable(Network network) {
StringBuilder K = a.K("Network becomes available: ");
K.append(network.toString());
Logging.d("NetworkMonitorAutoDetect", K.toString());
onNetworkChanged(network);
}
@Override // android.net.ConnectivityManager.NetworkCallback
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
StringBuilder K = a.K("capabilities changed: ");
K.append(networkCapabilities.toString());
Logging.d("NetworkMonitorAutoDetect", K.toString());
onNetworkChanged(network);
}
@Override // android.net.ConnectivityManager.NetworkCallback
public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
Logging.d("NetworkMonitorAutoDetect", "link properties changed");
onNetworkChanged(network);
}
@Override // android.net.ConnectivityManager.NetworkCallback
public void onLosing(Network network, int i) {
StringBuilder K = a.K("Network ");
K.append(network.toString());
K.append(" is about to lose in ");
K.append(i);
K.append("ms");
Logging.d("NetworkMonitorAutoDetect", K.toString());
}
@Override // android.net.ConnectivityManager.NetworkCallback
public void onLost(Network network) {
StringBuilder K = a.K("Network ");
K.append(network.toString());
K.append(" is disconnected");
Logging.d("NetworkMonitorAutoDetect", K.toString());
NetworkMonitorAutoDetect.access$100(NetworkMonitorAutoDetect.this).onNetworkDisconnect(NetworkMonitorAutoDetect.access$000(network));
}
}
public static class WifiDirectManagerDelegate extends BroadcastReceiver {
private static final int WIFI_P2P_NETWORK_HANDLE = 0;
private final Context context;
private final NetworkChangeDetector.Observer observer;
@Nullable
private NetworkChangeDetector.NetworkInformation wifiP2pNetworkInfo;
public WifiDirectManagerDelegate(NetworkChangeDetector.Observer observer, Context context) {
this.context = context;
this.observer = observer;
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.net.wifi.p2p.STATE_CHANGED");
intentFilter.addAction("android.net.wifi.p2p.CONNECTION_STATE_CHANGE");
context.registerReceiver(this, intentFilter);
if (Build.VERSION.SDK_INT > 28) {
WifiP2pManager wifiP2pManager = (WifiP2pManager) context.getSystemService("wifip2p");
wifiP2pManager.requestGroupInfo(wifiP2pManager.initialize(context, context.getMainLooper(), null), new q(this));
}
}
/* access modifiers changed from: private */
/* renamed from: onWifiP2pGroupChange */
public void a(@Nullable WifiP2pGroup wifiP2pGroup) {
if (wifiP2pGroup != null && wifiP2pGroup.getInterface() != null) {
try {
ArrayList list = Collections.list(NetworkInterface.getByName(wifiP2pGroup.getInterface()).getInetAddresses());
NetworkChangeDetector.IPAddress[] iPAddressArr = new NetworkChangeDetector.IPAddress[list.size()];
for (int i = 0; i < list.size(); i++) {
iPAddressArr[i] = new NetworkChangeDetector.IPAddress(((InetAddress) list.get(i)).getAddress());
}
NetworkChangeDetector.NetworkInformation networkInformation = new NetworkChangeDetector.NetworkInformation(wifiP2pGroup.getInterface(), NetworkChangeDetector.ConnectionType.CONNECTION_WIFI, NetworkChangeDetector.ConnectionType.CONNECTION_NONE, 0, iPAddressArr);
this.wifiP2pNetworkInfo = networkInformation;
this.observer.onNetworkConnect(networkInformation);
} catch (SocketException e) {
Logging.e("NetworkMonitorAutoDetect", "Unable to get WifiP2p network interface", e);
}
}
}
private void onWifiP2pStateChange(int i) {
if (i == 1) {
this.wifiP2pNetworkInfo = null;
this.observer.onNetworkDisconnect(0);
}
}
public List<NetworkChangeDetector.NetworkInformation> getActiveNetworkList() {
NetworkChangeDetector.NetworkInformation networkInformation = this.wifiP2pNetworkInfo;
return networkInformation != null ? Collections.singletonList(networkInformation) : Collections.emptyList();
}
@Override // android.content.BroadcastReceiver
@SuppressLint({"InlinedApi"})
public void onReceive(Context context, Intent intent) {
if ("android.net.wifi.p2p.CONNECTION_STATE_CHANGE".equals(intent.getAction())) {
a((WifiP2pGroup) intent.getParcelableExtra("p2pGroupInfo"));
} else if ("android.net.wifi.p2p.STATE_CHANGED".equals(intent.getAction())) {
onWifiP2pStateChange(intent.getIntExtra("wifi_p2p_state", 0));
}
}
public void release() {
this.context.unregisterReceiver(this);
}
}
public static class WifiManagerDelegate {
@Nullable
private final Context context;
public WifiManagerDelegate() {
this.context = null;
}
public WifiManagerDelegate(Context context) {
this.context = context;
}
public String getWifiSSID() {
WifiInfo wifiInfo;
String ssid;
Intent registerReceiver = this.context.registerReceiver(null, new IntentFilter("android.net.wifi.STATE_CHANGE"));
return (registerReceiver == null || (wifiInfo = (WifiInfo) registerReceiver.getParcelableExtra("wifiInfo")) == null || (ssid = wifiInfo.getSSID()) == null) ? "" : ssid;
}
}
@SuppressLint({"NewApi"})
public NetworkMonitorAutoDetect(NetworkChangeDetector.Observer observer, Context context) {
this.observer = observer;
this.context = context;
this.connectivityManagerDelegate = new ConnectivityManagerDelegate(context);
this.wifiManagerDelegate = new WifiManagerDelegate(context);
NetworkState networkState = this.connectivityManagerDelegate.getNetworkState();
this.connectionType = getConnectionType(networkState);
this.wifiSSID = getWifiSSID(networkState);
if (PeerConnectionFactory.fieldTrialsFindFullName("IncludeWifiDirect").equals("Enabled")) {
this.wifiDirectManagerDelegate = new WifiDirectManagerDelegate(observer, context);
}
registerReceiver();
if (this.connectivityManagerDelegate.supportNetworkCallback()) {
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback();
try {
this.connectivityManagerDelegate.requestMobileNetwork(networkCallback);
} catch (SecurityException unused) {
Logging.w("NetworkMonitorAutoDetect", "Unable to obtain permission to request a cellular network.");
networkCallback = null;
}
this.mobileNetworkCallback = networkCallback;
SimpleNetworkCallback simpleNetworkCallback = new SimpleNetworkCallback();
this.allNetworkCallback = simpleNetworkCallback;
this.connectivityManagerDelegate.registerNetworkCallback(simpleNetworkCallback);
return;
}
this.mobileNetworkCallback = null;
this.allNetworkCallback = null;
}
public static /* synthetic */ long access$000(Network network) {
return networkToNetId(network);
}
public static /* synthetic */ NetworkChangeDetector.Observer access$100(NetworkMonitorAutoDetect networkMonitorAutoDetect) {
return networkMonitorAutoDetect.observer;
}
public static /* synthetic */ ConnectivityManagerDelegate access$200(NetworkMonitorAutoDetect networkMonitorAutoDetect) {
return networkMonitorAutoDetect.connectivityManagerDelegate;
}
public static /* synthetic */ NetworkChangeDetector.ConnectionType access$400(NetworkState networkState) {
return getUnderlyingConnectionTypeForVpn(networkState);
}
private void connectionTypeChanged(NetworkState networkState) {
NetworkChangeDetector.ConnectionType connectionType = getConnectionType(networkState);
String wifiSSID = getWifiSSID(networkState);
if (connectionType != this.connectionType || !wifiSSID.equals(this.wifiSSID)) {
this.connectionType = connectionType;
this.wifiSSID = wifiSSID;
StringBuilder K = a.K("Network connectivity changed, type is: ");
K.append(this.connectionType);
Logging.d("NetworkMonitorAutoDetect", K.toString());
this.observer.onConnectionTypeChanged(connectionType);
}
}
public static NetworkChangeDetector.ConnectionType getConnectionType(NetworkState networkState) {
return getConnectionType(networkState.isConnected(), networkState.getNetworkType(), networkState.getNetworkSubType());
}
private static NetworkChangeDetector.ConnectionType getConnectionType(boolean z2, int i, int i2) {
if (!z2) {
return NetworkChangeDetector.ConnectionType.CONNECTION_NONE;
}
if (i != 0) {
return i != 1 ? i != 6 ? i != 7 ? i != 9 ? i != 17 ? NetworkChangeDetector.ConnectionType.CONNECTION_UNKNOWN : NetworkChangeDetector.ConnectionType.CONNECTION_VPN : NetworkChangeDetector.ConnectionType.CONNECTION_ETHERNET : NetworkChangeDetector.ConnectionType.CONNECTION_BLUETOOTH : NetworkChangeDetector.ConnectionType.CONNECTION_4G : NetworkChangeDetector.ConnectionType.CONNECTION_WIFI;
}
switch (i2) {
case 1:
case 2:
case 4:
case 7:
case 11:
case 16:
return NetworkChangeDetector.ConnectionType.CONNECTION_2G;
case 3:
case 5:
case 6:
case 8:
case 9:
case 10:
case 12:
case 14:
case 15:
case 17:
return NetworkChangeDetector.ConnectionType.CONNECTION_3G;
case 13:
case 18:
return NetworkChangeDetector.ConnectionType.CONNECTION_4G;
case 19:
default:
return NetworkChangeDetector.ConnectionType.CONNECTION_UNKNOWN_CELLULAR;
case 20:
return NetworkChangeDetector.ConnectionType.CONNECTION_5G;
}
}
private static NetworkChangeDetector.ConnectionType getUnderlyingConnectionTypeForVpn(NetworkState networkState) {
return networkState.getNetworkType() != 17 ? NetworkChangeDetector.ConnectionType.CONNECTION_NONE : getConnectionType(networkState.isConnected(), networkState.getUnderlyingNetworkTypeForVpn(), networkState.getUnderlyingNetworkSubtypeForVpn());
}
private String getWifiSSID(NetworkState networkState) {
return getConnectionType(networkState) != NetworkChangeDetector.ConnectionType.CONNECTION_WIFI ? "" : this.wifiManagerDelegate.getWifiSSID();
}
@SuppressLint({"NewApi"})
private static long networkToNetId(Network network) {
return Build.VERSION.SDK_INT >= 23 ? network.getNetworkHandle() : (long) Integer.parseInt(network.toString());
}
private void registerReceiver() {
if (!this.isRegistered) {
this.isRegistered = true;
this.context.registerReceiver(this, this.intentFilter);
}
}
private void unregisterReceiver() {
if (this.isRegistered) {
this.isRegistered = false;
this.context.unregisterReceiver(this);
}
}
@Override // org.webrtc.NetworkChangeDetector
public void destroy() {
ConnectivityManager.NetworkCallback networkCallback = this.allNetworkCallback;
if (networkCallback != null) {
this.connectivityManagerDelegate.releaseCallback(networkCallback);
}
ConnectivityManager.NetworkCallback networkCallback2 = this.mobileNetworkCallback;
if (networkCallback2 != null) {
this.connectivityManagerDelegate.releaseCallback(networkCallback2);
}
WifiDirectManagerDelegate wifiDirectManagerDelegate = this.wifiDirectManagerDelegate;
if (wifiDirectManagerDelegate != null) {
wifiDirectManagerDelegate.release();
}
unregisterReceiver();
}
@Override // org.webrtc.NetworkChangeDetector
@Nullable
public List<NetworkChangeDetector.NetworkInformation> getActiveNetworkList() {
List<NetworkChangeDetector.NetworkInformation> activeNetworkList = this.connectivityManagerDelegate.getActiveNetworkList();
if (activeNetworkList == null) {
return null;
}
ArrayList arrayList = new ArrayList(activeNetworkList);
WifiDirectManagerDelegate wifiDirectManagerDelegate = this.wifiDirectManagerDelegate;
if (wifiDirectManagerDelegate != null) {
arrayList.addAll(wifiDirectManagerDelegate.getActiveNetworkList());
}
return arrayList;
}
@Override // org.webrtc.NetworkChangeDetector
public NetworkChangeDetector.ConnectionType getCurrentConnectionType() {
return getConnectionType(getCurrentNetworkState());
}
public NetworkState getCurrentNetworkState() {
return this.connectivityManagerDelegate.getNetworkState();
}
public long getDefaultNetId() {
return this.connectivityManagerDelegate.getDefaultNetId();
}
public boolean isReceiverRegisteredForTesting() {
return this.isRegistered;
}
@Override // android.content.BroadcastReceiver
public void onReceive(Context context, Intent intent) {
NetworkState currentNetworkState = getCurrentNetworkState();
if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
connectionTypeChanged(currentNetworkState);
}
}
public void setConnectivityManagerDelegateForTests(ConnectivityManagerDelegate connectivityManagerDelegate) {
this.connectivityManagerDelegate = connectivityManagerDelegate;
}
public void setWifiManagerDelegateForTests(WifiManagerDelegate wifiManagerDelegate) {
this.wifiManagerDelegate = wifiManagerDelegate;
}
@Override // org.webrtc.NetworkChangeDetector
public boolean supportNetworkCallback() {
return this.connectivityManagerDelegate.supportNetworkCallback();
}
}