123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- package de.tudarmstadt.informatik.hostage.commons;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import java.security.KeyStore;
- import java.util.concurrent.ExecutionException;
- import org.apache.http.HttpVersion;
- import org.apache.http.client.HttpClient;
- import org.apache.http.conn.ClientConnectionManager;
- import org.apache.http.conn.scheme.PlainSocketFactory;
- import org.apache.http.conn.scheme.Scheme;
- import org.apache.http.conn.scheme.SchemeRegistry;
- import org.apache.http.conn.ssl.SSLSocketFactory;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
- import org.apache.http.params.BasicHttpParams;
- import org.apache.http.params.HttpParams;
- import org.apache.http.params.HttpProtocolParams;
- import org.apache.http.protocol.HTTP;
- import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
- import de.tudarmstadt.informatik.hostage.ui.MainActivity;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.net.wifi.WifiInfo;
- import android.net.wifi.WifiManager;
- import android.os.Environment;
- import android.text.TextUtils;
- /**
- * Helper class with some static methods for general usage.
- * @author Lars Pandikow
- * @author Wulf Pfeiffer
- *
- */
- public final class HelperUtils {
- /**
- * Gets SSID of the wireless network.
- * @param context Needs a context to get system recourses
- * @return SSID of wireless network if connected, else null.
- */
- public static String getSSID(Context context) {
- String ssid = null;
- ConnectivityManager connManager = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connManager
- .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
- if (networkInfo != null && networkInfo.isConnected()) {
- final WifiManager wifiManager = (WifiManager) context
- .getSystemService(Context.WIFI_SERVICE);
- final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
- if (connectionInfo != null
- && !TextUtils.isEmpty(connectionInfo.getSSID())) {
- ssid = connectionInfo.getSSID();
- }
- }
- return ssid;
- }
- /**
- * Gets BSSID of the wireless network.
- * @param context Needs a context to get system recourses.
- * @return BSSID of wireless network if connected, else null.
- */
- public static String getBSSID(Context context) {
- String bssid = null;
- ConnectivityManager connManager = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connManager
- .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
- if (networkInfo != null && networkInfo.isConnected()) {
- final WifiManager wifiManager = (WifiManager) context
- .getSystemService(Context.WIFI_SERVICE);
- final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
- if (connectionInfo != null
- && !TextUtils.isEmpty(connectionInfo.getSSID())) {
- bssid = connectionInfo.getBSSID();
- }
- }
- return bssid;
- }
- /**
- * Gets internal IP address of the device in a wireless network.
- * @param context Needs a context to get system recourses.
- * @return internal IP of the device in a wireless network if connected, else null.
- */
- public static String getInternalIP(Context context) {
- String ipAddress = null;
- ConnectivityManager connManager = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connManager
- .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
- if (networkInfo != null && networkInfo.isConnected()) {
- final WifiManager wifiManager = (WifiManager) context
- .getSystemService(Context.WIFI_SERVICE);
- final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
- if (connectionInfo != null) {
- try {
- ipAddress = InetAddress.getByAddress(
- unpackInetAddress(connectionInfo.getIpAddress()))
- .getHostAddress();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- }
- }
- }
- return ipAddress;
- }
- private static byte[] unpackInetAddress(int bytes) {
- return new byte[] { (byte) ((bytes) & 0xff),
- (byte) ((bytes >>> 8) & 0xff), (byte) ((bytes >>> 16) & 0xff),
- (byte) ((bytes >>> 24) & 0xff) };
- }
- /**
- * Determines and returns the external IP address of the device.
- * @param context Needs a context to get system recourses.
- * @return A String representation of the external IP of the device.
- * @see GetExternalIPTask
- */
- public static String getExternalIP(Context context) {
- String ipAddress = null;
- GetExternalIPTask task = new GetExternalIPTask();
- task.execute(new String[]{"http://ip2country.sourceforge.net/ip2c.php?format=JSON"});
- try {
- ipAddress = task.get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
- return ipAddress;
- }
-
- /**
- * Updates the connextion info and saves them in the the SharedPreferences for session data.
- * @param context Needs a context to get system recourses.
- * @see MainActivity#SESSION_DATA
- */
- public static void updateConnectionInfo(Context context) {
- SharedPreferences pref = context.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
- Editor editor = pref.edit();
- editor.putString(MainActivity.SSID, getSSID(context));
- editor.putString(MainActivity.BSSID, getBSSID(context));
- editor.putString(MainActivity.INTERNAL_IP, getInternalIP(context));
- editor.putString(MainActivity.EXTERNAL_IP, getExternalIP(context));
- editor.commit();
- }
- /**
- * Checks if external storage is available for read and write.
- * @return True if external storage is available for read and write, else false.
- */
- public static boolean isExternalStorageWritable() {
- String state = Environment.getExternalStorageState();
- if (Environment.MEDIA_MOUNTED.equals(state)) {
- return true;
- }
- return false;
- }
- /**
- * Creates a HttpClient with an own SSL Socket.
- * @return HttpsClient who accepts accepts all certificates.
- * @see MySSLSocketFactory
- */
- public static HttpClient createHttpClient() {
- try {
- KeyStore trustStore = KeyStore.getInstance(KeyStore
- .getDefaultType());
- trustStore.load(null, null);
- SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
- sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
- HttpParams params = new BasicHttpParams();
- HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
- HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
- SchemeRegistry registry = new SchemeRegistry();
- registry.register(new Scheme("http", PlainSocketFactory
- .getSocketFactory(), 80));
- registry.register(new Scheme("https", sf, 443));
- ClientConnectionManager ccm = new ThreadSafeClientConnManager(
- params, registry);
- return new DefaultHttpClient(ccm, params);
- } catch (Exception e) {
- e.printStackTrace();
- return new DefaultHttpClient();
- }
- }
-
- /**
- * Concatenates several byte arrays.
- * @param bytes The byte arrays.
- * @return A single byte arrays containing all the bytes from the given arrays in the order they are given.
- */
- public static byte[] concat(byte[]... bytes) {
- int newSize = 0;
- for (byte[] b : bytes)
- newSize += b.length;
- byte[] dst = new byte[newSize];
- int currentPos = 0;
- int newPos;
- for (byte[] b : bytes) {
- newPos = b.length;
- System.arraycopy(b, 0, dst, currentPos, newPos);
- currentPos += newPos;
- }
- return dst;
- }
- }
|