Example usage for android.content.res XmlResourceParser close

List of usage examples for android.content.res XmlResourceParser close

Introduction

In this page you can find the example usage for android.content.res XmlResourceParser close.

Prototype

public void close();

Source Link

Document

Close this parser.

Usage

From source file:com.vsmartcard.acardemulator.emulators.EmulatorSingleton.java

public static String[] getRegisteredAids(Context context) {
    List<String> aidList = new ArrayList<>();
    XmlResourceParser aidXmlParser = context.getResources().getXml(R.xml.aid_list);

    try {/*from   w w  w . java2 s. c o  m*/
        while (aidXmlParser.getEventType() != XmlPullParser.END_DOCUMENT) {
            if (aidXmlParser.getEventType() == XmlPullParser.START_TAG) {
                if (aidXmlParser.getName().equals("aid-filter")) {
                    int aid = aidXmlParser.getAttributeResourceValue(0, -1);
                    if (aid != -1) {
                        aidList.add(context.getResources().getString(aid));
                    }
                }
            }

            aidXmlParser.next();
        }

        aidXmlParser.close();
    } catch (XmlPullParserException | IOException e) {
        Log.e(TAG.substring(0, 23), "Couldn't parse aid xml list.");
    }

    return aidList.toArray(new String[0]);
}

From source file:Main.java

@SuppressWarnings("TryWithIdenticalCatches")
public static LayoutAnimationController loadLayoutAnimation(Context context, int id) throws NotFoundException {

    XmlResourceParser parser = null;
    try {//from www .j a v a2 s.  c o  m
        parser = context.getResources().getAnimation(id);
        return createLayoutAnimationFromXml(context, parser);
    } catch (XmlPullParserException ex) {
        NotFoundException rnf = new NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } catch (IOException ex) {
        NotFoundException rnf = new NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } finally {
        if (parser != null)
            parser.close();
    }
}

From source file:Main.java

/**
 * Loads an {@link Animation} object from a resource
 *
 * @param context Application context used to access resources
 * @param id The resource id of the animation to load
 * @return The animation object reference by the specified id
 * @throws NotFoundException when the animation cannot be loaded
 *///from   www . j  a va2  s .c o m
@SuppressWarnings("TryWithIdenticalCatches")
public static Animation loadAnimation(Context context, int id) throws NotFoundException {

    XmlResourceParser parser = null;
    try {
        parser = context.getResources().getAnimation(id);
        return createAnimationFromXml(context, parser);
    } catch (XmlPullParserException ex) {
        NotFoundException rnf = new NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } catch (IOException ex) {
        NotFoundException rnf = new NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } finally {
        if (parser != null)
            parser.close();
    }
}

From source file:Main.java

/**
 * Loads an {@link Interpolator} object from a resource
 *
 * @param context Application context used to access resources
 * @param id The resource id of the animation to load
 * @return The animation object reference by the specified id
 * @throws NotFoundException/*from   w  ww . ja va  2s .  c o m*/
 */
@SuppressWarnings("TryWithIdenticalCatches")
public static Interpolator loadInterpolator(Context context, int id) throws NotFoundException {
    XmlResourceParser parser = null;
    try {
        parser = context.getResources().getAnimation(id);
        return createInterpolatorFromXml(context, parser);
    } catch (XmlPullParserException ex) {
        NotFoundException rnf = new NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } catch (IOException ex) {
        NotFoundException rnf = new NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } finally {
        if (parser != null)
            parser.close();
    }

}

From source file:com.bilibili.magicasakura.utils.ColorStateListUtils.java

