HelperUtils.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package de.tudarmstadt.informatik.hostage.commons;
  2. import java.net.InetAddress;
  3. import java.net.UnknownHostException;
  4. import java.security.SecureRandom;
  5. import android.content.Context;
  6. import android.net.ConnectivityManager;
  7. import android.net.NetworkInfo;
  8. import android.net.wifi.WifiInfo;
  9. import android.net.wifi.WifiManager;
  10. import android.os.Environment;
  11. import android.text.TextUtils;
  12. /**
  13. * Helper class with some static methods for general usage.
  14. *
  15. * @author Lars Pandikow
  16. * @author Wulf Pfeiffer
  17. *
  18. */
  19. public final class HelperUtils {
  20. /**
  21. * Converts a byte array into a hexadecimal String, e.g. {0x00, 0x01} to
  22. * "00, 01".
  23. *
  24. * @param bytes
  25. * that will be converted.
  26. * @return converted String.
  27. */
  28. public static String bytesToHexString(byte[] bytes) {
  29. char[] hexArray = "0123456789ABCDEF".toCharArray();
  30. int v;
  31. StringBuffer buffer = new StringBuffer();
  32. for (int j = 0; j < bytes.length; j++) {
  33. v = bytes[j] & 0xFF;
  34. buffer.append(hexArray[v >>> 4]);
  35. buffer.append(hexArray[v & 0x0F]);
  36. if (j < bytes.length - 1)
  37. buffer.append(", ");
  38. }
  39. return buffer.toString();
  40. }
  41. /**
  42. * Converts a byte[] to a String, but only characters in ASCII between 32
  43. * and 127
  44. *
  45. * @param bytes
  46. * that are converted
  47. * @return converted String
  48. */
  49. public static String byteToStr(byte[] bytes) {
  50. int size = 0;
  51. for(byte b : bytes) {
  52. if(isLetter((char) b)) {
  53. size++;
  54. }
  55. }
  56. char[] chars = new char[size];
  57. for (int i = 0, j = 0; i < bytes.length && j < size; i++) {
  58. if (isLetter((char) bytes[i])) {
  59. chars[j] = (char) bytes[i];
  60. j++;
  61. }
  62. }
  63. return new String(chars);
  64. }
  65. /**
  66. * Concatenates several byte arrays.
  67. *
  68. * @param bytes
  69. * The byte arrays.
  70. * @return A single byte arrays containing all the bytes from the given
  71. * arrays in the order they are given.
  72. */
  73. public static byte[] concat(byte[]... bytes) {
  74. int newSize = 0;
  75. for (byte[] b : bytes)
  76. if (b != null)
  77. newSize += b.length;
  78. byte[] dst = new byte[newSize];
  79. int currentPos = 0;
  80. int newPos;
  81. for (byte[] b : bytes) {
  82. if (b != null) {
  83. newPos = b.length;
  84. System.arraycopy(b, 0, dst, currentPos, newPos);
  85. currentPos += newPos;
  86. }
  87. }
  88. return dst;
  89. }
  90. /**
  91. * Puts a 0x00 byte between each byte in a byte array.
  92. *
  93. * @param bytes
  94. * that need to be filled with 0x00.
  95. * @return filled byte array.
  96. */
  97. public static byte[] fillWithZero(byte[] bytes) {
  98. byte[] newBytes = new byte[(bytes.length * 2)];
  99. for (int i = 0, j = 0; i < bytes.length && j < newBytes.length; i++, j = j + 2) {
  100. newBytes[j] = bytes[i];
  101. newBytes[j + 1] = 0x00;
  102. }
  103. return newBytes;
  104. }
  105. /**
  106. * Puts a 0x00 byte between each byte and another 2 0x00 bytes at the end of
  107. * a byte array.
  108. *
  109. * @param bytes
  110. * that need to be filled with 0x00.
  111. * @return filled byte array.
  112. */
  113. public static byte[] fillWithZeroExtended(byte[] bytes) {
  114. byte[] zeroBytes = fillWithZero(bytes);
  115. byte[] newBytes = new byte[zeroBytes.length + 2];
  116. newBytes = HelperUtils.concat(zeroBytes, new byte[] { 0x00, 0x00 });
  117. return newBytes;
  118. }
  119. /**
  120. * Gets BSSID of the wireless network.
  121. *
  122. * @param context
  123. * Needs a context to get system recourses.
  124. * @return BSSID of wireless network if connected, else null.
  125. */
  126. public static String getBSSID(Context context) {
  127. String bssid = null;
  128. ConnectivityManager connManager = (ConnectivityManager) context
  129. .getSystemService(Context.CONNECTIVITY_SERVICE);
  130. NetworkInfo networkInfo = connManager
  131. .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  132. if (networkInfo != null && networkInfo.isConnected()) {
  133. final WifiManager wifiManager = (WifiManager) context
  134. .getSystemService(Context.WIFI_SERVICE);
  135. final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
  136. if (connectionInfo != null
  137. && !TextUtils.isEmpty(connectionInfo.getSSID())) {
  138. bssid = connectionInfo.getBSSID();
  139. }
  140. }
  141. return bssid;
  142. }
  143. /**
  144. * Gets internal IP address of the device in a wireless network.
  145. *
  146. * @param context
  147. * Needs a context to get system recourses.
  148. * @return internal IP of the device in a wireless network if connected,
  149. * else null.
  150. */
  151. public static String getInternalIP(Context context) {
  152. String ipAddress = null;
  153. ConnectivityManager connManager = (ConnectivityManager) context
  154. .getSystemService(Context.CONNECTIVITY_SERVICE);
  155. NetworkInfo networkInfo = connManager
  156. .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  157. if (networkInfo != null && networkInfo.isConnected()) {
  158. final WifiManager wifiManager = (WifiManager) context
  159. .getSystemService(Context.WIFI_SERVICE);
  160. final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
  161. if (connectionInfo != null) {
  162. try {
  163. ipAddress = InetAddress.getByAddress(
  164. unpackInetAddress(connectionInfo.getIpAddress()))
  165. .getHostAddress();
  166. } catch (UnknownHostException e) {
  167. e.printStackTrace();
  168. }
  169. }
  170. }
  171. return ipAddress;
  172. }
  173. /**
  174. * Gets SSID of the wireless network.
  175. *
  176. * @param context
  177. * Needs a context to get system recourses
  178. * @return SSID of wireless network if connected, else null.
  179. */
  180. public static String getSSID(Context context) {
  181. String ssid = null;
  182. ConnectivityManager connManager = (ConnectivityManager) context
  183. .getSystemService(Context.CONNECTIVITY_SERVICE);
  184. NetworkInfo networkInfo = connManager
  185. .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  186. if (networkInfo != null && networkInfo.isConnected()) {
  187. final WifiManager wifiManager = (WifiManager) context
  188. .getSystemService(Context.WIFI_SERVICE);
  189. final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
  190. if (connectionInfo != null
  191. && !TextUtils.isEmpty(connectionInfo.getSSID())) {
  192. ssid = connectionInfo.getSSID();
  193. }
  194. }
  195. return ssid;
  196. }
  197. /**
  198. * Gets the mac address of the devicek.
  199. *
  200. * @param context
  201. * Needs a context to get system recourses
  202. * @return MAC address of the device.
  203. */
  204. public static String getMacAdress(Context context) {
  205. String mac = null;
  206. WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
  207. WifiInfo connectionInfo = wifiManager.getConnectionInfo();
  208. mac = connectionInfo.getMacAddress();
  209. return mac;
  210. }
  211. /**
  212. * Produces a random String. The String can be of random length (minimum 1)
  213. * with a maximum length, or it can be forced to have the length that was
  214. * given.
  215. *
  216. * @param length
  217. * maximal / forced length of String.
  218. * @param forceLength
  219. * forces the String to be exact the given length instead of
  220. * maximum
  221. * @return random String.
  222. */
  223. public static String getRandomString(int length, boolean forceLength) {
  224. SecureRandom rndm = new SecureRandom();
  225. char[] c = new char[forceLength ? length : rndm.nextInt(length - 1) + 1];
  226. for (int i = 0; i < c.length; i++) {
  227. c[i] = (char) (rndm.nextInt(95) + 32);
  228. }
  229. return new String(c);
  230. }
  231. /**
  232. * Converts a String into a byte array, e.g. "00, 01" to {0x00, 0x01}.
  233. *
  234. * @param string
  235. * that will be converted.
  236. * @return converted byte array.
  237. */
  238. public static byte[] hexStringToBytes(String string) {
  239. String[] hexStrings = string.split(", ");
  240. byte[] bytes = new byte[hexStrings.length];
  241. for (int j = 0; j < hexStrings.length; j++) {
  242. bytes[j] = (byte) ((Character.digit(hexStrings[j].charAt(0), 16) << 4) + Character
  243. .digit(hexStrings[j].charAt(1), 16));
  244. }
  245. return bytes;
  246. }
  247. /**
  248. * Checks if external storage is available for read and write.
  249. *
  250. * @return True if external storage is available for read and write, else
  251. * false.
  252. */
  253. public static boolean isExternalStorageWritable() {
  254. String state = Environment.getExternalStorageState();
  255. if (Environment.MEDIA_MOUNTED.equals(state)) {
  256. return true;
  257. }
  258. return false;
  259. }
  260. /**
  261. * Generates a random byte[] of a specified size
  262. *
  263. * @param size
  264. * of the byte[]
  265. * @return random byte[]
  266. */
  267. public static byte[] randomBytes(int size) {
  268. byte[] bytes = new byte[size];
  269. SecureRandom rdm = new SecureRandom();
  270. rdm.nextBytes(bytes);
  271. return bytes;
  272. }
  273. /**
  274. * Turns around the values of an byte[], e.g. {0x00, 0x01, 0x02} turns into
  275. * {0x02, 0x01, 0x00}.
  276. *
  277. * @param bytes
  278. * array that is turned.
  279. * @return turned array.
  280. */
  281. public static byte[] turnByteArray(byte[] bytes) {
  282. byte[] tmp = new byte[bytes.length];
  283. for (int i = 0; i < bytes.length; i++) {
  284. tmp[i] = bytes[bytes.length - 1 - i];
  285. }
  286. return tmp;
  287. }
  288. /**
  289. * Determines if a character is in ASCII between 32 and 126
  290. *
  291. * @param character
  292. * that is checked
  293. * @return true if the character is between 32 and 126, else false
  294. */
  295. private static boolean isLetter(char character) {
  296. return (character > 31 && character < 127);
  297. }
  298. private static byte[] unpackInetAddress(int bytes) {
  299. return new byte[] { (byte) ((bytes) & 0xff),
  300. (byte) ((bytes >>> 8) & 0xff), (byte) ((bytes >>> 16) & 0xff),
  301. (byte) ((bytes >>> 24) & 0xff) };
  302. }
  303. public static boolean isWifiConnected(Context context){
  304. if(context == null) return false;
  305. ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  306. NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  307. return mWifi.isConnected();
  308. }
  309. }