Skip to content

Instantly share code, notes, and snippets.

@akmalxxx
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akmalxxx/4f3d7d042eed86c49741 to your computer and use it in GitHub Desktop.
Save akmalxxx/4f3d7d042eed86c49741 to your computer and use it in GitHub Desktop.
/* Storage access check */
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) return true;
return false;
}
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) return true;
return false;
}
/* Set activity fullscreen, must be call first inside oncreate */
public static void setFullScreen(Context context) {
((Activity)context).requestWindowFeature(Window.FEATURE_NO_TITLE);
((Activity)context).getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
/* get version integer code from manifest */
public static int getVersionCode(Context context) {
PackageInfo pi;
try {
pi= context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
} catch(Exception e){ return 1; }
return pi.versionCode;
}
/* Check if service is running on the background */
public static boolean isServiceRunning(Context context, Class c) { //MyClass.class
String name = c.getName();
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (name.equals(service.service.getClassName())) return true;
}
return false;
}
/* Play notification tone */
public static void playNotify(Context context) {
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
} catch (Exception e) {}
}
/* vibrate */
public static void vibrate(Context context) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(300);
}
/* Volley cancel request, use in stop/pause */
public static void volleyCancelRequest(RequestQueue requestQueue, Object tag) {
if (tag != null) {
requestQueue.cancelAll(tag);
} else { //cancel any request running
requestQueue.cancelAll(new RequestQueue.RequestFilter() {
@Override
public boolean apply(Request<?> request) { return true; }
});
}
}
/* layout inflate shortcut */
public static View inflateLayout(Context context, ViewGroup parent, int layoutXml, boolean isAttach) {
LayoutInflater inflater = LayoutInflater.from(context);
if (parent == null) return (View) inflater.inflate(layoutXml, null);
return (View) inflater.inflate(layoutXml, parent, isAttach);
}
/* get child views of a viewgroup */
public static List<View> getChildViews(ViewGroup parent) {
List<View> views = new ArrayList<View>();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) views.add(parent.getChildAt(i));
return views;
}
/* get view screenshot */
public static Bitmap getBitmapFromView(View view) {
boolean willNotCacheDrawing = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
view.destroyDrawingCache();
view.setDrawingCacheEnabled(false);
view.setWillNotCacheDrawing(willNotCacheDrawing);
return bitmap;
}
/* toggle show/hide keyboard */
public static void toggleKeyboard(Context context, View view, boolean isShow) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (isShow) {
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
} else {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
/* simulate physical touch on view */
public static void invokeTouch(final View view) {
(new Handler()).postDelayed(new Runnable() {
public void run() {
view.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
view.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));
}
}, 200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment