Search in sources :

Example 61 with TypedValue

use of android.util.TypedValue in project platform_frameworks_base by android.

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 62 with TypedValue

use of android.util.TypedValue in project platform_frameworks_base by android.

the class Resources method obtainTempTypedValue.

/**
     * Returns a TypedValue suitable for temporary use. The obtained TypedValue
     * should be released using {@link #releaseTempTypedValue(TypedValue)}.
     *
     * @return a typed value suitable for temporary use
     */
private TypedValue obtainTempTypedValue() {
    TypedValue tmpValue = null;
    synchronized (mTmpValueLock) {
        if (mTmpValue != null) {
            tmpValue = mTmpValue;
            mTmpValue = null;
        }
    }
    if (tmpValue == null) {
        return new TypedValue();
    }
    return tmpValue;
}
Also used : TypedValue(android.util.TypedValue)

Example 63 with TypedValue

use of android.util.TypedValue in project platform_frameworks_base by android.

the class Resources method getColor.

/**
     * Returns a themed color integer associated with a particular resource ID.
     * If the resource holds a complex {@link ColorStateList}, then the default
     * color from the set is returned.
     *
     * @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 theme The theme used to style the color attributes, may be
     *              {@code null}.
     *
     * @throws NotFoundException Throws NotFoundException if the given ID does
     *         not exist.
     *
     * @return A single color value in the form 0xAARRGGBB.
     */
@ColorInt
public int getColor(@ColorRes int id, @Nullable Theme theme) throws NotFoundException {
    final TypedValue value = obtainTempTypedValue();
    try {
        final ResourcesImpl impl = mResourcesImpl;
        impl.getValue(id, value, true);
        if (value.type >= TypedValue.TYPE_FIRST_INT && value.type <= TypedValue.TYPE_LAST_INT) {
            return value.data;
        } else if (value.type != TypedValue.TYPE_STRING) {
            throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) + " type #0x" + Integer.toHexString(value.type) + " is not valid");
        }
        final ColorStateList csl = impl.loadColorStateList(this, value, id, theme);
        return csl.getDefaultColor();
    } finally {
        releaseTempTypedValue(value);
    }
}
Also used : TypedValue(android.util.TypedValue) ColorInt(android.annotation.ColorInt)

Example 64 with TypedValue

use of android.util.TypedValue in project platform_frameworks_base by android.

the class Resources method getDrawable.

/**
     * Return a drawable object associated with a particular resource ID and
     * styled for the specified theme. Various types of objects will be
     * returned depending on the underlying resource -- for example, a solid
     * color, PNG image, scalable image, etc.
     *
     * @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 theme The theme used to style the drawable attributes, may be {@code null}.
     * @return Drawable An object that can be used to draw this resource.
     * @throws NotFoundException Throws NotFoundException if the given ID does
     *         not exist.
     */
public Drawable getDrawable(@DrawableRes int id, @Nullable Theme theme) throws NotFoundException {
    final TypedValue value = obtainTempTypedValue();
    try {
        final ResourcesImpl impl = mResourcesImpl;
        impl.getValue(id, value, true);
        return impl.loadDrawable(this, value, id, theme, true);
    } finally {
        releaseTempTypedValue(value);
    }
}
Also used : TypedValue(android.util.TypedValue)

Example 65 with TypedValue

use of android.util.TypedValue in project platform_frameworks_base by android.

the class WindowDecorActionBar method getThemedContext.

public Context getThemedContext() {
    if (mThemedContext == null) {
        TypedValue outValue = new TypedValue();
        Resources.Theme currentTheme = mContext.getTheme();
        currentTheme.resolveAttribute(com.android.internal.R.attr.actionBarWidgetTheme, outValue, true);
        final int targetThemeRes = outValue.resourceId;
        if (targetThemeRes != 0 && mContext.getThemeResId() != targetThemeRes) {
            mThemedContext = new ContextThemeWrapper(mContext, targetThemeRes);
        } else {
            mThemedContext = mContext;
        }
    }
    return mThemedContext;
}
Also used : ContextThemeWrapper(android.view.ContextThemeWrapper) Resources(android.content.res.Resources) TypedValue(android.util.TypedValue)

Aggregations

TypedValue (android.util.TypedValue)844 TypedArray (android.content.res.TypedArray)190 Resources (android.content.res.Resources)92 ContextThemeWrapper (android.view.ContextThemeWrapper)52 Context (android.content.Context)50 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)39 AttributeSet (android.util.AttributeSet)34 View (android.view.View)30 XmlResourceParser (android.content.res.XmlResourceParser)29 Point (android.graphics.Point)26 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)26 ColorStateList (android.content.res.ColorStateList)23 DisplayMetrics (android.util.DisplayMetrics)23 LinearLayout (android.widget.LinearLayout)20 Bundle (android.os.Bundle)19 SpannableString (android.text.SpannableString)19