Search in sources :

Example 41 with XmlResourceParser

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

the class XmlConfigSource method parseDebugOverridesResource.

private NetworkSecurityConfig.Builder parseDebugOverridesResource() throws IOException, XmlPullParserException, ParserException {
    Resources resources = mContext.getResources();
    String packageName = resources.getResourcePackageName(mResourceId);
    String entryName = resources.getResourceEntryName(mResourceId);
    int resId = resources.getIdentifier(entryName + "_debug", "xml", packageName);
    // No debug-overrides resource was found, nothing to parse.
    if (resId == 0) {
        return null;
    }
    NetworkSecurityConfig.Builder debugConfigBuilder = null;
    // Parse debug-overrides out of the _debug resource.
    try (XmlResourceParser parser = resources.getXml(resId)) {
        XmlUtils.beginDocument(parser, "network-security-config");
        int outerDepth = parser.getDepth();
        boolean seenDebugOverrides = false;
        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
            if ("debug-overrides".equals(parser.getName())) {
                if (seenDebugOverrides) {
                    throw new ParserException(parser, "Only one debug-overrides allowed");
                }
                if (mDebugBuild) {
                    debugConfigBuilder = parseConfigEntry(parser, null, null, CONFIG_DEBUG).get(0).first;
                } else {
                    XmlUtils.skipCurrentTag(parser);
                }
                seenDebugOverrides = true;
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
        }
    }
    return debugConfigBuilder;
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlResourceParser(android.content.res.XmlResourceParser) Resources(android.content.res.Resources)

Example 42 with XmlResourceParser

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

the class XmlConfigSource method ensureInitialized.

private void ensureInitialized() {
    synchronized (mLock) {
        if (mInitialized) {
            return;
        }
        try (XmlResourceParser parser = mContext.getResources().getXml(mResourceId)) {
            parseNetworkSecurityConfig(parser);
            mContext = null;
            mInitialized = true;
        } catch (Resources.NotFoundException | XmlPullParserException | IOException | ParserException e) {
            throw new RuntimeException("Failed to parse XML configuration from " + mContext.getResources().getResourceEntryName(mResourceId), e);
        }
    }
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 43 with XmlResourceParser

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

the class AutoText method init.

private void init(Resources r) {
    XmlResourceParser parser = r.getXml(com.android.internal.R.xml.autotext);
    StringBuilder right = new StringBuilder(RIGHT);
    mTrie = new char[DEFAULT];
    mTrie[TRIE_ROOT] = TRIE_NULL;
    mTrieUsed = TRIE_ROOT + 1;
    try {
        XmlUtils.beginDocument(parser, "words");
        String odest = "";
        char ooff = 0;
        while (true) {
            XmlUtils.nextElement(parser);
            String element = parser.getName();
            if (element == null || !(element.equals("word"))) {
                break;
            }
            String src = parser.getAttributeValue(null, "src");
            if (parser.next() == XmlPullParser.TEXT) {
                String dest = parser.getText();
                char off;
                if (dest.equals(odest)) {
                    off = ooff;
                } else {
                    off = (char) right.length();
                    right.append((char) dest.length());
                    right.append(dest);
                }
                add(src, off);
            }
        }
        // Don't let Resources cache a copy of all these strings.
        r.flushLayoutCache();
    } catch (XmlPullParserException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        parser.close();
    }
    mText = right.toString();
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 44 with XmlResourceParser

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

the class AnimatorInflater method loadStateListAnimator.

public static StateListAnimator loadStateListAnimator(Context context, int id) throws NotFoundException {
    final Resources resources = context.getResources();
    final ConfigurationBoundResourceCache<StateListAnimator> cache = resources.getStateListAnimatorCache();
    final Theme theme = context.getTheme();
    StateListAnimator animator = cache.getInstance(id, resources, theme);
    if (animator != null) {
        return animator;
    }
    XmlResourceParser parser = null;
    try {
        parser = resources.getAnimation(id);
        animator = createStateListAnimatorFromXml(context, parser, Xml.asAttributeSet(parser));
        if (animator != null) {
            animator.appendChangingConfigurations(getChangingConfigs(resources, id));
            final ConstantState<StateListAnimator> constantState = animator.createConstantState();
            if (constantState != null) {
                cache.put(id, theme, constantState);
                // return a clone so that the animator in constant state is never used.
                animator = constantState.newInstance(resources, theme);
            }
        }
        return animator;
    } catch (XmlPullParserException ex) {
        Resources.NotFoundException rnf = new Resources.NotFoundException("Can't load state list animator resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } catch (IOException ex) {
        Resources.NotFoundException rnf = new Resources.NotFoundException("Can't load state list animator resource ID #0x" + Integer.toHexString(id));
        rnf.initCause(ex);
        throw rnf;
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
Also used : NotFoundException(android.content.res.Resources.NotFoundException) XmlResourceParser(android.content.res.XmlResourceParser) Theme(android.content.res.Resources.Theme) NotFoundException(android.content.res.Resources.NotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) IOException(java.io.IOException)

Example 45 with XmlResourceParser

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

the class AnimatorInflater method loadAnimator.

/** @hide */
public static Animator loadAnimator(Resources resources, Theme theme, int id, float pathErrorScale) throws NotFoundException {
    final ConfigurationBoundResourceCache<Animator> animatorCache = resources.getAnimatorCache();
    Animator animator = animatorCache.getInstance(id, resources, theme);
    if (animator != null) {
        if (DBG_ANIMATOR_INFLATER) {
            Log.d(TAG, "loaded animator from cache, " + resources.getResourceName(id));
        }
        return animator;
    } else if (DBG_ANIMATOR_INFLATER) {
        Log.d(TAG, "cache miss for animator " + resources.getResourceName(id));
    }
    XmlResourceParser parser = null;
    try {
        parser = resources.getAnimation(id);
        animator = createAnimatorFromXml(resources, theme, parser, pathErrorScale);
        if (animator != null) {
            animator.appendChangingConfigurations(getChangingConfigs(resources, id));
            final ConstantState<Animator> constantState = animator.createConstantState();
            if (constantState != null) {
                if (DBG_ANIMATOR_INFLATER) {
                    Log.d(TAG, "caching animator for res " + resources.getResourceName(id));
                }
                animatorCache.put(id, theme, constantState);
                // create a new animator so that cached version is never used by the user
                animator = constantState.newInstance(resources, theme);
            }
        }
        return animator;
    } 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();
    }
}
Also used : NotFoundException(android.content.res.Resources.NotFoundException) XmlResourceParser(android.content.res.XmlResourceParser) NotFoundException(android.content.res.Resources.NotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) IOException(java.io.IOException)

Aggregations

XmlResourceParser (android.content.res.XmlResourceParser)508 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)267 IOException (java.io.IOException)247 AttributeSet (android.util.AttributeSet)241 Resources (android.content.res.Resources)146 TypedArray (android.content.res.TypedArray)97 Test (org.junit.Test)82 PackageManager (android.content.pm.PackageManager)68 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)68 ArrayList (java.util.ArrayList)52 ComponentName (android.content.ComponentName)49 Bundle (android.os.Bundle)44 ActivityInfo (android.content.pm.ActivityInfo)43 AssetManager (android.content.res.AssetManager)32 RemoteException (android.os.RemoteException)31 TypedValue (android.util.TypedValue)29 ResolveInfo (android.content.pm.ResolveInfo)27 Intent (android.content.Intent)25 ApplicationInfo (android.content.pm.ApplicationInfo)25 Context (android.content.Context)22