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

Commit 72c111de authored by Louis Chang's avatar Louis Chang
Browse files

Reduce RippleDrawable background opacity when no window focus

Lower the background opacity when the window loses focus
while the view still has focus, in order to prevent user
confusion in a multi-window environment.

Bug: 230355625
Test: atest RippleDrawableTest
Change-Id: I59cd7faf35f05a12451f015f8ef2da47077b1bf2
parent 8af0bcdd
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -1042,6 +1042,11 @@ package android.graphics.drawable {
    method public android.graphics.Xfermode getXfermode();
  }

  public class RippleDrawable extends android.graphics.drawable.LayerDrawable {
    method public float getTargetBackgroundOpacity();
    method public void setBackgroundActive(boolean, boolean, boolean, boolean);
  }

  public class ShapeDrawable extends android.graphics.drawable.Drawable {
    method public void setXfermode(@Nullable android.graphics.Xfermode);
  }
+24 −5
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.graphics.drawable;

import android.annotation.TestApi;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
@@ -321,6 +323,7 @@ public class RippleDrawable extends LayerDrawable {
        boolean pressed = false;
        boolean focused = false;
        boolean hovered = false;
        boolean windowFocused = false;

        for (int state : stateSet) {
            if (state == R.attr.state_enabled) {
@@ -331,10 +334,12 @@ public class RippleDrawable extends LayerDrawable {
                pressed = true;
            } else if (state == R.attr.state_hovered) {
                hovered = true;
            } else if (state == R.attr.state_window_focused) {
                windowFocused = true;
            }
        }
        setRippleActive(enabled && pressed);
        setBackgroundActive(hovered, focused, pressed);
        setBackgroundActive(hovered, focused, pressed, windowFocused);

        return changed;
    }
@@ -358,7 +363,10 @@ public class RippleDrawable extends LayerDrawable {
        }
    }

    private void setBackgroundActive(boolean hovered, boolean focused, boolean pressed) {
    /** @hide */
    @TestApi
    public void setBackgroundActive(boolean hovered, boolean focused, boolean pressed,
            boolean windowFocused) {
        if (mState.mRippleStyle == STYLE_SOLID) {
            if (mBackground == null && (hovered || focused)) {
                mBackground = new RippleBackground(this, mHotspotBounds, isBounded());
@@ -370,7 +378,7 @@ public class RippleDrawable extends LayerDrawable {
        } else {
            if (focused || hovered) {
                if (!pressed) {
                    enterPatternedBackgroundAnimation(focused, hovered);
                    enterPatternedBackgroundAnimation(focused, hovered, windowFocused);
                }
            } else {
                exitPatternedBackgroundAnimation();
@@ -840,9 +848,20 @@ public class RippleDrawable extends LayerDrawable {
        invalidateSelf(false);
    }

    private void enterPatternedBackgroundAnimation(boolean focused, boolean hovered) {
    /** @hide */
    @TestApi
    public float getTargetBackgroundOpacity() {
        return mTargetBackgroundOpacity;
    }

    private void enterPatternedBackgroundAnimation(boolean focused, boolean hovered,
            boolean windowFocused) {
        mBackgroundOpacity = 0;
        mTargetBackgroundOpacity = focused ? .6f : hovered ? .2f : 0f;
        if (focused) {
            mTargetBackgroundOpacity = windowFocused ? .6f : .2f;
        } else {
            mTargetBackgroundOpacity = hovered ? .2f : 0f;
        }
        if (mBackgroundAnimation != null) mBackgroundAnimation.cancel();
        // after cancel
        mRunBackgroundAnimation = true;