Search in sources :

Example 86 with AttributeSet

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

the class GradientColor method createFromXml.

/**
     * A public method to create GradientColor from a XML resource.
     */
public static GradientColor createFromXml(Resources r, XmlResourceParser parser, Theme theme) throws XmlPullParserException, IOException {
    final AttributeSet attrs = Xml.asAttributeSet(parser);
    int type;
    while ((type = parser.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");
    }
    return createFromXmlInner(r, parser, attrs, theme);
}
Also used : AttributeSet(android.util.AttributeSet) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 87 with AttributeSet

use of android.util.AttributeSet 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 88 with AttributeSet

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

the class ShortcutParser method parseShortcutsOneFile.

private static List<ShortcutInfo> parseShortcutsOneFile(ShortcutService service, ActivityInfo activityInfo, String packageName, @UserIdInt int userId, List<ShortcutInfo> result) throws IOException, XmlPullParserException {
    if (ShortcutService.DEBUG) {
        Slog.d(TAG, String.format("Checking main activity %s", activityInfo.getComponentName()));
    }
    XmlResourceParser parser = null;
    try {
        parser = service.injectXmlMetaData(activityInfo, METADATA_KEY);
        if (parser == null) {
            return result;
        }
        final ComponentName activity = new ComponentName(packageName, activityInfo.name);
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        int rank = 0;
        final int maxShortcuts = service.getMaxActivityShortcuts();
        int numShortcuts = 0;
        // We instantiate ShortcutInfo at <shortcut>, but we add it to the list at </shortcut>,
        // after parsing <intent>.  We keep the current one in here.
        ShortcutInfo currentShortcut = null;
        Set<String> categories = null;
        final ArrayList<Intent> intents = new ArrayList<>();
        outer: while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > 0)) {
            final int depth = parser.getDepth();
            final String tag = parser.getName();
            // When a shortcut tag is closing, publish.
            if ((type == XmlPullParser.END_TAG) && (depth == 2) && (TAG_SHORTCUT.equals(tag))) {
                if (currentShortcut == null) {
                    // Shortcut was invalid.
                    continue;
                }
                final ShortcutInfo si = currentShortcut;
                // Make sure to null out for the next iteration.
                currentShortcut = null;
                if (si.isEnabled()) {
                    if (intents.size() == 0) {
                        Log.e(TAG, "Shortcut " + si.getId() + " has no intent. Skipping it.");
                        continue;
                    }
                } else {
                    // Just set the default intent to disabled shortcuts.
                    intents.clear();
                    intents.add(new Intent(Intent.ACTION_VIEW));
                }
                if (numShortcuts >= maxShortcuts) {
                    Log.e(TAG, "More than " + maxShortcuts + " shortcuts found for " + activityInfo.getComponentName() + ". Skipping the rest.");
                    return result;
                }
                // Same flag as what TaskStackBuilder adds.
                intents.get(0).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME);
                try {
                    si.setIntents(intents.toArray(new Intent[intents.size()]));
                } catch (RuntimeException e) {
                    // This shouldn't happen because intents in XML can't have complicated
                    // extras, but just in case Intent.parseIntent() supports such a thing one
                    // day.
                    Log.e(TAG, "Shortcut's extras contain un-persistable values. Skipping it.");
                    continue;
                }
                intents.clear();
                if (categories != null) {
                    si.setCategories(categories);
                    categories = null;
                }
                if (result == null) {
                    result = new ArrayList<>();
                }
                result.add(si);
                numShortcuts++;
                rank++;
                if (ShortcutService.DEBUG) {
                    Slog.d(TAG, "Shortcut added: " + si.toInsecureString());
                }
                continue;
            }
            // Otherwise, just look at start tags.
            if (type != XmlPullParser.START_TAG) {
                continue;
            }
            if (depth == 1 && TAG_SHORTCUTS.equals(tag)) {
                // Root tag.
                continue;
            }
            if (depth == 2 && TAG_SHORTCUT.equals(tag)) {
                final ShortcutInfo si = parseShortcutAttributes(service, attrs, packageName, activity, userId, rank);
                if (si == null) {
                    // Shortcut was invalid.
                    continue;
                }
                if (ShortcutService.DEBUG) {
                    Slog.d(TAG, "Shortcut found: " + si.toInsecureString());
                }
                if (result != null) {
                    for (int i = result.size() - 1; i >= 0; i--) {
                        if (si.getId().equals(result.get(i).getId())) {
                            Log.e(TAG, "Duplicate shortcut ID detected. Skipping it.");
                            continue outer;
                        }
                    }
                }
                currentShortcut = si;
                categories = null;
                continue;
            }
            if (depth == 3 && TAG_INTENT.equals(tag)) {
                if ((currentShortcut == null) || !currentShortcut.isEnabled()) {
                    Log.e(TAG, "Ignoring excessive intent tag.");
                    continue;
                }
                final Intent intent = Intent.parseIntent(service.mContext.getResources(), parser, attrs);
                if (TextUtils.isEmpty(intent.getAction())) {
                    Log.e(TAG, "Shortcut intent action must be provided. activity=" + activity);
                    // Invalidate the current shortcut.
                    currentShortcut = null;
                    continue;
                }
                intents.add(intent);
                continue;
            }
            if (depth == 3 && TAG_CATEGORIES.equals(tag)) {
                if ((currentShortcut == null) || (currentShortcut.getCategories() != null)) {
                    continue;
                }
                final String name = parseCategories(service, attrs);
                if (TextUtils.isEmpty(name)) {
                    Log.e(TAG, "Empty category found. activity=" + activity);
                    continue;
                }
                if (categories == null) {
                    categories = new ArraySet<>();
                }
                categories.add(name);
                continue;
            }
            Log.w(TAG, String.format("Invalid tag '%s' found at depth %d", tag, depth));
        }
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
    return result;
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) ArraySet(android.util.ArraySet) ShortcutInfo(android.content.pm.ShortcutInfo) AttributeSet(android.util.AttributeSet) ArrayList(java.util.ArrayList) ComponentName(android.content.ComponentName) Intent(android.content.Intent)

