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