| 1 |
/* |
| 2 |
* Copyright (C) 2014 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 |
* in compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 |
* or implied. See the License for the specific language governing permissions and limitations under |
| 12 |
* the License. |
| 13 |
*/ |
| 14 |
|
| 15 |
package ${packageName}; |
| 16 |
|
| 17 |
import android.content.Context; |
| 18 |
import android.graphics.Point; |
| 19 |
import android.view.Display; |
| 20 |
import android.view.WindowManager; |
| 21 |
import android.widget.Toast; |
| 22 |
|
| 23 |
/** |
| 24 |
* A collection of utility methods, all static. |
| 25 |
*/ |
| 26 |
public class Utils { |
| 27 |
|
| 28 |
/* |
| 29 |
* Making sure public utility methods remain static |
| 30 |
*/ |
| 31 |
private Utils() { |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns the screen/display size |
| 36 |
*/ |
| 37 |
public static Point getDisplaySize(Context context) { |
| 38 |
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
| 39 |
Display display = wm.getDefaultDisplay(); |
| 40 |
Point size = new Point(); |
| 41 |
display.getSize(size); |
| 42 |
return size; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Shows a (long) toast |
| 47 |
*/ |
| 48 |
public static void showToast(Context context, String msg) { |
| 49 |
Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Shows a (long) toast. |
| 54 |
*/ |
| 55 |
public static void showToast(Context context, int resourceId) { |
| 56 |
Toast.makeText(context, context.getString(resourceId), Toast.LENGTH_LONG).show(); |
| 57 |
} |
| 58 |
|
| 59 |
public static int convertDpToPixel(Context ctx, int dp) { |
| 60 |
float density = ctx.getResources().getDisplayMetrics().density; |
| 61 |
return Math.round((float) dp * density); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Formats time in milliseconds to hh:mm:ss string format. |
| 66 |
*/ |
| 67 |
public static String formatMillis(int millis) { |
| 68 |
String result = ""; |
| 69 |
int hr = millis / 3600000; |
| 70 |
millis %= 3600000; |
| 71 |
int min = millis / 60000; |
| 72 |
millis %= 60000; |
| 73 |
int sec = millis / 1000; |
| 74 |
if (hr > 0) { |
| 75 |
result += hr + ":"; |
| 76 |
} |
| 77 |
if (min >= 0) { |
| 78 |
if (min > 9) { |
| 79 |
result += min + ":"; |
| 80 |
} else { |
| 81 |
result += "0" + min + ":"; |
| 82 |
} |
| 83 |
} |
| 84 |
if (sec > 9) { |
| 85 |
result += sec; |
| 86 |
} else { |
| 87 |
result += "0" + sec; |
| 88 |
} |
| 89 |
return result; |
| 90 |
} |
| 91 |
} |