Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.bell.cts.iptv.companion.sdk.network;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.net.wifi.WifiInfo;
- import android.net.wifi.WifiManager;
- import android.text.format.Formatter;
- import android.util.Log;
- import java.net.Inet4Address;
- import java.net.InetAddress;
- import java.net.InterfaceAddress;
- import java.net.NetworkInterface;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.List;
- import org.apache.commons.net.util.SubnetUtils;
- public class ConnectivityMonitor extends BroadcastReceiver {
- private Context androidContext;
- private String currentIpAddress;
- private NetworkType currentNetworkType;
- private String networkIdentity;
- private boolean registered = false;
- private List<ConnectivityStateListener> stateListeners;
- public interface ConnectivityStateListener {
- void onNetworkConnected(NetworkType networkType, String str, String str2);
- void onNetworkDisconnected();
- }
- public enum NetworkType {
- MOBILE,
- WIFI
- }
- public ConnectivityMonitor(Context context) {
- this.androidContext = context;
- this.stateListeners = new ArrayList(2);
- onReceive(context, null);
- }
- public void register() {
- if (!this.registered) {
- this.androidContext.registerReceiver(this, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
- this.registered = true;
- }
- }
- public void unregister() {
- /* JADX: method processing error */
- /*
- Error: java.lang.NullPointerException
- at jadx.core.dex.visitors.regions.ProcessTryCatchRegions.searchTryCatchDominators(ProcessTryCatchRegions.java:75)
- at jadx.core.dex.visitors.regions.ProcessTryCatchRegions.process(ProcessTryCatchRegions.java:45)
- at jadx.core.dex.visitors.regions.RegionMakerVisitor.postProcessRegions(RegionMakerVisitor.java:63)
- at jadx.core.dex.visitors.regions.RegionMakerVisitor.visit(RegionMakerVisitor.java:58)
- at jadx.core.dex.visitors.DepthTraversal.visit(DepthTraversal.java:31)
- at jadx.core.dex.visitors.DepthTraversal.visit(DepthTraversal.java:17)
- at jadx.core.ProcessClass.process(ProcessClass.java:37)
- at jadx.api.JadxDecompiler.processClass(JadxDecompiler.java:306)
- at jadx.api.JavaClass.decompile(JavaClass.java:62)
- at jadx.api.JadxDecompiler$1.run(JadxDecompiler.java:199)
- */
- /*
- r2 = this;
- r0 = r2.registered; Catch:{ IllegalArgumentException -> 0x000d }
- if (r0 == 0) goto L_0x0014; Catch:{ IllegalArgumentException -> 0x000d }
- L_0x0004:
- r0 = r2.androidContext; Catch:{ IllegalArgumentException -> 0x000d }
- r0.unregisterReceiver(r2); Catch:{ IllegalArgumentException -> 0x000d }
- r0 = 0; Catch:{ IllegalArgumentException -> 0x000d }
- r2.registered = r0; Catch:{ IllegalArgumentException -> 0x000d }
- goto L_0x0014;
- L_0x000d:
- r0 = "BellCompanionSDK";
- r1 = "ConnectivityMonitor wasn't registered.";
- android.util.Log.e(r0, r1);
- L_0x0014:
- return;
- */
- throw new UnsupportedOperationException("Method not decompiled: com.bell.cts.iptv.companion.sdk.network.ConnectivityMonitor.unregister():void");
- }
- public void onReceive(Context context, Intent intent) {
- try {
- ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
- context = connectivityManager != null ? connectivityManager.getActiveNetworkInfo() : null;
- if ((context == null || !context.isConnected()) && this.currentIpAddress != null) {
- this.currentIpAddress = null;
- this.currentNetworkType = null;
- this.networkIdentity = null;
- notifyDisconnected();
- } else if (context != null && context.isConnected() != null) {
- intent = getLocalIpAddress(context);
- NetworkType networkType = getNetworkType(context);
- if ((intent != null && !intent.equals(this.currentIpAddress)) || (networkType != null && networkType != this.currentNetworkType)) {
- this.currentIpAddress = intent;
- this.currentNetworkType = getNetworkType(context);
- this.networkIdentity = getNetworkId(context);
- notifyConnected(this.currentNetworkType, this.currentIpAddress, this.networkIdentity);
- }
- }
- } catch (Context context2) {
- Log.e("BellCompanionSDK", "Error handling network change.", context2);
- }
- }
- private String getNetworkId(NetworkInfo networkInfo) {
- if (networkInfo.getType() == 1) {
- WifiInfo connectionInfo = ((WifiManager) this.androidContext.getSystemService("wifi")).getConnectionInfo();
- if (connectionInfo != null) {
- return connectionInfo.getSSID();
- }
- }
- return networkInfo.getExtraInfo();
- }
- public String getCurrentIpAddress() {
- return this.currentIpAddress;
- }
- public String getNetmask() {
- if (((WifiManager) this.androidContext.getSystemService("wifi")).getDhcpInfo().netmask <= 0) {
- return extractMaskFromNetworkInterface();
- }
- return String.format("%d.%d.%d.%d", new Object[]{Integer.valueOf(((WifiManager) this.androidContext.getSystemService("wifi")).getDhcpInfo().netmask & 255), Integer.valueOf((((WifiManager) this.androidContext.getSystemService("wifi")).getDhcpInfo().netmask >> 8) & 255), Integer.valueOf((((WifiManager) this.androidContext.getSystemService("wifi")).getDhcpInfo().netmask >> 16) & 255), Integer.valueOf((((WifiManager) this.androidContext.getSystemService("wifi")).getDhcpInfo().netmask >> 24) & 255)});
- }
- private String extractMaskFromNetworkInterface() {
- try {
- NetworkInterface byInetAddress = NetworkInterface.getByInetAddress(InetAddress.getByName(String.valueOf(this.currentIpAddress)));
- if (byInetAddress != null) {
- List<InterfaceAddress> interfaceAddresses = byInetAddress.getInterfaceAddresses();
- if (!interfaceAddresses.isEmpty()) {
- for (InterfaceAddress interfaceAddress : interfaceAddresses) {
- if (this.currentIpAddress.equals(interfaceAddress.getAddress().getHostAddress())) {
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append(interfaceAddress.getAddress().getHostAddress());
- stringBuilder.append("/");
- stringBuilder.append(interfaceAddress.getNetworkPrefixLength());
- return new SubnetUtils(stringBuilder.toString()).getInfo().getNetmask();
- }
- }
- }
- }
- } catch (Throwable e) {
- Log.e("BellCompanionSDK", "Error extracting mask.", e);
- }
- return "0.0.0.0";
- }
- public NetworkType getCurrentNetworkType() {
- return this.currentNetworkType;
- }
- private String getLocalIpAddress(NetworkInfo networkInfo) {
- if (networkInfo.getType() == 1) {
- WifiManager wifiManager = (WifiManager) this.androidContext.getSystemService("wifi");
- this.networkIdentity = wifiManager.getConnectionInfo().getSSID();
- return Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());
- }
- this.networkIdentity = networkInfo.getExtraInfo();
- try {
- networkInfo = NetworkInterface.getNetworkInterfaces();
- while (networkInfo.hasMoreElements()) {
- Enumeration inetAddresses = ((NetworkInterface) networkInfo.nextElement()).getInetAddresses();
- while (inetAddresses.hasMoreElements()) {
- InetAddress inetAddress = (InetAddress) inetAddresses.nextElement();
- if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)) {
- return inetAddress.getHostAddress().toString();
- }
- }
- }
- } catch (NetworkInfo networkInfo2) {
- Log.e("IP Address", networkInfo2.toString());
- }
- return null;
- }
- private NetworkType getNetworkType(NetworkInfo networkInfo) {
- if (networkInfo.getType() != null) {
- return NetworkType.WIFI;
- }
- return NetworkType.MOBILE;
- }
- private void notifyConnected(NetworkType networkType, String str, String str2) {
- for (ConnectivityStateListener onNetworkConnected : this.stateListeners) {
- onNetworkConnected.onNetworkConnected(networkType, str, str2);
- }
- }
- private void notifyDisconnected() {
- for (ConnectivityStateListener onNetworkDisconnected : this.stateListeners) {
- onNetworkDisconnected.onNetworkDisconnected();
- }
- }
- public void addConnectivityStateListener(ConnectivityStateListener connectivityStateListener) {
- this.stateListeners.add(connectivityStateListener);
- }
- public String getNetworkIdentity() {
- return this.networkIdentity;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement