Utils.java (2968B)
1 package de.nordgedanken.rpicms; 2 3 import android.app.AlertDialog; 4 import android.content.Context; 5 import android.content.DialogInterface; 6 import android.graphics.Point; 7 import android.view.Display; 8 import android.view.WindowManager; 9 import android.widget.Toast; 10 11 /** 12 * A collection of utility methods, all static. 13 */ 14 public class Utils { 15 16 /* 17 * Making sure public utility methods remain static 18 */ 19 private Utils() { 20 } 21 22 /** 23 * Returns the screen/display size 24 * 25 * @param context 26 * @return 27 */ 28 public static Point getDisplaySize(Context context) { 29 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 30 Display display = wm.getDefaultDisplay(); 31 Point size = new Point(); 32 display.getSize(size); 33 int width = size.x; 34 int height = size.y; 35 return new Point(width, height); 36 } 37 38 /** 39 * Shows an error dialog with a given text message. 40 * 41 * @param context 42 * @param errorString 43 */ 44 45 public static final void showErrorDialog(Context context, String errorString) { 46 new AlertDialog.Builder(context).setTitle(R.string.error) 47 .setMessage(errorString) 48 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 49 @Override 50 public void onClick(DialogInterface dialog, int id) { 51 dialog.cancel(); 52 } 53 }) 54 .create() 55 .show(); 56 } 57 58 /** 59 * Shows a (long) toast 60 * 61 * @param context 62 * @param msg 63 */ 64 public static void showToast(Context context, String msg) { 65 Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); 66 } 67 68 /** 69 * Shows a (long) toast. 70 * 71 * @param context 72 * @param resourceId 73 */ 74 public static void showToast(Context context, int resourceId) { 75 Toast.makeText(context, context.getString(resourceId), Toast.LENGTH_LONG).show(); 76 } 77 78 /** 79 * Formats time in milliseconds to hh:mm:ss string format. 80 * 81 * @param millis 82 * @return 83 */ 84 public static String formatMillis(int millis) { 85 String result = ""; 86 int hr = millis / 3600000; 87 millis %= 3600000; 88 int min = millis / 60000; 89 millis %= 60000; 90 int sec = millis / 1000; 91 if (hr > 0) { 92 result += hr + ":"; 93 } 94 if (min >= 0) { 95 if (min > 9) { 96 result += min + ":"; 97 } else { 98 result += "0" + min + ":"; 99 } 100 } 101 if (sec > 9) { 102 result += sec; 103 } else { 104 result += "0" + sec; 105 } 106 return result; 107 } 108 109 public static int dpToPx(int dp, Context ctx) { 110 float density = ctx.getResources().getDisplayMetrics().density; 111 return Math.round((float) dp * density); 112 } 113 }