Search in sources :

Example 66 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by ResurrectionRemix.

the class DaylightHeaderProvider method loadHeaders.

private void loadHeaders() throws XmlPullParserException, IOException {
    mHeadersList = new ArrayList<DaylightHeaderInfo>();
    InputStream in = null;
    XmlPullParser parser = null;
    try {
        if (mHeaderName == null) {
            if (DEBUG)
                Log.i(TAG, "Load header pack config daylight_header.xml");
            in = mRes.getAssets().open("daylight_header.xml");
        } else {
            int idx = mHeaderName.lastIndexOf(".");
            String headerConfigFile = mHeaderName.substring(idx + 1) + ".xml";
            if (DEBUG)
                Log.i(TAG, "Load header pack config " + headerConfigFile);
            in = mRes.getAssets().open(headerConfigFile);
        }
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        parser = factory.newPullParser();
        parser.setInput(in, "UTF-8");
        loadResourcesFromXmlParser(parser);
    } finally {
        // Cleanup resources
        if (parser instanceof XmlResourceParser) {
            ((XmlResourceParser) parser).close();
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) InputStream(java.io.InputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) IOException(java.io.IOException)

Example 67 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by ResurrectionRemix.

the class UsbSettingsManager method handlePackageUpdateLocked.

private boolean handlePackageUpdateLocked(String packageName, ActivityInfo aInfo, String metaDataName) {
    XmlResourceParser parser = null;
    boolean changed = false;
    try {
        parser = aInfo.loadXmlMetaData(mPackageManager, metaDataName);
        if (parser == null)
            return false;
        XmlUtils.nextElement(parser);
        while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
            String tagName = parser.getName();
            if ("usb-device".equals(tagName)) {
                DeviceFilter filter = DeviceFilter.read(parser);
                if (clearCompatibleMatchesLocked(packageName, filter)) {
                    changed = true;
                }
            } else if ("usb-accessory".equals(tagName)) {
                AccessoryFilter filter = AccessoryFilter.read(parser);
                if (clearCompatibleMatchesLocked(packageName, filter)) {
                    changed = true;
                }
            }
            XmlUtils.nextElement(parser);
        }
    } catch (Exception e) {
        Slog.w(TAG, "Unable to load component info " + aInfo.toString(), e);
    } finally {
        if (parser != null)
            parser.close();
    }
    return changed;
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ActivityNotFoundException(android.content.ActivityNotFoundException)

Example 68 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by ResurrectionRemix.

the class UsbSettingsManager method packageMatchesLocked.

// Checks to see if a package matches a device or accessory.
// Only one of device and accessory should be non-null.
private boolean packageMatchesLocked(ResolveInfo info, String metaDataName, UsbDevice device, UsbAccessory accessory) {
    ActivityInfo ai = info.activityInfo;
    XmlResourceParser parser = null;
    try {
        parser = ai.loadXmlMetaData(mPackageManager, metaDataName);
        if (parser == null) {
            Slog.w(TAG, "no meta-data for " + info);
            return false;
        }
        XmlUtils.nextElement(parser);
        while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
            String tagName = parser.getName();
            if (device != null && "usb-device".equals(tagName)) {
                DeviceFilter filter = DeviceFilter.read(parser);
                if (filter.matches(device)) {
                    return true;
                }
            } else if (accessory != null && "usb-accessory".equals(tagName)) {
                AccessoryFilter filter = AccessoryFilter.read(parser);
                if (filter.matches(accessory)) {
                    return true;
                }
            }
            XmlUtils.nextElement(parser);
        }
    } catch (Exception e) {
        Slog.w(TAG, "Unable to load component info " + info.toString(), e);
    } finally {
        if (parser != null)
            parser.close();
    }
    return false;
}
Also used : ActivityInfo(android.content.pm.ActivityInfo) XmlResourceParser(android.content.res.XmlResourceParser) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ActivityNotFoundException(android.content.ActivityNotFoundException)

Example 69 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by ResurrectionRemix.

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 70 with XmlResourceParser

use of android.content.res.XmlResourceParser in project android_frameworks_base by ResurrectionRemix.

the class LayoutInflater_Delegate method parseInclude.

@LayoutlibDelegate
public static void parseInclude(LayoutInflater thisInflater, XmlPullParser parser, Context context, View parent, AttributeSet attrs) throws XmlPullParserException, IOException {
    int type;
    if (parent instanceof ViewGroup) {
        // Apply a theme wrapper, if requested. This is sort of a weird
        // edge case, since developers think the <include> overwrites
        // values in the AttributeSet of the included View. So, if the
        // included View has a theme attribute, we'll need to ignore it.
        final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
        final int themeResId = ta.getResourceId(0, 0);
        final boolean hasThemeOverride = themeResId != 0;
        if (hasThemeOverride) {
            context = new ContextThemeWrapper(context, themeResId);
        }
        ta.recycle();
        // If the layout is pointing to a theme attribute, we have to
        // massage the value to get a resource identifier out of it.
        int layout = attrs.getAttributeResourceValue(null, ATTR_LAYOUT, 0);
        if (layout == 0) {
            final String value = attrs.getAttributeValue(null, ATTR_LAYOUT);
            if (value == null || value.length() <= 0) {
                throw new InflateException("You must specify a layout in the" + " include tag: <include layout=\"@layout/layoutID\" />");
            }
            // Attempt to resolve the "?attr/name" string to an identifier.
            layout = context.getResources().getIdentifier(value.substring(1), null, null);
        }
        // ---- START CHANGES
        if (layout != 0) {
            final TypedValue tempValue = new TypedValue();
            if (context.getTheme().resolveAttribute(layout, tempValue, true)) {
                layout = tempValue.resourceId;
            }
        }
        if (layout == 0) {
            final String value = attrs.getAttributeValue(null, ATTR_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 = thisInflater.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.
                    thisInflater.rInflate(childParser, parent, context, childAttrs, false);
                } else {
                    final View view = thisInflater.createViewFromTag(parent, childName, context, childAttrs, hasThemeOverride);
                    final ViewGroup group = (ViewGroup) parent;
                    final TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Include);
                    final int id = a.getResourceId(com.android.internal.R.styleable.Include_id, View.NO_ID);
                    final int visibility = a.getInt(com.android.internal.R.styleable.Include_visibility, -1);
                    a.recycle();
                    // 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 {
                        // ---- START CHANGES
                        sIsInInclude = true;
                        // ---- END CHANGES
                        params = group.generateLayoutParams(attrs);
                    } catch (RuntimeException ignored) {
                    // Ignore, just fail over to child attrs.
                    } finally {
                        // ---- START CHANGES
                        sIsInInclude = false;
                    // ---- END CHANGES
                    }
                    if (params == null) {
                        params = group.generateLayoutParams(childAttrs);
                    }
                    view.setLayoutParams(params);
                    // Inflate all children.
                    thisInflater.rInflateChildren(childParser, view, childAttrs, true);
                    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");
    }
    LayoutInflater.consumeChildElements(parser);
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) TypedArray(android.content.res.TypedArray) TypedValue(android.util.TypedValue) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Aggregations

XmlResourceParser (android.content.res.XmlResourceParser)332 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)211 IOException (java.io.IOException)203 Resources (android.content.res.Resources)114 AttributeSet (android.util.AttributeSet)113 TypedArray (android.content.res.TypedArray)80 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)64 PackageManager (android.content.pm.PackageManager)45 ActivityInfo (android.content.pm.ActivityInfo)43 Bundle (android.os.Bundle)42 ComponentName (android.content.ComponentName)41 ArrayList (java.util.ArrayList)30 TypedValue (android.util.TypedValue)29 AssetManager (android.content.res.AssetManager)28 RemoteException (android.os.RemoteException)28 Intent (android.content.Intent)22 ApplicationInfo (android.content.pm.ApplicationInfo)18 InflateException (android.view.InflateException)18 FileNotFoundException (java.io.FileNotFoundException)18 NotFoundException (android.content.res.Resources.NotFoundException)17