Example usage for android.content.res Resources obtainAttributes

List of usage examples for android.content.res Resources obtainAttributes

Introduction

In this page you can find the example usage for android.content.res Resources obtainAttributes.

Prototype

public TypedArray obtainAttributes(AttributeSet set, @StyleableRes int[] attrs) 

Source Link

Document

Retrieve a set of basic attribute values from an AttributeSet, not performing styling of them using a theme and/or style resources.

Usage

From source file:com.android.server.MountService.java

private void readStorageListLocked() {
    mVolumes.clear();/*from w  w w.  j a  v  a 2 s  . co  m*/
    mVolumeStates.clear();

    Resources resources = mContext.getResources();

    int id = com.android.internal.R.xml.storage_list;
    XmlResourceParser parser = resources.getXml(id);
    AttributeSet attrs = Xml.asAttributeSet(parser);

    try {
        XmlUtils.beginDocument(parser, TAG_STORAGE_LIST);
        while (true) {
            XmlUtils.nextElement(parser);

            String element = parser.getName();
            if (element == null)
                break;

            if (TAG_STORAGE.equals(element)) {
                TypedArray a = resources.obtainAttributes(attrs, com.android.internal.R.styleable.Storage);

                String path = a.getString(com.android.internal.R.styleable.Storage_mountPoint);
                int descriptionId = a.getResourceId(com.android.internal.R.styleable.Storage_storageDescription,
                        -1);
                CharSequence description = a
                        .getText(com.android.internal.R.styleable.Storage_storageDescription);
                boolean primary = a.getBoolean(com.android.internal.R.styleable.Storage_primary, false);
                boolean removable = a.getBoolean(com.android.internal.R.styleable.Storage_removable, false);
                boolean emulated = a.getBoolean(com.android.internal.R.styleable.Storage_emulated, false);
                int mtpReserve = a.getInt(com.android.internal.R.styleable.Storage_mtpReserve, 0);
                boolean allowMassStorage = a
                        .getBoolean(com.android.internal.R.styleable.Storage_allowMassStorage, false);
                boolean allowMtp = a.getBoolean(com.android.internal.R.styleable.Storage_allowMtp, true);
                // resource parser does not support longs, so XML value is in megabytes
                long maxFileSize = a.getInt(com.android.internal.R.styleable.Storage_maxFileSize, 0) * 1024L
                        * 1024L;

                Slog.d(TAG,
                        "got storage path: " + path + " description: " + description + " primary: " + primary
                                + " removable: " + removable + " emulated: " + emulated + " mtpReserve: "
                                + mtpReserve + " allowMassStorage: " + allowMassStorage + " maxFileSize: "
                                + maxFileSize + " allowMtp: " + allowMtp);

                if (emulated) {
                    // For devices with emulated storage, we create separate
                    // volumes for each known user.
                    mEmulatedTemplate = new StorageVolume(null, descriptionId, true, false, true, mtpReserve,
                            false, maxFileSize, null, allowMtp);

                    final UserManagerService userManager = UserManagerService.getInstance();
                    for (UserInfo user : userManager.getUsers(false)) {
                        createEmulatedVolumeForUserLocked(user.getUserHandle());
                    }

                } else {
                    if (path == null || description == null) {
                        Slog.e(TAG, "Missing storage path or description in readStorageList");
                    } else {
                        final StorageVolume volume = new StorageVolume(new File(path), descriptionId, primary,
                                removable, emulated, mtpReserve, allowMassStorage, maxFileSize, null, allowMtp);
                        addVolumeLocked(volume);

                        // Until we hear otherwise, treat as unmounted
                        mVolumeStates.put(volume.getPath(), Environment.MEDIA_UNMOUNTED);
                        volume.setState(Environment.MEDIA_UNMOUNTED);
                    }
                }

                a.recycle();
            }
        }
    } catch (XmlPullParserException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        // Compute storage ID for each physical volume; emulated storage is
        // always 0 when defined.
        int index = isExternalStorageEmulated() ? 1 : 0;
        for (StorageVolume volume : mVolumes) {
            if (!volume.isEmulated()) {
                volume.setStorageId(index++);
            }
        }
        parser.close();
    }
}

From source file:android.content.pm.PackageParser.java

