Search in sources :

Example 26 with TypedValue

use of android.util.TypedValue in project android_frameworks_base by ParanoidAndroid.

the class Resources method parseBundleExtra.

/**
     * Parse a name/value pair out of an XML tag holding that data.  The
     * AttributeSet must be holding the data defined by
     * {@link android.R.styleable#Extra}.  The following value types are supported:
     * <ul>
     * <li> {@link TypedValue#TYPE_STRING}:
     * {@link Bundle#putCharSequence Bundle.putCharSequence()}
     * <li> {@link TypedValue#TYPE_INT_BOOLEAN}:
     * {@link Bundle#putCharSequence Bundle.putBoolean()}
     * <li> {@link TypedValue#TYPE_FIRST_INT}-{@link TypedValue#TYPE_LAST_INT}:
     * {@link Bundle#putCharSequence Bundle.putBoolean()}
     * <li> {@link TypedValue#TYPE_FLOAT}:
     * {@link Bundle#putCharSequence Bundle.putFloat()}
     * </ul>
     * 
     * @param tagName The name of the tag these attributes come from; this is
     * only used for reporting error messages.
     * @param attrs The attributes from which to retrieve the name/value pair.
     * @param outBundle The Bundle in which to place the parsed value.
     * @throws XmlPullParserException If the attributes are not valid.
     */
public void parseBundleExtra(String tagName, AttributeSet attrs, Bundle outBundle) throws XmlPullParserException {
    TypedArray sa = obtainAttributes(attrs, com.android.internal.R.styleable.Extra);
    String name = sa.getString(com.android.internal.R.styleable.Extra_name);
    if (name == null) {
        sa.recycle();
        throw new XmlPullParserException("<" + tagName + "> requires an android:name attribute at " + attrs.getPositionDescription());
    }
    TypedValue v = sa.peekValue(com.android.internal.R.styleable.Extra_value);
    if (v != null) {
        if (v.type == TypedValue.TYPE_STRING) {
            CharSequence cs = v.coerceToString();
            outBundle.putCharSequence(name, cs);
        } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
            outBundle.putBoolean(name, v.data != 0);
        } else if (v.type >= TypedValue.TYPE_FIRST_INT && v.type <= TypedValue.TYPE_LAST_INT) {
            outBundle.putInt(name, v.data);
        } else if (v.type == TypedValue.TYPE_FLOAT) {
            outBundle.putFloat(name, v.getFloat());
        } else {
            sa.recycle();
            throw new XmlPullParserException("<" + tagName + "> only supports string, integer, float, color, and boolean at " + attrs.getPositionDescription());
        }
    } else {
        sa.recycle();
        throw new XmlPullParserException("<" + tagName + "> requires an android:value or android:resource attribute at " + attrs.getPositionDescription());
    }
    sa.recycle();
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException) TypedValue(android.util.TypedValue)

Example 27 with TypedValue

use of android.util.TypedValue in project android_frameworks_base by ParanoidAndroid.

the class Resources method openRawResource.

/**
     * Open a data stream for reading a raw resource.  This can only be used
     * with resources whose value is the name of an asset files -- that is, it can be
     * used to open drawable, sound, and raw resources; it will fail on string
     * and color resources.
     * 
     * @param id The resource identifier to open, as generated by the appt
     *           tool.
     * 
     * @return InputStream Access to the resource data.
     *
     * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
     * 
     */
public InputStream openRawResource(int id) throws NotFoundException {
    TypedValue value;
    synchronized (mAccessLock) {
        value = mTmpValue;
        if (value == null) {
            value = new TypedValue();
        } else {
            mTmpValue = null;
        }
    }
    InputStream res = openRawResource(id, value);
    synchronized (mAccessLock) {
        if (mTmpValue == null) {
            mTmpValue = value;
        }
    }
    return res;
}
Also used : InputStream(java.io.InputStream) TypedValue(android.util.TypedValue)

Example 28 with TypedValue

use of android.util.TypedValue in project android_frameworks_base by ParanoidAndroid.

the class Resources method getDrawable.

