Search in sources :

Example 46 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 47 with XmlResourceParser

use of android.content.res.XmlResourceParser in project phonegap-facebook-plugin by Wizcorp.

the class PluginManager method loadPlugins.

/**
     * Load plugins from res/xml/config.xml
     */
public void loadPlugins() {
    // First checking the class namespace for config.xml
    int id = this.ctx.getActivity().getResources().getIdentifier("config", "xml", this.ctx.getActivity().getClass().getPackage().getName());
    if (id == 0) {
        // If we couldn't find config.xml there, we'll look in the namespace from AndroidManifest.xml
        id = this.ctx.getActivity().getResources().getIdentifier("config", "xml", this.ctx.getActivity().getPackageName());
        if (id == 0) {
            this.pluginConfigurationMissing();
            //We have the error, we need to exit without crashing!
            return;
        }
    }
    XmlResourceParser xml = this.ctx.getActivity().getResources().getXml(id);
    int eventType = -1;
    String service = "", pluginClass = "", paramType = "";
    boolean onload = false;
    boolean insideFeature = false;
    while (eventType != XmlResourceParser.END_DOCUMENT) {
        if (eventType == XmlResourceParser.START_TAG) {
            String strNode = xml.getName();
            if (strNode.equals("url-filter")) {
                Log.w(TAG, "Plugin " + service + " is using deprecated tag <url-filter>");
                if (urlMap.get(service) == null) {
                    urlMap.put(service, new ArrayList<String>(2));
                }
                List<String> filters = urlMap.get(service);
                filters.add(xml.getAttributeValue(null, "value"));
            } else if (strNode.equals("feature")) {
                //Check for supported feature sets  aka. plugins (Accelerometer, Geolocation, etc)
                //Set the bit for reading params
                insideFeature = true;
                service = xml.getAttributeValue(null, "name");
            } else if (insideFeature && strNode.equals("param")) {
                paramType = xml.getAttributeValue(null, "name");
                if (// check if it is using the older service param
                paramType.equals("service"))
                    service = xml.getAttributeValue(null, "value");
                else if (paramType.equals("package") || paramType.equals("android-package"))
                    pluginClass = xml.getAttributeValue(null, "value");
                else if (paramType.equals("onload"))
                    onload = "true".equals(xml.getAttributeValue(null, "value"));
            }
        } else if (eventType == XmlResourceParser.END_TAG) {
            String strNode = xml.getName();
            if (strNode.equals("feature") || strNode.equals("plugin")) {
                PluginEntry entry = new PluginEntry(service, pluginClass, onload);
                this.addService(entry);
                //Empty the strings to prevent plugin loading bugs
                service = "";
                pluginClass = "";
                insideFeature = false;
            }
        }
        try {
            eventType = xml.next();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) PluginEntry(org.apache.cordova.PluginEntry) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 48 with XmlResourceParser

use of android.content.res.XmlResourceParser in project remusic by aa112901.

the class ColorStateListUtils method createColorStateList.

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;
    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;
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) AttributeSet(android.util.AttributeSet) ColorStateList(android.content.res.ColorStateList) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) TypedValue(android.util.TypedValue)

Example 49 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 50 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)332 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)211 IOException (java.io.IOException)203 Resources (android.content.res.Resources)114 AttributeSet (android.util.AttributeSet)113 TypedArray (android.content.res.TypedArray)80 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)64 PackageManager (android.content.pm.PackageManager)45 ActivityInfo (android.content.pm.ActivityInfo)43 Bundle (android.os.Bundle)42 ComponentName (android.content.ComponentName)41 ArrayList (java.util.ArrayList)30 TypedValue (android.util.TypedValue)29 AssetManager (android.content.res.AssetManager)28 RemoteException (android.os.RemoteException)28 Intent (android.content.Intent)22 ApplicationInfo (android.content.pm.ApplicationInfo)18 InflateException (android.view.InflateException)18 FileNotFoundException (java.io.FileNotFoundException)18 NotFoundException (android.content.res.Resources.NotFoundException)17