private static VerifierInfo parseVerifier(Resources res, XmlPullParser parser, AttributeSet attrs, int flags) {
    final TypedArray sa = res.obtainAttributes(attrs,
            com.android.internal.R.styleable.AndroidManifestPackageVerifier);

    final String packageName = sa
            .getNonResourceString(com.android.internal.R.styleable.AndroidManifestPackageVerifier_name);

    final String encodedPublicKey = sa
            .getNonResourceString(com.android.internal.R.styleable.AndroidManifestPackageVerifier_publicKey);

    sa.recycle();/*from  w  w w.  j  a v  a  2  s  .  co m*/

    if (packageName == null || packageName.length() == 0) {
        Slog.i(TAG, "verifier package name was null; skipping");
        return null;
    }

    final PublicKey publicKey = parsePublicKey(encodedPublicKey);
    if (publicKey == null) {
        Slog.i(TAG, "Unable to parse verifier public key for " + packageName);
        return null;
    }

    return new VerifierInfo(packageName, publicKey);
}

From source file:android.content.pm.PackageParser.java

private boolean parseUsesPermission(Package pkg, Resources res, XmlResourceParser parser, AttributeSet attrs)
        throws XmlPullParserException, IOException {
    TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifestUsesPermission);

    // Note: don't allow this value to be a reference to a resource
    // that may change.
    String name = sa.getNonResourceString(com.android.internal.R.styleable.AndroidManifestUsesPermission_name);

    int maxSdkVersion = 0;
    TypedValue val = sa.peekValue(com.android.internal.R.styleable.AndroidManifestUsesPermission_maxSdkVersion);
    if (val != null) {
        if (val.type >= TypedValue.TYPE_FIRST_INT && val.type <= TypedValue.TYPE_LAST_INT) {
            maxSdkVersion = val.data;
        }/*from  www. j  a va 2  s. c o  m*/
    }

    sa.recycle();

    if ((maxSdkVersion == 0) || (maxSdkVersion >= Build.VERSION.RESOURCES_SDK_INT)) {
        if (name != null) {
            int index = pkg.requestedPermissions.indexOf(name);
            if (index == -1) {
                pkg.requestedPermissions.add(name.intern());
            } else {
                Slog.w(TAG, "Ignoring duplicate uses-permissions/uses-permissions-sdk-m: " + name
                        + " in package: " + pkg.packageName + " at: " + parser.getPositionDescription());
            }
        }
    }

    XmlUtils.skipCurrentTag(parser);
    return true;
}

From source file:android.content.pm.PackageParser.java

private FeatureInfo parseUsesFeature(Resources res, AttributeSet attrs)
        throws XmlPullParserException, IOException {
    FeatureInfo fi = new FeatureInfo();
    TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifestUsesFeature);
    // Note: don't allow this value to be a reference to a resource
    // that may change.
    fi.name = sa.getNonResourceString(com.android.internal.R.styleable.AndroidManifestUsesFeature_name);
    if (fi.name == null) {
        fi.reqGlEsVersion = sa.getInt(com.android.internal.R.styleable.AndroidManifestUsesFeature_glEsVersion,
                FeatureInfo.GL_ES_VERSION_UNDEFINED);
    }/*ww  w.  j  a v  a  2s  . c  o  m*/
    if (sa.getBoolean(com.android.internal.R.styleable.AndroidManifestUsesFeature_required, true)) {
        fi.flags |= FeatureInfo.FLAG_REQUIRED;
    }
    sa.recycle();
    return fi;
}

From source file:android.content.pm.PackageParser.java

