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