Android Open Source - AIRShare Network Utils






From Project

Back to project page AIRShare.

License

The source code is released under:

Apache License

If you think the Android project AIRShare listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ggt.airshare.utils;
//ww w  . j  a  v  a2  s . c  o  m
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import org.apache.commons.lang3.StringUtils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

/**
 * Network utils. Detect if wifi is activated. Get ip address on wifi network,
 * etc ...
 * 
 * @author gduche
 */
public class NetworkUtils {

  public static class WifiDetails {
    public String ipAddress;
    public String ssid;
  }

  private static final String LOG_TAG = "NetworkUtils";

  public static WifiDetails getWifiDetails(Context context) {
    WifiDetails wifiDetails = null;
    if (isWifiApActivated(context)) {
      wifiDetails = new NetworkUtils.WifiDetails();
      // phone as HotSpot case
      WifiConfiguration wifiConfiguration = getWifiApConfiguration(context);
      if (wifiConfiguration != null) {
        wifiDetails.ssid = wifiConfiguration.SSID;
        wifiDetails.ipAddress = getWifiApIpAddress();
      }
    } else {
      // phone maybe connected on wifi normally
      ConnectivityManager connManager = (ConnectivityManager) context
          .getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo networkInfo = connManager
          .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

      if (networkInfo.isConnected()) {
        wifiDetails = new NetworkUtils.WifiDetails();
        final WifiManager wifiManager = (WifiManager) context
            .getSystemService(Context.WIFI_SERVICE);
        final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
        if (connectionInfo != null
            && !StringUtils.isBlank(connectionInfo.getSSID())) {
          wifiDetails.ssid = connectionInfo.getSSID();
        }
        if (connectionInfo != null) {
          wifiDetails.ipAddress = convertInToIpAddress(connectionInfo
              .getIpAddress());
        }
        return wifiDetails;
      }
    }

    return wifiDetails;
  }

  private static String getWifiApIpAddress() {
    try {
      for (Enumeration<NetworkInterface> en = NetworkInterface
          .getNetworkInterfaces(); en.hasMoreElements();) {
        NetworkInterface intf = en.nextElement();
        if (intf.getName().contains("wlan")) {
          for (Enumeration<InetAddress> enumIpAddr = intf
              .getInetAddresses(); enumIpAddr.hasMoreElements();) {
            InetAddress inetAddress = enumIpAddr.nextElement();
            if (!inetAddress.isLoopbackAddress()
                && (inetAddress.getAddress().length == 4)) {
              Log.d(LOG_TAG, inetAddress.getHostAddress());
              return inetAddress.getHostAddress();
            }
          }
        }
      }
    } catch (SocketException ex) {
      Log.e(LOG_TAG, ex.toString());
    }
    return null;
  }

  private static WifiConfiguration getWifiApConfiguration(Context context) {
    WifiConfiguration wifiConfiguration = null;
    try {
      WifiManager manager = (WifiManager) context
          .getSystemService(Context.WIFI_SERVICE);
      Method method = manager.getClass().getDeclaredMethod(
          "getWifiApConfiguration");
      method.setAccessible(true); // in the case of visibility change in
      // future APIs
      wifiConfiguration = (WifiConfiguration) method.invoke(manager);
    } catch (NoSuchMethodException e) {
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    } catch (InvocationTargetException e) {
    }
    return wifiConfiguration;

  }

  private static boolean isWifiApActivated(Context context) {
    boolean value = false;
    try {
      WifiManager manager = (WifiManager) context
          .getSystemService(Context.WIFI_SERVICE);
      Method method = manager.getClass().getDeclaredMethod(
          "isWifiApEnabled");
      method.setAccessible(true); // in the case of visibility change in
      // future APIs
      value = (Boolean) method.invoke(manager);
    } catch (NoSuchMethodException e) {
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    } catch (InvocationTargetException e) {
    }
    return value;
  }

  private static String convertInToIpAddress(int ipAddress) {
    byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();
    byte[] bytes2 = new byte[4];
    bytes2[0] = bytes[3];
    bytes2[1] = bytes[2];
    bytes2[2] = bytes[1];
    bytes2[3] = bytes[0];
    try {
      InetAddress address = InetAddress.getByAddress(bytes2);
      return address.getHostAddress();
    } catch (UnknownHostException e) {
      return null;
    }
  }

}




Java Source Code List

com.ggt.airshare.AIRShareApplication.java
com.ggt.airshare.LauncherActivity.java
com.ggt.airshare.MotherActivity.java
com.ggt.airshare.ShairingActivity.java
com.ggt.airshare.httpserver.NanoHTTPD.java
com.ggt.airshare.httpserver.ShAIReHttpServerListener.java
com.ggt.airshare.httpserver.ShAIReHttpServer.java
com.ggt.airshare.httpserver.ShaireServiceListener.java
com.ggt.airshare.httpserver.ShaireService.java
com.ggt.airshare.urlshortener.UrlShortenerException.java
com.ggt.airshare.urlshortener.UrlShortenerListener.java
com.ggt.airshare.urlshortener.UrlShortener.java
com.ggt.airshare.utils.ContactsUtils.java
com.ggt.airshare.utils.FileUtils.java
com.ggt.airshare.utils.HTMLUtils.java
com.ggt.airshare.utils.NetworkUtils.java
com.ggt.airshare.utils.ShAIReConstants.java
com.google.zxing.client.android.Contents.java
com.google.zxing.client.android.FinishListener.java
com.google.zxing.client.android.Intents.java
com.google.zxing.client.android.encode.ContactEncoder.java
com.google.zxing.client.android.encode.EncodeActivity.java
com.google.zxing.client.android.encode.Formatter.java
com.google.zxing.client.android.encode.MECARDContactEncoder.java
com.google.zxing.client.android.encode.QRCodeEncoder.java
com.google.zxing.client.android.encode.VCardContactEncoder.java
com.ianhanniballake.localstorage.LocalStorageProvider.java
com.ipaulpro.afilechooser.FileChooserActivity.java
com.ipaulpro.afilechooser.FileListAdapter.java
com.ipaulpro.afilechooser.FileListFragment.java
com.ipaulpro.afilechooser.FileLoader.java
com.ipaulpro.afilechooser.utils.FileUtils.java
de.psdev.licensesdialog.LicenseResolver.java
de.psdev.licensesdialog.LicensesDialogFragment.java
de.psdev.licensesdialog.LicensesDialog.java
de.psdev.licensesdialog.NoticesHtmlBuilder.java
de.psdev.licensesdialog.NoticesXmlParser.java
de.psdev.licensesdialog.SingleLicenseDialogFragment.java
de.psdev.licensesdialog.SingleLicenseDialog.java
de.psdev.licensesdialog.licenses.ApacheSoftwareLicense20.java
de.psdev.licensesdialog.licenses.BSD3ClauseLicense.java
de.psdev.licensesdialog.licenses.ISCLicense.java
de.psdev.licensesdialog.licenses.License.java
de.psdev.licensesdialog.licenses.MITLicense.java
de.psdev.licensesdialog.licenses.NanoHttpdLicense.java
de.psdev.licensesdialog.licenses.ViewerJSLicense.java
de.psdev.licensesdialog.licenses.XstreamLicense.java
de.psdev.licensesdialog.model.Notice.java
de.psdev.licensesdialog.model.Notices.java