Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c078c605 authored by Alan Viverette's avatar Alan Viverette
Browse files

Attach drawable resource ID and name to getDrawable() exceptions

Wraps the entire getDrawable() method in a try/catch block. Clears the
stack trace from the re-thrown exception, since we only need the trace
from the original exception.

Also clears stack traces from re-thrown RuntimeExceptions in applyTheme
implementations.

Change-Id: I92396abf9e748eef78777174b297a09e118f5e70
parent c72b3101
Loading
Loading
Loading
Loading
+77 −59
Original line number Diff line number Diff line
@@ -2421,6 +2421,7 @@ public class Resources {

    @Nullable
    Drawable loadDrawable(TypedValue value, int id, Theme theme) throws NotFoundException {
        try {
            if (TRACE_FOR_PRELOAD) {
                // Log only framework resources
                if ((id >>> 24) == 0x1) {
@@ -2490,6 +2491,23 @@ public class Resources {
            }

            return dr;
        } catch (Exception e) {
            String name;
            try {
                name = getResourceName(id);
            } catch (NotFoundException e2) {
                name = "(missing name)";
            }

            // The target drawable might fail to load for any number of
            // reasons, but we always want to include the resource name.
            // Since the client already expects this method to throw a
            // NotFoundException, just throw one of those.
            final NotFoundException nfe = new NotFoundException("Drawable " + name
                    + " with resource ID #0x" + Integer.toHexString(id), e);
            nfe.setStackTrace(new StackTraceElement[0]);
            throw nfe;
        }
    }

    private void cacheDrawable(TypedValue value, boolean isColorDrawable, DrawableCache caches,
+1 −1
Original line number Diff line number Diff line
@@ -153,7 +153,7 @@ public class AnimatedRotateDrawable extends DrawableWrapper implements Animatabl
                updateStateFromTypedArray(a);
                verifyRequiredAttributes(a);
            } catch (XmlPullParserException e) {
                throw new RuntimeException(e);
                rethrowAsRuntimeException(e);
            } finally {
                a.recycle();
            }
+1 −1
Original line number Diff line number Diff line
@@ -829,7 +829,7 @@ public class BitmapDrawable extends Drawable {
            try {
                updateStateFromTypedArray(a);
            } catch (XmlPullParserException e) {
                throw new RuntimeException(e);
                rethrowAsRuntimeException(e);
            } finally {
                a.recycle();
            }
+1 −1
Original line number Diff line number Diff line
@@ -111,7 +111,7 @@ public class ClipDrawable extends DrawableWrapper {
                updateStateFromTypedArray(a);
                verifyRequiredAttributes(a);
            } catch (XmlPullParserException e) {
                throw new RuntimeException(e);
                rethrowAsRuntimeException(e);
            } finally {
                a.recycle();
            }
+14 −0
Original line number Diff line number Diff line
@@ -1429,6 +1429,20 @@ public abstract class Drawable {
        return densityDpi == 0 ? DisplayMetrics.DENSITY_DEFAULT : densityDpi;
    }

    /**
     * Re-throws an exception as a {@link RuntimeException} with an empty stack
     * trace to avoid cluttering the log. The original exception's stack trace
     * will still be included.
     *
     * @param cause the exception to re-throw
     * @throws RuntimeException
     */
    static void rethrowAsRuntimeException(Exception cause) throws RuntimeException {
        final RuntimeException e = new RuntimeException(cause);
        e.setStackTrace(new StackTraceElement[0]);
        throw e;
    }

    /**
     * Parses a {@link android.graphics.PorterDuff.Mode} from a tintMode
     * attribute's enum value.
Loading