Search in sources :

Example 6 with AttributeSet

use of android.util.AttributeSet in project android_frameworks_base by ParanoidAndroid.

the class ColorStateList method createFromXml.

/**
     * Create a ColorStateList from an XML document, given a set of {@link Resources}.
     */
public static ColorStateList createFromXml(Resources r, XmlPullParser parser) throws XmlPullParserException, IOException {
    AttributeSet attrs = Xml.asAttributeSet(parser);
    int type;
    while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
    }
    if (type != XmlPullParser.START_TAG) {
        throw new XmlPullParserException("No start tag found");
    }
    return createFromXmlInner(r, parser, attrs);
}
Also used : AttributeSet(android.util.AttributeSet) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 7 with AttributeSet

use of android.util.AttributeSet in project android_frameworks_base by ParanoidAndroid.

the class PreferenceActivity method loadHeadersFromResource.

/**
     * Parse the given XML file as a header description, adding each
     * parsed Header into the target list.
     *
     * @param resid The XML resource to load and parse.
     * @param target The list in which the parsed headers should be placed.
     */
public void loadHeadersFromResource(int resid, List<Header> target) {
    XmlResourceParser parser = null;
    try {
        parser = getResources().getXml(resid);
        AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
        // Parse next until start tag is found
        }
        String nodeName = parser.getName();
        if (!"preference-headers".equals(nodeName)) {
            throw new RuntimeException("XML document must start with <preference-headers> tag; found" + nodeName + " at " + parser.getPositionDescription());
        }
        Bundle curBundle = null;
        final int outerDepth = parser.getDepth();
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                continue;
            }
            nodeName = parser.getName();
            if ("header".equals(nodeName)) {
                Header header = new Header();
                TypedArray sa = getResources().obtainAttributes(attrs, com.android.internal.R.styleable.PreferenceHeader);
                header.id = sa.getResourceId(com.android.internal.R.styleable.PreferenceHeader_id, (int) HEADER_ID_UNDEFINED);
                TypedValue tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_title);
                if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                    if (tv.resourceId != 0) {
                        header.titleRes = tv.resourceId;
                    } else {
                        header.title = tv.string;
                    }
                }
                tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_summary);
                if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                    if (tv.resourceId != 0) {
                        header.summaryRes = tv.resourceId;
                    } else {
                        header.summary = tv.string;
                    }
                }
                tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_breadCrumbTitle);
                if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                    if (tv.resourceId != 0) {
                        header.breadCrumbTitleRes = tv.resourceId;
                    } else {
                        header.breadCrumbTitle = tv.string;
                    }
                }
                tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_breadCrumbShortTitle);
                if (tv != null && tv.type == TypedValue.TYPE_STRING) {
                    if (tv.resourceId != 0) {
                        header.breadCrumbShortTitleRes = tv.resourceId;
                    } else {
                        header.breadCrumbShortTitle = tv.string;
                    }
                }
                header.iconRes = sa.getResourceId(com.android.internal.R.styleable.PreferenceHeader_icon, 0);
                header.fragment = sa.getString(com.android.internal.R.styleable.PreferenceHeader_fragment);
                sa.recycle();
                if (curBundle == null) {
                    curBundle = new Bundle();
                }
                final int innerDepth = parser.getDepth();
                while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
                    if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                        continue;
                    }
                    String innerNodeName = parser.getName();
                    if (innerNodeName.equals("extra")) {
                        getResources().parseBundleExtra("extra", attrs, curBundle);
                        XmlUtils.skipCurrentTag(parser);
                    } else if (innerNodeName.equals("intent")) {
                        header.intent = Intent.parseIntent(getResources(), parser, attrs);
                    } else {
                        XmlUtils.skipCurrentTag(parser);
                    }
                }
                if (curBundle.size() > 0) {
                    header.fragmentArguments = curBundle;
                    curBundle = null;
                }
                target.add(header);
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
        }
    } catch (XmlPullParserException e) {
        throw new RuntimeException("Error parsing headers", e);
    } catch (IOException e) {
        throw new RuntimeException("Error parsing headers", e);
    } finally {
        if (parser != null)
            parser.close();
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) Bundle(android.os.Bundle) TypedArray(android.content.res.TypedArray) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) TypedValue(android.util.TypedValue)

Example 8 with AttributeSet