static ColorStateList createColorStateList(Context context, int resId) {
    if (resId <= 0)
        return null;

    TypedValue value = new TypedValue();
    context.getResources().getValue(resId, value, true);
    ColorStateList cl = null;/* w  w  w.j av a  2s . c om*/
    if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        //Assume that "color/theme_color_primary" and "color/theme_color_profile" have the same color value;
        //However, "color/theme_color_primary" need to replace by themeId, "color/theme_color_profile" not.
        //If use value.data may cause "color/theme_color_profile" still been replaced by themeId
        cl = ColorStateList.valueOf(ThemeUtils.replaceColorById(context, value.resourceId));
    } else {
        final String file = value.string.toString();
        try {
            if (file.endsWith("xml")) {
                final XmlResourceParser rp = context.getResources().getAssets()
                        .openXmlResourceParser(value.assetCookie, file);
                final AttributeSet attrs = Xml.asAttributeSet(rp);
                int type;

                while ((type = rp.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
                    // Seek parser to start tag.
                }

                if (type != XmlPullParser.START_TAG) {
                    throw new XmlPullParserException("No start tag found");
                }

                cl = createFromXmlInner(context, rp, attrs);
                rp.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }
    return cl;
}

From source file:com.hippo.anani.AnimationUtils.java

/**
 * Loads an {@link Interpolator} object from a resource
 *
 * @param context Application context used to access resources
 * @param id The resource id of the animation to load
 * @return The animation object reference by the specified id
 * @throws Resources.NotFoundException/*w  w w .  j  a  v  a  2s  .  c o m*/
 */
public static Interpolator loadInterpolator(Context context, @InterpolatorRes int id)
        throws Resources.NotFoundException {
    XmlResourceParser parser = null;
    try {
        //noinspection ResourceType
        parser = context.getResources().getAnimation(id);
        return createInterpolatorFromXml(context, parser);
    } catch (XmlPullParserException ex) {
        Resources.NotFoundException rnf = new Resources.NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } catch (IOException ex) {
        Resources.NotFoundException rnf = new Resources.NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } finally {
        if (parser != null)
            parser.close();
    }
}

From source file:net.peterkuterna.android.apps.devoxxsched.io.LocalExecutor.java

public void execute(int resId, XmlHandler handler) throws XmlHandlerException {
    final XmlResourceParser parser = mRes.getXml(resId);
    try {//from www.  j a va 2s  . com
        handler.setLocalSync(true);
        handler.parseAndApply(parser, mResolver);
    } finally {
        parser.close();
    }
}

From source file:android.support.graphics.drawable.AnimationUtilsCompat.java

/**
 * Loads an {@link Interpolator} object from a resource
 *
 * @param context Application context used to access resources
 * @param id      The resource id of the animation to load
 * @return The animation object reference by the specified id
 *///w  w w. ja v  a  2 s .  com
public static Interpolator loadInterpolator(Context context, int id) throws NotFoundException {
    // From API 21, we added path Interpolator .
    if (Build.VERSION.SDK_INT >= 21) {
        return AnimationUtils.loadInterpolator(context, id);
    }

    XmlResourceParser parser = null;
    try {
        // Special treatment for the interpolator introduced at API 21.
        if (id == AndroidResources.FAST_OUT_LINEAR_IN) {
            return new FastOutLinearInInterpolator();
        } else if (id == AndroidResources.FAST_OUT_SLOW_IN) {
            return new FastOutSlowInInterpolator();
        } else if (id == AndroidResources.LINEAR_OUT_SLOW_IN) {
            return new LinearOutSlowInInterpolator();
        }
        parser = context.getResources().getAnimation(id);
        return createInterpolatorFromXml(context, context.getResources(), context.getTheme(), parser);
    } catch (XmlPullParserException ex) {
        NotFoundException rnf = new NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } catch (IOException ex) {
        NotFoundException rnf = new NotFoundException(
                "Can't load animation resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } finally {
        if (parser != null)
            parser.close();
    }

}

From source file:com.dm.material.dashboard.candybar.helpers.IconsHelper.java

@NonNull
public static List<Icon> getIconsList(@NonNull Context context) throws Exception {
    XmlResourceParser parser = context.getResources().getXml(R.xml.drawable);
    int eventType = parser.getEventType();
    String section = "";
    List<Icon> icons = new ArrayList<>();
    List<Icon> sections = new ArrayList<>();

    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {
            if (parser.getName().equals("category")) {
                String title = parser.getAttributeValue(null, "title");
                if (!section.equals(title)) {
                    if (section.length() > 0)
                        sections.add(new Icon(section, icons));
                }/* w  ww  . j a  v a 2  s .c  o m*/
                section = title;
                icons = new ArrayList<>();
            } else if (parser.getName().equals("item")) {
                String name = parser.getAttributeValue(null, "drawable");
                int id = DrawableHelper.getResourceId(context, name);
                if (id > 0) {
                    icons.add(new Icon(name, id));
                }
            }
        }

        eventType = parser.next();
    }
    sections.add(new Icon(section, icons));
    parser.close();
    return sections;
}

From source file:android.content.res.VectorResources.java

@Override
Drawable loadDrawable(TypedValue value, int id) throws NotFoundException {
    boolean isColorDrawable = false;
    if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT && value.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        isColorDrawable = true;/*from   w ww . j ava2s .c o m*/
    }
    final long key = isColorDrawable ? value.data : (((long) value.assetCookie) << 32) | value.data;

    Drawable dr = getCachedDrawable(isColorDrawable ? mColorDrawableCache : mDrawableCache, key);

    if (dr != null) {
        return dr;
    }

    if (isColorDrawable) {
        dr = new ColorDrawable(value.data);
    }

    if (dr == null) {
        if (value.string == null) {
            throw new NotFoundException("Resource is not a Drawable (color or path): " + value);
        }

        String file = value.string.toString();

        if (file.endsWith(".xml")) {
            // Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
            try {
                XmlResourceParser rp = getXml(id);
                // XmlResourceParser rp = loadXmlResourceParser(
                //         file, id, value.assetCookie, "drawable");
                dr = createDrawableFromXml(rp);
                rp.close();
            } catch (Exception e) {
                // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
                NotFoundException rnf = new NotFoundException(
                        "File " + file + " from drawable resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(e);
                throw rnf;
            }
            // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);

        } else {
            // Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, file);
            try {
                InputStream is = openRawResource(id, value);
                //InputStream is = mAssets.openNonAsset(
                //        value.assetCookie, file, AssetManager.ACCESS_STREAMING);
                //                System.out.println("Opened file " + file + ": " + is);
                dr = Drawable.createFromResourceStream(this, value, is, file, null);
                is.close();
                //                System.out.println("Created stream: " + dr);
            } catch (Exception e) {
                // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
                NotFoundException rnf = new NotFoundException(
                        "File " + file + " from drawable resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(e);
                throw rnf;
            }
            // Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
        }
    }
    Drawable.ConstantState cs;
    if (dr != null) {
        dr.setChangingConfigurations(value.changingConfigurations);
        cs = dr.getConstantState();
        if (cs != null) {
            synchronized (mAccessLock) {
                //Log.i(TAG, "Saving cached drawable @ #" +
                //        Integer.toHexString(key.intValue())
                //        + " in " + this + ": " + cs);
                if (isColorDrawable) {
                    mColorDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
                } else {
                    mDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
                }
            }
        }
    }

    return dr;
}