Search in sources :

Example 1 with ColorInt

use of android.annotation.ColorInt in project platform_frameworks_base by android.

the class Utils method getColorAccent.

@ColorInt
public static int getColorAccent(Context context) {
    TypedArray ta = context.obtainStyledAttributes(new int[] { android.R.attr.colorAccent });
    @ColorInt int colorAccent = ta.getColor(0, 0);
    ta.recycle();
    return colorAccent;
}
Also used : ColorInt(android.annotation.ColorInt) TypedArray(android.content.res.TypedArray) ColorInt(android.annotation.ColorInt)

Example 2 with ColorInt

use of android.annotation.ColorInt 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 3 with ColorInt

use of android.annotation.ColorInt in project platform_frameworks_base by android.

the class TypedArray method getColor.

/**
     * Retrieve the color value for the attribute at <var>index</var>.  If
     * the attribute references a color resource holding a complex
     * {@link android.content.res.ColorStateList}, then the default color from
     * the set is returned.
     * <p>
     * This method will throw an exception if the attribute is defined but is
     * not an integer color or color state list.
     *
     * @param index Index of attribute to retrieve.
     * @param defValue Value to return if the attribute is not defined or
     *                 not a resource.
     *
     * @return Attribute color value, or defValue if not defined.
     * @throws RuntimeException if the TypedArray has already been recycled.
     * @throws UnsupportedOperationException if the attribute is defined but is
     *         not an integer color or color state list.
     */
@ColorInt
public int getColor(@StyleableRes int index, @ColorInt int defValue) {
    if (mRecycled) {
        throw new RuntimeException("Cannot make calls to a recycled instance!");
    }
    final int attrIndex = index;
    index *= AssetManager.STYLE_NUM_ENTRIES;
    final int[] data = mData;
    final int type = data[index + AssetManager.STYLE_TYPE];
    if (type == TypedValue.TYPE_NULL) {
        return defValue;
    } else if (type >= TypedValue.TYPE_FIRST_INT && type <= TypedValue.TYPE_LAST_INT) {
        return data[index + AssetManager.STYLE_DATA];
    } else if (type == TypedValue.TYPE_STRING) {
        final TypedValue value = mValue;
        if (getValueAt(index, value)) {
            final ColorStateList csl = mResources.loadColorStateList(value, value.resourceId, mTheme);
            return csl.getDefaultColor();
        }
        return defValue;
    } else if (type == TypedValue.TYPE_ATTRIBUTE) {
        final TypedValue value = mValue;
        getValueAt(index, value);
        throw new UnsupportedOperationException("Failed to resolve attribute at index " + attrIndex + ": " + value);
    }
    throw new UnsupportedOperationException("Can't convert value at index " + attrIndex + " to color: type=0x" + Integer.toHexString(type));
}
Also used : TypedValue(android.util.TypedValue) ColorInt(android.annotation.ColorInt)

Example 4 with ColorInt

use of android.annotation.ColorInt in project android_frameworks_base by ResurrectionRemix.

the class TypedArray method getColor.

/**
     * Retrieve the color value for the attribute at <var>index</var>.  If
     * the attribute references a color resource holding a complex
     * {@link android.content.res.ColorStateList}, then the default color from
     * the set is returned.
     * <p>
     * This method will throw an exception if the attribute is defined but is
     * not an integer color or color state list.
     *
     * @param index Index of attribute to retrieve.
     * @param defValue Value to return if the attribute is not defined or
     *                 not a resource.
     *
     * @return Attribute color value, or defValue if not defined.
     * @throws RuntimeException if the TypedArray has already been recycled.
     * @throws UnsupportedOperationException if the attribute is defined but is
     *         not an integer color or color state list.
     */