private Instrumentation parseInstrumentation(Package owner, Resources res, XmlPullParser parser,
        AttributeSet attrs, String[] outError) throws XmlPullParserException, IOException {
    TypedArray sa = res.obtainAttributes(attrs,
            com.android.internal.R.styleable.AndroidManifestInstrumentation);

    if (mParseInstrumentationArgs == null) {
        mParseInstrumentationArgs = new ParsePackageItemArgs(owner, outError,
                com.android.internal.R.styleable.AndroidManifestInstrumentation_name,
                com.android.internal.R.styleable.AndroidManifestInstrumentation_label,
                com.android.internal.R.styleable.AndroidManifestInstrumentation_icon,
                com.android.internal.R.styleable.AndroidManifestInstrumentation_logo,
                com.android.internal.R.styleable.AndroidManifestInstrumentation_banner);
        mParseInstrumentationArgs.tag = "<instrumentation>";
    }//from   w  ww .  j a  v a2s .  co  m

    mParseInstrumentationArgs.sa = sa;

    Instrumentation a = new Instrumentation(mParseInstrumentationArgs, new InstrumentationInfo());
    if (outError[0] != null) {
        sa.recycle();
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return null;
    }

    String str;
    // Note: don't allow this value to be a reference to a resource
    // that may change.
    str = sa.getNonResourceString(
            com.android.internal.R.styleable.AndroidManifestInstrumentation_targetPackage);
    a.info.targetPackage = str != null ? str.intern() : null;

    a.info.handleProfiling = sa
            .getBoolean(com.android.internal.R.styleable.AndroidManifestInstrumentation_handleProfiling, false);

    a.info.functionalTest = sa
            .getBoolean(com.android.internal.R.styleable.AndroidManifestInstrumentation_functionalTest, false);

    sa.recycle();

    if (a.info.targetPackage == null) {
        outError[0] = "<instrumentation> does not specify targetPackage";
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return null;
    }

    if (!parseAllMetaData(res, parser, attrs, "<instrumentation>", a, outError)) {
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return null;
    }

    owner.instrumentation.add(a);

    return a;
}

From source file:android.content.pm.PackageParser.java

private Bundle parseMetaData(Resources res, XmlPullParser parser, AttributeSet attrs, Bundle data,
        String[] outError) throws XmlPullParserException, IOException {

    TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifestMetaData);

    if (data == null) {
        data = new Bundle();
    }/* w  w  w  .  j  a v a 2  s  .c om*/

    String name = sa.getNonConfigurationString(com.android.internal.R.styleable.AndroidManifestMetaData_name,
            0);
    if (name == null) {
        outError[0] = "<meta-data> requires an android:name attribute";
        sa.recycle();
        return null;
    }

    name = name.intern();

    TypedValue v = sa.peekValue(com.android.internal.R.styleable.AndroidManifestMetaData_resource);
    if (v != null && v.resourceId != 0) {
        //Slog.i(TAG, "Meta data ref " + name + ": " + v);
        data.putInt(name, v.resourceId);
    } else {
        v = sa.peekValue(com.android.internal.R.styleable.AndroidManifestMetaData_value);
        //Slog.i(TAG, "Meta data " + name + ": " + v);
        if (v != null) {
            if (v.type == TypedValue.TYPE_STRING) {
                CharSequence cs = v.coerceToString();
                data.putString(name, cs != null ? cs.toString().intern() : null);
            } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
                data.putBoolean(name, v.data != 0);
            } else if (v.type >= TypedValue.TYPE_FIRST_INT && v.type <= TypedValue.TYPE_LAST_INT) {
                data.putInt(name, v.data);
            } else if (v.type == TypedValue.TYPE_FLOAT) {
                data.putFloat(name, v.getFloat());
            } else {
                if (!RIGID_PARSER) {
                    Slog.w(TAG,
                            "<meta-data> only supports string, integer, float, color, boolean, and resource reference types: "
                                    + parser.getName() + " at " + mArchiveSourcePath + " "
                                    + parser.getPositionDescription());
                } else {
                    outError[0] = "<meta-data> only supports string, integer, float, color, boolean, and resource reference types";
                    data = null;
                }
            }
        } else {
            outError[0] = "<meta-data> requires an android:value or android:resource attribute";
            data = null;
        }
    }

    sa.recycle();

    XmlUtils.skipCurrentTag(parser);

    return data;
}

From source file:android.content.pm.PackageParser.java

private Permission parsePermissionTree(Package owner, Resources res, XmlPullParser parser, AttributeSet attrs,
        String[] outError) throws XmlPullParserException, IOException {
    Permission perm = new Permission(owner);

    TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifestPermissionTree);

    if (!parsePackageItemInfo(owner, perm.info, outError, "<permission-tree>", sa,
            com.android.internal.R.styleable.AndroidManifestPermissionTree_name,
            com.android.internal.R.styleable.AndroidManifestPermissionTree_label,
            com.android.internal.R.styleable.AndroidManifestPermissionTree_icon,
            com.android.internal.R.styleable.AndroidManifestPermissionTree_logo,
            com.android.internal.R.styleable.AndroidManifestPermissionTree_banner)) {
        sa.recycle();/*from  ww w  .j ava  2s  . c  om*/
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return null;
    }

    sa.recycle();

    int index = perm.info.name.indexOf('.');
    if (index > 0) {
        index = perm.info.name.indexOf('.', index + 1);
    }
    if (index < 0) {
        outError[0] = "<permission-tree> name has less than three segments: " + perm.info.name;
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return null;
    }

    perm.info.descriptionRes = 0;
    perm.info.protectionLevel = PermissionInfo.PROTECTION_NORMAL;
    perm.tree = true;

    if (!parseAllMetaData(res, parser, attrs, "<permission-tree>", perm, outError)) {
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return null;
    }

    owner.permissions.add(perm);

    return perm;
}

