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

Commit 5baa0870 authored by Nader Jawad's avatar Nader Jawad
Browse files

Do not parse angle if it is negative

If the Gradient angle is negative after
modding by 360, maintain the previous platform
behavior of TOP_BOTTOM oreintation instead of
attempting to wrap it

Test: Added CTS test to verify various negative
angle measurements
Bug: 142111195

Change-Id: Id9c050a9e15717bfaff331c6a37cb34cbce0f060
parent 91ede359
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -75,6 +75,7 @@ import android.graphics.RenderNode;
import android.graphics.Shader;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.hardware.display.DisplayManagerGlobal;
import android.net.Uri;
import android.os.Build;
@@ -5228,6 +5229,8 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
            sBrokenWindowBackground = targetSdkVersion < Build.VERSION_CODES.Q;
            GradientDrawable.sWrapNegativeAngleMeasurements =
                    targetSdkVersion >= Build.VERSION_CODES.Q;
            sCompatibilityDone = true;
        }
    }
+53 −27
Original line number Diff line number Diff line
@@ -97,6 +97,14 @@ import java.lang.annotation.RetentionPolicy;
 * @attr ref android.R.styleable#GradientDrawablePadding_bottom
 */
public class GradientDrawable extends Drawable {

    /**
     * Flag to determine if we should wrap negative gradient angle measurements
     * for API levels that support it
     * @hide
     */
    public static boolean sWrapNegativeAngleMeasurements = true;

    /**
     * Shape is a rectangle, possibly with rounded corners
     */
@@ -151,6 +159,9 @@ public class GradientDrawable extends Drawable {
    /** Radius is a fraction of the bounds size. */
    private static final int RADIUS_TYPE_FRACTION_PARENT = 2;

    /** Default orientation for GradientDrawable **/
    private static final Orientation DEFAULT_ORIENTATION = Orientation.TOP_BOTTOM;

    /** @hide */
    @IntDef({RADIUS_TYPE_PIXELS, RADIUS_TYPE_FRACTION, RADIUS_TYPE_FRACTION_PARENT})
    @Retention(RetentionPolicy.SOURCE)
@@ -207,7 +218,7 @@ public class GradientDrawable extends Drawable {
    }

    public GradientDrawable() {
        this(new GradientState(Orientation.TOP_BOTTOM, null), null);
        this(new GradientState(DEFAULT_ORIENTATION, null), null);
    }

    /**
@@ -1757,8 +1768,20 @@ public class GradientDrawable extends Drawable {
        }

        int angle = (int) a.getFloat(R.styleable.GradientDrawableGradient_angle, st.mAngle);

        // GradientDrawable historically has not parsed negative angle measurements and always
        // stays on the default orientation for API levels older than Q.
        // Only configure the orientation if the angle is greater than zero.
        // Otherwise fallback on Orientation.TOP_BOTTOM
        // In Android Q and later, actually wrap the negative angle measurement to the correct
        // value
        if (sWrapNegativeAngleMeasurements) {
            st.mAngle = ((angle % 360) + 360) % 360; // offset negative angle measures
        } else {
            st.mAngle = angle % 360;
        }

        if (st.mAngle >= 0) {
            switch (st.mAngle) {
                case 0:
                    st.mOrientation = Orientation.LEFT_RIGHT;
@@ -1785,6 +1808,9 @@ public class GradientDrawable extends Drawable {
                    st.mOrientation = Orientation.TL_BR;
                    break;
            }
        } else {
            st.mOrientation = DEFAULT_ORIENTATION;
        }

        final TypedValue tv = a.peekValue(R.styleable.GradientDrawableGradient_gradientRadius);
        if (tv != null) {