package de.tudarmstadt.informatik.hostage.commons; import java.net.InetAddress; import java.net.UnknownHostException; import java.security.SecureRandom; import android.content.Context; 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 { /** * Converts a byte array into a hexadecimal String, e.g. {0x00, 0x01} to * "00, 01". * * @param bytes * that will be converted. * @return converted String. */ public static String bytesToHexString(byte[] bytes) { char[] hexArray = "0123456789ABCDEF".toCharArray(); int v; StringBuffer buffer = new StringBuffer(); for (int j = 0; j < bytes.length; j++) { v = bytes[j] & 0xFF; buffer.append(hexArray[v >>> 4]); buffer.append(hexArray[v & 0x0F]); if (j < bytes.length - 1) buffer.append(", "); } return buffer.toString(); } /** * Converts a byte[] to a String, but only characters in ASCII between 32 * and 127 * * @param bytes * that are converted * @return converted String */ public static String byteToStr(byte[] bytes) { int size = 0; for(byte b : bytes) { if(isLetter((char) b)) { size++; } } char[] chars = new char[size]; for (int i = 0, j = 0; i < bytes.length && j < size; i++) { if (isLetter((char) bytes[i])) { chars[j] = (char) bytes[i]; j++; } } return new String(chars); } /** * 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) if (b != null) newSize += b.length; byte[] dst = new byte[newSize]; int currentPos = 0; int newPos; for (byte[] b : bytes) { if (b != null) { newPos = b.length; System.arraycopy(b, 0, dst, currentPos, newPos); currentPos += newPos; } } return dst; } /** * Puts a 0x00 byte between each byte in a byte array. * * @param bytes * that need to be filled with 0x00. * @return filled byte array. */ public static byte[] fillWithZero(byte[] bytes) { byte[] newBytes = new byte[(bytes.length * 2)]; for (int i = 0, j = 0; i < bytes.length && j < newBytes.length; i++, j = j + 2) { newBytes[j] = bytes[i]; newBytes[j + 1] = 0x00; } return newBytes; } /** * Puts a 0x00 byte between each byte and another 2 0x00 bytes at the end of * a byte array. * * @param bytes * that need to be filled with 0x00. * @return filled byte array. */ public static byte[] fillWithZeroExtended(byte[] bytes) { byte[] zeroBytes = fillWithZero(bytes); byte[] newBytes = new byte[zeroBytes.length + 2]; newBytes = HelperUtils.concat(zeroBytes, new byte[] { 0x00, 0x00 }); return newBytes; } /** * 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; } /** * 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 the mac address of the devicek. * * @param context * Needs a context to get system recourses * @return MAC address of the device. */ public static String getMacAdress(Context context) { String mac = null; WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo connectionInfo = wifiManager.getConnectionInfo(); mac = connectionInfo.getMacAddress(); return mac; } /** * Produces a random String. The String can be of random length (minimum 1) * with a maximum length, or it can be forced to have the length that was * given. * * @param length * maximal / forced length of String. * @param forceLength * forces the String to be exact the given length instead of * maximum * @return random String. */ public static String getRandomString(int length, boolean forceLength) { SecureRandom rndm = new SecureRandom(); char[] c = new char[forceLength ? length : rndm.nextInt(length - 1) + 1]; for (int i = 0; i < c.length; i++) { c[i] = (char) (rndm.nextInt(95) + 32); } return new String(c); } /** * Converts a String into a byte array, e.g. "00, 01" to {0x00, 0x01}. * * @param string * that will be converted. * @return converted byte array. */ public static byte[] hexStringToBytes(String string) { String[] hexStrings = string.split(", "); byte[] bytes = new byte[hexStrings.length]; for (int j = 0; j < hexStrings.length; j++) { bytes[j] = (byte) ((Character.digit(hexStrings[j].charAt(0), 16) << 4) + Character .digit(hexStrings[j].charAt(1), 16)); } return bytes; } /** * 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; } /** * Generates a random byte[] of a specified size * * @param size * of the byte[] * @return random byte[] */ public static byte[] randomBytes(int size) { byte[] bytes = new byte[size]; SecureRandom rdm = new SecureRandom(); rdm.nextBytes(bytes); return bytes; } /** * Turns around the values of an byte[], e.g. {0x00, 0x01, 0x02} turns into * {0x02, 0x01, 0x00}. * * @param bytes * array that is turned. * @return turned array. */ public static byte[] turnByteArray(byte[] bytes) { byte[] tmp = new byte[bytes.length]; for (int i = 0; i < bytes.length; i++) { tmp[i] = bytes[bytes.length - 1 - i]; } return tmp; } /** * Determines if a character is in ASCII between 32 and 126 * * @param character * that is checked * @return true if the character is between 32 and 126, else false */ private static boolean isLetter(char character) { return (character > 31 && character < 127); } private static byte[] unpackInetAddress(int bytes) { return new byte[] { (byte) ((bytes) & 0xff), (byte) ((bytes >>> 8) & 0xff), (byte) ((bytes >>> 16) & 0xff), (byte) ((bytes >>> 24) & 0xff) }; } public static boolean isWifiConnected(Context context){ if(context == null) return false; ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); return mWifi.isConnected(); } }