Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 19 | import static com.android.systemui.statusbar.StatusBarIconView.STATE_DOT; |
| 20 | import static com.android.systemui.statusbar.StatusBarIconView.STATE_HIDDEN; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 21 | import static com.android.systemui.statusbar.StatusBarIconView.STATE_ICON; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 22 | |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 23 | import android.annotation.Nullable; |
| 24 | import android.content.Context; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 25 | import android.graphics.Canvas; |
| 26 | import android.graphics.Color; |
| 27 | import android.graphics.Paint; |
| 28 | import android.graphics.Paint.Style; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 29 | import android.util.AttributeSet; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 30 | import android.util.Log; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 31 | import android.view.View; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 32 | |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 33 | import com.android.keyguard.AlphaOptimizedLinearLayout; |
| 34 | import com.android.systemui.R; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 35 | import com.android.systemui.statusbar.StatusIconDisplayable; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 36 | import com.android.systemui.statusbar.notification.stack.AnimationFilter; |
| 37 | import com.android.systemui.statusbar.notification.stack.AnimationProperties; |
| 38 | import com.android.systemui.statusbar.notification.stack.ViewState; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 39 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 40 | import java.util.ArrayList; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 41 | |
Evan Laird | 937d9fa | 2018-02-08 10:12:16 -0800 | [diff] [blame] | 42 | /** |
| 43 | * A container for Status bar system icons. Limits the number of system icons and handles overflow |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 44 | * similar to {@link NotificationIconContainer}. |
Evan Laird | 937d9fa | 2018-02-08 10:12:16 -0800 | [diff] [blame] | 45 | * |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 46 | * Children are expected to implement {@link StatusIconDisplayable} |
Evan Laird | 937d9fa | 2018-02-08 10:12:16 -0800 | [diff] [blame] | 47 | */ |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 48 | public class StatusIconContainer extends AlphaOptimizedLinearLayout { |
| 49 | |
| 50 | private static final String TAG = "StatusIconContainer"; |
Evan Laird | e0fbc3e | 2018-02-13 11:41:00 -0500 | [diff] [blame] | 51 | private static final boolean DEBUG = false; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 52 | private static final boolean DEBUG_OVERFLOW = false; |
Evan Laird | 97ae20c | 2018-05-10 13:13:27 -0400 | [diff] [blame] | 53 | // Max 8 status icons including battery |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 54 | private static final int MAX_ICONS = 7; |
| 55 | private static final int MAX_DOTS = 1; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 56 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 57 | private int mDotPadding; |
| 58 | private int mStaticDotDiameter; |
| 59 | private int mUnderflowWidth; |
| 60 | private int mUnderflowStart = 0; |
| 61 | // Whether or not we can draw into the underflow space |
| 62 | private boolean mNeedsUnderflow; |
| 63 | // Individual StatusBarIconViews draw their etc dots centered in this width |
| 64 | private int mIconDotFrameWidth; |
| 65 | private boolean mShouldRestrictIcons = true; |
| 66 | // Used to count which states want to be visible during layout |
| 67 | private ArrayList<StatusIconState> mLayoutStates = new ArrayList<>(); |
| 68 | // So we can count and measure properly |
| 69 | private ArrayList<View> mMeasureViews = new ArrayList<>(); |
| 70 | |
Evan Laird | 937d9fa | 2018-02-08 10:12:16 -0800 | [diff] [blame] | 71 | public StatusIconContainer(Context context) { |
| 72 | this(context, null); |
| 73 | } |
| 74 | |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 75 | public StatusIconContainer(Context context, AttributeSet attrs) { |
| 76 | super(context, attrs); |
Evan Laird | feec2ab | 2018-05-02 12:47:26 -0400 | [diff] [blame] | 77 | initDimens(); |
| 78 | setWillNotDraw(!DEBUG_OVERFLOW); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 82 | protected void onFinishInflate() { |
| 83 | super.onFinishInflate(); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | public void setShouldRestrictIcons(boolean should) { |
| 87 | mShouldRestrictIcons = should; |
| 88 | } |
| 89 | |
Evan Laird | feec2ab | 2018-05-02 12:47:26 -0400 | [diff] [blame] | 90 | public boolean isRestrictingIcons() { |
| 91 | return mShouldRestrictIcons; |
| 92 | } |
| 93 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 94 | private void initDimens() { |
| 95 | // This is the same value that StatusBarIconView uses |
| 96 | mIconDotFrameWidth = getResources().getDimensionPixelSize( |
| 97 | com.android.internal.R.dimen.status_bar_icon_size); |
| 98 | mDotPadding = getResources().getDimensionPixelSize(R.dimen.overflow_icon_dot_padding); |
| 99 | int radius = getResources().getDimensionPixelSize(R.dimen.overflow_dot_radius); |
| 100 | mStaticDotDiameter = 2 * radius; |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 101 | mUnderflowWidth = mIconDotFrameWidth + (MAX_DOTS - 1) * (mStaticDotDiameter + mDotPadding); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 105 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 106 | float midY = getHeight() / 2.0f; |
| 107 | |
| 108 | // Layout all child views so that we can move them around later |
| 109 | for (int i = 0; i < getChildCount(); i++) { |
| 110 | View child = getChildAt(i); |
| 111 | int width = child.getMeasuredWidth(); |
| 112 | int height = child.getMeasuredHeight(); |
| 113 | int top = (int) (midY - height / 2.0f); |
| 114 | child.layout(0, top, width, top + height); |
| 115 | } |
| 116 | |
| 117 | resetViewStates(); |
| 118 | calculateIconTranslations(); |
| 119 | applyIconStates(); |
| 120 | } |
| 121 | |
| 122 | @Override |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 123 | protected void onDraw(Canvas canvas) { |
| 124 | super.onDraw(canvas); |
| 125 | if (DEBUG_OVERFLOW) { |
| 126 | Paint paint = new Paint(); |
| 127 | paint.setStyle(Style.STROKE); |
| 128 | paint.setColor(Color.RED); |
| 129 | |
| 130 | // Show bounding box |
| 131 | canvas.drawRect(getPaddingStart(), 0, getWidth() - getPaddingEnd(), getHeight(), paint); |
| 132 | |
| 133 | // Show etc box |
| 134 | paint.setColor(Color.GREEN); |
| 135 | canvas.drawRect( |
| 136 | mUnderflowStart, 0, mUnderflowStart + mUnderflowWidth, getHeight(), paint); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | @Override |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 141 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 142 | mMeasureViews.clear(); |
| 143 | int mode = MeasureSpec.getMode(widthMeasureSpec); |
| 144 | final int width = MeasureSpec.getSize(widthMeasureSpec); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 145 | final int count = getChildCount(); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 146 | // Collect all of the views which want to be laid out |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 147 | for (int i = 0; i < count; i++) { |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 148 | StatusIconDisplayable icon = (StatusIconDisplayable) getChildAt(i); |
| 149 | if (icon.isIconVisible() && !icon.isIconBlocked()) { |
| 150 | mMeasureViews.add((View) icon); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | int visibleCount = mMeasureViews.size(); |
| 155 | int maxVisible = visibleCount <= MAX_ICONS ? MAX_ICONS : MAX_ICONS - 1; |
Evan Laird | 97ae20c | 2018-05-10 13:13:27 -0400 | [diff] [blame] | 156 | int totalWidth = mPaddingLeft + mPaddingRight; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 157 | boolean trackWidth = true; |
| 158 | |
| 159 | // Measure all children so that they report the correct width |
| 160 | int childWidthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.UNSPECIFIED); |
| 161 | mNeedsUnderflow = mShouldRestrictIcons && visibleCount > MAX_ICONS; |
| 162 | for (int i = 0; i < mMeasureViews.size(); i++) { |
| 163 | // Walking backwards |
| 164 | View child = mMeasureViews.get(visibleCount - i - 1); |
| 165 | measureChild(child, childWidthSpec, heightMeasureSpec); |
| 166 | if (mShouldRestrictIcons) { |
| 167 | if (i < maxVisible && trackWidth) { |
| 168 | totalWidth += getViewTotalMeasuredWidth(child); |
| 169 | } else if (trackWidth) { |
| 170 | // We've hit the icon limit; add space for dots |
| 171 | totalWidth += mUnderflowWidth; |
| 172 | trackWidth = false; |
| 173 | } |
| 174 | } else { |
| 175 | totalWidth += getViewTotalMeasuredWidth(child); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (mode == MeasureSpec.EXACTLY) { |
| 180 | if (!mNeedsUnderflow && totalWidth > width) { |
| 181 | mNeedsUnderflow = true; |
| 182 | } |
| 183 | setMeasuredDimension(width, MeasureSpec.getSize(heightMeasureSpec)); |
| 184 | } else { |
| 185 | if (mode == MeasureSpec.AT_MOST && totalWidth > width) { |
| 186 | mNeedsUnderflow = true; |
| 187 | totalWidth = width; |
| 188 | } |
| 189 | setMeasuredDimension(totalWidth, MeasureSpec.getSize(heightMeasureSpec)); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void onViewAdded(View child) { |
| 195 | super.onViewAdded(child); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 196 | StatusIconState vs = new StatusIconState(); |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 197 | vs.justAdded = true; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 198 | child.setTag(R.id.status_bar_view_state_tag, vs); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void onViewRemoved(View child) { |
| 203 | super.onViewRemoved(child); |
| 204 | child.setTag(R.id.status_bar_view_state_tag, null); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Layout is happening from end -> start |
| 209 | */ |
| 210 | private void calculateIconTranslations() { |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 211 | mLayoutStates.clear(); |
Evan Laird | 97ae20c | 2018-05-10 13:13:27 -0400 | [diff] [blame] | 212 | float width = getWidth(); |
| 213 | float translationX = width - getPaddingEnd(); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 214 | float contentStart = getPaddingStart(); |
| 215 | int childCount = getChildCount(); |
| 216 | // Underflow === don't show content until that index |
Evan Laird | feec2ab | 2018-05-02 12:47:26 -0400 | [diff] [blame] | 217 | if (DEBUG) android.util.Log.d(TAG, "calculateIconTranslations: start=" + translationX |
| 218 | + " width=" + width + " underflow=" + mNeedsUnderflow); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 219 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 220 | // Collect all of the states which want to be visible |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 221 | for (int i = childCount - 1; i >= 0; i--) { |
| 222 | View child = getChildAt(i); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 223 | StatusIconDisplayable iconView = (StatusIconDisplayable) child; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 224 | StatusIconState childState = getViewStateFromChild(child); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 225 | |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 226 | if (!iconView.isIconVisible() || iconView.isIconBlocked()) { |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 227 | childState.visibleState = STATE_HIDDEN; |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 228 | if (DEBUG) Log.d(TAG, "skipping child (" + iconView.getSlot() + ") not visible"); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 229 | continue; |
| 230 | } |
| 231 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 232 | childState.visibleState = STATE_ICON; |
| 233 | childState.xTranslation = translationX - getViewTotalWidth(child); |
| 234 | mLayoutStates.add(0, childState); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 235 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 236 | translationX -= getViewTotalWidth(child); |
| 237 | } |
| 238 | |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 239 | // Show either 1-MAX_ICONS icons, or (MAX_ICONS - 1) icons + overflow |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 240 | int totalVisible = mLayoutStates.size(); |
| 241 | int maxVisible = totalVisible <= MAX_ICONS ? MAX_ICONS : MAX_ICONS - 1; |
| 242 | |
| 243 | mUnderflowStart = 0; |
| 244 | int visible = 0; |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 245 | int firstUnderflowIndex = -1; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 246 | for (int i = totalVisible - 1; i >= 0; i--) { |
| 247 | StatusIconState state = mLayoutStates.get(i); |
| 248 | // Allow room for underflow if we found we need it in onMeasure |
| 249 | if (mNeedsUnderflow && (state.xTranslation < (contentStart + mUnderflowWidth))|| |
| 250 | (mShouldRestrictIcons && visible >= maxVisible)) { |
| 251 | firstUnderflowIndex = i; |
| 252 | break; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 253 | } |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 254 | mUnderflowStart = (int) Math.max(contentStart, state.xTranslation - mUnderflowWidth); |
| 255 | visible++; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | if (firstUnderflowIndex != -1) { |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 259 | int totalDots = 0; |
| 260 | int dotWidth = mStaticDotDiameter + mDotPadding; |
| 261 | int dotOffset = mUnderflowStart + mUnderflowWidth - mIconDotFrameWidth; |
| 262 | for (int i = firstUnderflowIndex; i >= 0; i--) { |
| 263 | StatusIconState state = mLayoutStates.get(i); |
| 264 | if (totalDots < MAX_DOTS) { |
| 265 | state.xTranslation = dotOffset; |
| 266 | state.visibleState = STATE_DOT; |
| 267 | dotOffset -= dotWidth; |
| 268 | totalDots++; |
| 269 | } else { |
| 270 | state.visibleState = STATE_HIDDEN; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
Evan Laird | 6303613 | 2018-02-02 16:30:41 -0500 | [diff] [blame] | 274 | |
| 275 | // Stole this from NotificationIconContainer. Not optimal but keeps the layout logic clean |
| 276 | if (isLayoutRtl()) { |
| 277 | for (int i = 0; i < childCount; i++) { |
| 278 | View child = getChildAt(i); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 279 | StatusIconState state = getViewStateFromChild(child); |
Evan Laird | 6303613 | 2018-02-02 16:30:41 -0500 | [diff] [blame] | 280 | state.xTranslation = width - state.xTranslation - child.getWidth(); |
| 281 | } |
| 282 | } |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | private void applyIconStates() { |
| 286 | for (int i = 0; i < getChildCount(); i++) { |
| 287 | View child = getChildAt(i); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 288 | StatusIconState vs = getViewStateFromChild(child); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 289 | if (vs != null) { |
| 290 | vs.applyToView(child); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | private void resetViewStates() { |
| 296 | for (int i = 0; i < getChildCount(); i++) { |
| 297 | View child = getChildAt(i); |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 298 | StatusIconState vs = getViewStateFromChild(child); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 299 | if (vs == null) { |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | vs.initFrom(child); |
| 304 | vs.alpha = 1.0f; |
Evan Laird | 620ea2c | 2018-06-21 11:18:19 -0400 | [diff] [blame] | 305 | vs.hidden = false; |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 309 | private static @Nullable StatusIconState getViewStateFromChild(View child) { |
| 310 | return (StatusIconState) child.getTag(R.id.status_bar_view_state_tag); |
| 311 | } |
| 312 | |
| 313 | private static int getViewTotalMeasuredWidth(View child) { |
| 314 | return child.getMeasuredWidth() + child.getPaddingStart() + child.getPaddingEnd(); |
| 315 | } |
| 316 | |
| 317 | private static int getViewTotalWidth(View child) { |
| 318 | return child.getWidth() + child.getPaddingStart() + child.getPaddingEnd(); |
| 319 | } |
| 320 | |
| 321 | public static class StatusIconState extends ViewState { |
| 322 | /// StatusBarIconView.STATE_* |
| 323 | public int visibleState = STATE_ICON; |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 324 | public boolean justAdded = true; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 325 | |
| 326 | @Override |
| 327 | public void applyToView(View view) { |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 328 | if (!(view instanceof StatusIconDisplayable)) { |
| 329 | return; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 330 | } |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 331 | StatusIconDisplayable icon = (StatusIconDisplayable) view; |
| 332 | AnimationProperties animationProperties = null; |
Evan Laird | 620ea2c | 2018-06-21 11:18:19 -0400 | [diff] [blame] | 333 | boolean animateVisibility = true; |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 334 | |
Evan Laird | 620ea2c | 2018-06-21 11:18:19 -0400 | [diff] [blame] | 335 | // Figure out which properties of the state transition (if any) we need to animate |
| 336 | if (justAdded |
| 337 | || icon.getVisibleState() == STATE_HIDDEN && visibleState == STATE_ICON) { |
| 338 | // Icon is appearing, fade it in by putting it where it will be and animating alpha |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 339 | super.applyToView(view); |
Evan Laird | 620ea2c | 2018-06-21 11:18:19 -0400 | [diff] [blame] | 340 | view.setAlpha(0.f); |
| 341 | icon.setVisibleState(STATE_HIDDEN); |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 342 | animationProperties = ADD_ICON_PROPERTIES; |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 343 | } else if (icon.getVisibleState() != visibleState) { |
Evan Laird | 620ea2c | 2018-06-21 11:18:19 -0400 | [diff] [blame] | 344 | if (icon.getVisibleState() == STATE_ICON && visibleState == STATE_HIDDEN) { |
| 345 | // Disappearing, don't do anything fancy |
| 346 | animateVisibility = false; |
| 347 | } else { |
| 348 | // all other transitions (to/from dot, etc) |
| 349 | animationProperties = ANIMATE_ALL_PROPERTIES; |
| 350 | } |
| 351 | } else if (visibleState != STATE_HIDDEN && xTranslation != view.getTranslationX()) { |
| 352 | // Visibility isn't changing, just animate position |
| 353 | animationProperties = X_ANIMATION_PROPERTIES; |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 354 | } |
| 355 | |
Evan Laird | 620ea2c | 2018-06-21 11:18:19 -0400 | [diff] [blame] | 356 | icon.setVisibleState(visibleState, animateVisibility); |
| 357 | if (animationProperties != null) { |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 358 | animateTo(view, animationProperties); |
| 359 | } else { |
| 360 | super.applyToView(view); |
| 361 | } |
| 362 | |
| 363 | justAdded = false; |
Evan Laird | 20b87bf | 2018-04-12 09:54:11 -0400 | [diff] [blame] | 364 | } |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 365 | } |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 366 | |
| 367 | private static final AnimationProperties ADD_ICON_PROPERTIES = new AnimationProperties() { |
| 368 | private AnimationFilter mAnimationFilter = new AnimationFilter().animateAlpha(); |
| 369 | |
| 370 | @Override |
| 371 | public AnimationFilter getAnimationFilter() { |
| 372 | return mAnimationFilter; |
| 373 | } |
| 374 | }.setDuration(200).setDelay(50); |
| 375 | |
Evan Laird | 620ea2c | 2018-06-21 11:18:19 -0400 | [diff] [blame] | 376 | private static final AnimationProperties X_ANIMATION_PROPERTIES = new AnimationProperties() { |
Evan Laird | 150701d4 | 2018-05-04 16:14:00 -0400 | [diff] [blame] | 377 | private AnimationFilter mAnimationFilter = new AnimationFilter().animateX(); |
| 378 | |
| 379 | @Override |
| 380 | public AnimationFilter getAnimationFilter() { |
| 381 | return mAnimationFilter; |
| 382 | } |
| 383 | }.setDuration(200); |
Evan Laird | 620ea2c | 2018-06-21 11:18:19 -0400 | [diff] [blame] | 384 | |
| 385 | private static final AnimationProperties ANIMATE_ALL_PROPERTIES = new AnimationProperties() { |
| 386 | private AnimationFilter mAnimationFilter = new AnimationFilter().animateX().animateY() |
| 387 | .animateAlpha().animateScale(); |
| 388 | |
| 389 | @Override |
| 390 | public AnimationFilter getAnimationFilter() { |
| 391 | return mAnimationFilter; |
| 392 | } |
| 393 | }.setDuration(200); |
Evan Laird | 058c8ae | 2018-01-12 14:26:10 -0500 | [diff] [blame] | 394 | } |