From b6864ac6cde109be320e18188209eb8420c585f1 Mon Sep 17 00:00:00 2001 From: Deepanshu Gupta Date: Thu, 15 Jan 2015 15:25:49 -0800 Subject: Resource resolution fixes. 1. Some dynamic ids weren't created and resulted in ResourceNotFound exceptions. 2. Prevent NPE if a style attribute (eg. style="?attr/foo"), which cannot be resolved, is resolved. This effectively, also results in making it harder to catch misconfigured themes. However, support library does it, and we don't want to throw errors when the library is working as intended. Change-Id: I731d8fb9209eb72b464d235d1072d416e132970b --- .../src/android/content/res/BridgeTypedArray.java | 26 ++++++++++++++++------ .../layoutlib/bridge/android/BridgeContext.java | 6 ++++- 2 files changed, 24 insertions(+), 8 deletions(-) (limited to 'tools/layoutlib') diff --git a/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java b/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java index a2bd6d7..1803692 100644 --- a/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java +++ b/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java @@ -614,15 +614,27 @@ public final class BridgeTypedArray extends TypedArray { int pos = value.indexOf('/'); String idName = value.substring(pos + 1); - - // if this is a framework id - if (mPlatformFile || value.startsWith("@android") || value.startsWith("@+android")) { - // look for idName in the android R classes + 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.getProjectCallback().getResourceId(ResourceType.ID, idName); + } + return idValue == null ? defValue : idValue; + } + // This calls the same method as in if(create), but doesn't create a dynamic id, if + // one is not found. + if (isFrameworkId) { return mContext.getFrameworkResourceValue(ResourceType.ID, idName, defValue); + } else { + return mContext.getProjectResourceValue(ResourceType.ID, idName, defValue); } - - // look for idName in the project R class. - return mContext.getProjectResourceValue(ResourceType.ID, idName, defValue); } // not a direct id valid reference? resolve it diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java index 8523f1a..2adeb67 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java @@ -288,6 +288,11 @@ public final class BridgeContext extends Context { value = mRenderResources.resolveResValue(value); } + if (value == null) { + // unable to find the attribute. + return false; + } + // check if this is a style resource if (value instanceof StyleResourceValue) { // get the id that will represent this style. @@ -295,7 +300,6 @@ public final class BridgeContext extends Context { return true; } - int a; // if this is a framework value. if (value.isFramework()) { -- cgit v1.1