From source file:android.content.pm.PackageParser.java

private PermissionGroup parsePermissionGroup(Package owner, int flags, Resources res, XmlPullParser parser,
        AttributeSet attrs, String[] outError) throws XmlPullParserException, IOException {
    PermissionGroup perm = new PermissionGroup(owner);

    TypedArray sa = res.obtainAttributes(attrs,
            com.android.internal.R.styleable.AndroidManifestPermissionGroup);

    if (!parsePackageItemInfo(owner, perm.info, outError, "<permission-group>", sa,
            com.android.internal.R.styleable.AndroidManifestPermissionGroup_name,
            com.android.internal.R.styleable.AndroidManifestPermissionGroup_label,
            com.android.internal.R.styleable.AndroidManifestPermissionGroup_icon,
            com.android.internal.R.styleable.AndroidManifestPermissionGroup_logo,
            com.android.internal.R.styleable.AndroidManifestPermissionGroup_banner)) {
        sa.recycle();//from   w w  w  .  j  ava  2s. c  om
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return null;
    }

    perm.info.descriptionRes = sa
            .getResourceId(com.android.internal.R.styleable.AndroidManifestPermissionGroup_description, 0);
    perm.info.flags = sa
            .getInt(com.android.internal.R.styleable.AndroidManifestPermissionGroup_permissionGroupFlags, 0);
    perm.info.priority = sa.getInt(com.android.internal.R.styleable.AndroidManifestPermissionGroup_priority, 0);
    if (perm.info.priority > 0 && (flags & PARSE_IS_SYSTEM) == 0) {
        perm.info.priority = 0;
    }

    sa.recycle();

    if (!parseAllMetaData(res, parser, attrs, "<permission-group>", perm, outError)) {
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return null;
    }

    owner.permissionGroups.add(perm);

    return perm;
}

From source file:android.content.pm.PackageParser.java

private Permission parsePermission(Package owner, Resources res, XmlPullParser parser, AttributeSet attrs,
        String[] outError) throws XmlPullParserException, IOException {
    Permission perm = new Permission(owner);

    TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifestPermission);

    if (!parsePackageItemInfo(owner, perm.info, outError, "<permission>", sa,
            com.android.internal.R.styleable.AndroidManifestPermission_name,
            com.android.internal.R.styleable.AndroidManifestPermission_label,
            com.android.internal.R.styleable.AndroidManifestPermission_icon,
            com.android.internal.R.styleable.AndroidManifestPermission_logo,
            com.android.internal.R.styleable.AndroidManifestPermission_banner)) {
        sa.recycle();/*  w ww  . j a va 2 s.  co m*/
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return null;
    }

    // Note: don't allow this value to be a reference to a resource
    // that may change.
    perm.info.group = sa
            .getNonResourceString(com.android.internal.R.styleable.AndroidManifestPermission_permissionGroup);
    if (perm.info.group != null) {
        perm.info.group = perm.info.group.intern();
    }

    perm.info.descriptionRes = sa
            .getResourceId(com.android.internal.R.styleable.AndroidManifestPermission_description, 0);

    perm.info.protectionLevel = sa.getInt(
            com.android.internal.R.styleable.AndroidManifestPermission_protectionLevel,
            PermissionInfo.PROTECTION_NORMAL);

    perm.info.flags = sa.getInt(com.android.internal.R.styleable.AndroidManifestPermission_permissionFlags, 0);

    sa.recycle();

    if (perm.info.protectionLevel == -1) {
        outError[0] = "<permission> does not specify protectionLevel";
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return null;
    }

    perm.info.protectionLevel = PermissionInfo.fixProtectionLevel(perm.info.protectionLevel);

    if ((perm.info.protectionLevel & PermissionInfo.PROTECTION_MASK_FLAGS) != 0) {
        if ((perm.info.protectionLevel
                & PermissionInfo.PROTECTION_MASK_BASE) != PermissionInfo.PROTECTION_SIGNATURE) {
            outError[0] = "<permission>  protectionLevel specifies a flag but is "
                    + "not based on signature type";
            mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
            return null;
        }
    }

    if (!parseAllMetaData(res, parser, attrs, "<permission>", perm, outError)) {
        mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
        return null;
    }

    owner.permissions.add(perm);

    return perm;
}

