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

Commit 566b3033 authored by Diego Perez's avatar Diego Perez
Browse files

Add support for GradientColor in layoutlib

Change-Id: Ia9a55a9e00d7ddb5263f3dbe46b5da8dde457526
parent bf8ff76f
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -3,7 +3,7 @@
    <extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
    <extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
    <option name="MAIN_CLASS_NAME" value="com.android.tools.layoutlib.create.Main" />
    <option name="MAIN_CLASS_NAME" value="com.android.tools.layoutlib.create.Main" />
    <option name="VM_PARAMETERS" value="-ea" />
    <option name="VM_PARAMETERS" value="-ea" />
    <option name="PROGRAM_PARAMETERS" value="out/host/common/obj/JAVA_LIBRARIES/temp_layoutlib_intermediates/javalib.jar out/target/common/obj/JAVA_LIBRARIES/core-libart_intermediates/classes.jar out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar out/target/common/obj/JAVA_LIBRARIES/icu4j-icudata-jarjar_intermediates/classes.jar out/target/common/obj/JAVA_LIBRARIES/icu4j-icutzdata-jarjar_intermediates/classes.jar out/target/common/obj/JAVA_LIBRARIES/ext_intermediates/classes.jar out/target/common/obj/JAVA_LIBRARIES/ext_intermediates/javalib.jar" />
    <option name="PROGRAM_PARAMETERS" value="out/host/common/obj/JAVA_LIBRARIES/temp_layoutlib_intermediates/javalib.jar out/target/common/obj/JAVA_LIBRARIES/core-libart_intermediates/classes.jar out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar out/host/common/obj/JAVA_LIBRARIES/icu4j-icudata-host-jarjar_intermediates/classes-jarjar.jar out/host/common/obj/JAVA_LIBRARIES/icu4j-icutzdata-host-jarjar_intermediates/classes-jarjar.jar out/target/common/obj/JAVA_LIBRARIES/ext_intermediates/classes.jar out/target/common/obj/JAVA_LIBRARIES/ext_intermediates/javalib.jar" />
    <option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$/../../../../" />
    <option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$/../../../../" />
    <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
    <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
    <option name="ALTERNATIVE_JRE_PATH" value="" />
    <option name="ALTERNATIVE_JRE_PATH" value="" />
+6 −67
Original line number Original line Diff line number Diff line
@@ -25,14 +25,9 @@ import com.android.ide.common.rendering.api.StyleResourceValue;
import com.android.internal.util.XmlUtils;
import com.android.internal.util.XmlUtils;
import com.android.layoutlib.bridge.Bridge;
import com.android.layoutlib.bridge.Bridge;
import com.android.layoutlib.bridge.android.BridgeContext;
import com.android.layoutlib.bridge.android.BridgeContext;
import com.android.layoutlib.bridge.android.BridgeXmlBlockParser;
import com.android.layoutlib.bridge.impl.ParserFactory;
import com.android.layoutlib.bridge.impl.ResourceHelper;
import com.android.layoutlib.bridge.impl.ResourceHelper;
import com.android.resources.ResourceType;
import com.android.resources.ResourceType;


import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.annotation.Nullable;
import android.annotation.Nullable;
import android.content.res.Resources.NotFoundException;
import android.content.res.Resources.NotFoundException;
import android.content.res.Resources.Theme;
import android.content.res.Resources.Theme;
@@ -42,7 +37,6 @@ import android.util.TypedValue;
import android.view.LayoutInflater_Delegate;
import android.view.LayoutInflater_Delegate;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.LayoutParams;


