use of com.android.ide.common.rendering.api.StyleResourceValue in project android_frameworks_base by DirtyUnicorns.
the class BridgeTypedArray method getResourceId.
/**
* Retrieve the resource identifier for the attribute at
* <var>index</var>. Note that attribute resource as resolved when
* the overall {@link TypedArray} object is retrieved. As a
* result, this function will return the resource identifier of the
* final resource value that was found, <em>not</em> necessarily the
* original resource that was specified by the attribute.
*
* @param index Index of attribute to retrieve.
* @param defValue Value to return if the attribute is not defined or
* not a resource.
*
* @return Attribute resource identifier, or defValue if not defined.
*/
@Override
public int getResourceId(int index, int defValue) {
if (index < 0 || index >= mResourceData.length) {
return defValue;
}
// get the Resource for this index
ResourceValue resValue = mResourceData[index];
// no data, return the default value.
if (resValue == null) {
return defValue;
}
// check if this is a style resource
if (resValue instanceof StyleResourceValue) {
// get the id that will represent this style.
return mContext.getDynamicIdByStyle((StyleResourceValue) resValue);
}
// (and getValue() returning null!). We need to handle this!
if (resValue.getResourceType() != null) {
// if this is a framework id
if (mPlatformFile || resValue.isFramework()) {
// look for idName in the android R classes
return mContext.getFrameworkResourceValue(resValue.getResourceType(), resValue.getName(), defValue);
}
// look for idName in the project R class.
return mContext.getProjectResourceValue(resValue.getResourceType(), resValue.getName(), defValue);
}
// else, try to get the value, and resolve it somehow.
String value = resValue.getValue();
if (value == null) {
return defValue;
}
// if the value is just an integer, return it.
try {
int i = Integer.parseInt(value);
if (Integer.toString(i).equals(value)) {
return i;
}
} catch (NumberFormatException e) {
// pass
}
// if this is a reference to an id, find it.
if (value.startsWith("@id/") || value.startsWith("@+") || value.startsWith("@android:id/")) {
int pos = value.indexOf('/');
String idName = value.substring(pos + 1);
boolean create = value.startsWith("@+");
boolean isFrameworkId = mPlatformFile || value.startsWith("@android") || value.startsWith("@+android");
// Look for the idName in project or android R class depending on isPlatform.
if (create) {
Integer idValue;
if (isFrameworkId) {
idValue = Bridge.getResourceId(ResourceType.ID, idName);
} else {
idValue = mContext.getLayoutlibCallback().getResourceId(ResourceType.ID, idName);
}
return idValue == null ? defValue : idValue;
}
// one is not found.
if (isFrameworkId) {
return mContext.getFrameworkResourceValue(ResourceType.ID, idName, defValue);
} else {
return mContext.getProjectResourceValue(ResourceType.ID, idName, defValue);
}
}
// not a direct id valid reference? resolve it
Integer idValue;
if (resValue.isFramework()) {
idValue = Bridge.getResourceId(resValue.getResourceType(), resValue.getName());
} else {
idValue = mContext.getLayoutlibCallback().getResourceId(resValue.getResourceType(), resValue.getName());
}
if (idValue != null) {
return idValue;
}
Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_RESOLVE, String.format("Unable to resolve id \"%1$s\" for attribute \"%2$s\"", value, mNames[index]), resValue);
return defValue;
}
use of com.android.ide.common.rendering.api.StyleResourceValue in project android_frameworks_base by DirtyUnicorns.
the class Resources_Theme_Delegate method resolveStyle.
@Nullable
private static StyleResourceValue resolveStyle(int nativeResid) {
if (nativeResid == 0) {
return null;
}
BridgeContext context = RenderSessionImpl.getCurrentContext();
ResourceReference theme = context.resolveId(nativeResid);
if (theme.isFramework()) {
return (StyleResourceValue) context.getRenderResources().getFrameworkResource(ResourceType.STYLE, theme.getName());
} else {
return (StyleResourceValue) context.getRenderResources().getProjectResource(ResourceType.STYLE, theme.getName());
}
}
use of com.android.ide.common.rendering.api.StyleResourceValue in project android_frameworks_base by DirtyUnicorns.
the class AppCompatActionBar method getInflater.
@Override
protected LayoutInflater getInflater(BridgeContext context) {
// Other than the resource resolution part, the code has been taken from the support
// library. see code from line 269 onwards in
// https://android.googlesource.com/platform/frameworks/support/+/android-5.1.0_r1/v7/appcompat/src/android/support/v7/app/ActionBarActivityDelegateBase.java
Context themedContext = context;
RenderResources resources = context.getRenderResources();
ResourceValue actionBarTheme = resources.findItemInTheme("actionBarTheme", false);
if (actionBarTheme != null) {
// resolve it, if needed.
actionBarTheme = resources.resolveResValue(actionBarTheme);
}
if (actionBarTheme instanceof StyleResourceValue) {
int styleId = context.getDynamicIdByStyle(((StyleResourceValue) actionBarTheme));
if (styleId != 0) {
themedContext = new ContextThemeWrapper(context, styleId);
}
}
return LayoutInflater.from(themedContext);
}
use of com.android.ide.common.rendering.api.StyleResourceValue in project android by JetBrains.
the class StyleFilterTest method assertStylesSorted.
private void assertStylesSorted(@NotNull String context, @NotNull List<StyleResourceValue> styles, int minUserCount, int minLibraryCount, int minFrameworkCount, List<String> userSample, List<String> librarySample, List<String> frameworkSample) {
boolean success = false;
try {
Set<String> userSampleStyles = new HashSet<>(userSample);
Set<String> librarySampleStyles = new HashSet<>(librarySample);
Set<String> frameworkSampleStyles = new HashSet<>(frameworkSample);
String previousStyleName = "";
int index = 0;
// All user styles should be sorted
for (; index < styles.size() && styles.get(index).isUserDefined(); index++) {
StyleResourceValue style = styles.get(index);
assertStyle(styles.get(index), style.getName(), !FRAMEWORK, USER_DEFINED, IS_DERIVED_STYLE, false);
assertTrue(previousStyleName.compareTo(style.getName()) < 0);
userSampleStyles.remove(style.getName());
previousStyleName = style.getName();
}
int actualUserCount = index;
assertAtLeast(context + " user style count", minUserCount, actualUserCount);
assertEmpty(context, userSampleStyles);
// All library styles should be sorted
int previousIndex = index;
previousStyleName = "";
for (; index < styles.size() && !styles.get(index).isFramework(); index++) {
StyleResourceValue style = styles.get(index);
assertStyle(styles.get(index), style.getName(), !FRAMEWORK, !USER_DEFINED, IS_DERIVED_STYLE, false);
assertTrue(previousStyleName.compareTo(style.getName()) < 0);
librarySampleStyles.remove(style.getName());
previousStyleName = style.getName();
}
int actualLibraryCount = index - previousIndex;
assertAtLeast(context + " library style count", minLibraryCount, actualLibraryCount);
assertEmpty(context, librarySampleStyles);
// All framework styles should be sorted
previousIndex = index;
previousStyleName = "";
for (; index < styles.size(); index++) {
StyleResourceValue style = styles.get(index);
assertStyle(styles.get(index), style.getName(), FRAMEWORK, !USER_DEFINED, IS_DERIVED_STYLE, false);
assertTrue(previousStyleName.compareTo(style.getName()) < 0);
frameworkSampleStyles.remove(style.getName());
previousStyleName = style.getName();
}
int actualFrameworkCount = index - previousIndex;
assertAtLeast(context + " framework style count", minFrameworkCount, actualFrameworkCount);
assertEmpty(context, frameworkSampleStyles);
assertEquals("All styles should be in 3 sections", styles.size(), index);
success = true;
} finally {
if (!success) {
dumpStyles(styles);
}
}
}
use of com.android.ide.common.rendering.api.StyleResourceValue in project android by JetBrains.
the class ResolutionUtilsTest method testGetQualifiedName.
/*
* The test SDK only includes some resources. It only includes a few incomplete styles.
*/
public void testGetQualifiedName() {
StyleResourceValue styleResourceValue = new StyleResourceValue(ResourceType.STYLE, "myStyle", true, null);
assertEquals("android:myStyle", ResolutionUtils.getQualifiedStyleName(styleResourceValue));
styleResourceValue = new StyleResourceValue(ResourceType.STYLE, "myStyle", false, null);
assertEquals("myStyle", ResolutionUtils.getQualifiedStyleName(styleResourceValue));
}
Aggregations