From source file:android.content.pm.PackageParser.java

private Service parseService(Package owner, Resources res, XmlPullParser parser, AttributeSet attrs, int flags,
        String[] outError) throws XmlPullParserException, IOException {
    TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifestService);

    if (mParseServiceArgs == null) {
        mParseServiceArgs = new ParseComponentArgs(owner, outError,
                com.android.internal.R.styleable.AndroidManifestService_name,
                com.android.internal.R.styleable.AndroidManifestService_label,
                com.android.internal.R.styleable.AndroidManifestService_icon,
                com.android.internal.R.styleable.AndroidManifestService_logo,
                com.android.internal.R.styleable.AndroidManifestService_banner, mSeparateProcesses,
                com.android.internal.R.styleable.AndroidManifestService_process,
                com.android.internal.R.styleable.AndroidManifestService_description,
                com.android.internal.R.styleable.AndroidManifestService_enabled);
        mParseServiceArgs.tag = "<service>";
    }/*from  w  w w  .  j a  v  a 2s. c o  m*/

    mParseServiceArgs.sa = sa;
    mParseServiceArgs.flags = flags;

    Service s = new Service(mParseServiceArgs, new ServiceInfo());
    if (outError[0] != null) {
        sa.recycle();
        return null;
    }

    boolean setExported = sa.hasValue(com.android.internal.R.styleable.AndroidManifestService_exported);
    if (setExported) {
        s.info.exported = sa.getBoolean(com.android.internal.R.styleable.AndroidManifestService_exported,
                false);
    }

    String str = sa
            .getNonConfigurationString(com.android.internal.R.styleable.AndroidManifestService_permission, 0);
    if (str == null) {
        s.info.permission = owner.applicationInfo.permission;
    } else {
        s.info.permission = str.length() > 0 ? str.toString().intern() : null;
    }

    s.info.flags = 0;
    if (sa.getBoolean(com.android.internal.R.styleable.AndroidManifestService_stopWithTask, false)) {
        s.info.flags |= ServiceInfo.FLAG_STOP_WITH_TASK;
    }
    if (sa.getBoolean(com.android.internal.R.styleable.AndroidManifestService_isolatedProcess, false)) {
        s.info.flags |= ServiceInfo.FLAG_ISOLATED_PROCESS;
    }
    if (sa.getBoolean(com.android.internal.R.styleable.AndroidManifestService_singleUser, false)) {
        s.info.flags |= ServiceInfo.FLAG_SINGLE_USER;
        if (s.info.exported && (flags & PARSE_IS_PRIVILEGED) == 0) {
            Slog.w(TAG, "Service exported request ignored due to singleUser: " + s.className + " at "
                    + mArchiveSourcePath + " " + parser.getPositionDescription());
            s.info.exported = false;
            setExported = true;
        }
    }

    sa.recycle();

    if ((owner.applicationInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
        // A heavy-weight application can not have services in its main process
        // We can do direct compare because we intern all strings.
        if (s.info.processName == owner.packageName) {
            outError[0] = "Heavy-weight applications can not have services in main process";
            return null;
        }
    }

    int outerDepth = parser.getDepth();
    int type;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
            continue;
        }

        if (parser.getName().equals("intent-filter")) {
            ServiceIntentInfo intent = new ServiceIntentInfo(s);
            if (!parseIntent(res, parser, attrs, true, false, intent, outError)) {
                return null;
            }

            s.intents.add(intent);
        } else if (parser.getName().equals("meta-data")) {
            if ((s.metaData = parseMetaData(res, parser, attrs, s.metaData, outError)) == null) {
                return null;
            }
        } else {
            if (!RIGID_PARSER) {
                Slog.w(TAG, "Unknown element under <service>: " + parser.getName() + " at " + mArchiveSourcePath
                        + " " + parser.getPositionDescription());
                XmlUtils.skipCurrentTag(parser);
                continue;
            } else {
                outError[0] = "Bad element under <service>: " + parser.getName();
                return null;
            }
        }
    }

    if (!setExported) {
        s.info.exported = s.intents.size() > 0;
    }

    return s;
}