@ColorInt
public int getColor(@StyleableRes int index, @ColorInt int defValue) {
    if (mRecycled) {
        throw new RuntimeException("Cannot make calls to a recycled instance!");
    }
    final int attrIndex = index;
    index *= AssetManager.STYLE_NUM_ENTRIES;
    final int[] data = mData;
    final int type = data[index + AssetManager.STYLE_TYPE];
    if (type == TypedValue.TYPE_NULL) {
        return defValue;
    } else if (type >= TypedValue.TYPE_FIRST_INT && type <= TypedValue.TYPE_LAST_INT) {
        return data[index + AssetManager.STYLE_DATA];
    } else if (type == TypedValue.TYPE_STRING) {
        final TypedValue value = mValue;
        if (getValueAt(index, value)) {
            final ColorStateList csl = mResources.loadColorStateList(value, value.resourceId, mTheme);
            return csl.getDefaultColor();
        }
        return defValue;
    } else if (type == TypedValue.TYPE_ATTRIBUTE) {
        final TypedValue value = mValue;
        getValueAt(index, value);
        throw new UnsupportedOperationException("Failed to resolve attribute at index " + attrIndex + ": " + value);
    }
    throw new UnsupportedOperationException("Can't convert value at index " + attrIndex + " to color: type=0x" + Integer.toHexString(type));
}
Also used : TypedValue(android.util.TypedValue) ColorInt(android.annotation.ColorInt)

Example 5 with ColorInt

use of android.annotation.ColorInt in project android_frameworks_base by ResurrectionRemix.

the class DataUsageDetailView method bind.

public void bind(DataUsageController.DataUsageInfo info) {
    final Resources res = mContext.getResources();
    final int titleId;
    final long bytes;
    @ColorInt int usageColor = 0;
    final String top;
    String bottom = null;
    if (info.usageLevel < info.warningLevel || info.limitLevel <= 0) {
        // under warning, or no limit
        titleId = R.string.quick_settings_cellular_detail_data_usage;
        bytes = info.usageLevel;
        top = res.getString(R.string.quick_settings_cellular_detail_data_warning, formatBytes(info.warningLevel));
    } else if (info.usageLevel <= info.limitLevel) {
        // over warning, under limit
        titleId = R.string.quick_settings_cellular_detail_remaining_data;
        bytes = info.limitLevel - info.usageLevel;
        top = res.getString(R.string.quick_settings_cellular_detail_data_used, formatBytes(info.usageLevel));
        bottom = res.getString(R.string.quick_settings_cellular_detail_data_limit, formatBytes(info.limitLevel));
    } else {
        // over limit
        titleId = R.string.quick_settings_cellular_detail_over_limit;
        bytes = info.usageLevel - info.limitLevel;
        top = res.getString(R.string.quick_settings_cellular_detail_data_used, formatBytes(info.usageLevel));
        bottom = res.getString(R.string.quick_settings_cellular_detail_data_limit, formatBytes(info.limitLevel));
        usageColor = mContext.getColor(R.color.system_warning_color);
    }
    if (usageColor == 0) {
        usageColor = Utils.getColorAccent(mContext);
    }
    final TextView title = (TextView) findViewById(android.R.id.title);
    title.setText(titleId);
    final TextView usage = (TextView) findViewById(R.id.usage_text);
    usage.setText(formatBytes(bytes));
    usage.setTextColor(usageColor);
    final DataUsageGraph graph = (DataUsageGraph) findViewById(R.id.usage_graph);
    graph.setLevels(info.limitLevel, info.warningLevel, info.usageLevel);
    final TextView carrier = (TextView) findViewById(R.id.usage_carrier_text);
    carrier.setText(info.carrier);
    final TextView period = (TextView) findViewById(R.id.usage_period_text);
    period.setText(info.period);
    final TextView infoTop = (TextView) findViewById(R.id.usage_info_top_text);
    infoTop.setVisibility(top != null ? View.VISIBLE : View.GONE);
    infoTop.setText(top);
    final TextView infoBottom = (TextView) findViewById(R.id.usage_info_bottom_text);
    infoBottom.setVisibility(bottom != null ? View.VISIBLE : View.GONE);
    infoBottom.setText(bottom);
    boolean showLevel = info.warningLevel > 0 || info.limitLevel > 0;
    graph.setVisibility(showLevel ? View.VISIBLE : View.GONE);
    if (!showLevel) {
        infoTop.setVisibility(View.GONE);
    }
}
Also used : ColorInt(android.annotation.ColorInt) DataUsageGraph(com.android.systemui.qs.DataUsageGraph) TextView(android.widget.TextView) Resources(android.content.res.Resources)

Aggregations

ColorInt (android.annotation.ColorInt)20 TypedValue (android.util.TypedValue)10 Resources (android.content.res.Resources)5 TypedArray (android.content.res.TypedArray)5 TextView (android.widget.TextView)5 DataUsageGraph (com.android.systemui.qs.DataUsageGraph)5