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
This commit is contained in:
Nader Jawad
2018-04-09 09:54:48 -07:00
parent 02adb97ec2
commit 85ff47e3fc
9 changed files with 52 additions and 73 deletions

View File

@@ -793,8 +793,6 @@ Landroid/graphics/NinePatch$InsetStruct;-><init>(IIIIIIIIFIF)V
Landroid/graphics/NinePatch;->mBitmap:Landroid/graphics/Bitmap;
Landroid/graphics/Picture;->mNativePicture:J
Landroid/graphics/PixelXorXfermode;-><init>(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;-><init>(JI)V
Landroid/graphics/Region;->mNativeRegion:J

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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);

View File

@@ -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();
}

View File

@@ -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

View File

@@ -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();
}

View File

@@ -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();
}