use of android.util.AttributeSet in project android_frameworks_base by ParanoidAndroid.

the class GenericInflater method inflate.

/**
     * Inflate a new hierarchy from the specified XML node. Throws
     * InflaterException if there is an error.
     * <p>
     * <em><strong>Important</strong></em>&nbsp;&nbsp;&nbsp;For performance
     * reasons, inflation relies heavily on pre-processing of XML files
     * that is done at build time. Therefore, it is not currently possible to
     * use inflater with an XmlPullParser over a plain XML file at runtime.
     * 
     * @param parser XML dom node containing the description of the
     *        hierarchy.
     * @param root Optional to be the parent of the generated hierarchy (if
     *        <em>attachToRoot</em> is true), or else simply an object that
     *        provides a set of values for root of the returned
     *        hierarchy (if <em>attachToRoot</em> is false.)
     * @param attachToRoot Whether the inflated hierarchy should be attached to
     *        the root parameter?
     * @return The root of the inflated hierarchy. If root was supplied and
     *         attachToRoot is true, this is root; otherwise it is the root of
     *         the inflated XML file.
     */
public T inflate(XmlPullParser parser, P root, boolean attachToRoot) {
    synchronized (mConstructorArgs) {
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        mConstructorArgs[0] = mContext;
        T result = (T) root;
        try {
            // Look for the root node.
            int type;
            while ((type = parser.next()) != parser.START_TAG && type != parser.END_DOCUMENT) {
                ;
            }
            if (type != parser.START_TAG) {
                throw new InflateException(parser.getPositionDescription() + ": No start tag found!");
            }
            if (DEBUG) {
                System.out.println("**************************");
                System.out.println("Creating root: " + parser.getName());
                System.out.println("**************************");
            }
            // Temp is the root that was found in the xml
            T xmlRoot = createItemFromTag(parser, parser.getName(), attrs);
            result = (T) onMergeRoots(root, attachToRoot, (P) xmlRoot);
            if (DEBUG) {
                System.out.println("-----> start inflating children");
            }
            // Inflate all children under temp
            rInflate(parser, result, attrs);
            if (DEBUG) {
                System.out.println("-----> done inflating children");
            }
        } catch (InflateException e) {
            throw e;
        } catch (XmlPullParserException e) {
            InflateException ex = new InflateException(e.getMessage());
            ex.initCause(e);
            throw ex;
        } catch (IOException e) {
            InflateException ex = new InflateException(parser.getPositionDescription() + ": " + e.getMessage());
            ex.initCause(e);
            throw ex;
        }
        return result;
    }
}
Also used : AttributeSet(android.util.AttributeSet) InflateException(android.view.InflateException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 9 with AttributeSet

use of android.util.AttributeSet in project android_frameworks_base by ParanoidAndroid.

the class LayoutInflater method parseInclude.

private void parseInclude(XmlPullParser parser, View parent, AttributeSet attrs) throws XmlPullParserException, IOException {
    int type;
    if (parent instanceof ViewGroup) {
        final int layout = attrs.getAttributeResourceValue(null, "layout", 0);
        if (layout == 0) {
            final String value = attrs.getAttributeValue(null, "layout");
            if (value == null) {
                throw new InflateException("You must specifiy a layout in the" + " include tag: <include layout=\"@layout/layoutID\" />");
            } else {
                throw new InflateException("You must specifiy a valid layout " + "reference. The layout ID " + value + " is not valid.");
            }
        } else {
            final XmlResourceParser childParser = getContext().getResources().getLayout(layout);
            try {
                final AttributeSet childAttrs = Xml.asAttributeSet(childParser);
                while ((type = childParser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
                // Empty.
                }
                if (type != XmlPullParser.START_TAG) {
                    throw new InflateException(childParser.getPositionDescription() + ": No start tag found!");
                }
                final String childName = childParser.getName();
                if (TAG_MERGE.equals(childName)) {
                    // Inflate all children.
                    rInflate(childParser, parent, childAttrs, false);
                } else {
                    final View view = createViewFromTag(parent, childName, childAttrs);
                    final ViewGroup group = (ViewGroup) parent;
                    // We try to load the layout params set in the <include /> tag. If
                    // they don't exist, we will rely on the layout params set in the
                    // included XML file.
                    // During a layoutparams generation, a runtime exception is thrown
                    // if either layout_width or layout_height is missing. We catch
                    // this exception and set localParams accordingly: true means we
                    // successfully loaded layout params from the <include /> tag,
                    // false means we need to rely on the included layout params.
                    ViewGroup.LayoutParams params = null;
                    try {
                        params = group.generateLayoutParams(attrs);
                    } catch (RuntimeException e) {
                        params = group.generateLayoutParams(childAttrs);
                    } finally {
                        if (params != null) {
                            view.setLayoutParams(params);
                        }
                    }
                    // Inflate all children.
                    rInflate(childParser, view, childAttrs, true);
                    // Attempt to override the included layout's android:id with the
                    // one set on the <include /> tag itself.
                    TypedArray a = mContext.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View, 0, 0);
                    int id = a.getResourceId(com.android.internal.R.styleable.View_id, View.NO_ID);
                    // While we're at it, let's try to override android:visibility.
                    int visibility = a.getInt(com.android.internal.R.styleable.View_visibility, -1);
                    a.recycle();
                    if (id != View.NO_ID) {
                        view.setId(id);
                    }
                    switch(visibility) {
                        case 0:
                            view.setVisibility(View.VISIBLE);
                            break;
                        case 1:
                            view.setVisibility(View.INVISIBLE);
                            break;
                        case 2:
                            view.setVisibility(View.GONE);
                            break;
                    }
                    group.addView(view);
                }
            } finally {
                childParser.close();
            }
        }
    } else {
        throw new InflateException("<include /> can only be used inside of a ViewGroup");
    }
    final int currentDepth = parser.getDepth();
    while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT) {
    // Empty
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) TypedArray(android.content.res.TypedArray)

Example 10 with AttributeSet

use of android.util.AttributeSet in project android_frameworks_base by ParanoidAndroid.

the class AnimationUtils method createInterpolatorFromXml.

private static Interpolator createInterpolatorFromXml(Context c, XmlPullParser parser) throws XmlPullParserException, IOException {
    Interpolator interpolator = null;
    // Make sure we are on a start tag.
    int type;
    int depth = parser.getDepth();
    while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        AttributeSet attrs = Xml.asAttributeSet(parser);
        String name = parser.getName();
        if (name.equals("linearInterpolator")) {
            interpolator = new LinearInterpolator(c, attrs);
        } else if (name.equals("accelerateInterpolator")) {
            interpolator = new AccelerateInterpolator(c, attrs);
        } else if (name.equals("decelerateInterpolator")) {
            interpolator = new DecelerateInterpolator(c, attrs);
        } else if (name.equals("accelerateDecelerateInterpolator")) {
            interpolator = new AccelerateDecelerateInterpolator(c, attrs);
        } else if (name.equals("cycleInterpolator")) {
            interpolator = new CycleInterpolator(c, attrs);
        } else if (name.equals("anticipateInterpolator")) {
            interpolator = new AnticipateInterpolator(c, attrs);
        } else if (name.equals("overshootInterpolator")) {
            interpolator = new OvershootInterpolator(c, attrs);
        } else if (name.equals("anticipateOvershootInterpolator")) {
            interpolator = new AnticipateOvershootInterpolator(c, attrs);
        } else if (name.equals("bounceInterpolator")) {
            interpolator = new BounceInterpolator(c, attrs);
        } else {
            throw new RuntimeException("Unknown interpolator name: " + parser.getName());
        }
    }
    return interpolator;
}
Also used : AttributeSet(android.util.AttributeSet)

Aggregations

AttributeSet (android.util.AttributeSet)262 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)160 IOException (java.io.IOException)125 XmlResourceParser (android.content.res.XmlResourceParser)113 TypedArray (android.content.res.TypedArray)78 Resources (android.content.res.Resources)46 Test (org.junit.Test)42 TypedValue (android.util.TypedValue)34 InflateException (android.view.InflateException)28 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)24 PackageManager (android.content.pm.PackageManager)22 Context (android.content.Context)20 ComponentName (android.content.ComponentName)18 XmlPullParser (org.xmlpull.v1.XmlPullParser)17 Intent (android.content.Intent)11 RemoteException (android.os.RemoteException)11 Bundle (android.os.Bundle)9 ArrayList (java.util.ArrayList)9 ActivityInfo (android.content.pm.ActivityInfo)7 ApplicationInfo (android.content.pm.ApplicationInfo)7