halfhp / androidplot

Charts and plots for Android
http://androidplot.com
Apache License 2.0
505 stars 159 forks source link

Android Plot crashes when setting color from resource id #88

Open planetmarshall opened 5 years ago

planetmarshall commented 5 years ago

Summary

Applying a style where a color is specified as a resource ID causes an App using Android Plot to crash.

Expected Results

With a style resource such as

<config
linePaint.color="@color/accent_blue"
/>

Applying the style in a formatter should apply the specified color resource.

Actual Results

Applying the style in a formatter causes the app to crash.

Steps to Reproduce

  1. Define a color resource
  2. Define a style xml resource as above referring to the color resource
  3. Apply the style resource using
        LineAndPointFormatter profileFormatter = new LineAndPointFormatter(this, R.xml.style);

Reproduced in

Androidplot 1.5.6 Google Pixel with Android 9 (API Level 28) Compile SDK Version 26 Android Build tools 28.0.2

Stacktrace

 Caused by: java.lang.IllegalArgumentException: Unknown color
        at android.graphics.Color.parseColor(Color.java:1400)
        at com.halfhp.fig.Fig.parseIntAttr(Fig.java:58)
        at com.halfhp.fig.Fig.inflateParams(Fig.java:163)
        at com.halfhp.fig.Fig.configure(Fig.java:283)
        at com.halfhp.fig.Fig.configure(Fig.java:208)
        at com.halfhp.fig.Fig.configure(Fig.java:234)
        at com.halfhp.fig.Fig.configure(Fig.java:253)
        at com.androidplot.ui.Formatter.configure(Formatter.java:52)

Notes

The issue appears to be caused by the following code in Fig.java

private static int parseIntAttr(String value) {
        if (Character.isDigit(value.charAt(0))) {
            return Integer.parseInt(value);
        } else if (value.startsWith(RESOURCE_ID_PREFIX)) {
            // it's a resId
            return Color.parseColor(value);
      ...

However, the Android Color.parseColor function does not accept resource IDs:

https://developer.android.com/reference/android/graphics/Color#parseColor(java.lang.String)

halfhp commented 5 years ago

Thanks for the report - will investigate. Fig does in fact support the use of resource id's so off the top of my head I'm not sure where things are breaking here.

halfhp commented 5 years ago

Found the bug in Fig. Expect to have it fixed for the 1.5.8 release.