import java.io.File;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Map;
import java.util.Map;
@@ -305,77 +299,22 @@ public final class BridgeTypedArray extends TypedArray {
        return defValue;
        return defValue;
    }
    }


    @Override
    public ComplexColor getComplexColor(int index) {
        // TODO: Support GradientColor
        return getColorStateList(index);
    }

    /**
     * Retrieve the ColorStateList for the attribute at <var>index</var>.
     * The value may be either a single solid color or a reference to
     * a color or complex {@link android.content.res.ColorStateList} description.
     *
     * @param index Index of attribute to retrieve.
     *
     * @return ColorStateList for the attribute, or null if not defined.
     */
    @Override
    @Override
    public ColorStateList getColorStateList(int index) {
    public ColorStateList getColorStateList(int index) {
        if (!hasValue(index)) {
        if (!hasValue(index)) {
            return null;
            return null;
        }
        }


        ResourceValue resValue = mResourceData[index];
        return ResourceHelper.getColorStateList(mResourceData[index], mContext);
        String value = resValue.getValue();

        if (value == null) {
            return null;
        }


        try {
            // Get the state list file content from callback to parse PSI file
            XmlPullParser parser = mContext.getLayoutlibCallback().getXmlFileParser(value);
            if (parser == null) {
                // If used with a version of Android Studio that does not implement getXmlFileParser
                // fall back to reading the file from disk
                File f = new File(value);
                if (f.isFile()) {
                    parser = ParserFactory.create(f);
                }
            }
            if (parser != null) {
                BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser(
                        parser, mContext, resValue.isFramework());
                try {
                    return ColorStateList.createFromXml(mContext.getResources(), blockParser,
                            mContext.getTheme());
                } finally {
                    blockParser.ensurePopped();
                }
    }
    }
        } catch (XmlPullParserException e) {
            Bridge.getLog().error(LayoutLog.TAG_BROKEN,
                    "Failed to configure parser for " + value, e, null);
            return null;
        } catch (Exception e) {
            // this is an error and not warning since the file existence is checked before
            // attempting to parse it.
            Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ,
                    "Failed to parse file " + value, e, null);


    @Override
    public ComplexColor getComplexColor(int index) {
        if (!hasValue(index)) {
            return null;
            return null;
        }
        }


        try {
        return ResourceHelper.getComplexColor(mResourceData[index], mContext);
            int color = ResourceHelper.getColor(value);
            return ColorStateList.valueOf(color);
        } catch (NumberFormatException e) {
            Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, e.getMessage(), e, null);
        }

        return null;
    }
    }


    /**
    /**
+46 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.content.res;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.res.Resources.Theme;
import android.util.AttributeSet;

import java.io.IOException;

/**
 * Class that provides access to the {@link GradientColor#createFromXmlInner(Resources,
 * XmlPullParser, AttributeSet, Theme)} and {@link ColorStateList#createFromXmlInner(Resources,
 * XmlPullParser, AttributeSet, Theme)} methods
 */
public class ComplexColor_Accessor {
    public static GradientColor createGradientColorFromXmlInner(@NonNull Resources r,
            @NonNull XmlPullParser parser, @NonNull AttributeSet attrs, @Nullable Theme theme)
            throws IOException, XmlPullParserException {
        return GradientColor.createFromXmlInner(r, parser, attrs, theme);
    }

