Search in sources :

Example 6 with XmlResourceParser

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

the class InputManagerService method visitKeyboardLayoutsInPackage.

private void visitKeyboardLayoutsInPackage(PackageManager pm, ActivityInfo receiver, String keyboardName, KeyboardLayoutVisitor visitor) {
    Bundle metaData = receiver.metaData;
    if (metaData == null) {
        return;
    }
    int configResId = metaData.getInt(InputManager.META_DATA_KEYBOARD_LAYOUTS);
    if (configResId == 0) {
        Log.w(TAG, "Missing meta-data '" + InputManager.META_DATA_KEYBOARD_LAYOUTS + "' on receiver " + receiver.packageName + "/" + receiver.name);
        return;
    }
    CharSequence receiverLabel = receiver.loadLabel(pm);
    String collection = receiverLabel != null ? receiverLabel.toString() : "";
    try {
        Resources resources = pm.getResourcesForApplication(receiver.applicationInfo);
        XmlResourceParser parser = resources.getXml(configResId);
        try {
            XmlUtils.beginDocument(parser, "keyboard-layouts");
            for (; ; ) {
                XmlUtils.nextElement(parser);
                String element = parser.getName();
                if (element == null) {
                    break;
                }
                if (element.equals("keyboard-layout")) {
                    TypedArray a = resources.obtainAttributes(parser, com.android.internal.R.styleable.KeyboardLayout);
                    try {
                        String name = a.getString(com.android.internal.R.styleable.KeyboardLayout_name);
                        String label = a.getString(com.android.internal.R.styleable.KeyboardLayout_label);
                        int keyboardLayoutResId = a.getResourceId(com.android.internal.R.styleable.KeyboardLayout_keyboardLayout, 0);
                        if (name == null || label == null || keyboardLayoutResId == 0) {
                            Log.w(TAG, "Missing required 'name', 'label' or 'keyboardLayout' " + "attributes in keyboard layout " + "resource from receiver " + receiver.packageName + "/" + receiver.name);
                        } else {
                            String descriptor = KeyboardLayoutDescriptor.format(receiver.packageName, receiver.name, name);
                            if (keyboardName == null || name.equals(keyboardName)) {
                                visitor.visitKeyboardLayout(resources, descriptor, label, collection, keyboardLayoutResId);
                            }
                        }
                    } finally {
                        a.recycle();
                    }
                } else {
                    Log.w(TAG, "Skipping unrecognized element '" + element + "' in keyboard layout resource from receiver " + receiver.packageName + "/" + receiver.name);
                }
            }
        } finally {
            parser.close();
        }
    } catch (Exception ex) {
        Log.w(TAG, "Could not parse keyboard layout resource from receiver " + receiver.packageName + "/" + receiver.name, ex);
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) Bundle(android.os.Bundle) TypedArray(android.content.res.TypedArray) Resources(android.content.res.Resources) SettingNotFoundException(android.provider.Settings.SettingNotFoundException) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(android.content.res.Resources.NotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException)

Example 7 with XmlResourceParser

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

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

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

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

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

the class LayoutInflater_Delegate method parseInclude.

@LayoutlibDelegate
public static void parseInclude(LayoutInflater thisInflater, 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 = 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, childAttrs, false);
                } else {
                    final View view = thisInflater.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 {
                        // ---- START CHANGES
                        sIsInInclude = true;
                        // ---- END CHANGES
                        params = group.generateLayoutParams(attrs);
                    } catch (RuntimeException e) {
                        // ---- START CHANGES
                        sIsInInclude = false;
                        // ---- END CHANGES
                        params = group.generateLayoutParams(childAttrs);
                    } finally {
                        // ---- START CHANGES
                        sIsInInclude = false;
                        if (params != null) {
                            view.setLayoutParams(params);
                        }
                    }
                    // Inflate all children.
                    thisInflater.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 = thisInflater.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) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 10 with XmlResourceParser

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

the class AliasActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    XmlResourceParser parser = null;
    try {
        ActivityInfo ai = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
        parser = ai.loadXmlMetaData(getPackageManager(), ALIAS_META_DATA);
        if (parser == null) {
            throw new RuntimeException("Alias requires a meta-data field " + ALIAS_META_DATA);
        }
        Intent intent = parseAlias(parser);
        if (intent == null) {
            throw new RuntimeException("No <intent> tag found in alias description");
        }
        startActivity(intent);
        finish();
    } catch (PackageManager.NameNotFoundException e) {
        throw new RuntimeException("Error parsing alias", e);
    } catch (XmlPullParserException e) {
        throw new RuntimeException("Error parsing alias", e);
    } catch (IOException e) {
        throw new RuntimeException("Error parsing alias", e);
    } finally {
        if (parser != null)
            parser.close();
    }
}
Also used : ActivityInfo(android.content.pm.ActivityInfo) XmlResourceParser(android.content.res.XmlResourceParser) PackageManager(android.content.pm.PackageManager) Intent(android.content.Intent) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) 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