Example 89 with AttributeSet

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

the class Drawable method createFromXml.

/**
     * Create a drawable from an XML document using an optional {@link Theme}.
     * For more information on how to create resources in XML, see
     * <a href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a>.
     */
public static Drawable createFromXml(Resources r, XmlPullParser parser, Theme theme) throws XmlPullParserException, IOException {
    AttributeSet attrs = Xml.asAttributeSet(parser);
    int type;
    //noinspection StatementWithEmptyBody
    while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
    // Empty loop.
    }
    if (type != XmlPullParser.START_TAG) {
        throw new XmlPullParserException("No start tag found");
    }
    Drawable drawable = createFromXmlInner(r, parser, attrs, theme);
    if (drawable == null) {
        throw new RuntimeException("Unknown initial tag: " + parser.getName());
    }
    return drawable;
}
Also used : AttributeSet(android.util.AttributeSet) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 90 with AttributeSet

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

the class DreamBackend method getSettingsComponentName.

private static ComponentName getSettingsComponentName(PackageManager pm, ResolveInfo resolveInfo) {
    if (resolveInfo == null || resolveInfo.serviceInfo == null || resolveInfo.serviceInfo.metaData == null)
        return null;
    String cn = null;
    XmlResourceParser parser = null;
    Exception caughtException = null;
    try {
        parser = resolveInfo.serviceInfo.loadXmlMetaData(pm, DreamService.DREAM_META_DATA);
        if (parser == null) {
            Log.w(TAG, "No " + DreamService.DREAM_META_DATA + " meta-data");
            return null;
        }
        Resources res = pm.getResourcesForApplication(resolveInfo.serviceInfo.applicationInfo);
        AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
        }
        String nodeName = parser.getName();
        if (!"dream".equals(nodeName)) {
            Log.w(TAG, "Meta-data does not start with dream tag");
            return null;
        }
        TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.Dream);
        cn = sa.getString(com.android.internal.R.styleable.Dream_settingsActivity);
        sa.recycle();
    } catch (PackageManager.NameNotFoundException | IOException | XmlPullParserException e) {
        caughtException = e;
    } finally {
        if (parser != null)
            parser.close();
    }
    if (caughtException != null) {
        Log.w(TAG, "Error parsing : " + resolveInfo.serviceInfo.packageName, caughtException);
        return null;
    }
    if (cn != null && cn.indexOf('/') < 0) {
        cn = resolveInfo.serviceInfo.packageName + "/" + cn;
    }
    return cn == null ? null : ComponentName.unflattenFromString(cn);
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) TypedArray(android.content.res.TypedArray) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) IOException(java.io.IOException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

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