Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Joe Onorato | 79de0c5 | 2010-05-26 17:03:26 -0400 | [diff] [blame] | 17 | package com.android.systemui.statusbar; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 18 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 19 | import static com.android.systemui.statusbar.policy.DarkIconDispatcher.getTint; |
| 20 | |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
| 23 | import android.animation.ObjectAnimator; |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 24 | import android.animation.ValueAnimator; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 25 | import android.app.Notification; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Adrian Roos | a8e18ef | 2016-05-26 17:17:02 -0700 | [diff] [blame] | 27 | import android.content.pm.ApplicationInfo; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 28 | import android.content.res.ColorStateList; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 29 | import android.content.res.Configuration; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 30 | import android.content.res.Resources; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 31 | import android.graphics.Canvas; |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 32 | import android.graphics.Color; |
Adrian Roos | c3e8cfc | 2017-07-17 17:09:39 +0200 | [diff] [blame] | 33 | import android.graphics.ColorMatrixColorFilter; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 34 | import android.graphics.Paint; |
| 35 | import android.graphics.Rect; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 36 | import android.graphics.drawable.Drawable; |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 37 | import android.graphics.drawable.Icon; |
Adrian Roos | a8e18ef | 2016-05-26 17:17:02 -0700 | [diff] [blame] | 38 | import android.os.Parcelable; |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 39 | import android.os.UserHandle; |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 40 | import android.service.notification.StatusBarNotification; |
Lucas Dupin | 83519da | 2017-06-21 11:58:31 -0700 | [diff] [blame] | 41 | import android.support.v4.graphics.ColorUtils; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 42 | import android.text.TextUtils; |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 43 | import android.util.AttributeSet; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 44 | import android.util.FloatProperty; |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 45 | import android.util.Log; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 46 | import android.util.Property; |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 47 | import android.util.TypedValue; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 48 | import android.view.ViewDebug; |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 49 | import android.view.accessibility.AccessibilityEvent; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 50 | import android.view.animation.Interpolator; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 51 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 52 | import com.android.internal.statusbar.StatusBarIcon; |
Lucas Dupin | 83519da | 2017-06-21 11:58:31 -0700 | [diff] [blame] | 53 | import com.android.internal.util.NotificationColorUtil; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 54 | import com.android.systemui.Interpolators; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 55 | import com.android.systemui.R; |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 56 | import com.android.systemui.statusbar.notification.NotificationIconDozeHelper; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 57 | import com.android.systemui.statusbar.notification.NotificationUtils; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 58 | |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 59 | import java.text.NumberFormat; |
Adrian Roos | c3e8cfc | 2017-07-17 17:09:39 +0200 | [diff] [blame] | 60 | import java.util.Arrays; |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 61 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 62 | public class StatusBarIconView extends AnimatedImageView implements StatusIconDisplayable { |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 63 | public static final int NO_COLOR = 0; |
Adrian Roos | c3e8cfc | 2017-07-17 17:09:39 +0200 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Multiply alpha values with (1+DARK_ALPHA_BOOST) when dozing. The chosen value boosts |
| 67 | * everything above 30% to 50%, making it appear on 1bit color depths. |
| 68 | */ |
| 69 | private static final float DARK_ALPHA_BOOST = 0.67f; |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 70 | private final int ANIMATION_DURATION_FAST = 100; |
| 71 | |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 72 | public static final int STATE_ICON = 0; |
| 73 | public static final int STATE_DOT = 1; |
| 74 | public static final int STATE_HIDDEN = 2; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 75 | |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 76 | private static final String TAG = "StatusBarIconView"; |
| 77 | private static final Property<StatusBarIconView, Float> ICON_APPEAR_AMOUNT |
| 78 | = new FloatProperty<StatusBarIconView>("iconAppearAmount") { |
| 79 | |
| 80 | @Override |
| 81 | public void setValue(StatusBarIconView object, float value) { |
| 82 | object.setIconAppearAmount(value); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Float get(StatusBarIconView object) { |
| 87 | return object.getIconAppearAmount(); |
| 88 | } |
| 89 | }; |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 90 | private static final Property<StatusBarIconView, Float> DOT_APPEAR_AMOUNT |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 91 | = new FloatProperty<StatusBarIconView>("dot_appear_amount") { |
| 92 | |
| 93 | @Override |
| 94 | public void setValue(StatusBarIconView object, float value) { |
| 95 | object.setDotAppearAmount(value); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Float get(StatusBarIconView object) { |
| 100 | return object.getDotAppearAmount(); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | private boolean mAlwaysScaleIcon; |
Adrian Roos | 138f034 | 2017-04-28 09:11:28 -0700 | [diff] [blame] | 105 | private int mStatusBarIconDrawingSizeDark = 1; |
| 106 | private int mStatusBarIconDrawingSize = 1; |
| 107 | private int mStatusBarIconSize = 1; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 108 | private StatusBarIcon mIcon; |
| 109 | @ViewDebug.ExportedProperty private String mSlot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 110 | private Drawable mNumberBackground; |
| 111 | private Paint mNumberPain; |
| 112 | private int mNumberX; |
| 113 | private int mNumberY; |
| 114 | private String mNumberText; |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 115 | private StatusBarNotification mNotification; |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 116 | private final boolean mBlocked; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 117 | private int mDensity; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 118 | private float mIconScale = 1.0f; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 119 | private final Paint mDotPaint = new Paint(); |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 120 | private float mDotRadius; |
| 121 | private int mStaticDotRadius; |
| 122 | private int mVisibleState = STATE_ICON; |
| 123 | private float mIconAppearAmount = 1.0f; |
| 124 | private ObjectAnimator mIconAppearAnimator; |
| 125 | private ObjectAnimator mDotAnimator; |
| 126 | private float mDotAppearAmount; |
Selim Cinek | 2b549f4 | 2016-11-22 16:38:51 -0800 | [diff] [blame] | 127 | private OnVisibilityChangedListener mOnVisibilityChangedListener; |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 128 | private int mDrawableColor; |
| 129 | private int mIconColor; |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 130 | private int mDecorColor; |
| 131 | private float mDarkAmount; |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 132 | private ValueAnimator mColorAnimator; |
| 133 | private int mCurrentSetColor = NO_COLOR; |
| 134 | private int mAnimationStartColor = NO_COLOR; |
| 135 | private final ValueAnimator.AnimatorUpdateListener mColorUpdater |
| 136 | = animation -> { |
| 137 | int newColor = NotificationUtils.interpolateColors(mAnimationStartColor, mIconColor, |
| 138 | animation.getAnimatedFraction()); |
| 139 | setColorInternal(newColor); |
| 140 | }; |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 141 | private final NotificationIconDozeHelper mDozer; |
Lucas Dupin | 83519da | 2017-06-21 11:58:31 -0700 | [diff] [blame] | 142 | private int mContrastedDrawableColor; |
| 143 | private int mCachedContrastBackgroundColor = NO_COLOR; |
Adrian Roos | c3e8cfc | 2017-07-17 17:09:39 +0200 | [diff] [blame] | 144 | private float[] mMatrix; |
| 145 | private ColorMatrixColorFilter mMatrixColorFilter; |
Selim Cinek | d95ca7c | 2017-07-26 12:20:38 -0700 | [diff] [blame] | 146 | private boolean mIsInShelf; |
Selim Cinek | 887da3c | 2017-10-06 13:37:32 -0700 | [diff] [blame] | 147 | private Runnable mLayoutRunnable; |
Selim Cinek | d03518c | 2018-03-15 12:13:51 -0700 | [diff] [blame] | 148 | private boolean mDismissed; |
| 149 | private Runnable mOnDismissListener; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 150 | |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 151 | public StatusBarIconView(Context context, String slot, StatusBarNotification sbn) { |
| 152 | this(context, slot, sbn, false); |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 155 | public StatusBarIconView(Context context, String slot, StatusBarNotification sbn, |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 156 | boolean blocked) { |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 157 | super(context); |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 158 | mDozer = new NotificationIconDozeHelper(context); |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 159 | mBlocked = blocked; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 160 | mSlot = slot; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 161 | mNumberPain = new Paint(); |
| 162 | mNumberPain.setTextAlign(Paint.Align.CENTER); |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 163 | mNumberPain.setColor(context.getColor(R.drawable.notification_number_text_color)); |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 164 | mNumberPain.setAntiAlias(true); |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 165 | setNotification(sbn); |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 166 | setScaleType(ScaleType.CENTER); |
| 167 | mDensity = context.getResources().getDisplayMetrics().densityDpi; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 168 | if (mNotification != null) { |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 169 | setDecorColor(getContext().getColor( |
Selim Cinek | c7f5a82 | 2018-03-20 19:32:06 -0700 | [diff] [blame] | 170 | com.android.internal.R.color.notification_default_color_light)); |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 171 | } |
| 172 | reloadDimens(); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 173 | maybeUpdateIconScaleDimens(); |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 174 | } |
Daniel Sandler | 26c84b1 | 2011-07-27 00:09:40 -0400 | [diff] [blame] | 175 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 176 | /** Should always be preceded by {@link #reloadDimens()} */ |
Adrian Roos | 138f034 | 2017-04-28 09:11:28 -0700 | [diff] [blame] | 177 | private void maybeUpdateIconScaleDimens() { |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 178 | // We do not resize and scale system icons (on the right), only notification icons (on the |
| 179 | // left). |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 180 | if (mNotification != null || mAlwaysScaleIcon) { |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 181 | updateIconScale(); |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 182 | } |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 183 | } |
Daniel Sandler | abff032 | 2011-09-27 11:19:34 -0400 | [diff] [blame] | 184 | |
Adrian Roos | 138f034 | 2017-04-28 09:11:28 -0700 | [diff] [blame] | 185 | private void updateIconScale() { |
| 186 | final float imageBounds = NotificationUtils.interpolate( |
| 187 | mStatusBarIconDrawingSize, |
| 188 | mStatusBarIconDrawingSizeDark, |
| 189 | mDarkAmount); |
| 190 | final int outerBounds = mStatusBarIconSize; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 191 | mIconScale = (float)imageBounds / (float)outerBounds; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Adrian Roos | ceac4a0 | 2017-05-30 20:25:52 -0700 | [diff] [blame] | 194 | public float getIconScaleFullyDark() { |
| 195 | return (float) mStatusBarIconDrawingSizeDark / mStatusBarIconDrawingSize; |
| 196 | } |
| 197 | |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 198 | public float getIconScale() { |
| 199 | return mIconScale; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
| 203 | protected void onConfigurationChanged(Configuration newConfig) { |
| 204 | super.onConfigurationChanged(newConfig); |
| 205 | int density = newConfig.densityDpi; |
| 206 | if (density != mDensity) { |
| 207 | mDensity = density; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 208 | reloadDimens(); |
Adrian Roos | 138f034 | 2017-04-28 09:11:28 -0700 | [diff] [blame] | 209 | maybeUpdateIconScaleDimens(); |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 210 | updateDrawable(); |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | private void reloadDimens() { |
| 215 | boolean applyRadius = mDotRadius == mStaticDotRadius; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 216 | Resources res = getResources(); |
| 217 | mStaticDotRadius = res.getDimensionPixelSize(R.dimen.overflow_dot_radius); |
| 218 | mStatusBarIconSize = res.getDimensionPixelSize(R.dimen.status_bar_icon_size); |
| 219 | mStatusBarIconDrawingSizeDark = |
| 220 | res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size_dark); |
| 221 | mStatusBarIconDrawingSize = |
| 222 | res.getDimensionPixelSize(R.dimen.status_bar_icon_drawing_size); |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 223 | if (applyRadius) { |
| 224 | mDotRadius = mStaticDotRadius; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 225 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 228 | public void setNotification(StatusBarNotification notification) { |
Christoph Studer | a0506e7 | 2014-07-31 20:27:39 +0200 | [diff] [blame] | 229 | mNotification = notification; |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 230 | if (notification != null) { |
| 231 | setContentDescription(notification.getNotification()); |
| 232 | } |
Christoph Studer | a0506e7 | 2014-07-31 20:27:39 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 235 | public StatusBarIconView(Context context, AttributeSet attrs) { |
| 236 | super(context, attrs); |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 237 | mDozer = new NotificationIconDozeHelper(context); |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 238 | mBlocked = false; |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 239 | mAlwaysScaleIcon = true; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 240 | reloadDimens(); |
| 241 | updateIconScale(); |
Selim Cinek | 3e7592d | 2016-04-11 09:35:54 +0800 | [diff] [blame] | 242 | mDensity = context.getResources().getDisplayMetrics().densityDpi; |
Daniel Sandler | 05e2414 | 2011-11-10 11:56:49 -0500 | [diff] [blame] | 243 | } |
| 244 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 245 | private static boolean streq(String a, String b) { |
Joe Onorato | 66d7d01 | 2010-05-14 10:05:10 -0700 | [diff] [blame] | 246 | if (a == b) { |
| 247 | return true; |
| 248 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 249 | if (a == null && b != null) { |
| 250 | return false; |
| 251 | } |
| 252 | if (a != null && b == null) { |
| 253 | return false; |
| 254 | } |
| 255 | return a.equals(b); |
| 256 | } |
| 257 | |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 258 | public boolean equalIcons(Icon a, Icon b) { |
| 259 | if (a == b) return true; |
| 260 | if (a.getType() != b.getType()) return false; |
| 261 | switch (a.getType()) { |
| 262 | case Icon.TYPE_RESOURCE: |
| 263 | return a.getResPackage().equals(b.getResPackage()) && a.getResId() == b.getResId(); |
| 264 | case Icon.TYPE_URI: |
| 265 | return a.getUriString().equals(b.getUriString()); |
| 266 | default: |
| 267 | return false; |
| 268 | } |
| 269 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 270 | /** |
| 271 | * Returns whether the set succeeded. |
| 272 | */ |
| 273 | public boolean set(StatusBarIcon icon) { |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 274 | final boolean iconEquals = mIcon != null && equalIcons(mIcon.icon, icon.icon); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 275 | final boolean levelEquals = iconEquals |
| 276 | && mIcon.iconLevel == icon.iconLevel; |
| 277 | final boolean visibilityEquals = mIcon != null |
| 278 | && mIcon.visible == icon.visible; |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 279 | final boolean numberEquals = mIcon != null |
| 280 | && mIcon.number == icon.number; |
| 281 | mIcon = icon.clone(); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 282 | setContentDescription(icon.contentDescription); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 283 | if (!iconEquals) { |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 284 | if (!updateDrawable(false /* no clear */)) return false; |
Selim Cinek | 2b6eb803 | 2016-12-29 14:22:21 +0100 | [diff] [blame] | 285 | // we have to clear the grayscale tag since it may have changed |
| 286 | setTag(R.id.icon_is_grayscale, null); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 287 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 288 | if (!levelEquals) { |
| 289 | setImageLevel(icon.iconLevel); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 290 | } |
Daniel Sandler | 26c84b1 | 2011-07-27 00:09:40 -0400 | [diff] [blame] | 291 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 292 | if (!numberEquals) { |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 293 | if (icon.number > 0 && getContext().getResources().getBoolean( |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 294 | R.bool.config_statusBarShowNumber)) { |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 295 | if (mNumberBackground == null) { |
| 296 | mNumberBackground = getContext().getResources().getDrawable( |
| 297 | R.drawable.ic_notification_overlay); |
| 298 | } |
| 299 | placeNumber(); |
| 300 | } else { |
| 301 | mNumberBackground = null; |
| 302 | mNumberText = null; |
| 303 | } |
| 304 | invalidate(); |
| 305 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 306 | if (!visibilityEquals) { |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 307 | setVisibility(icon.visible && !mBlocked ? VISIBLE : GONE); |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 308 | } |
Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 309 | return true; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 312 | public void updateDrawable() { |
| 313 | updateDrawable(true /* with clear */); |
| 314 | } |
| 315 | |
| 316 | private boolean updateDrawable(boolean withClear) { |
Jorim Jaggi | 66ac133 | 2015-01-21 19:22:26 +0100 | [diff] [blame] | 317 | if (mIcon == null) { |
| 318 | return false; |
| 319 | } |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 320 | Drawable drawable; |
| 321 | try { |
| 322 | drawable = getIcon(mIcon); |
| 323 | } catch (OutOfMemoryError e) { |
| 324 | Log.w(TAG, "OOM while inflating " + mIcon.icon + " for slot " + mSlot); |
| 325 | return false; |
| 326 | } |
| 327 | |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 328 | if (drawable == null) { |
Adrian Roos | fb2d0cc | 2017-01-31 15:10:00 -0800 | [diff] [blame] | 329 | Log.w(TAG, "No icon for slot " + mSlot + "; " + mIcon.icon); |
Fabrice Di Meglio | 82fca5d | 2013-01-09 15:42:31 -0800 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | if (withClear) { |
| 333 | setImageDrawable(null); |
| 334 | } |
| 335 | setImageDrawable(drawable); |
| 336 | return true; |
| 337 | } |
| 338 | |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 339 | public Icon getSourceIcon() { |
| 340 | return mIcon.icon; |
| 341 | } |
| 342 | |
Joe Onorato | f551054 | 2010-06-01 07:46:41 -0700 | [diff] [blame] | 343 | private Drawable getIcon(StatusBarIcon icon) { |
| 344 | return getIcon(getContext(), icon); |
| 345 | } |
| 346 | |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 347 | /** |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 348 | * Returns the right icon to use for this item |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 349 | * |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 350 | * @param context Context to use to get resources |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 351 | * @return Drawable for this item, or null if the package or item could not |
| 352 | * be found |
| 353 | */ |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 354 | public static Drawable getIcon(Context context, StatusBarIcon statusBarIcon) { |
| 355 | int userId = statusBarIcon.user.getIdentifier(); |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 356 | if (userId == UserHandle.USER_ALL) { |
Xiaohui Chen | 87d0e25 | 2015-07-30 15:38:16 -0700 | [diff] [blame] | 357 | userId = UserHandle.USER_SYSTEM; |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 358 | } |
Anthony Chen | 55e8e1e | 2016-01-08 10:31:46 -0800 | [diff] [blame] | 359 | |
| 360 | Drawable icon = statusBarIcon.icon.loadDrawableAsUser(context, userId); |
| 361 | |
| 362 | TypedValue typedValue = new TypedValue(); |
| 363 | context.getResources().getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true); |
| 364 | float scaleFactor = typedValue.getFloat(); |
| 365 | |
| 366 | // No need to scale the icon, so return it as is. |
| 367 | if (scaleFactor == 1.f) { |
| 368 | return icon; |
| 369 | } |
| 370 | |
| 371 | return new ScalingDrawableWrapper(icon, scaleFactor); |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 372 | } |
Joe Onorato | b77f53b | 2010-05-28 19:59:51 -0400 | [diff] [blame] | 373 | |
| 374 | public StatusBarIcon getStatusBarIcon() { |
| 375 | return mIcon; |
| 376 | } |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 377 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 378 | @Override |
| 379 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 380 | super.onInitializeAccessibilityEvent(event); |
| 381 | if (mNotification != null) { |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 382 | event.setParcelableData(mNotification.getNotification()); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | @Override |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 387 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 388 | super.onSizeChanged(w, h, oldw, oldh); |
| 389 | if (mNumberBackground != null) { |
| 390 | placeNumber(); |
| 391 | } |
| 392 | } |
| 393 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 394 | @Override |
Jorim Jaggi | 66ac133 | 2015-01-21 19:22:26 +0100 | [diff] [blame] | 395 | public void onRtlPropertiesChanged(int layoutDirection) { |
| 396 | super.onRtlPropertiesChanged(layoutDirection); |
| 397 | updateDrawable(); |
| 398 | } |
| 399 | |
| 400 | @Override |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 401 | protected void onDraw(Canvas canvas) { |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 402 | if (mIconAppearAmount > 0.0f) { |
| 403 | canvas.save(); |
| 404 | canvas.scale(mIconScale * mIconAppearAmount, mIconScale * mIconAppearAmount, |
| 405 | getWidth() / 2, getHeight() / 2); |
| 406 | super.onDraw(canvas); |
| 407 | canvas.restore(); |
| 408 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 409 | |
| 410 | if (mNumberBackground != null) { |
| 411 | mNumberBackground.draw(canvas); |
| 412 | canvas.drawText(mNumberText, mNumberX, mNumberY, mNumberPain); |
| 413 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 414 | if (mDotAppearAmount != 0.0f) { |
| 415 | float radius; |
| 416 | float alpha; |
| 417 | if (mDotAppearAmount <= 1.0f) { |
| 418 | radius = mDotRadius * mDotAppearAmount; |
| 419 | alpha = 1.0f; |
| 420 | } else { |
| 421 | float fadeOutAmount = mDotAppearAmount - 1.0f; |
| 422 | alpha = 1.0f - fadeOutAmount; |
| 423 | radius = NotificationUtils.interpolate(mDotRadius, getWidth() / 4, fadeOutAmount); |
| 424 | } |
| 425 | mDotPaint.setAlpha((int) (alpha * 255)); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 426 | canvas.drawCircle(mStatusBarIconSize / 2, getHeight() / 2, radius, mDotPaint); |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 427 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 428 | } |
| 429 | |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 430 | @Override |
Joe Onorato | f9ec03c | 2010-09-23 15:09:18 -0700 | [diff] [blame] | 431 | protected void debug(int depth) { |
| 432 | super.debug(depth); |
| 433 | Log.d("View", debugIndent(depth) + "slot=" + mSlot); |
| 434 | Log.d("View", debugIndent(depth) + "icon=" + mIcon); |
| 435 | } |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 436 | |
| 437 | void placeNumber() { |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 438 | final String str; |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 439 | final int tooBig = getContext().getResources().getInteger( |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 440 | android.R.integer.status_bar_notification_info_maxnum); |
| 441 | if (mIcon.number > tooBig) { |
John Spurlock | 0153478 | 2014-01-13 11:59:22 -0500 | [diff] [blame] | 442 | str = getContext().getResources().getString( |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 443 | android.R.string.status_bar_notification_info_overflow); |
| 444 | } else { |
| 445 | NumberFormat f = NumberFormat.getIntegerInstance(); |
| 446 | str = f.format(mIcon.number); |
| 447 | } |
| 448 | mNumberText = str; |
| 449 | |
Joe Onorato | 6c01a11 | 2010-10-04 17:38:47 -0400 | [diff] [blame] | 450 | final int w = getWidth(); |
| 451 | final int h = getHeight(); |
| 452 | final Rect r = new Rect(); |
| 453 | mNumberPain.getTextBounds(str, 0, str.length(), r); |
| 454 | final int tw = r.right - r.left; |
| 455 | final int th = r.bottom - r.top; |
| 456 | mNumberBackground.getPadding(r); |
| 457 | int dw = r.left + tw + r.right; |
| 458 | if (dw < mNumberBackground.getMinimumWidth()) { |
| 459 | dw = mNumberBackground.getMinimumWidth(); |
| 460 | } |
| 461 | mNumberX = w-r.right-((dw-r.right-r.left)/2); |
| 462 | int dh = r.top + th + r.bottom; |
| 463 | if (dh < mNumberBackground.getMinimumWidth()) { |
| 464 | dh = mNumberBackground.getMinimumWidth(); |
| 465 | } |
| 466 | mNumberY = h-r.bottom-((dh-r.top-th-r.bottom)/2); |
| 467 | mNumberBackground.setBounds(w-dw, h-dh, w, h); |
| 468 | } |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 469 | |
| 470 | private void setContentDescription(Notification notification) { |
| 471 | if (notification != null) { |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 472 | String d = contentDescForNotification(mContext, notification); |
| 473 | if (!TextUtils.isEmpty(d)) { |
| 474 | setContentDescription(d); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | } |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 478 | |
| 479 | public String toString() { |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 480 | return "StatusBarIconView(slot=" + mSlot + " icon=" + mIcon |
Daniel Sandler | 7579bca | 2011-08-18 15:47:26 -0400 | [diff] [blame] | 481 | + " notification=" + mNotification + ")"; |
| 482 | } |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 483 | |
Selim Cinek | 72fc8db | 2017-06-06 18:07:47 -0700 | [diff] [blame] | 484 | public StatusBarNotification getNotification() { |
| 485 | return mNotification; |
| 486 | } |
| 487 | |
Jason Monk | 3b23007 | 2015-05-29 11:11:29 -0400 | [diff] [blame] | 488 | public String getSlot() { |
| 489 | return mSlot; |
| 490 | } |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 491 | |
| 492 | |
| 493 | public static String contentDescForNotification(Context c, Notification n) { |
Adrian Roos | a8e18ef | 2016-05-26 17:17:02 -0700 | [diff] [blame] | 494 | String appName = ""; |
| 495 | try { |
| 496 | Notification.Builder builder = Notification.Builder.recoverBuilder(c, n); |
| 497 | appName = builder.loadHeaderAppName(); |
| 498 | } catch (RuntimeException e) { |
| 499 | Log.e(TAG, "Unable to recover builder", e); |
| 500 | // Trying to get the app name from the app info instead. |
| 501 | Parcelable appInfo = n.extras.getParcelable( |
| 502 | Notification.EXTRA_BUILDER_APPLICATION_INFO); |
| 503 | if (appInfo instanceof ApplicationInfo) { |
| 504 | appName = String.valueOf(((ApplicationInfo) appInfo).loadLabel( |
| 505 | c.getPackageManager())); |
| 506 | } |
| 507 | } |
Adrian Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 508 | |
Julia Reynolds | 7f9ce78 | 2016-05-17 15:34:34 -0400 | [diff] [blame] | 509 | CharSequence title = n.extras.getCharSequence(Notification.EXTRA_TITLE); |
Adrian Roos | f76600e | 2017-04-19 15:32:43 -0700 | [diff] [blame] | 510 | CharSequence text = n.extras.getCharSequence(Notification.EXTRA_TEXT); |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 511 | CharSequence ticker = n.tickerText; |
Adrian Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 512 | |
Adrian Roos | f76600e | 2017-04-19 15:32:43 -0700 | [diff] [blame] | 513 | // Some apps just put the app name into the title |
| 514 | CharSequence titleOrText = TextUtils.equals(title, appName) ? text : title; |
| 515 | |
| 516 | CharSequence desc = !TextUtils.isEmpty(titleOrText) ? titleOrText |
Adrian Roos | 6e782a5 | 2017-03-30 18:34:10 -0700 | [diff] [blame] | 517 | : !TextUtils.isEmpty(ticker) ? ticker : ""; |
Adrian Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 518 | |
| 519 | return c.getString(R.string.accessibility_desc_notification_icon, appName, desc); |
Adrian Roos | eba0582 | 2016-04-22 17:09:27 -0700 | [diff] [blame] | 520 | } |
Adrian Roos | 51548e6 | 2016-04-28 11:20:26 -0700 | [diff] [blame] | 521 | |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 522 | /** |
| 523 | * Set the color that is used to draw decoration like the overflow dot. This will not be applied |
| 524 | * to the drawable. |
| 525 | */ |
| 526 | public void setDecorColor(int iconTint) { |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 527 | mDecorColor = iconTint; |
| 528 | updateDecorColor(); |
| 529 | } |
| 530 | |
| 531 | private void updateDecorColor() { |
| 532 | int color = NotificationUtils.interpolateColors(mDecorColor, Color.WHITE, mDarkAmount); |
| 533 | if (mDotPaint.getColor() != color) { |
| 534 | mDotPaint.setColor(color); |
| 535 | |
| 536 | if (mDotAppearAmount != 0) { |
| 537 | invalidate(); |
| 538 | } |
| 539 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 542 | /** |
| 543 | * Set the static color that should be used for the drawable of this icon if it's not |
| 544 | * transitioning this also immediately sets the color. |
| 545 | */ |
| 546 | public void setStaticDrawableColor(int color) { |
| 547 | mDrawableColor = color; |
| 548 | setColorInternal(color); |
Lucas Dupin | 83519da | 2017-06-21 11:58:31 -0700 | [diff] [blame] | 549 | updateContrastedStaticColor(); |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 550 | mIconColor = color; |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 551 | mDozer.setColor(color); |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | private void setColorInternal(int color) { |
Adrian Roos | f3eacd3 | 2017-04-14 16:35:58 -0700 | [diff] [blame] | 555 | mCurrentSetColor = color; |
| 556 | updateIconColor(); |
| 557 | } |
| 558 | |
| 559 | private void updateIconColor() { |
| 560 | if (mCurrentSetColor != NO_COLOR) { |
Adrian Roos | c3e8cfc | 2017-07-17 17:09:39 +0200 | [diff] [blame] | 561 | if (mMatrixColorFilter == null) { |
| 562 | mMatrix = new float[4 * 5]; |
| 563 | mMatrixColorFilter = new ColorMatrixColorFilter(mMatrix); |
| 564 | } |
| 565 | int color = NotificationUtils.interpolateColors( |
| 566 | mCurrentSetColor, Color.WHITE, mDarkAmount); |
| 567 | updateTintMatrix(mMatrix, color, DARK_ALPHA_BOOST * mDarkAmount); |
| 568 | mMatrixColorFilter.setColorMatrixArray(mMatrix); |
| 569 | setColorFilter(mMatrixColorFilter); |
| 570 | invalidate(); // setColorFilter only invalidates if the filter instance changed. |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 571 | } else { |
Adrian Roos | f3eacd3 | 2017-04-14 16:35:58 -0700 | [diff] [blame] | 572 | mDozer.updateGrayscale(this, mDarkAmount); |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 573 | } |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Adrian Roos | c3e8cfc | 2017-07-17 17:09:39 +0200 | [diff] [blame] | 576 | /** |
| 577 | * Updates {@param array} such that it represents a matrix that changes RGB to {@param color} |
Adrian Roos | 24dbff3 | 2017-08-02 16:57:11 +0200 | [diff] [blame] | 578 | * and multiplies the alpha channel with the color's alpha+{@param alphaBoost}. |
Adrian Roos | c3e8cfc | 2017-07-17 17:09:39 +0200 | [diff] [blame] | 579 | */ |
| 580 | private static void updateTintMatrix(float[] array, int color, float alphaBoost) { |
| 581 | Arrays.fill(array, 0); |
| 582 | array[4] = Color.red(color); |
| 583 | array[9] = Color.green(color); |
| 584 | array[14] = Color.blue(color); |
Adrian Roos | 24dbff3 | 2017-08-02 16:57:11 +0200 | [diff] [blame] | 585 | array[18] = Color.alpha(color) / 255f + alphaBoost; |
Adrian Roos | c3e8cfc | 2017-07-17 17:09:39 +0200 | [diff] [blame] | 586 | } |
| 587 | |
Selim Cinek | 875ba9b | 2017-02-13 16:20:17 -0800 | [diff] [blame] | 588 | public void setIconColor(int iconColor, boolean animate) { |
| 589 | if (mIconColor != iconColor) { |
| 590 | mIconColor = iconColor; |
| 591 | if (mColorAnimator != null) { |
| 592 | mColorAnimator.cancel(); |
| 593 | } |
| 594 | if (mCurrentSetColor == iconColor) { |
| 595 | return; |
| 596 | } |
| 597 | if (animate && mCurrentSetColor != NO_COLOR) { |
| 598 | mAnimationStartColor = mCurrentSetColor; |
| 599 | mColorAnimator = ValueAnimator.ofFloat(0.0f, 1.0f); |
| 600 | mColorAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN); |
| 601 | mColorAnimator.setDuration(ANIMATION_DURATION_FAST); |
| 602 | mColorAnimator.addUpdateListener(mColorUpdater); |
| 603 | mColorAnimator.addListener(new AnimatorListenerAdapter() { |
| 604 | @Override |
| 605 | public void onAnimationEnd(Animator animation) { |
| 606 | mColorAnimator = null; |
| 607 | mAnimationStartColor = NO_COLOR; |
| 608 | } |
| 609 | }); |
| 610 | mColorAnimator.start(); |
| 611 | } else { |
| 612 | setColorInternal(iconColor); |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | public int getStaticDrawableColor() { |
| 618 | return mDrawableColor; |
| 619 | } |
| 620 | |
Lucas Dupin | 83519da | 2017-06-21 11:58:31 -0700 | [diff] [blame] | 621 | /** |
| 622 | * A drawable color that passes GAR on a specific background. |
| 623 | * This value is cached. |
| 624 | * |
| 625 | * @param backgroundColor Background to test against. |
| 626 | * @return GAR safe version of {@link StatusBarIconView#getStaticDrawableColor()}. |
| 627 | */ |
| 628 | int getContrastedStaticDrawableColor(int backgroundColor) { |
| 629 | if (mCachedContrastBackgroundColor != backgroundColor) { |
| 630 | mCachedContrastBackgroundColor = backgroundColor; |
| 631 | updateContrastedStaticColor(); |
| 632 | } |
| 633 | return mContrastedDrawableColor; |
| 634 | } |
| 635 | |
| 636 | private void updateContrastedStaticColor() { |
Lucas Dupin | ddbaf84 | 2017-06-23 09:30:49 -0700 | [diff] [blame] | 637 | if (Color.alpha(mCachedContrastBackgroundColor) != 255) { |
| 638 | mContrastedDrawableColor = mDrawableColor; |
Lucas Dupin | 83519da | 2017-06-21 11:58:31 -0700 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | // We'll modify the color if it doesn't pass GAR |
| 642 | int contrastedColor = mDrawableColor; |
| 643 | if (!NotificationColorUtil.satisfiesTextContrast(mCachedContrastBackgroundColor, |
| 644 | contrastedColor)) { |
| 645 | float[] hsl = new float[3]; |
| 646 | ColorUtils.colorToHSL(mDrawableColor, hsl); |
| 647 | // This is basically a light grey, pushing the color will only distort it. |
| 648 | // Best thing to do in here is to fallback to the default color. |
| 649 | if (hsl[1] < 0.2f) { |
| 650 | contrastedColor = Notification.COLOR_DEFAULT; |
| 651 | } |
| 652 | contrastedColor = NotificationColorUtil.resolveContrastColor(mContext, |
| 653 | contrastedColor, mCachedContrastBackgroundColor); |
| 654 | } |
| 655 | mContrastedDrawableColor = contrastedColor; |
| 656 | } |
| 657 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 658 | @Override |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 659 | public void setVisibleState(int state) { |
| 660 | setVisibleState(state, true /* animate */, null /* endRunnable */); |
| 661 | } |
| 662 | |
| 663 | public void setVisibleState(int state, boolean animate) { |
| 664 | setVisibleState(state, animate, null); |
| 665 | } |
| 666 | |
| 667 | @Override |
| 668 | public boolean hasOverlappingRendering() { |
| 669 | return false; |
| 670 | } |
| 671 | |
| 672 | public void setVisibleState(int visibleState, boolean animate, Runnable endRunnable) { |
Selim Cinek | d03518c | 2018-03-15 12:13:51 -0700 | [diff] [blame] | 673 | setVisibleState(visibleState, animate, endRunnable, 0); |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Set the visibleState of this view. |
| 678 | * |
| 679 | * @param visibleState The new state. |
| 680 | * @param animate Should we animate? |
| 681 | * @param endRunnable The runnable to run at the end. |
| 682 | * @param duration The duration of an animation or 0 if the default should be taken. |
| 683 | */ |
| 684 | public void setVisibleState(int visibleState, boolean animate, Runnable endRunnable, |
| 685 | long duration) { |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 686 | boolean runnableAdded = false; |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 687 | if (visibleState != mVisibleState) { |
| 688 | mVisibleState = visibleState; |
Selim Cinek | 5ece5a7 | 2017-02-15 11:47:42 -0800 | [diff] [blame] | 689 | if (mIconAppearAnimator != null) { |
| 690 | mIconAppearAnimator.cancel(); |
| 691 | } |
| 692 | if (mDotAnimator != null) { |
| 693 | mDotAnimator.cancel(); |
| 694 | } |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 695 | if (animate) { |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 696 | float targetAmount = 0.0f; |
| 697 | Interpolator interpolator = Interpolators.FAST_OUT_LINEAR_IN; |
| 698 | if (visibleState == STATE_ICON) { |
| 699 | targetAmount = 1.0f; |
| 700 | interpolator = Interpolators.LINEAR_OUT_SLOW_IN; |
| 701 | } |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 702 | float currentAmount = getIconAppearAmount(); |
| 703 | if (targetAmount != currentAmount) { |
| 704 | mIconAppearAnimator = ObjectAnimator.ofFloat(this, ICON_APPEAR_AMOUNT, |
| 705 | currentAmount, targetAmount); |
| 706 | mIconAppearAnimator.setInterpolator(interpolator); |
Selim Cinek | d03518c | 2018-03-15 12:13:51 -0700 | [diff] [blame] | 707 | mIconAppearAnimator.setDuration(duration == 0 ? ANIMATION_DURATION_FAST |
| 708 | : duration); |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 709 | mIconAppearAnimator.addListener(new AnimatorListenerAdapter() { |
| 710 | @Override |
| 711 | public void onAnimationEnd(Animator animation) { |
| 712 | mIconAppearAnimator = null; |
| 713 | runRunnable(endRunnable); |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 714 | } |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 715 | }); |
| 716 | mIconAppearAnimator.start(); |
| 717 | runnableAdded = true; |
| 718 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 719 | |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 720 | targetAmount = visibleState == STATE_ICON ? 2.0f : 0.0f; |
| 721 | interpolator = Interpolators.FAST_OUT_LINEAR_IN; |
| 722 | if (visibleState == STATE_DOT) { |
| 723 | targetAmount = 1.0f; |
| 724 | interpolator = Interpolators.LINEAR_OUT_SLOW_IN; |
| 725 | } |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 726 | currentAmount = getDotAppearAmount(); |
| 727 | if (targetAmount != currentAmount) { |
| 728 | mDotAnimator = ObjectAnimator.ofFloat(this, DOT_APPEAR_AMOUNT, |
| 729 | currentAmount, targetAmount); |
Selim Cinek | d03518c | 2018-03-15 12:13:51 -0700 | [diff] [blame] | 730 | mDotAnimator.setInterpolator(interpolator);; |
| 731 | mDotAnimator.setDuration(duration == 0 ? ANIMATION_DURATION_FAST |
| 732 | : duration); |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 733 | final boolean runRunnable = !runnableAdded; |
| 734 | mDotAnimator.addListener(new AnimatorListenerAdapter() { |
| 735 | @Override |
| 736 | public void onAnimationEnd(Animator animation) { |
| 737 | mDotAnimator = null; |
| 738 | if (runRunnable) { |
| 739 | runRunnable(endRunnable); |
| 740 | } |
| 741 | } |
| 742 | }); |
| 743 | mDotAnimator.start(); |
| 744 | runnableAdded = true; |
| 745 | } |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 746 | } else { |
| 747 | setIconAppearAmount(visibleState == STATE_ICON ? 1.0f : 0.0f); |
Selim Cinek | 01a73f9 | 2016-12-06 16:13:42 -0800 | [diff] [blame] | 748 | setDotAppearAmount(visibleState == STATE_DOT ? 1.0f |
| 749 | : visibleState == STATE_ICON ? 2.0f |
| 750 | : 0.0f); |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 751 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 752 | } |
Selim Cinek | 65d418e | 2016-11-29 15:42:34 -0800 | [diff] [blame] | 753 | if (!runnableAdded) { |
| 754 | runRunnable(endRunnable); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | private void runRunnable(Runnable runnable) { |
| 759 | if (runnable != null) { |
| 760 | runnable.run(); |
| 761 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Selim Cinek | 5b5beb01 | 2016-11-08 18:11:58 -0800 | [diff] [blame] | 764 | public void setIconAppearAmount(float iconAppearAmount) { |
Selim Cinek | 9ef119c | 2017-03-01 15:13:36 -0800 | [diff] [blame] | 765 | if (mIconAppearAmount != iconAppearAmount) { |
| 766 | mIconAppearAmount = iconAppearAmount; |
| 767 | invalidate(); |
| 768 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | public float getIconAppearAmount() { |
| 772 | return mIconAppearAmount; |
| 773 | } |
| 774 | |
| 775 | public int getVisibleState() { |
| 776 | return mVisibleState; |
| 777 | } |
| 778 | |
| 779 | public void setDotAppearAmount(float dotAppearAmount) { |
Selim Cinek | 9ef119c | 2017-03-01 15:13:36 -0800 | [diff] [blame] | 780 | if (mDotAppearAmount != dotAppearAmount) { |
| 781 | mDotAppearAmount = dotAppearAmount; |
| 782 | invalidate(); |
| 783 | } |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Selim Cinek | 2b549f4 | 2016-11-22 16:38:51 -0800 | [diff] [blame] | 786 | @Override |
| 787 | public void setVisibility(int visibility) { |
| 788 | super.setVisibility(visibility); |
| 789 | if (mOnVisibilityChangedListener != null) { |
| 790 | mOnVisibilityChangedListener.onVisibilityChanged(visibility); |
| 791 | } |
| 792 | } |
| 793 | |
Selim Cinek | 49014f8 | 2016-11-04 14:55:30 -0700 | [diff] [blame] | 794 | public float getDotAppearAmount() { |
| 795 | return mDotAppearAmount; |
| 796 | } |
Selim Cinek | 2b549f4 | 2016-11-22 16:38:51 -0800 | [diff] [blame] | 797 | |
| 798 | public void setOnVisibilityChangedListener(OnVisibilityChangedListener listener) { |
| 799 | mOnVisibilityChangedListener = listener; |
| 800 | } |
| 801 | |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 802 | public void setDark(boolean dark, boolean fade, long delay) { |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 803 | mDozer.setIntensityDark(f -> { |
| 804 | mDarkAmount = f; |
Adrian Roos | 138f034 | 2017-04-28 09:11:28 -0700 | [diff] [blame] | 805 | updateIconScale(); |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 806 | updateDecorColor(); |
Adrian Roos | f3eacd3 | 2017-04-14 16:35:58 -0700 | [diff] [blame] | 807 | updateIconColor(); |
Adrian Roos | fe0071f | 2017-07-06 18:45:10 +0200 | [diff] [blame] | 808 | updateAllowAnimation(); |
Adrian Roos | 456e005 | 2017-04-04 16:44:29 -0700 | [diff] [blame] | 809 | }, dark, fade, delay); |
| 810 | } |
| 811 | |
Adrian Roos | fe0071f | 2017-07-06 18:45:10 +0200 | [diff] [blame] | 812 | private void updateAllowAnimation() { |
| 813 | if (mDarkAmount == 0 || mDarkAmount == 1) { |
| 814 | setAllowAnimation(mDarkAmount == 0); |
| 815 | } |
| 816 | } |
| 817 | |
Selim Cinek | 1385905 | 2017-10-05 17:12:18 -0700 | [diff] [blame] | 818 | /** |
| 819 | * This method returns the drawing rect for the view which is different from the regular |
| 820 | * drawing rect, since we layout all children at position 0 and usually the translation is |
| 821 | * neglected. The standard implementation doesn't account for translation. |
| 822 | * |
| 823 | * @param outRect The (scrolled) drawing bounds of the view. |
| 824 | */ |
| 825 | @Override |
| 826 | public void getDrawingRect(Rect outRect) { |
| 827 | super.getDrawingRect(outRect); |
| 828 | float translationX = getTranslationX(); |
| 829 | float translationY = getTranslationY(); |
| 830 | outRect.left += translationX; |
| 831 | outRect.right += translationX; |
| 832 | outRect.top += translationY; |
| 833 | outRect.bottom += translationY; |
| 834 | } |
| 835 | |
Selim Cinek | d95ca7c | 2017-07-26 12:20:38 -0700 | [diff] [blame] | 836 | public void setIsInShelf(boolean isInShelf) { |
| 837 | mIsInShelf = isInShelf; |
| 838 | } |
| 839 | |
| 840 | public boolean isInShelf() { |
| 841 | return mIsInShelf; |
| 842 | } |
| 843 | |
Selim Cinek | 887da3c | 2017-10-06 13:37:32 -0700 | [diff] [blame] | 844 | @Override |
| 845 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 846 | super.onLayout(changed, left, top, right, bottom); |
| 847 | if (mLayoutRunnable != null) { |
| 848 | mLayoutRunnable.run(); |
| 849 | mLayoutRunnable = null; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | public void executeOnLayout(Runnable runnable) { |
| 854 | mLayoutRunnable = runnable; |
| 855 | } |
| 856 | |
Selim Cinek | d03518c | 2018-03-15 12:13:51 -0700 | [diff] [blame] | 857 | public void setDismissed() { |
| 858 | mDismissed = true; |
| 859 | if (mOnDismissListener != null) { |
| 860 | mOnDismissListener.run(); |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | public boolean isDismissed() { |
| 865 | return mDismissed; |
| 866 | } |
| 867 | |
| 868 | public void setOnDismissListener(Runnable onDismissListener) { |
| 869 | mOnDismissListener = onDismissListener; |
| 870 | } |
| 871 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 872 | @Override |
| 873 | public void onDarkChanged(Rect area, float darkIntensity, int tint) { |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame^] | 874 | int areaTint = getTint(area, this, tint); |
| 875 | ColorStateList color = ColorStateList.valueOf(areaTint); |
| 876 | setImageTintList(color); |
| 877 | setDecorColor(areaTint); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | @Override |
| 881 | public boolean isIconVisible() { |
| 882 | return mIcon != null && mIcon.visible; |
| 883 | } |
| 884 | |
| 885 | @Override |
| 886 | public boolean isIconBlocked() { |
| 887 | return mBlocked; |
| 888 | } |
| 889 | |
Selim Cinek | 2b549f4 | 2016-11-22 16:38:51 -0800 | [diff] [blame] | 890 | public interface OnVisibilityChangedListener { |
| 891 | void onVisibilityChanged(int newVisibility); |
| 892 | } |
Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 893 | } |