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