Search in sources :

Example 86 with XmlResourceParser

use of android.content.res.XmlResourceParser in project MagicaSakura by Bilibili.

the class DrawableUtils method createDrawable.

static Drawable createDrawable(Context context, int resId) {
    if (resId <= 0)
        return null;
    final TypedValue typedValue = new TypedValue();
    final Resources res = context.getResources();
    res.getValue(resId, typedValue, true);
    Drawable dr = null;
    if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT) {
        dr = new ColorDrawable(ThemeUtils.replaceColorById(context, resId));
    } else {
        try {
            if (typedValue.string != null && typedValue.string.toString().endsWith("xml")) {
                final XmlResourceParser rp = res.getXml(resId);
                final AttributeSet attrs = Xml.asAttributeSet(rp);
                int type;
                while ((type = rp.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
                // Empty loop
                }
                if (type != XmlPullParser.START_TAG) {
                    throw new XmlPullParserException("No start tag found");
                }
                dr = createFromXmlInner(context, rp, attrs);
                rp.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }
    return dr;
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) ColorDrawable(android.graphics.drawable.ColorDrawable) AttributeSet(android.util.AttributeSet) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) IOException(java.io.IOException) TypedValue(android.util.TypedValue)

Example 87 with XmlResourceParser

use of android.content.res.XmlResourceParser in project ActionBarSherlock by JakeWharton.

the class ResourcesCompat method loadLogoFromManifest.

/**
     * Attempt to programmatically load the logo from the manifest file of an
     * activity by using an XML pull parser. This should allow us to read the
     * logo attribute regardless of the platform it is being run on.
     *
     * @param activity Activity instance.
     * @return Logo resource ID.
     */
public static int loadLogoFromManifest(Activity activity) {
    int logo = 0;
    try {
        final String thisPackage = activity.getClass().getName();
        if (ActionBarSherlock.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 (ActionBarSherlock.DEBUG)
                        Log.d(TAG, "Got <application>");
                    for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
                        if (ActionBarSherlock.DEBUG)
                            Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i));
                        if ("logo".equals(xml.getAttributeName(i))) {
                            logo = xml.getAttributeResourceValue(i, 0);
                            //out of for loop
                            break;
                        }
                    }
                } else if ("activity".equals(name)) {
                    //Check if the <activity> is us and has the attribute
                    if (ActionBarSherlock.DEBUG)
                        Log.d(TAG, "Got <activity>");
                    Integer activityLogo = null;
                    String activityPackage = null;
                    boolean isOurActivity = false;
                    for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
                        if (ActionBarSherlock.DEBUG)
                            Log.d(TAG, xml.getAttributeName(i) + ": " + xml.getAttributeValue(i));
                        //We need both uiOptions and name attributes
                        String attrName = xml.getAttributeName(i);
                        if ("logo".equals(attrName)) {
                            activityLogo = xml.getAttributeResourceValue(i, 0);
                        } else if ("name".equals(attrName)) {
                            activityPackage = ActionBarSherlockCompat.cleanActivityName(packageName, xml.getAttributeValue(i));
                            if (!thisPackage.equals(activityPackage)) {
                                //on to the next
                                break;
                            }
                            isOurActivity = true;
                        }
                        //Make sure we have both attributes before processing
                        if ((activityLogo != null) && (activityPackage != null)) {
                            //Our activity, logo specified, override with our value
                            logo = activityLogo.intValue();
                        }
                    }
                    if (isOurActivity) {
                        //do any more processing of the manifest
                        break;
                    }
                }
            }
            eventType = xml.nextToken();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (ActionBarSherlock.DEBUG)
        Log.i(TAG, "Returning " + Integer.toHexString(logo));
    return logo;
}
Also used : AssetManager(android.content.res.AssetManager) XmlResourceParser(android.content.res.XmlResourceParser)

Example 88 with XmlResourceParser

use of android.content.res.XmlResourceParser in project ActionBarSherlock by JakeWharton.

the class ActionBarSherlockCompat method loadUiOptionsFromManifest.

private static int loadUiOptionsFromManifest(Activity activity) {
    int uiOptions = 0;
    try {
        final String thisPackage = activity.getClass().getName();
        if (ActionBarSherlock.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 (ActionBarSherlock.DEBUG)
                        Log.d(TAG, "Got <application>");
                    for (int i = xml.getAttributeCount() - 1; i >= 0; i--) {
                        if (ActionBarSherlock.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 (ActionBarSherlock.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 (ActionBarSherlock.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 (ActionBarSherlock.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 89 with XmlResourceParser

use of android.content.res.XmlResourceParser in project ActionBarSherlock by JakeWharton.

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

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

the class PackageParser method parseBaseApk.

private Package parseBaseApk(File apkFile, AssetManager assets, int flags) throws PackageParserException {
    final String apkPath = apkFile.getAbsolutePath();
    String volumeUuid = null;
    if (apkPath.startsWith(MNT_EXPAND)) {
        final int end = apkPath.indexOf('/', MNT_EXPAND.length());
        volumeUuid = apkPath.substring(MNT_EXPAND.length(), end);
    }
    mParseError = PackageManager.INSTALL_SUCCEEDED;
    mArchiveSourcePath = apkFile.getAbsolutePath();
    if (DEBUG_JAR)
        Slog.d(TAG, "Scanning base APK: " + apkPath);
    final int cookie = loadApkIntoAssetManager(assets, apkPath, flags);
    Resources res = null;
    XmlResourceParser parser = null;
    try {
        res = new Resources(assets, mMetrics, null);
        assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Build.VERSION.RESOURCES_SDK_INT);
        parser = assets.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);
        final String[] outError = new String[1];
        final Package pkg = parseBaseApk(res, parser, flags, outError);
        if (pkg == null) {
            throw new PackageParserException(mParseError, apkPath + " (at " + parser.getPositionDescription() + "): " + outError[0]);
        }
        pkg.setVolumeUuid(volumeUuid);
        pkg.setApplicationVolumeUuid(volumeUuid);
        pkg.setBaseCodePath(apkPath);
        pkg.setSignatures(null);
        return pkg;
    } catch (PackageParserException e) {
        throw e;
    } catch (Exception e) {
        throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION, "Failed to read manifest from " + apkPath, e);
    } finally {
        IoUtils.closeQuietly(parser);
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) Resources(android.content.res.Resources) GeneralSecurityException(java.security.GeneralSecurityException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException)

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