diff options
| author | Yigit Boyar <yboyar@google.com> | 2019-02-06 12:34:21 -0800 |
|---|---|---|
| committer | Yigit Boyar <yboyar@google.com> | 2019-02-08 22:02:22 +0000 |
| commit | 08b1d993cfcede386b8bcdf78c1eed9a290834eb (patch) | |
| tree | 93bc7ffc7db23237fc3b062e80878fcbd679e4c7 /core/java/android | |
| parent | aa0fc10c7aaee6117a87540488a6780e04e06456 (diff) | |
Add edge effect color APIs to ScrollViews
This CL adds APIs to ScrollView and HorizontalScrollView to
set edge effect colors.
Previously, edge effect fields were accessed via reflection
by many apps to obtain this functionality.
For apps targeting Q, those fields will not be accessible
anymore and instead we'll ask them to use the new APIs.
Bug: 123769386
Bug: 123768600
Bug: 124052619
Bug: 124053130
Test: HorizontalScrollViewTest, ScrollViewTest
Change-Id: I32cbf0b01658955d34662c9a9aaca87b54ab7706
Diffstat (limited to 'core/java/android')
| -rw-r--r-- | core/java/android/widget/HorizontalScrollView.java | 114 | ||||
| -rw-r--r-- | core/java/android/widget/ScrollView.java | 117 |
2 files changed, 188 insertions, 43 deletions
diff --git a/core/java/android/widget/HorizontalScrollView.java b/core/java/android/widget/HorizontalScrollView.java index 1c5f837728a0..5a85012a2fca 100644 --- a/core/java/android/widget/HorizontalScrollView.java +++ b/core/java/android/widget/HorizontalScrollView.java @@ -16,6 +16,7 @@ package android.widget; +import android.annotation.ColorInt; import android.annotation.NonNull; import android.annotation.UnsupportedAppUsage; import android.content.Context; @@ -80,10 +81,24 @@ public class HorizontalScrollView extends FrameLayout { private final Rect mTempRect = new Rect(); @UnsupportedAppUsage private OverScroller mScroller; - @UnsupportedAppUsage - private EdgeEffect mEdgeGlowLeft; - @UnsupportedAppUsage - private EdgeEffect mEdgeGlowRight; + /** + * Tracks the state of the left edge glow. + * + * Even though this field is practically final, we cannot make it final because there are apps + * setting it via reflection and they need to keep working until they target Q. + */ + @NonNull + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 124053130) + private EdgeEffect mEdgeGlowLeft = new EdgeEffect(getContext()); + + /** + * Tracks the state of the bottom edge glow. + * + * Even though this field is practically final, we cannot make it final because there are apps + * setting it via reflection and they need to keep working until they target Q. + */ + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 124052619) + private EdgeEffect mEdgeGlowRight = new EdgeEffect(getContext()); /** * Position of the last motion event. @@ -216,6 +231,74 @@ public class HorizontalScrollView extends FrameLayout { } /** + * Sets the edge effect color for both left and right edge effects. + * + * @param color The color for the edge effects. + * @see #setLeftEdgeEffectColor(int) + * @see #setRightEdgeEffectColor(int) + * @see #getLeftEdgeEffectColor() + * @see #getRightEdgeEffectColor() + */ + public void setEdgeEffectColor(@ColorInt int color) { + setLeftEdgeEffectColor(color); + setRightEdgeEffectColor(color); + } + + /** + * Sets the right edge effect color. + * + * @param color The color for the right edge effect. + * @see #setLeftEdgeEffectColor(int) + * @see #setEdgeEffectColor(int) + * @see #getLeftEdgeEffectColor() + * @see #getRightEdgeEffectColor() + */ + public void setRightEdgeEffectColor(@ColorInt int color) { + mEdgeGlowRight.setColor(color); + } + + /** + * Sets the left edge effect color. + * + * @param color The color for the left edge effect. + * @see #setRightEdgeEffectColor(int) + * @see #setEdgeEffectColor(int) + * @see #getLeftEdgeEffectColor() + * @see #getRightEdgeEffectColor() + */ + public void setLeftEdgeEffectColor(@ColorInt int color) { + mEdgeGlowLeft.setColor(color); + } + + /** + * Returns the left edge effect color. + * + * @return The left edge effect color. + * @see #setEdgeEffectColor(int) + * @see #setLeftEdgeEffectColor(int) + * @see #setRightEdgeEffectColor(int) + * @see #getRightEdgeEffectColor() + */ + @ColorInt + public int getLeftEdgeEffectColor() { + return mEdgeGlowLeft.getColor(); + } + + /** + * Returns the right edge effect color. + * + * @return The right edge effect color. + * @see #setEdgeEffectColor(int) + * @see #setLeftEdgeEffectColor(int) + * @see #setRightEdgeEffectColor(int) + * @see #getLeftEdgeEffectColor() + */ + @ColorInt + public int getRightEdgeEffectColor() { + return mEdgeGlowRight.getColor(); + } + + /** * @return The maximum amount this scroll view will scroll in response to * an arrow event. */ @@ -665,7 +748,7 @@ public class HorizontalScrollView extends FrameLayout { mEdgeGlowLeft.onRelease(); } } - if (mEdgeGlowLeft != null + if (shouldDisplayEdgeEffects() && (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished())) { postInvalidateOnAnimation(); } @@ -693,7 +776,7 @@ public class HorizontalScrollView extends FrameLayout { mIsBeingDragged = false; recycleVelocityTracker(); - if (mEdgeGlowLeft != null) { + if (shouldDisplayEdgeEffects()) { mEdgeGlowLeft.onRelease(); mEdgeGlowRight.onRelease(); } @@ -708,7 +791,7 @@ public class HorizontalScrollView extends FrameLayout { mIsBeingDragged = false; recycleVelocityTracker(); - if (mEdgeGlowLeft != null) { + if (shouldDisplayEdgeEffects()) { mEdgeGlowLeft.onRelease(); mEdgeGlowRight.onRelease(); } @@ -1650,26 +1733,15 @@ public class HorizontalScrollView extends FrameLayout { } } - @Override - public void setOverScrollMode(int mode) { - if (mode != OVER_SCROLL_NEVER) { - if (mEdgeGlowLeft == null) { - Context context = getContext(); - mEdgeGlowLeft = new EdgeEffect(context); - mEdgeGlowRight = new EdgeEffect(context); - } - } else { - mEdgeGlowLeft = null; - mEdgeGlowRight = null; - } - super.setOverScrollMode(mode); + private boolean shouldDisplayEdgeEffects() { + return getOverScrollMode() != OVER_SCROLL_NEVER; } @SuppressWarnings({"SuspiciousNameCombination"}) @Override public void draw(Canvas canvas) { super.draw(canvas); - if (mEdgeGlowLeft != null) { + if (shouldDisplayEdgeEffects()) { final int scrollX = mScrollX; if (!mEdgeGlowLeft.isFinished()) { final int restoreCount = canvas.save(); diff --git a/core/java/android/widget/ScrollView.java b/core/java/android/widget/ScrollView.java index 24bc9f1e4985..07b34f433dc7 100644 --- a/core/java/android/widget/ScrollView.java +++ b/core/java/android/widget/ScrollView.java @@ -16,6 +16,7 @@ package android.widget; +import android.annotation.ColorInt; import android.annotation.NonNull; import android.annotation.UnsupportedAppUsage; import android.content.Context; @@ -89,10 +90,25 @@ public class ScrollView extends FrameLayout { private final Rect mTempRect = new Rect(); @UnsupportedAppUsage private OverScroller mScroller; - @UnsupportedAppUsage - private EdgeEffect mEdgeGlowTop; - @UnsupportedAppUsage - private EdgeEffect mEdgeGlowBottom; + /** + * Tracks the state of the top edge glow. + * + * Even though this field is practically final, we cannot make it final because there are apps + * setting it via reflection and they need to keep working until they target Q. + */ + @NonNull + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 123768600) + private EdgeEffect mEdgeGlowTop = new EdgeEffect(getContext()); + + /** + * Tracks the state of the bottom edge glow. + * + * Even though this field is practically final, we cannot make it final because there are apps + * setting it via reflection and they need to keep working until they target Q. + */ + @NonNull + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 123769386) + private EdgeEffect mEdgeGlowBottom = new EdgeEffect(getContext()); /** * Position of the last motion event. @@ -247,6 +263,74 @@ public class ScrollView extends FrameLayout { } /** + * Sets the edge effect color for both top and bottom edge effects. + * + * @param color The color for the edge effects. + * @see #setTopEdgeEffectColor(int) + * @see #setBottomEdgeEffectColor(int) + * @see #getTopEdgeEffectColor() + * @see #getBottomEdgeEffectColor() + */ + public void setEdgeEffectColor(@ColorInt int color) { + setTopEdgeEffectColor(color); + setBottomEdgeEffectColor(color); + } + + /** + * Sets the bottom edge effect color. + * + * @param color The color for the bottom edge effect. + * @see #setTopEdgeEffectColor(int) + * @see #setEdgeEffectColor(int) + * @see #getTopEdgeEffectColor() + * @see #getBottomEdgeEffectColor() + */ + public void setBottomEdgeEffectColor(@ColorInt int color) { + mEdgeGlowBottom.setColor(color); + } + + /** + * Sets the top edge effect color. + * + * @param color The color for the top edge effect. + * @see #setBottomEdgeEffectColor(int) + * @see #setEdgeEffectColor(int) + * @see #getTopEdgeEffectColor() + * @see #getBottomEdgeEffectColor() + */ + public void setTopEdgeEffectColor(@ColorInt int color) { + mEdgeGlowTop.setColor(color); + } + + /** + * Returns the top edge effect color. + * + * @return The top edge effect color. + * @see #setEdgeEffectColor(int) + * @see #setTopEdgeEffectColor(int) + * @see #setBottomEdgeEffectColor(int) + * @see #getBottomEdgeEffectColor() + */ + @ColorInt + public int getTopEdgeEffectColor() { + return mEdgeGlowTop.getColor(); + } + + /** + * Returns the bottom edge effect color. + * + * @return The bottom edge effect color. + * @see #setEdgeEffectColor(int) + * @see #setTopEdgeEffectColor(int) + * @see #setBottomEdgeEffectColor(int) + * @see #getTopEdgeEffectColor() + */ + @ColorInt + public int getBottomEdgeEffectColor() { + return mEdgeGlowBottom.getColor(); + } + + /** * @return The maximum amount this scroll view will scroll in response to * an arrow event. */ @@ -624,6 +708,10 @@ public class ScrollView extends FrameLayout { return mIsBeingDragged; } + private boolean shouldDisplayEdgeEffects() { + return getOverScrollMode() != OVER_SCROLL_NEVER; + } + @Override public boolean onTouchEvent(MotionEvent ev) { initVelocityTrackerIfNotExists(); @@ -732,7 +820,7 @@ public class ScrollView extends FrameLayout { mEdgeGlowTop.onRelease(); } } - if (mEdgeGlowTop != null + if (shouldDisplayEdgeEffects() && (!mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) { postInvalidateOnAnimation(); } @@ -1670,7 +1758,7 @@ public class ScrollView extends FrameLayout { recycleVelocityTracker(); - if (mEdgeGlowTop != null) { + if (shouldDisplayEdgeEffects()) { mEdgeGlowTop.onRelease(); mEdgeGlowBottom.onRelease(); } @@ -1700,21 +1788,6 @@ public class ScrollView extends FrameLayout { } @Override - public void setOverScrollMode(int mode) { - if (mode != OVER_SCROLL_NEVER) { - if (mEdgeGlowTop == null) { - Context context = getContext(); - mEdgeGlowTop = new EdgeEffect(context); - mEdgeGlowBottom = new EdgeEffect(context); - } - } else { - mEdgeGlowTop = null; - mEdgeGlowBottom = null; - } - super.setOverScrollMode(mode); - } - - @Override public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) { return (nestedScrollAxes & SCROLL_AXIS_VERTICAL) != 0; } @@ -1758,7 +1831,7 @@ public class ScrollView extends FrameLayout { @Override public void draw(Canvas canvas) { super.draw(canvas); - if (mEdgeGlowTop != null) { + if (shouldDisplayEdgeEffects()) { final int scrollY = mScrollY; final boolean clipToPadding = getClipToPadding(); if (!mEdgeGlowTop.isFinished()) { |
