Search in sources :

Example 1 with XmlResourceParser

use of android.content.res.XmlResourceParser in project cw-omnibus by commonsguy.

the class MenuInflater method inflate.

/**
     * Inflate a menu hierarchy from the specified XML resource. Throws
     * {@link InflateException} if there is an error.
     *
     * @param menuRes Resource ID for an XML layout resource to load (e.g.,
     *            <code>R.menu.main_activity</code>)
     * @param menu The Menu to inflate into. The items and submenus will be
     *            added to this Menu.
     */
public void inflate(int menuRes, Menu menu) {
    XmlResourceParser parser = null;
    try {
        parser = mContext.getResources().getLayout(menuRes);
        AttributeSet attrs = Xml.asAttributeSet(parser);
        parseMenu(parser, attrs, menu);
    } 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 2 with XmlResourceParser

use of android.content.res.XmlResourceParser in project AndroidTraining by mixi-inc.

the class ActionBarSherlockCompat method loadUiOptionsFromManifest.

private static int loadUiOptionsFromManifest(Activity activity) {
    int uiOptions = 0;
    try {
        final String thisPackage = activity.getClass().getName();
        if (BuildConfig.DEBUG)
            Log.i(TAG, "Parsing AndroidManifest.xml for " + thisPackage);
        final String packageName = activity.getApplicationInfo().packageName;
        final AssetManager am = activity.createPackageContext(packageName, 0).getAssets();
        final XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");
        int eventType = xml.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                String name = xml.getName();
                if ("application".equals(name)) {
                    //Check if the <application> has the attribute
                    if (BuildConfig.DEBUG)
                        Log.d(TAG, "Got <application>");
                    for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
                        if (BuildConfig.DEBUG)
                            Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i));
                        if ("uiOptions".equals(xml.getAttributeName(i))) {
                            uiOptions = xml.getAttributeIntValue(i, 0);
                            //out of for loop
                            break;
                        }
                    }
                } else if ("activity".equals(name)) {
                    //Check if the <activity> is us and has the attribute
                    if (BuildConfig.DEBUG)
                        Log.d(TAG, "Got <activity>");
                    Integer activityUiOptions = null;
                    String activityPackage = null;
                    boolean isOurActivity = false;
                    for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
                        if (BuildConfig.DEBUG)
                            Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i));
                        //We need both uiOptions and name attributes
                        String attrName = xml.getAttributeName(i);
                        if ("uiOptions".equals(attrName)) {
                            activityUiOptions = xml.getAttributeIntValue(i, 0);
                        } else if ("name".equals(attrName)) {
                            activityPackage = cleanActivityName(packageName, xml.getAttributeValue(i));
                            if (!thisPackage.equals(activityPackage)) {
                                //out of for loop
                                break;
                            }
                            isOurActivity = true;
                        }
                        //Make sure we have both attributes before processing
                        if ((activityUiOptions != null) && (activityPackage != null)) {
                            //Our activity, uiOptions specified, override with our value
                            uiOptions = activityUiOptions.intValue();
                        }
                    }
                    if (isOurActivity) {
                        //do any more processing of the manifest
                        break;
                    }
                }
            }
            eventType = xml.nextToken();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (BuildConfig.DEBUG)
        Log.i(TAG, "Returning " + Integer.toHexString(uiOptions));
    return uiOptions;
}
Also used : AssetManager(android.content.res.AssetManager) XmlResourceParser(android.content.res.XmlResourceParser) AndroidRuntimeException(android.util.AndroidRuntimeException)

Example 3 with XmlResourceParser

use of android.content.res.XmlResourceParser in project Inscription by MartinvanZ.

the class ChangeLogDialog method getHTMLChangelog.

//Get the changelog in html code, this will be shown in the dialog's webview
private String getHTMLChangelog(final int resourceId, final Resources resources, final int version) {
    boolean releaseFound = false;
    final StringBuilder changelogBuilder = new StringBuilder();
    changelogBuilder.append("<html><head>").append(getStyle()).append("</head><body>");
    final XmlResourceParser xml = resources.getXml(resourceId);
    try {
        int eventType = xml.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if ((eventType == XmlPullParser.START_TAG) && (xml.getName().equals("release"))) {
                //Check if the version matches the release tag.
                //When version is 0 every release tag is parsed.
                final int versioncode = Integer.parseInt(xml.getAttributeValue(null, "versioncode"));
                if ((version == 0) || (versioncode == version)) {
                    parseReleaseTag(changelogBuilder, xml);
                    //At lease one release tag has been parsed.
                    releaseFound = true;
                }
            }
            eventType = xml.next();
        }
    } catch (XmlPullParserException e) {
        Log.e(TAG, e.getMessage(), e);
        return "";
    } catch (IOException e) {
        Log.e(TAG, e.getMessage(), e);
        return "";
    } finally {
        xml.close();
    }
    changelogBuilder.append("</body></html>");
    //Check if there was a release tag parsed, if not return an empty string.
    if (releaseFound) {
        return changelogBuilder.toString();
    } else {
        return "";
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint)

Example 4 with XmlResourceParser

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

the class GlobalKeyManager method loadGlobalKeys.

