HelperUtils.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package de.tudarmstadt.informatik.hostage.commons;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import java.security.KeyStore;
  5. import java.util.concurrent.ExecutionException;
  6. import org.apache.http.HttpVersion;
  7. import org.apache.http.client.HttpClient;
  8. import org.apache.http.conn.ClientConnectionManager;
  9. import org.apache.http.conn.scheme.PlainSocketFactory;
  10. import org.apache.http.conn.scheme.Scheme;
  11. import org.apache.http.conn.scheme.SchemeRegistry;
  12. import org.apache.http.conn.ssl.SSLSocketFactory;
  13. import org.apache.http.impl.client.DefaultHttpClient;
  14. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  15. import org.apache.http.params.BasicHttpParams;
  16. import org.apache.http.params.HttpParams;
  17. import org.apache.http.params.HttpProtocolParams;
  18. import org.apache.http.protocol.HTTP;
  19. import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
  20. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  21. import android.content.Context;
  22. import android.content.SharedPreferences;
  23. import android.content.SharedPreferences.Editor;
  24. import android.net.ConnectivityManager;
  25. import android.net.NetworkInfo;
  26. import android.net.wifi.WifiInfo;
  27. import android.net.wifi.WifiManager;
  28. import android.os.Environment;
  29. import android.text.TextUtils;
  30. /**
  31. * Helper class with some static methods for general usage.
  32. * @author Lars Pandikow
  33. * @author Wulf Pfeiffer
  34. *
  35. */
  36. public final class HelperUtils {
  37. /**
  38. * Gets SSID of the wireless network.
  39. * @param context Needs a context to get system recourses
  40. * @return SSID of wireless network if connected, else null.
  41. */
  42. public static String getSSID(Context context) {
  43. String ssid = null;
  44. ConnectivityManager connManager = (ConnectivityManager) context
  45. .getSystemService(Context.CONNECTIVITY_SERVICE);
  46. NetworkInfo networkInfo = connManager
  47. .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  48. if (networkInfo != null && networkInfo.isConnected()) {
  49. final WifiManager wifiManager = (WifiManager) context
  50. .getSystemService(Context.WIFI_SERVICE);
  51. final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
  52. if (connectionInfo != null
  53. && !TextUtils.isEmpty(connectionInfo.getSSID())) {
  54. ssid = connectionInfo.getSSID();
  55. }
  56. }
  57. return ssid;
  58. }
  59. /**
  60. * Gets BSSID of the wireless network.
  61. * @param context Needs a context to get system recourses.
  62. * @return BSSID of wireless network if connected, else null.
  63. */
  64. public static String getBSSID(Context context) {
  65. String bssid = null;
  66. ConnectivityManager connManager = (ConnectivityManager) context
  67. .getSystemService(Context.CONNECTIVITY_SERVICE);
  68. NetworkInfo networkInfo = connManager
  69. .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  70. if (networkInfo != null && networkInfo.isConnected()) {
  71. final WifiManager wifiManager = (WifiManager) context
  72. .getSystemService(Context.WIFI_SERVICE);
  73. final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
  74. if (connectionInfo != null
  75. && !TextUtils.isEmpty(connectionInfo.getSSID())) {
  76. bssid = connectionInfo.getBSSID();
  77. }
  78. }
  79. return bssid;
  80. }
  81. /**
  82. * Gets internal IP address of the device in a wireless network.
  83. * @param context Needs a context to get system recourses.
  84. * @return internal IP of the device in a wireless network if connected, else null.
  85. */
  86. public static String getInternalIP(Context context) {
  87. String ipAddress = null;
  88. ConnectivityManager connManager = (ConnectivityManager) context
  89. .getSystemService(Context.CONNECTIVITY_SERVICE);
  90. NetworkInfo networkInfo = connManager
  91. .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  92. if (networkInfo != null && networkInfo.isConnected()) {
  93. final WifiManager wifiManager = (WifiManager) context
  94. .getSystemService(Context.WIFI_SERVICE);
  95. final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
  96. if (connectionInfo != null) {
  97. try {
  98. ipAddress = InetAddress.getByAddress(
  99. unpackInetAddress(connectionInfo.getIpAddress()))
  100. .getHostAddress();
  101. } catch (UnknownHostException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. }
  106. return ipAddress;
  107. }
  108. private static byte[] unpackInetAddress(int bytes) {
  109. return new byte[] { (byte) ((bytes) & 0xff),
  110. (byte) ((bytes >>> 8) & 0xff), (byte) ((bytes >>> 16) & 0xff),
  111. (byte) ((bytes >>> 24) & 0xff) };
  112. }
  113. /**
  114. * Determines and returns the external IP address of the device.
  115. * @param context Needs a context to get system recourses.
  116. * @return A String representation of the external IP of the device.
  117. * @see GetExternalIPTask
  118. */
  119. public static String getExternalIP(Context context) {
  120. String ipAddress = null;
  121. GetExternalIPTask task = new GetExternalIPTask();
  122. task.execute(new String[]{"http://ip2country.sourceforge.net/ip2c.php?format=JSON"});
  123. try {
  124. ipAddress = task.get();
  125. } catch (InterruptedException e) {
  126. e.printStackTrace();
  127. } catch (ExecutionException e) {
  128. e.printStackTrace();
  129. }
  130. return ipAddress;
  131. }
  132. /**
  133. * Updates the connextion info and saves them in the the SharedPreferences for session data.
  134. * @param context Needs a context to get system recourses.
  135. * @see MainActivity#SESSION_DATA
  136. */
  137. public static void updateConnectionInfo(Context context) {
  138. SharedPreferences pref = context.getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  139. Editor editor = pref.edit();
  140. editor.putString(MainActivity.SSID, getSSID(context));
  141. editor.putString(MainActivity.BSSID, getBSSID(context));
  142. editor.putString(MainActivity.INTERNAL_IP, getInternalIP(context));
  143. editor.putString(MainActivity.EXTERNAL_IP, getExternalIP(context));
  144. editor.commit();
  145. }
  146. /**
  147. * Checks if external storage is available for read and write.
  148. * @return True if external storage is available for read and write, else false.
  149. */
  150. public static boolean isExternalStorageWritable() {
  151. String state = Environment.getExternalStorageState();
  152. if (Environment.MEDIA_MOUNTED.equals(state)) {
  153. return true;
  154. }
  155. return false;
  156. }
  157. /**
  158. * Creates a HttpClient with an own SSL Socket.
  159. * @return HttpsClient who accepts accepts all certificates.
  160. * @see MySSLSocketFactory
  161. */
  162. public static HttpClient createHttpClient() {
  163. try {
  164. KeyStore trustStore = KeyStore.getInstance(KeyStore
  165. .getDefaultType());
  166. trustStore.load(null, null);
  167. SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
  168. sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  169. HttpParams params = new BasicHttpParams();
  170. HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  171. HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
  172. SchemeRegistry registry = new SchemeRegistry();
  173. registry.register(new Scheme("http", PlainSocketFactory
  174. .getSocketFactory(), 80));
  175. registry.register(new Scheme("https", sf, 443));
  176. ClientConnectionManager ccm = new ThreadSafeClientConnManager(
  177. params, registry);
  178. return new DefaultHttpClient(ccm, params);
  179. } catch (Exception e) {
  180. e.printStackTrace();
  181. return new DefaultHttpClient();
  182. }
  183. }
  184. /**
  185. * Concatenates several byte arrays.
  186. * @param bytes The byte arrays.
  187. * @return A single byte arrays containing all the bytes from the given arrays in the order they are given.
  188. */
  189. public static byte[] concat(byte[]... bytes) {
  190. int newSize = 0;
  191. for (byte[] b : bytes)
  192. newSize += b.length;
  193. byte[] dst = new byte[newSize];
  194. int currentPos = 0;
  195. int newPos;
  196. for (byte[] b : bytes) {
  197. newPos = b.length;
  198. System.arraycopy(b, 0, dst, currentPos, newPos);
  199. currentPos += newPos;
  200. }
  201. return dst;
  202. }
  203. }