Advertisement
Guest User

Untitled

a guest
May 31st, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.39 KB | None | 0 0
  1. package com.bell.cts.iptv.companion.sdk.network;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.net.ConnectivityManager;
  8. import android.net.NetworkInfo;
  9. import android.net.wifi.WifiInfo;
  10. import android.net.wifi.WifiManager;
  11. import android.text.format.Formatter;
  12. import android.util.Log;
  13. import java.net.Inet4Address;
  14. import java.net.InetAddress;
  15. import java.net.InterfaceAddress;
  16. import java.net.NetworkInterface;
  17. import java.util.ArrayList;
  18. import java.util.Enumeration;
  19. import java.util.List;
  20. import org.apache.commons.net.util.SubnetUtils;
  21.  
  22. public class ConnectivityMonitor extends BroadcastReceiver {
  23.     private Context androidContext;
  24.     private String currentIpAddress;
  25.     private NetworkType currentNetworkType;
  26.     private String networkIdentity;
  27.     private boolean registered = false;
  28.     private List<ConnectivityStateListener> stateListeners;
  29.  
  30.     public interface ConnectivityStateListener {
  31.         void onNetworkConnected(NetworkType networkType, String str, String str2);
  32.  
  33.         void onNetworkDisconnected();
  34.     }
  35.  
  36.     public enum NetworkType {
  37.         MOBILE,
  38.         WIFI
  39.     }
  40.  
  41.     public ConnectivityMonitor(Context context) {
  42.         this.androidContext = context;
  43.         this.stateListeners = new ArrayList(2);
  44.         onReceive(context, null);
  45.     }
  46.  
  47.     public void register() {
  48.         if (!this.registered) {
  49.             this.androidContext.registerReceiver(this, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
  50.             this.registered = true;
  51.         }
  52.     }
  53.  
  54.     public void unregister() {
  55.         /* JADX: method processing error */
  56. /*
  57. Error: java.lang.NullPointerException
  58.     at jadx.core.dex.visitors.regions.ProcessTryCatchRegions.searchTryCatchDominators(ProcessTryCatchRegions.java:75)
  59.     at jadx.core.dex.visitors.regions.ProcessTryCatchRegions.process(ProcessTryCatchRegions.java:45)
  60.     at jadx.core.dex.visitors.regions.RegionMakerVisitor.postProcessRegions(RegionMakerVisitor.java:63)
  61.     at jadx.core.dex.visitors.regions.RegionMakerVisitor.visit(RegionMakerVisitor.java:58)
  62.     at jadx.core.dex.visitors.DepthTraversal.visit(DepthTraversal.java:31)
  63.     at jadx.core.dex.visitors.DepthTraversal.visit(DepthTraversal.java:17)
  64.     at jadx.core.ProcessClass.process(ProcessClass.java:37)
  65.     at jadx.api.JadxDecompiler.processClass(JadxDecompiler.java:306)
  66.     at jadx.api.JavaClass.decompile(JavaClass.java:62)
  67.     at jadx.api.JadxDecompiler$1.run(JadxDecompiler.java:199)
  68. */
  69.         /*
  70.         r2 = this;
  71.         r0 = r2.registered;  Catch:{ IllegalArgumentException -> 0x000d }
  72.         if (r0 == 0) goto L_0x0014;  Catch:{ IllegalArgumentException -> 0x000d }
  73.     L_0x0004:
  74.         r0 = r2.androidContext;  Catch:{ IllegalArgumentException -> 0x000d }
  75.         r0.unregisterReceiver(r2);   Catch:{ IllegalArgumentException -> 0x000d }
  76.         r0 = 0;  Catch:{ IllegalArgumentException -> 0x000d }
  77.         r2.registered = r0;  Catch:{ IllegalArgumentException -> 0x000d }
  78.         goto L_0x0014;
  79.     L_0x000d:
  80.         r0 = "BellCompanionSDK";
  81.         r1 = "ConnectivityMonitor wasn't registered.";
  82.         android.util.Log.e(r0, r1);
  83.     L_0x0014:
  84.         return;
  85.         */
  86.         throw new UnsupportedOperationException("Method not decompiled: com.bell.cts.iptv.companion.sdk.network.ConnectivityMonitor.unregister():void");
  87.     }
  88.  
  89.     public void onReceive(Context context, Intent intent) {
  90.         try {
  91.             ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
  92.             context = connectivityManager != null ? connectivityManager.getActiveNetworkInfo() : null;
  93.             if ((context == null || !context.isConnected()) && this.currentIpAddress != null) {
  94.                 this.currentIpAddress = null;
  95.                 this.currentNetworkType = null;
  96.                 this.networkIdentity = null;
  97.                 notifyDisconnected();
  98.             } else if (context != null && context.isConnected() != null) {
  99.                 intent = getLocalIpAddress(context);
  100.                 NetworkType networkType = getNetworkType(context);
  101.                 if ((intent != null && !intent.equals(this.currentIpAddress)) || (networkType != null && networkType != this.currentNetworkType)) {
  102.                     this.currentIpAddress = intent;
  103.                     this.currentNetworkType = getNetworkType(context);
  104.                     this.networkIdentity = getNetworkId(context);
  105.                     notifyConnected(this.currentNetworkType, this.currentIpAddress, this.networkIdentity);
  106.                 }
  107.             }
  108.         } catch (Context context2) {
  109.             Log.e("BellCompanionSDK", "Error handling network change.", context2);
  110.         }
  111.     }
  112.  
  113.     private String getNetworkId(NetworkInfo networkInfo) {
  114.         if (networkInfo.getType() == 1) {
  115.             WifiInfo connectionInfo = ((WifiManager) this.androidContext.getSystemService("wifi")).getConnectionInfo();
  116.             if (connectionInfo != null) {
  117.                 return connectionInfo.getSSID();
  118.             }
  119.         }
  120.         return networkInfo.getExtraInfo();
  121.     }
  122.  
  123.     public String getCurrentIpAddress() {
  124.         return this.currentIpAddress;
  125.     }
  126.  
  127.     public String getNetmask() {
  128.         if (((WifiManager) this.androidContext.getSystemService("wifi")).getDhcpInfo().netmask <= 0) {
  129.             return extractMaskFromNetworkInterface();
  130.         }
  131.         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)});
  132.     }
  133.  
  134.     private String extractMaskFromNetworkInterface() {
  135.         try {
  136.             NetworkInterface byInetAddress = NetworkInterface.getByInetAddress(InetAddress.getByName(String.valueOf(this.currentIpAddress)));
  137.             if (byInetAddress != null) {
  138.                 List<InterfaceAddress> interfaceAddresses = byInetAddress.getInterfaceAddresses();
  139.                 if (!interfaceAddresses.isEmpty()) {
  140.                     for (InterfaceAddress interfaceAddress : interfaceAddresses) {
  141.                         if (this.currentIpAddress.equals(interfaceAddress.getAddress().getHostAddress())) {
  142.                             StringBuilder stringBuilder = new StringBuilder();
  143.                             stringBuilder.append(interfaceAddress.getAddress().getHostAddress());
  144.                             stringBuilder.append("/");
  145.                             stringBuilder.append(interfaceAddress.getNetworkPrefixLength());
  146.                             return new SubnetUtils(stringBuilder.toString()).getInfo().getNetmask();
  147.                         }
  148.                     }
  149.                 }
  150.             }
  151.         } catch (Throwable e) {
  152.             Log.e("BellCompanionSDK", "Error extracting mask.", e);
  153.         }
  154.         return "0.0.0.0";
  155.     }
  156.  
  157.     public NetworkType getCurrentNetworkType() {
  158.         return this.currentNetworkType;
  159.     }
  160.  
  161.     private String getLocalIpAddress(NetworkInfo networkInfo) {
  162.         if (networkInfo.getType() == 1) {
  163.             WifiManager wifiManager = (WifiManager) this.androidContext.getSystemService("wifi");
  164.             this.networkIdentity = wifiManager.getConnectionInfo().getSSID();
  165.             return Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());
  166.         }
  167.         this.networkIdentity = networkInfo.getExtraInfo();
  168.         try {
  169.             networkInfo = NetworkInterface.getNetworkInterfaces();
  170.             while (networkInfo.hasMoreElements()) {
  171.                 Enumeration inetAddresses = ((NetworkInterface) networkInfo.nextElement()).getInetAddresses();
  172.                 while (inetAddresses.hasMoreElements()) {
  173.                     InetAddress inetAddress = (InetAddress) inetAddresses.nextElement();
  174.                     if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)) {
  175.                         return inetAddress.getHostAddress().toString();
  176.                     }
  177.                 }
  178.             }
  179.         } catch (NetworkInfo networkInfo2) {
  180.             Log.e("IP Address", networkInfo2.toString());
  181.         }
  182.         return null;
  183.     }
  184.  
  185.     private NetworkType getNetworkType(NetworkInfo networkInfo) {
  186.         if (networkInfo.getType() != null) {
  187.             return NetworkType.WIFI;
  188.         }
  189.         return NetworkType.MOBILE;
  190.     }
  191.  
  192.     private void notifyConnected(NetworkType networkType, String str, String str2) {
  193.         for (ConnectivityStateListener onNetworkConnected : this.stateListeners) {
  194.             onNetworkConnected.onNetworkConnected(networkType, str, str2);
  195.         }
  196.     }
  197.  
  198.     private void notifyDisconnected() {
  199.         for (ConnectivityStateListener onNetworkDisconnected : this.stateListeners) {
  200.             onNetworkDisconnected.onNetworkDisconnected();
  201.         }
  202.     }
  203.  
  204.     public void addConnectivityStateListener(ConnectivityStateListener connectivityStateListener) {
  205.         this.stateListeners.add(connectivityStateListener);
  206.     }
  207.  
  208.     public String getNetworkIdentity() {
  209.         return this.networkIdentity;
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement