Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce7c99c6 authored by Aurimas Liutikas's avatar Aurimas Liutikas
Browse files

Add source style for each TypedValue.

This API will be used by Android Studio layout inspector to
query where a given resource attribute originated from to allow
developers to have an easier way to find what needs to be changed
to affect a given resource attribute.

Test: CTS added in the topic
Bug: 111439551
Change-Id: Ifbb52b39912ed0444030a86164290a1cd4155250
parent de784fbf
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -12324,6 +12324,7 @@ package android.content.res {
    method public java.lang.String getPositionDescription();
    method public int getResourceId(int, int);
    method public android.content.res.Resources getResources();
    method public int getSourceStyleResourceId(int, int);
    method public java.lang.String getString(int);
    method public java.lang.CharSequence getText(int);
    method public java.lang.CharSequence[] getTextArray(int);
@@ -48251,6 +48252,7 @@ package android.util {
    field public int data;
    field public int density;
    field public int resourceId;
    field public int sourceStyleResourceId;
    field public java.lang.CharSequence string;
    field public int type;
  }
+30 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.content.res;
import android.annotation.AnyRes;
import android.annotation.ColorInt;
import android.annotation.Nullable;
import android.annotation.StyleRes;
import android.annotation.StyleableRes;
import android.annotation.UnsupportedAppUsage;
import android.content.pm.ActivityInfo;
@@ -63,13 +64,15 @@ public class TypedArray {
    }

    // STYLE_ prefixed constants are offsets within the typed data array.
    static final int STYLE_NUM_ENTRIES = 6;
    // Keep this in sync with libs/androidfw/include/androidfw/AttributeResolution.h
    static final int STYLE_NUM_ENTRIES = 7;
    static final int STYLE_TYPE = 0;
    static final int STYLE_DATA = 1;
    static final int STYLE_ASSET_COOKIE = 2;
    static final int STYLE_RESOURCE_ID = 3;
    static final int STYLE_CHANGING_CONFIGURATIONS = 4;
    static final int STYLE_DENSITY = 5;
    static final int SYTLE_SOURCE_STYLE_RESOURCE_ID = 6;

    @UnsupportedAppUsage
    private final Resources mResources;
@@ -1097,6 +1100,31 @@ public class TypedArray {
        return mData[index + STYLE_TYPE];
    }

    /**
     * Returns the resource ID of the style against which the specified attribute was resolved,
     * otherwise returns defValue.
     *
     * @param index Index of attribute whose source style to retrieve.
     * @param defValue Value to return if the attribute is not defined or
     *                 not a resource.
     *
     * @return Attribute source style resource ID or defValue if it was not resolved in any style.
     * @throws RuntimeException if the TypedArray has already been recycled.
     */
    @StyleRes
    public int getSourceStyleResourceId(@StyleableRes int index, @StyleRes int defValue) {
        if (mRecycled) {
            throw new RuntimeException("Cannot make calls to a recycled instance!");
        }

        index *= STYLE_NUM_ENTRIES;
        final int resid = mData[index + SYTLE_SOURCE_STYLE_RESOURCE_ID];
        if (resid != 0) {
            return resid;
        }
        return defValue;
    }

    /**
     * Determines whether there is an attribute at <var>index</var>.
     * <p>
@@ -1309,6 +1337,7 @@ public class TypedArray {
                data[index + STYLE_CHANGING_CONFIGURATIONS]);
        outValue.density = data[index + STYLE_DENSITY];
        outValue.string = (type == TypedValue.TYPE_STRING) ? loadStringValueAt(index) : null;
        outValue.sourceStyleResourceId = data[index + SYTLE_SOURCE_STYLE_RESOURCE_ID];
        return true;
    }

+6 −0
Original line number Diff line number Diff line
@@ -216,6 +216,12 @@ public class TypedValue {
     * */
    public int density;

    /**
     * If the Value came from a style resource, this holds the corresponding style resource id
     * against which the attribute was resolved.
     */
    public int sourceStyleResourceId;

    /* ------------------------------------------------------------ */

    /** Return the data for this value as a float.  Only use for values
+6 −1
Original line number Diff line number Diff line
@@ -286,6 +286,7 @@ void ApplyStyle(Theme* theme, ResXMLParser* xml_parser, uint32_t def_style_attr,
    value.dataType = Res_value::TYPE_NULL;
    value.data = Res_value::DATA_NULL_UNDEFINED;
    config.density = 0;
    uint32_t source_style_resid = 0;

    // Try to find a value for this attribute...  we prioritize values
    // coming from, first XML attributes, then XML style, then default
@@ -309,6 +310,7 @@ void ApplyStyle(Theme* theme, ResXMLParser* xml_parser, uint32_t def_style_attr,
        cookie = entry->cookie;
        type_set_flags = style_flags;
        value = entry->value;
        source_style_resid = entry->style;
        if (kDebugStyles) {
          ALOGI("-> From style: type=0x%x, data=0x%08x, style=0x%08x", value.dataType, value.data,
              entry->style);
@@ -325,8 +327,10 @@ void ApplyStyle(Theme* theme, ResXMLParser* xml_parser, uint32_t def_style_attr,
        type_set_flags = def_style_flags;
        value = entry->value;
        if (kDebugStyles) {
          ALOGI("-> From def style: type=0x%x, data=0x%08x", value.dataType, value.data);
          ALOGI("-> From def style: type=0x%x, data=0x%08x, style=0x%08x", value.dataType, value.data,
              entry->style);
        }
        source_style_resid = entry->style;
      }
    }

@@ -382,6 +386,7 @@ void ApplyStyle(Theme* theme, ResXMLParser* xml_parser, uint32_t def_style_attr,
    out_values[STYLE_RESOURCE_ID] = resid;
    out_values[STYLE_CHANGING_CONFIGURATIONS] = type_set_flags;
    out_values[STYLE_DENSITY] = config.density;
    out_values[SYTLE_SOURCE_STYLE] = source_style_resid;

    if (value.dataType != Res_value::TYPE_NULL || value.data == Res_value::DATA_NULL_EMPTY) {
      indices_idx++;
+5 −3
Original line number Diff line number Diff line
@@ -23,15 +23,17 @@
namespace android {

// Offsets into the outValues array populated by the methods below. outValues is a uint32_t
// array, but each logical element takes up 6 uint32_t-sized physical elements.
// array, but each logical element takes up 7 uint32_t-sized physical elements.
// Keep these in sync with android.content.res.TypedArray java class
enum {
  STYLE_NUM_ENTRIES = 6,
  STYLE_NUM_ENTRIES = 7,
  STYLE_TYPE = 0,
  STYLE_DATA = 1,
  STYLE_ASSET_COOKIE = 2,
  STYLE_RESOURCE_ID = 3,
  STYLE_CHANGING_CONFIGURATIONS = 4,
  STYLE_DENSITY = 5
  STYLE_DENSITY = 5,
  SYTLE_SOURCE_STYLE = 6
};

// These are all variations of the same method. They each perform the exact same operation,