/**
     * Return a drawable object associated with a particular resource ID.
     * Various types of objects will be returned depending on the underlying
     * resource -- for example, a solid color, PNG image, scalable image, etc.
     * The Drawable API hides these implementation details.
     *
     * <p class="note"><strong>Note:</strong> Prior to
     * {@link android.os.Build.VERSION_CODES#JELLY_BEAN}, this function
     * would not correctly retrieve the final configuration density when
     * the resource ID passed here is an alias to another Drawable resource.
     * This means that if the density configuration of the alias resource
     * is different than the actual resource, the density of the returned
     * Drawable would be incorrect, resulting in bad scaling.  To work
     * around this, you can instead retrieve the Drawable through
     * {@link TypedArray#getDrawable TypedArray.getDrawable}.  Use
     * {@link android.content.Context#obtainStyledAttributes(int[])
     * Context.obtainStyledAttributes} with
     * an array containing the resource ID of interest to create the TypedArray.</p>
     *
     * @param id The desired resource identifier, as generated by the aapt
     *           tool. This integer encodes the package, type, and resource
     *           entry. The value 0 is an invalid identifier.
     *
     * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
     * 
     * @return Drawable An object that can be used to draw this resource.
     */
public Drawable getDrawable(int id) throws NotFoundException {
    TypedValue value;
    synchronized (mAccessLock) {
        value = mTmpValue;
        if (value == null) {
            value = new TypedValue();
        } else {
            mTmpValue = null;
        }
        getValue(id, value, true);
    }
    Drawable res = loadDrawable(value, id);
    synchronized (mAccessLock) {
        if (mTmpValue == null) {
            mTmpValue = value;
        }
    }
    return res;
}
Also used : ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) TypedValue(android.util.TypedValue)

Example 29 with TypedValue

use of android.util.TypedValue in project android_frameworks_base by ParanoidAndroid.

the class AssetManager method getResourceBagText.

/**
     * Retrieve the string value associated with a particular resource
     * identifier for the current configuration / skin.
     */
/*package*/
final CharSequence getResourceBagText(int ident, int bagEntryId) {
    synchronized (this) {
        TypedValue tmpValue = mValue;
        int block = loadResourceBagValue(ident, bagEntryId, tmpValue, true);
        if (block >= 0) {
            if (tmpValue.type == TypedValue.TYPE_STRING) {
                return mStringBlocks[block].get(tmpValue.data);
            }
            return tmpValue.coerceToString();
        }
    }
    return null;
}
Also used : TypedValue(android.util.TypedValue)

Example 30 with TypedValue

use of android.util.TypedValue in project android_frameworks_base by ParanoidAndroid.

the class Resources method getFraction.

/**
     * Retrieve a fractional unit for a particular resource ID.
     * 
     * @param id The desired resource identifier, as generated by the aapt
     *           tool. This integer encodes the package, type, and resource
     *           entry. The value 0 is an invalid identifier.
     * @param base The base value of this fraction.  In other words, a 
     *             standard fraction is multiplied by this value.
     * @param pbase The parent base value of this fraction.  In other 
     *             words, a parent fraction (nn%p) is multiplied by this
     *             value.
     * 
     * @return Attribute fractional value multiplied by the appropriate 
     * base value.
     *  
     * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
     */
public float getFraction(int id, int base, int pbase) {
    synchronized (mAccessLock) {
        TypedValue value = mTmpValue;
        if (value == null) {
            mTmpValue = value = new TypedValue();
        }
        getValue(id, value, true);
        if (value.type == TypedValue.TYPE_FRACTION) {
            return TypedValue.complexToFraction(value.data, base, pbase);
        }
        throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) + " type #0x" + Integer.toHexString(value.type) + " is not valid");
    }
}
Also used : TypedValue(android.util.TypedValue)

Aggregations

TypedValue (android.util.TypedValue)836 TypedArray (android.content.res.TypedArray)185 Resources (android.content.res.Resources)92 ContextThemeWrapper (android.view.ContextThemeWrapper)52 Context (android.content.Context)49 Drawable (android.graphics.drawable.Drawable)49 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)46 TextView (android.widget.TextView)44 Paint (android.graphics.Paint)41 IOException (java.io.IOException)40 AttributeSet (android.util.AttributeSet)34 XmlResourceParser (android.content.res.XmlResourceParser)29 View (android.view.View)27 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)26 Point (android.graphics.Point)25 ColorStateList (android.content.res.ColorStateList)23 DisplayMetrics (android.util.DisplayMetrics)23 LinearLayout (android.widget.LinearLayout)20 SpannableString (android.text.SpannableString)19 Bundle (android.os.Bundle)18