private void loadGlobalKeys(Context context) {
    XmlResourceParser parser = null;
    try {
        parser = context.getResources().getXml(com.android.internal.R.xml.global_keys);
        XmlUtils.beginDocument(parser, TAG_GLOBAL_KEYS);
        int version = parser.getAttributeIntValue(null, ATTR_VERSION, 0);
        if (GLOBAL_KEY_FILE_VERSION == version) {
            while (true) {
                XmlUtils.nextElement(parser);
                String element = parser.getName();
                if (element == null) {
                    break;
                }
                if (TAG_KEY.equals(element)) {
                    String keyCodeName = parser.getAttributeValue(null, ATTR_KEY_CODE);
                    String componentName = parser.getAttributeValue(null, ATTR_COMPONENT);
                    int keyCode = KeyEvent.keyCodeFromString(keyCodeName);
                    if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
                        mKeyMapping.put(keyCode, ComponentName.unflattenFromString(componentName));
                    }
                }
            }
        }
    } catch (Resources.NotFoundException e) {
        Log.w(TAG, "global keys file not found", e);
    } catch (XmlPullParserException e) {
        Log.w(TAG, "XML parser exception reading global keys file", e);
    } catch (IOException e) {
        Log.w(TAG, "I/O exception reading global keys file", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) IOException(java.io.IOException)

Example 5 with XmlResourceParser

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

the class AppWidgetServiceImpl method parseProviderInfoXml.

private Provider parseProviderInfoXml(ComponentName component, ResolveInfo ri) {
    Provider p = null;
    ActivityInfo activityInfo = ri.activityInfo;
    XmlResourceParser parser = null;
    try {
        parser = activityInfo.loadXmlMetaData(mContext.getPackageManager(), AppWidgetManager.META_DATA_APPWIDGET_PROVIDER);
        if (parser == null) {
            Slog.w(TAG, "No " + AppWidgetManager.META_DATA_APPWIDGET_PROVIDER + " meta-data for " + "AppWidget provider '" + component + '\'');
            return null;
        }
        AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
        // drain whitespace, comments, etc.
        }
        String nodeName = parser.getName();
        if (!"appwidget-provider".equals(nodeName)) {
            Slog.w(TAG, "Meta-data does not start with appwidget-provider tag for" + " AppWidget provider '" + component + '\'');
            return null;
        }
        p = new Provider();
        AppWidgetProviderInfo info = p.info = new AppWidgetProviderInfo();
        info.provider = component;
        p.uid = activityInfo.applicationInfo.uid;
        Resources res = mContext.getPackageManager().getResourcesForApplicationAsUser(activityInfo.packageName, mUserId);
        TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AppWidgetProviderInfo);
        // These dimensions has to be resolved in the application's context.
        // We simply send back the raw complex data, which will be
        // converted to dp in {@link AppWidgetManager#getAppWidgetInfo}.
        TypedValue value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minWidth);
        info.minWidth = value != null ? value.data : 0;
        value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minHeight);
        info.minHeight = value != null ? value.data : 0;
        value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minResizeWidth);
        info.minResizeWidth = value != null ? value.data : info.minWidth;
        value = sa.peekValue(com.android.internal.R.styleable.AppWidgetProviderInfo_minResizeHeight);
        info.minResizeHeight = value != null ? value.data : info.minHeight;
        info.updatePeriodMillis = sa.getInt(com.android.internal.R.styleable.AppWidgetProviderInfo_updatePeriodMillis, 0);
        info.initialLayout = sa.getResourceId(com.android.internal.R.styleable.AppWidgetProviderInfo_initialLayout, 0);
        info.initialKeyguardLayout = sa.getResourceId(com.android.internal.R.styleable.AppWidgetProviderInfo_initialKeyguardLayout, 0);
        String className = sa.getString(com.android.internal.R.styleable.AppWidgetProviderInfo_configure);
        if (className != null) {
            info.configure = new ComponentName(component.getPackageName(), className);
        }
        info.label = activityInfo.loadLabel(mContext.getPackageManager()).toString();
        info.icon = ri.getIconResource();
        info.previewImage = sa.getResourceId(com.android.internal.R.styleable.AppWidgetProviderInfo_previewImage, 0);
        info.autoAdvanceViewId = sa.getResourceId(com.android.internal.R.styleable.AppWidgetProviderInfo_autoAdvanceViewId, -1);
        info.resizeMode = sa.getInt(com.android.internal.R.styleable.AppWidgetProviderInfo_resizeMode, AppWidgetProviderInfo.RESIZE_NONE);
        info.widgetCategory = sa.getInt(com.android.internal.R.styleable.AppWidgetProviderInfo_widgetCategory, AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN);
        sa.recycle();
    } catch (Exception e) {
        // Ok to catch Exception here, because anything going wrong because
        // of what a client process passes to us should not be fatal for the
        // system process.
        Slog.w(TAG, "XML parsing failed for AppWidget provider '" + component + '\'', e);
        return null;
    } finally {
        if (parser != null)
            parser.close();
    }
    return p;
}
Also used : ActivityInfo(android.content.pm.ActivityInfo) XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) TypedArray(android.content.res.TypedArray) AppWidgetProviderInfo(android.appwidget.AppWidgetProviderInfo) ComponentName(android.content.ComponentName) Resources(android.content.res.Resources) Point(android.graphics.Point) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) TypedValue(android.util.TypedValue)

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