Add @empty support.
Bug: http://b.android.com/162428
Change-Id: Id7a981de2fce21213e12cdabcce4104f146fd4d3
diff --git a/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java b/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java
index 18036927..ae2c173 100644
--- a/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java
+++ b/tools/layoutlib/bridge/src/android/content/res/BridgeTypedArray.java
@@ -16,6 +16,7 @@
package android.content.res;
+import com.android.annotations.Nullable;
import com.android.ide.common.rendering.api.AttrResourceValue;
import com.android.ide.common.rendering.api.LayoutLog;
import com.android.ide.common.rendering.api.RenderResources;
@@ -40,9 +41,12 @@
import android.view.ViewGroup.LayoutParams;
import java.io.File;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
+import static com.android.ide.common.rendering.api.RenderResources.*;
+
/**
* Custom implementation of TypedArray to handle non compiled resources.
*/
@@ -56,6 +60,11 @@
private final String[] mNames;
private final boolean[] mIsFramework;
+ // Contains ids that are @empty. We still store null in mResourceData for that index, since we
+ // want to save on the check against empty, each time a resource value is requested.
+ @Nullable
+ private int[] mEmptyIds;
+
public BridgeTypedArray(BridgeResources resources, BridgeContext context, int len,
boolean platformFile) {
super(resources, null, null, 0);
@@ -90,19 +99,32 @@
// fills TypedArray.mIndices which is used to implement getIndexCount/getIndexAt
// first count the array size
int count = 0;
+ ArrayList<Integer> emptyIds = null;
for (int i = 0; i < mResourceData.length; i++) {
ResourceValue data = mResourceData[i];
if (data != null) {
- if (RenderResources.REFERENCE_NULL.equals(data.getValue())) {
- // No need to store this resource value. This saves needless checking for
- // "@null" every time an attribute is requested.
+ String dataValue = data.getValue();
+ if (REFERENCE_NULL.equals(dataValue) || REFERENCE_UNDEFINED.equals(dataValue)) {
mResourceData[i] = null;
+ } else if (REFERENCE_EMPTY.equals(dataValue)) {
+ mResourceData[i] = null;
+ if (emptyIds == null) {
+ emptyIds = new ArrayList<Integer>(4);
+ }
+ emptyIds.add(i);
} else {
count++;
}
}
}
+ if (emptyIds != null) {
+ mEmptyIds = new int[emptyIds.size()];
+ for (int i = 0; i < emptyIds.size(); i++) {
+ mEmptyIds[i] = emptyIds.get(i);
+ }
+ }
+
// allocate the table with an extra to store the size
mIndices = new int[count+1];
mIndices[0] = count;
@@ -748,6 +770,12 @@
return index >= 0 && index < mResourceData.length && mResourceData[index] != null;
}
+ @Override
+ public boolean hasValueOrEmpty(int index) {
+ return hasValue(index) || index >= 0 && index < mResourceData.length &&
+ mEmptyIds != null && Arrays.binarySearch(mEmptyIds, index) >= 0;
+ }
+
/**
* Retrieve the raw TypedValue for the attribute at <var>index</var>
* and return a temporary object holding its data. This object is only