Search in sources :

Example 16 with InflateException

use of android.view.InflateException in project android_frameworks_base by DirtyUnicorns.

the class AnimatorInflater method getPVH.

private static PropertyValuesHolder getPVH(TypedArray styledAttributes, int valueType, int valueFromId, int valueToId, String propertyName) {
    TypedValue tvFrom = styledAttributes.peekValue(valueFromId);
    boolean hasFrom = (tvFrom != null);
    int fromType = hasFrom ? tvFrom.type : 0;
    TypedValue tvTo = styledAttributes.peekValue(valueToId);
    boolean hasTo = (tvTo != null);
    int toType = hasTo ? tvTo.type : 0;
    if (valueType == VALUE_TYPE_UNDEFINED) {
        // Check whether it's color type. If not, fall back to default type (i.e. float type)
        if ((hasFrom && isColorType(fromType)) || (hasTo && isColorType(toType))) {
            valueType = VALUE_TYPE_COLOR;
        } else {
            valueType = VALUE_TYPE_FLOAT;
        }
    }
    boolean getFloats = (valueType == VALUE_TYPE_FLOAT);
    PropertyValuesHolder returnValue = null;
    if (valueType == VALUE_TYPE_PATH) {
        String fromString = styledAttributes.getString(valueFromId);
        String toString = styledAttributes.getString(valueToId);
        PathParser.PathData nodesFrom = fromString == null ? null : new PathParser.PathData(fromString);
        PathParser.PathData nodesTo = toString == null ? null : new PathParser.PathData(toString);
        if (nodesFrom != null || nodesTo != null) {
            if (nodesFrom != null) {
                TypeEvaluator evaluator = new PathDataEvaluator();
                if (nodesTo != null) {
                    if (!PathParser.canMorph(nodesFrom, nodesTo)) {
                        throw new InflateException(" Can't morph from " + fromString + " to " + toString);
                    }
                    returnValue = PropertyValuesHolder.ofObject(propertyName, evaluator, nodesFrom, nodesTo);
                } else {
                    returnValue = PropertyValuesHolder.ofObject(propertyName, evaluator, (Object) nodesFrom);
                }
            } else if (nodesTo != null) {
                TypeEvaluator evaluator = new PathDataEvaluator();
                returnValue = PropertyValuesHolder.ofObject(propertyName, evaluator, (Object) nodesTo);
            }
        }
    } else {
        TypeEvaluator evaluator = null;
        // Integer and float value types are handled here.
        if (valueType == VALUE_TYPE_COLOR) {
            // special case for colors: ignore valueType and get ints
            evaluator = ArgbEvaluator.getInstance();
        }
        if (getFloats) {
            float valueFrom;
            float valueTo;
            if (hasFrom) {
                if (fromType == TypedValue.TYPE_DIMENSION) {
                    valueFrom = styledAttributes.getDimension(valueFromId, 0f);
                } else {
                    valueFrom = styledAttributes.getFloat(valueFromId, 0f);
                }
                if (hasTo) {
                    if (toType == TypedValue.TYPE_DIMENSION) {
                        valueTo = styledAttributes.getDimension(valueToId, 0f);
                    } else {
                        valueTo = styledAttributes.getFloat(valueToId, 0f);
                    }
                    returnValue = PropertyValuesHolder.ofFloat(propertyName, valueFrom, valueTo);
                } else {
                    returnValue = PropertyValuesHolder.ofFloat(propertyName, valueFrom);
                }
            } else {
                if (toType == TypedValue.TYPE_DIMENSION) {
                    valueTo = styledAttributes.getDimension(valueToId, 0f);
                } else {
                    valueTo = styledAttributes.getFloat(valueToId, 0f);
                }
                returnValue = PropertyValuesHolder.ofFloat(propertyName, valueTo);
            }
        } else {
            int valueFrom;
            int valueTo;
            if (hasFrom) {
                if (fromType == TypedValue.TYPE_DIMENSION) {
                    valueFrom = (int) styledAttributes.getDimension(valueFromId, 0f);
                } else if (isColorType(fromType)) {
                    valueFrom = styledAttributes.getColor(valueFromId, 0);
                } else {
                    valueFrom = styledAttributes.getInt(valueFromId, 0);
                }
                if (hasTo) {
                    if (toType == TypedValue.TYPE_DIMENSION) {
                        valueTo = (int) styledAttributes.getDimension(valueToId, 0f);
                    } else if (isColorType(toType)) {
                        valueTo = styledAttributes.getColor(valueToId, 0);
                    } else {
                        valueTo = styledAttributes.getInt(valueToId, 0);
                    }
                    returnValue = PropertyValuesHolder.ofInt(propertyName, valueFrom, valueTo);
                } else {
                    returnValue = PropertyValuesHolder.ofInt(propertyName, valueFrom);
                }
            } else {
                if (hasTo) {
                    if (toType == TypedValue.TYPE_DIMENSION) {
                        valueTo = (int) styledAttributes.getDimension(valueToId, 0f);
                    } else if (isColorType(toType)) {
                        valueTo = styledAttributes.getColor(valueToId, 0);
                    } else {
                        valueTo = styledAttributes.getInt(valueToId, 0);
                    }
                    returnValue = PropertyValuesHolder.ofInt(propertyName, valueTo);
                }
            }
        }
        if (returnValue != null && evaluator != null) {
            returnValue.setEvaluator(evaluator);
        }
    }
    return returnValue;
}
Also used : PathParser(android.util.PathParser) InflateException(android.view.InflateException) TypedValue(android.util.TypedValue)

