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

Commit 9ca97747 authored by Deepanshu Gupta's avatar Deepanshu Gupta
Browse files

Add @empty support.

Bug: http://b.android.com/162428
Change-Id: Id7a981de2fce21213e12cdabcce4104f146fd4d3
parent 9d6e9587
Loading
Loading
Loading
Loading
+31 −3
Original line number Diff line number Diff line
@@ -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.LayoutInflater_Delegate;
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 @@ public final class BridgeTypedArray extends TypedArray {
    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 @@ public final class BridgeTypedArray extends TypedArray {
        // 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 @@ public final class BridgeTypedArray extends TypedArray {
        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