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