Example 17 with InflateException

use of android.view.InflateException in project android_frameworks_base by DirtyUnicorns.

the class SimpleInflater method inflate.

public void inflate(int menuRes) {
    XmlResourceParser parser = null;
    try {
        parser = mContext.getResources().getLayout(menuRes);
        AttributeSet attrs = Xml.asAttributeSet(parser);
        parseMenu(parser, attrs);
    } catch (XmlPullParserException e) {
        throw new InflateException("Error inflating menu XML", e);
    } catch (IOException e) {
        throw new InflateException("Error inflating menu XML", e);
    } finally {
        if (parser != null)
            parser.close();
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) InflateException(android.view.InflateException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 18 with InflateException

use of android.view.InflateException in project android_frameworks_base by DirtyUnicorns.

the class TransitionInflater method createCustom.

private Object createCustom(AttributeSet attrs, Class expectedType, String tag) {
    String className = attrs.getAttributeValue(null, "class");
    if (className == null) {
        throw new InflateException(tag + " tag must have a 'class' attribute");
    }
    try {
        synchronized (sConstructors) {
            Constructor constructor = sConstructors.get(className);
            if (constructor == null) {
                Class c = mContext.getClassLoader().loadClass(className).asSubclass(expectedType);
                if (c != null) {
                    constructor = c.getConstructor(sConstructorSignature);
                    constructor.setAccessible(true);
                    sConstructors.put(className, constructor);
                }
            }
            return constructor.newInstance(mContext, attrs);
        }
    } catch (InstantiationException e) {
        throw new InflateException("Could not instantiate " + expectedType + " class " + className, e);
    } catch (ClassNotFoundException e) {
        throw new InflateException("Could not instantiate " + expectedType + " class " + className, e);
    } catch (InvocationTargetException e) {
        throw new InflateException("Could not instantiate " + expectedType + " class " + className, e);
    } catch (NoSuchMethodException e) {
        throw new InflateException("Could not instantiate " + expectedType + " class " + className, e);
    } catch (IllegalAccessException e) {
        throw new InflateException("Could not instantiate " + expectedType + " class " + className, e);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) InflateException(android.view.InflateException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 19 with InflateException

use of android.view.InflateException in project android_frameworks_base by DirtyUnicorns.

the class DrawableInflater method inflateFromClass.

@NonNull
private Drawable inflateFromClass(@NonNull String className) {
    try {
        Constructor<? extends Drawable> constructor;
        synchronized (CONSTRUCTOR_MAP) {
            constructor = CONSTRUCTOR_MAP.get(className);
            if (constructor == null) {
                final Class<? extends Drawable> clazz = mClassLoader.loadClass(className).asSubclass(Drawable.class);
                constructor = clazz.getConstructor();
                CONSTRUCTOR_MAP.put(className, constructor);
            }
        }
        return constructor.newInstance();
    } catch (NoSuchMethodException e) {
        final InflateException ie = new InflateException("Error inflating class " + className);
        ie.initCause(e);
        throw ie;
    } catch (ClassCastException e) {
        // If loaded class is not a Drawable subclass.
        final InflateException ie = new InflateException("Class is not a Drawable " + className);
        ie.initCause(e);
        throw ie;
    } catch (ClassNotFoundException e) {
        // If loadClass fails, we should propagate the exception.
        final InflateException ie = new InflateException("Class not found " + className);
        ie.initCause(e);
        throw ie;
    } catch (Exception e) {
        final InflateException ie = new InflateException("Error inflating class " + className);
        ie.initCause(e);
        throw ie;
    }
}
Also used : InflateException(android.view.InflateException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) InflateException(android.view.InflateException) NonNull(android.annotation.NonNull)

Example 20 with InflateException

use of android.view.InflateException in project android_frameworks_base by AOSPA.

the class TransitionInflater method createTransitionFromXml.

//
// Transition loading
//
private Transition createTransitionFromXml(XmlPullParser parser, AttributeSet attrs, Transition parent) throws XmlPullParserException, IOException {
    Transition transition = null;
    // Make sure we are on a start tag.
    int type;
    int depth = parser.getDepth();
    TransitionSet transitionSet = (parent instanceof TransitionSet) ? (TransitionSet) parent : null;
    while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if ("fade".equals(name)) {
            transition = new Fade(mContext, attrs);
        } else if ("changeBounds".equals(name)) {
            transition = new ChangeBounds(mContext, attrs);
        } else if ("slide".equals(name)) {
            transition = new Slide(mContext, attrs);
        } else if ("explode".equals(name)) {
            transition = new Explode(mContext, attrs);
        } else if ("changeImageTransform".equals(name)) {
            transition = new ChangeImageTransform(mContext, attrs);
        } else if ("changeTransform".equals(name)) {
            transition = new ChangeTransform(mContext, attrs);
        } else if ("changeClipBounds".equals(name)) {
            transition = new ChangeClipBounds(mContext, attrs);
        } else if ("autoTransition".equals(name)) {
            transition = new AutoTransition(mContext, attrs);
        } else if ("recolor".equals(name)) {
            transition = new Recolor(mContext, attrs);
        } else if ("changeScroll".equals(name)) {
            transition = new ChangeScroll(mContext, attrs);
        } else if ("transitionSet".equals(name)) {
            transition = new TransitionSet(mContext, attrs);
        } else if ("transition".equals(name)) {
            transition = (Transition) createCustom(attrs, Transition.class, "transition");
        } else if ("targets".equals(name)) {
            getTargetIds(parser, attrs, parent);
        } else if ("arcMotion".equals(name)) {
            parent.setPathMotion(new ArcMotion(mContext, attrs));
        } else if ("pathMotion".equals(name)) {
            parent.setPathMotion((PathMotion) createCustom(attrs, PathMotion.class, "pathMotion"));
        } else if ("patternPathMotion".equals(name)) {
            parent.setPathMotion(new PatternPathMotion(mContext, attrs));
        } else {
            throw new RuntimeException("Unknown scene name: " + parser.getName());
        }
        if (transition != null) {
            if (!parser.isEmptyElementTag()) {
                createTransitionFromXml(parser, attrs, transition);
            }
            if (transitionSet != null) {
                transitionSet.addTransition(transition);
                transition = null;
            } else if (parent != null) {
                throw new InflateException("Could not add transition to another transition.");
            }
        }
    }
    return transition;
}
Also used : InflateException(android.view.InflateException)

Aggregations

InflateException (android.view.InflateException)100 IOException (java.io.IOException)51 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)48 AttributeSet (android.util.AttributeSet)30 XmlResourceParser (android.content.res.XmlResourceParser)19 Constructor (java.lang.reflect.Constructor)13 View (android.view.View)12 Path (android.graphics.Path)10 SuppressLint (android.annotation.SuppressLint)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 StringTokenizer (java.util.StringTokenizer)6 NonNull (android.annotation.NonNull)5 PathParser (android.util.PathParser)5 TypedValue (android.util.TypedValue)5 NonNull (android.support.annotation.NonNull)4 ExpandedMenuView (android.support.v7.internal.view.menu.ExpandedMenuView)4 ActionBarView (android.support.v7.internal.widget.ActionBarView)4 ViewGroup (android.view.ViewGroup)4 AlertDialog (android.app.AlertDialog)3 LayoutInflater (android.view.LayoutInflater)3