1010import java .util .ArrayList ;
1111
1212/**
13- * utilities class that has some helper methods
13+ * Utilities class that has some helper methods. Needs to be initialized by
14+ * calling Utils.init(...) before usage. Inside the Chart.init() method, this is
15+ * done, if the Utils are used before that, Utils.init(...) needs to be called
16+ * manually.
1417 *
1518 * @author Philipp Jahoda
1619 */
@@ -19,7 +22,7 @@ public abstract class Utils {
1922 private static Resources mRes ;
2023
2124 /**
22- * initialize method, called inside the Char .init() method.
25+ * initialize method, called inside the Chart .init() method.
2326 *
2427 * @param res
2528 */
@@ -51,27 +54,37 @@ public static String formatDecimal(double number, int digits) {
5154
5255 /**
5356 * This method converts dp unit to equivalent pixels, depending on device
54- * density.
57+ * density. NEEDS UTILS TO BE INITIALIZED BEFORE USAGE.
5558 *
5659 * @param dp A value in dp (density independent pixels) unit. Which we need
5760 * to convert into pixels
5861 * @return A float value to represent px equivalent to dp depending on
5962 * device density
6063 */
6164 public static float convertDpToPixel (float dp ) {
65+
66+ if (mRes == null )
67+ throw new IllegalStateException (
68+ "Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before calling Utils.convertDpToPixel(...)." );
69+
6270 DisplayMetrics metrics = mRes .getDisplayMetrics ();
6371 float px = dp * (metrics .densityDpi / 160f );
6472 return px ;
6573 }
6674
6775 /**
6876 * This method converts device specific pixels to density independent
69- * pixels.
77+ * pixels. NEEDS UTILS TO BE INITIALIZED BEFORE USAGE.
7078 *
7179 * @param px A value in px (pixels) unit. Which we need to convert into db
7280 * @return A float value to represent dp equivalent to px value
7381 */
7482 public static float convertPixelsToDp (float px ) {
83+
84+ if (mRes == null )
85+ throw new IllegalStateException (
86+ "Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before calling Utils.convertPixelsToDp(...)." );
87+
7588 DisplayMetrics metrics = mRes .getDisplayMetrics ();
7689 float dp = px / (metrics .densityDpi / 160f );
7790 return dp ;
@@ -88,7 +101,7 @@ public static float convertPixelsToDp(float px) {
88101 public static int calcTextWidth (Paint paint , String demoText ) {
89102 return (int ) paint .measureText (demoText );
90103 }
91-
104+
92105 /**
93106 * calculates the approximate height of a text, depending on a demo text
94107 * avoid repeated calls (e.g. inside drawing methods)
0 commit comments