    public static ColorStateList createColorStateListFromXmlInner(@NonNull Resources r,
            @NonNull XmlPullParser parser, @NonNull AttributeSet attrs, @Nullable Theme theme)
            throws IOException, XmlPullParserException {
        return ColorStateList.createFromXmlInner(r, parser, attrs, theme);
    }
}
+4 −0
Original line number Original line Diff line number Diff line
@@ -227,6 +227,10 @@ public class Paint_Delegate {
        mColorFilter = ColorFilter_Delegate.getDelegate(colorFilterPtr);
        mColorFilter = ColorFilter_Delegate.getDelegate(colorFilterPtr);
    }
    }


    public void setShader(long shaderPtr) {
        mShader = Shader_Delegate.getDelegate(shaderPtr);
    }

    /**
    /**
     * Returns the {@link Shader} delegate or null if none have been set
     * Returns the {@link Shader} delegate or null if none have been set
     *
     *
+21 −3
Original line number Original line Diff line number Diff line
@@ -211,12 +211,16 @@ public class VectorDrawable_Delegate {


    @LayoutlibDelegate
    @LayoutlibDelegate
    static void nUpdateFullPathFillGradient(long pathPtr, long fillGradientPtr) {
    static void nUpdateFullPathFillGradient(long pathPtr, long fillGradientPtr) {
        VFullPath_Delegate path = getDelegate(pathPtr);


        path.setFillGradient(fillGradientPtr);
    }
    }


    @LayoutlibDelegate
    @LayoutlibDelegate
    static void nUpdateFullPathStrokeGradient(long pathPtr, long strokeGradientPtr) {
    static void nUpdateFullPathStrokeGradient(long pathPtr, long strokeGradientPtr) {
        VFullPath_Delegate path = getDelegate(pathPtr);


        path.setStrokeGradient(strokeGradientPtr);
    }
    }


    @LayoutlibDelegate
    @LayoutlibDelegate
@@ -540,6 +544,8 @@ public class VectorDrawable_Delegate {
        float mStrokeWidth = 0;
        float mStrokeWidth = 0;


        int mFillColor = Color.TRANSPARENT;
        int mFillColor = Color.TRANSPARENT;
        long mStrokeGradient = 0;
        long mFillGradient = 0;
        float mStrokeAlpha = 1.0f;
        float mStrokeAlpha = 1.0f;
        float mFillAlpha = 1.0f;
        float mFillAlpha = 1.0f;
        float mTrimPathStart = 0;
        float mTrimPathStart = 0;
@@ -569,6 +575,9 @@ public class VectorDrawable_Delegate {
            mStrokeLineCap = copy.mStrokeLineCap;
            mStrokeLineCap = copy.mStrokeLineCap;
            mStrokeLineJoin = copy.mStrokeLineJoin;
            mStrokeLineJoin = copy.mStrokeLineJoin;
            mStrokeMiterlimit = copy.mStrokeMiterlimit;
            mStrokeMiterlimit = copy.mStrokeMiterlimit;

            mStrokeGradient = copy.mStrokeGradient;
            mFillGradient = copy.mFillGradient;
        }
        }


        private int getStrokeLineCap() {
        private int getStrokeLineCap() {
@@ -704,6 +713,14 @@ public class VectorDrawable_Delegate {
        private float getStrokeMiterlimit() {
        private float getStrokeMiterlimit() {
            return mStrokeMiterlimit;
            return mStrokeMiterlimit;
        }
        }

        private void setStrokeGradient(long gradientPtr) {
            mStrokeGradient = gradientPtr;
        }

        private void setFillGradient(long gradientPtr) {
            mFillGradient = gradientPtr;
        }
    }
    }


    private static class VGroup_Delegate implements VNativeObject {
    private static class VGroup_Delegate implements VNativeObject {
@@ -1046,11 +1063,11 @@ public class VectorDrawable_Delegate {
                    final Paint fillPaint = mFillPaint;
                    final Paint fillPaint = mFillPaint;
                    fillPaint.setColor(applyAlpha(fullPath.mFillColor, fullPath.mFillAlpha));
                    fillPaint.setColor(applyAlpha(fullPath.mFillColor, fullPath.mFillAlpha));
                    Paint_Delegate fillPaintDelegate = Paint_Delegate.getDelegate(fillPaint
                    Paint_Delegate fillPaintDelegate = Paint_Delegate.getDelegate(fillPaint
                            .getNativeInstance
                            .getNativeInstance());
                            ());
                    // mFillPaint can not be null at this point so we will have a delegate
                    // mFillPaint can not be null at this point so we will have a delegate
                    assert fillPaintDelegate != null;
                    assert fillPaintDelegate != null;
                    fillPaintDelegate.setColorFilter(filterPtr);
                    fillPaintDelegate.setColorFilter(filterPtr);
                    fillPaintDelegate.setShader(fullPath.mFillGradient);
                    Canvas_Delegate.native_drawPath(canvasPtr, mRenderPath.mNativePath, fillPaint
                    Canvas_Delegate.native_drawPath(canvasPtr, mRenderPath.mNativePath, fillPaint
                            .getNativeInstance());
                            .getNativeInstance());
                }
                }
@@ -1080,6 +1097,7 @@ public class VectorDrawable_Delegate {
                    strokePaintDelegate.setColorFilter(filterPtr);
                    strokePaintDelegate.setColorFilter(filterPtr);
                    final float finalStrokeScale = minScale * matrixScale;
                    final float finalStrokeScale = minScale * matrixScale;
                    strokePaint.setStrokeWidth(fullPath.mStrokeWidth * finalStrokeScale);
                    strokePaint.setStrokeWidth(fullPath.mStrokeWidth * finalStrokeScale);
                    strokePaintDelegate.setShader(fullPath.mStrokeGradient);
                    Canvas_Delegate.native_drawPath(canvasPtr, mRenderPath.mNativePath, strokePaint
                    Canvas_Delegate.native_drawPath(canvasPtr, mRenderPath.mNativePath, strokePaint
                            .getNativeInstance());
                            .getNativeInstance());
                }
                }
Loading