Last active
March 15, 2016 07:13
-
-
Save uniruddh/7f0f34165204a551f72a to your computer and use it in GitHub Desktop.
Helper class for most important and basic methods for any Android App.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Utils { | |
/* | |
* Usage: Check if device have internet connection or not, before communicating with server. | |
* @param ctx: Context form which this method will be called | |
* @return Boolean: If internet is available or not. | |
*/ | |
public static boolean isNetConnected(Context ctx) { | |
Runtime runtime = Runtime.getRuntime(); | |
try { | |
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8"); | |
int exitValue = ipProcess.waitFor(); | |
return (exitValue == 0); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return false; | |
} | |
/* | |
* Usage: Convert a Byte array to Hexa decimal String | |
* @param bytes: Input byte array | |
* @return String: Hexadecimal string | |
*/ | |
public static String bytesArrayToHexString(byte[] bytes) { | |
char[] hexChars = new char[bytes.length * 2]; | |
for (int j = 0; j < bytes.length; j++) { | |
int v = bytes[j] & 0xFF; | |
hexChars[j * 2] = hexArray[v >>> 4]; | |
hexChars[j * 2 + 1] = hexArray[v & 0x0F]; | |
} | |
return new String(hexChars); | |
} | |
/* | |
* Usage: Get MD5 representation of any String | |
* @param data: Input String | |
* @return String: MD5 string | |
*/ | |
public static String getMd5(String data) { | |
try { | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
return bytesArrayToHexString(md.digest(data.getBytes("UTF-8"))); | |
} catch (UnsupportedEncodingException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (NoSuchAlgorithmException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
/* | |
* Usage: Get Bitmap for given File | |
* @param file: Input File | |
* @return Bitmap: Bitmap image | |
*/ | |
public static Bitmap getBitmapFromFile(File file) { | |
try { | |
if (!file.exists()) { | |
return null; | |
} | |
return BitmapFactory.decodeFile(file.getPath()); | |
} catch (Exception e) { | |
return null; | |
} | |
} | |
/* | |
* Usage: Convert String to Base64 String | |
* @param input: Input String | |
* @return String: Base64 representation of String | |
*/ | |
public static String encodeBase64(String input) { | |
byte[] data = new byte[0]; | |
try { | |
data = input.getBytes("UTF-8"); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
return Base64.encodeToString(data, Base64.NO_PADDING); | |
} | |
/* | |
* Usage: Decode Base64 String to String | |
* @param input: Input Base64 version of String | |
* @return String: String decoded form Base64 | |
*/ | |
public static String decodeBase64(String input) { | |
byte[] data = Base64.decode(input, Base64.NO_PADDING); | |
try { | |
return new String(data, "UTF-8"); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
/* | |
* Usage: To check whether device have removable external storage | |
* @return Boolean: if device has removable external storage or not | |
*/ | |
public static boolean isExternalStorageRemovable() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { | |
return Environment.isExternalStorageRemovable(); | |
} | |
return true; | |
} | |
/* | |
* Usage: Check whether device is connected to wifi or not | |
* @param ctx: Context of calling method | |
* @return Boolean: true if device is connected to wifi, else false | |
*/ | |
public static boolean isWifiConnected(Context ctx) { | |
ConnectivityManager connManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo activeNetwork = connManager.getActiveNetworkInfo(); | |
if (activeNetwork != null) { // connected to the internet | |
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) { | |
// Connected to wifi | |
return true; | |
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { | |
// Connected to the mobile provider's data plan | |
return false; | |
} | |
} else { | |
// Not connected to the internet | |
return false; | |
} | |
return false; | |
} | |
/* | |
* Usage: Generate random number of given length | |
* @param length: length of required random number | |
* @return String: Random number in String type of input length | |
*/ | |
public static String generateRandomNumber(int length) { | |
Random random = new Random(); | |
char[] digits = new char[length]; | |
digits[0] = (char) (random.nextInt(9) + '1'); | |
for (int i = 1; i < length; i++) { | |
digits[i] = (char) (random.nextInt(10) + '0'); | |
} | |
return new String(digits); | |
} | |
/* | |
* Usage: Check whether your service is running or not | |
* @param ctx: Context of calling method | |
* @param serviceClass: Class of service which needs to be checked | |
* @return Boolean: true if given service class is running, else false | |
*/ | |
public static boolean isMyServiceRunning(Context ctx, Class<?> serviceClass) { | |
ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); | |
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { | |
if (serviceClass.getName().equals(service.service.getClassName())) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/* | |
* Usage: To find nth occurrence of a character in given String | |
* @param str: input String in which to find character occurrence | |
* @param chars: characters to be searched in String | |
* @param n: required nth occurrence | |
* @return Int: Position of nth occurrence of chars in input String | |
*/ | |
public static int nthOccurrence(String str, String chars, int n) { | |
int pos = str.indexOf(chars, 0); | |
while (n-- > 0 && pos != -1) | |
pos = str.indexOf(chars, pos + 1); | |
return pos; | |
} | |
/* | |
* Usage: Show Toast message to user. | |
* @param ctx: Context of calling method | |
* @param msg: Message to be displayed in Toast. | |
*/ | |
public static void showToast(Context ctx, String msg) { | |
Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show(); | |
} | |
/** | |
* Usage: Check whether device is tablet or phone | |
* @param context: Context of Calling method | |
* @return Boolean: true if device is tablet, else false | |
*/ | |
public static boolean isTablet(Context context) { | |
boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4); | |
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE); | |
return (xlarge || large); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment