From 85ff47e3fc50aca0befb16b233f7cac2630a0d73 Mon Sep 17 00:00:00 2001 From: Nader Jawad Date: Mon, 9 Apr 2018 09:54:48 -0700 Subject: [PATCH] Removed hidden mutable APIs from PorterDuffColorFilter Removed PorterDuffColorFilter#setColor and PorterDuffColorFilter#setMode as the public facing ColorFilter API is immutable. These framework internal APIs were causing issues with Drawables as updates to state of the ColorFilter would not be propagated up to the Drawable to cause an invalidation. Fixes: 77723600 Test: Ran atest on SystemUITest and CtsGraphicsTest modules Change-Id: I935c9e35ffa225735b951bb3b1eb753ea5815a84 --- config/hiddenapi-light-greylist.txt | 2 - .../graphics/PorterDuffColorFilter.java | 41 ------------------- .../android/graphics/drawable/Drawable.java | 15 ++++--- .../graphics/drawable/RippleDrawable.java | 5 ++- .../drawable/UserIconDrawable.java | 18 ++++++-- .../android/systemui/statusbar/ScrimView.java | 15 +++++-- .../NotificationIconDozeHelper.java | 12 ++++-- .../statusbar/phone/BarTransitions.java | 15 ++++--- .../test/hwui/ColorFiltersMutateActivity.java | 2 +- 9 files changed, 52 insertions(+), 73 deletions(-) diff --git a/config/hiddenapi-light-greylist.txt b/config/hiddenapi-light-greylist.txt index a1e71f46b61b8..973968469e488 100644 --- a/config/hiddenapi-light-greylist.txt +++ b/config/hiddenapi-light-greylist.txt @@ -793,8 +793,6 @@ Landroid/graphics/NinePatch$InsetStruct;->(IIIIIIIIFIF)V Landroid/graphics/NinePatch;->mBitmap:Landroid/graphics/Bitmap; Landroid/graphics/Picture;->mNativePicture:J Landroid/graphics/PixelXorXfermode;->(I)V -Landroid/graphics/PorterDuffColorFilter;->setColor(I)V -Landroid/graphics/PorterDuffColorFilter;->setMode(Landroid/graphics/PorterDuff$Mode;)V Landroid/graphics/Rect;->scale(F)V Landroid/graphics/Region;->(JI)V Landroid/graphics/Region;->mNativeRegion:J diff --git a/graphics/java/android/graphics/PorterDuffColorFilter.java b/graphics/java/android/graphics/PorterDuffColorFilter.java index 01d5825dd1e09..88a63223ea402 100644 --- a/graphics/java/android/graphics/PorterDuffColorFilter.java +++ b/graphics/java/android/graphics/PorterDuffColorFilter.java @@ -35,8 +35,6 @@ public class PorterDuffColorFilter extends ColorFilter { * @param mode The porter-duff mode that is applied * * @see Color - * @see #setColor(int) - * @see #setMode(android.graphics.PorterDuff.Mode) */ public PorterDuffColorFilter(@ColorInt int color, @NonNull PorterDuff.Mode mode) { mColor = color; @@ -48,7 +46,6 @@ public class PorterDuffColorFilter extends ColorFilter { * is applied. * * @see Color - * @see #setColor(int) * * @hide */ @@ -57,31 +54,11 @@ public class PorterDuffColorFilter extends ColorFilter { return mColor; } - /** - * Specifies the color to tint the source pixels with when this color - * filter is applied. - * - * @param color An ARGB {@link Color color} - * - * @see Color - * @see #getColor() - * @see #getMode() - * - * @hide - */ - public void setColor(@ColorInt int color) { - if (mColor != color) { - mColor = color; - discardNativeInstance(); - } - } - /** * Returns the Porter-Duff mode used to composite this color filter's * color with the source pixel when this filter is applied. * * @see PorterDuff - * @see #setMode(android.graphics.PorterDuff.Mode) * * @hide */ @@ -89,24 +66,6 @@ public class PorterDuffColorFilter extends ColorFilter { return mMode; } - /** - * Specifies the Porter-Duff mode to use when compositing this color - * filter's color with the source pixel at draw time. - * - * @see PorterDuff - * @see #getMode() - * @see #getColor() - * - * @hide - */ - public void setMode(@NonNull PorterDuff.Mode mode) { - if (mode == null) { - throw new IllegalArgumentException("mode must be non-null"); - } - mMode = mode; - discardNativeInstance(); - } - @Override long createNativeInstance() { return native_CreatePorterDuffFilter(mColor, mMode.nativeInt); diff --git a/graphics/java/android/graphics/drawable/Drawable.java b/graphics/java/android/graphics/drawable/Drawable.java index 8af2fd8bbb5ec..b77d74a7c675a 100644 --- a/graphics/java/android/graphics/drawable/Drawable.java +++ b/graphics/java/android/graphics/drawable/Drawable.java @@ -16,11 +16,6 @@ package android.graphics.drawable; -import com.android.internal.R; - -import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; - import android.annotation.AttrRes; import android.annotation.ColorInt; import android.annotation.IntRange; @@ -57,6 +52,11 @@ import android.util.TypedValue; import android.util.Xml; import android.view.View; +import com.android.internal.R; + +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; @@ -1516,12 +1516,11 @@ public abstract class Drawable { } final int color = tint.getColorForState(getState(), Color.TRANSPARENT); - if (tintFilter == null) { + if (tintFilter == null || tintFilter.getColor() != color + || tintFilter.getMode() != tintMode) { return new PorterDuffColorFilter(color, tintMode); } - tintFilter.setColor(color); - tintFilter.setMode(tintMode); return tintFilter; } diff --git a/graphics/java/android/graphics/drawable/RippleDrawable.java b/graphics/java/android/graphics/drawable/RippleDrawable.java index 0da61c29bd8db..266a6ac87d539 100644 --- a/graphics/java/android/graphics/drawable/RippleDrawable.java +++ b/graphics/java/android/graphics/drawable/RippleDrawable.java @@ -888,7 +888,10 @@ public class RippleDrawable extends LayerDrawable { // The ripple timing depends on the paint's alpha value, so we need // to push just the alpha channel into the paint and let the filter // handle the full-alpha color. - mMaskColorFilter.setColor(color | 0xFF000000); + int maskColor = color | 0xFF000000; + if (mMaskColorFilter.getColor() != maskColor) { + mMaskColorFilter = new PorterDuffColorFilter(maskColor, mMaskColorFilter.getMode()); + } p.setColor(color & 0xFF000000); p.setColorFilter(mMaskColorFilter); p.setShader(mMaskShader); diff --git a/packages/SettingsLib/src/com/android/settingslib/drawable/UserIconDrawable.java b/packages/SettingsLib/src/com/android/settingslib/drawable/UserIconDrawable.java index 54d1aba09ae38..148b7a72b705b 100644 --- a/packages/SettingsLib/src/com/android/settingslib/drawable/UserIconDrawable.java +++ b/packages/SettingsLib/src/com/android/settingslib/drawable/UserIconDrawable.java @@ -16,6 +16,7 @@ package com.android.settingslib.drawable; +import android.annotation.ColorInt; import android.annotation.DrawableRes; import android.annotation.NonNull; import android.app.admin.DevicePolicyManager; @@ -251,11 +252,8 @@ public class UserIconDrawable extends Drawable implements Drawable.Callback { mPaint.setColorFilter(null); } else { int color = mTintColor.getColorForState(getState(), mTintColor.getDefaultColor()); - if (mPaint.getColorFilter() == null) { + if (shouldUpdateColorFilter(color, mTintMode)) { mPaint.setColorFilter(new PorterDuffColorFilter(color, mTintMode)); - } else { - ((PorterDuffColorFilter) mPaint.getColorFilter()).setMode(mTintMode); - ((PorterDuffColorFilter) mPaint.getColorFilter()).setColor(color); } } @@ -263,6 +261,18 @@ public class UserIconDrawable extends Drawable implements Drawable.Callback { } } + private boolean shouldUpdateColorFilter(@ColorInt int color, PorterDuff.Mode mode) { + ColorFilter colorFilter = mPaint.getColorFilter(); + if (colorFilter instanceof PorterDuffColorFilter) { + PorterDuffColorFilter porterDuffColorFilter = (PorterDuffColorFilter) colorFilter; + int currentColor = porterDuffColorFilter.getColor(); + PorterDuff.Mode currentMode = porterDuffColorFilter.getMode(); + return currentColor != color || currentMode != mode; + } else { + return false; + } + } + @Override public void setAlpha(int alpha) { mPaint.setAlpha(alpha); diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/ScrimView.java b/packages/SystemUI/src/com/android/systemui/statusbar/ScrimView.java index 1807465004c68..c99bdef364928 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/ScrimView.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/ScrimView.java @@ -26,6 +26,7 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Point; import android.graphics.PorterDuff; +import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffColorFilter; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; @@ -272,12 +273,18 @@ public class ScrimView extends View implements ConfigurationController.Configura tintAmount); drawable.setColors(mainTinted, secondaryTinted, animated); } else { - if (mColorFilter == null) { - mColorFilter = new PorterDuffColorFilter(mTintColor, PorterDuff.Mode.SRC_OVER); + boolean hasAlpha = Color.alpha(mTintColor) != 0; + if (hasAlpha) { + PorterDuff.Mode targetMode = mColorFilter == null ? Mode.SRC_OVER : + mColorFilter.getMode(); + if (mColorFilter == null || mColorFilter.getColor() != mTintColor) { + mColorFilter = new PorterDuffColorFilter(mTintColor, targetMode); + } } else { - mColorFilter.setColor(mTintColor); + mColorFilter = null; } - mDrawable.setColorFilter(Color.alpha(mTintColor) == 0 ? null : mColorFilter); + + mDrawable.setColorFilter(mColorFilter); mDrawable.invalidateSelf(); } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationIconDozeHelper.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationIconDozeHelper.java index 9f79ef2491d97..43e45b14cfbe5 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationIconDozeHelper.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationIconDozeHelper.java @@ -16,9 +16,10 @@ package com.android.systemui.statusbar.notification; +import android.annotation.Nullable; import android.content.Context; import android.graphics.Color; -import android.graphics.PorterDuff; +import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffColorFilter; import android.graphics.drawable.Drawable; import android.widget.ImageView; @@ -29,8 +30,9 @@ public class NotificationIconDozeHelper extends NotificationDozeHelper { private final int mImageDarkAlpha; private final int mImageDarkColor = 0xffffffff; - private final PorterDuffColorFilter mImageColorFilter = new PorterDuffColorFilter( - 0, PorterDuff.Mode.SRC_ATOP); + + @Nullable + private PorterDuffColorFilter mImageColorFilter = null; private int mColor = Color.BLACK; @@ -80,7 +82,9 @@ public class NotificationIconDozeHelper extends NotificationDozeHelper { private void updateImageColorFilter(ImageView target, float intensity) { int color = NotificationUtils.interpolateColors(mColor, mImageDarkColor, intensity); - mImageColorFilter.setColor(color); + if (mImageColorFilter == null || mImageColorFilter.getColor() != color) { + mImageColorFilter = new PorterDuffColorFilter(color, Mode.SRC_ATOP); + } Drawable imageDrawable = target.getDrawable(); // Also, the notification might have been modified during the animation, so background diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/BarTransitions.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/BarTransitions.java index 4bca79715422c..5ec822dcbe084 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/BarTransitions.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/BarTransitions.java @@ -183,20 +183,19 @@ public class BarTransitions { @Override public void setTint(int color) { - if (mTintFilter == null) { - mTintFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN); - } else { - mTintFilter.setColor(color); + PorterDuff.Mode targetMode = mTintFilter == null ? Mode.SRC_IN : + mTintFilter.getMode(); + if (mTintFilter == null || mTintFilter.getColor() != color) { + mTintFilter = new PorterDuffColorFilter(color, targetMode); } invalidateSelf(); } @Override public void setTintMode(Mode tintMode) { - if (mTintFilter == null) { - mTintFilter = new PorterDuffColorFilter(0, tintMode); - } else { - mTintFilter.setMode(tintMode); + int targetColor = mTintFilter == null ? 0 : mTintFilter.getColor(); + if (mTintFilter == null || mTintFilter.getMode() != tintMode) { + mTintFilter = new PorterDuffColorFilter(targetColor, tintMode); } invalidateSelf(); } diff --git a/tests/HwAccelerationTest/src/com/android/test/hwui/ColorFiltersMutateActivity.java b/tests/HwAccelerationTest/src/com/android/test/hwui/ColorFiltersMutateActivity.java index a7bdabd646840..0787d823756c6 100644 --- a/tests/HwAccelerationTest/src/com/android/test/hwui/ColorFiltersMutateActivity.java +++ b/tests/HwAccelerationTest/src/com/android/test/hwui/ColorFiltersMutateActivity.java @@ -106,7 +106,7 @@ public class ColorFiltersMutateActivity extends Activity { mPorterDuffColor = porterDuffColor; final PorterDuffColorFilter filter = (PorterDuffColorFilter) mBlendPaint.getColorFilter(); - filter.setColor(mPorterDuffColor); + mBlendPaint.setColorFilter(new PorterDuffColorFilter(porterDuffColor, filter.getMode())); invalidate(); }