HelperUtils.java 8.8 KB

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