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