Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 17 | package com.android.systemui.statusbar; |
| 18 | |
| 19 | import android.content.Context; |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 20 | import android.graphics.Outline; |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 21 | import android.graphics.Paint; |
| 22 | import android.graphics.PorterDuff; |
| 23 | import android.graphics.PorterDuffXfermode; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 24 | import android.graphics.Rect; |
| 25 | import android.util.AttributeSet; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 26 | import android.view.View; |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 27 | import android.view.ViewGroup; |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 28 | import android.view.ViewOutlineProvider; |
Selim Cinek | bb3d1cf | 2014-10-31 00:12:56 +0100 | [diff] [blame] | 29 | import android.view.ViewTreeObserver; |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 30 | import android.view.animation.Interpolator; |
| 31 | import android.view.animation.LinearInterpolator; |
Selim Cinek | c9c00ae | 2014-05-20 03:33:40 +0200 | [diff] [blame] | 32 | import android.widget.FrameLayout; |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 33 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 34 | import com.android.systemui.R; |
| 35 | |
| 36 | /** |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 37 | * A frame layout containing the actual payload of the notification, including the contracted, |
| 38 | * expanded and heads up layout. This class is responsible for clipping the content and and |
| 39 | * switching between the expanded, contracted and the heads up view depending on its clipped size. |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 40 | */ |
Selim Cinek | c9c00ae | 2014-05-20 03:33:40 +0200 | [diff] [blame] | 41 | public class NotificationContentView extends FrameLayout { |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 42 | |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 43 | private static final long ANIMATION_DURATION_LENGTH = 170; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 44 | private static final int VISIBLE_TYPE_CONTRACTED = 0; |
| 45 | private static final int VISIBLE_TYPE_EXPANDED = 1; |
| 46 | private static final int VISIBLE_TYPE_HEADSUP = 2; |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 47 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 48 | private final Rect mClipBounds = new Rect(); |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 49 | private final int mSmallHeight; |
| 50 | private final int mHeadsUpHeight; |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 51 | private final int mRoundRectRadius; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 52 | private final Interpolator mLinearInterpolator = new LinearInterpolator(); |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 53 | private final boolean mRoundRectClippingEnabled; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 54 | |
| 55 | private View mContractedChild; |
| 56 | private View mExpandedChild; |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 57 | private View mHeadsUpChild; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 58 | |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 59 | private NotificationViewWrapper mContractedWrapper; |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 60 | private NotificationViewWrapper mExpandedWrapper; |
| 61 | private NotificationViewWrapper mHeadsUpWrapper; |
Selim Cinek | c9c00ae | 2014-05-20 03:33:40 +0200 | [diff] [blame] | 62 | private int mClipTopAmount; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 63 | private int mContentHeight; |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 64 | private int mUnrestrictedContentHeight; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 65 | private int mVisibleType = VISIBLE_TYPE_CONTRACTED; |
John Spurlock | e15452b | 2014-08-21 09:44:39 -0400 | [diff] [blame] | 66 | private boolean mDark; |
Christoph Studer | a7fe631 | 2014-06-27 19:32:44 +0200 | [diff] [blame] | 67 | private final Paint mFadePaint = new Paint(); |
Selim Cinek | bb3d1cf | 2014-10-31 00:12:56 +0100 | [diff] [blame] | 68 | private boolean mAnimate; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 69 | private boolean mIsHeadsUp; |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 70 | private final ViewTreeObserver.OnPreDrawListener mEnableAnimationPredrawListener |
Selim Cinek | bb3d1cf | 2014-10-31 00:12:56 +0100 | [diff] [blame] | 71 | = new ViewTreeObserver.OnPreDrawListener() { |
| 72 | @Override |
| 73 | public boolean onPreDraw() { |
| 74 | mAnimate = true; |
| 75 | getViewTreeObserver().removeOnPreDrawListener(this); |
| 76 | return true; |
| 77 | } |
| 78 | }; |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 79 | |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 80 | private final ViewOutlineProvider mOutlineProvider = new ViewOutlineProvider() { |
| 81 | @Override |
| 82 | public void getOutline(View view, Outline outline) { |
| 83 | outline.setRoundRect(0, 0, view.getWidth(), mUnrestrictedContentHeight, |
| 84 | mRoundRectRadius); |
| 85 | } |
| 86 | }; |
| 87 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 88 | public NotificationContentView(Context context, AttributeSet attrs) { |
| 89 | super(context, attrs); |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 90 | mFadePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD)); |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 91 | mSmallHeight = getResources().getDimensionPixelSize(R.dimen.notification_min_height); |
| 92 | mHeadsUpHeight = getResources().getDimensionPixelSize(R.dimen.notification_mid_height); |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 93 | mRoundRectRadius = getResources().getDimensionPixelSize( |
| 94 | R.dimen.notification_material_rounded_rect_radius); |
| 95 | mRoundRectClippingEnabled = getResources().getBoolean( |
| 96 | R.bool.config_notifications_round_rect_clipping); |
Selim Cinek | 1a521f3 | 2014-11-03 17:39:29 +0100 | [diff] [blame] | 97 | reset(true); |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 98 | setOutlineProvider(mOutlineProvider); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 102 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 103 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 104 | boolean hasFixedHeight = heightMode == MeasureSpec.EXACTLY; |
| 105 | boolean isHeightLimited = heightMode == MeasureSpec.AT_MOST; |
| 106 | int maxSize = Integer.MAX_VALUE; |
| 107 | if (hasFixedHeight || isHeightLimited) { |
| 108 | maxSize = MeasureSpec.getSize(heightMeasureSpec); |
| 109 | } |
| 110 | int maxChildHeight = 0; |
| 111 | if (mContractedChild != null) { |
| 112 | int size = Math.min(maxSize, mSmallHeight); |
| 113 | mContractedChild.measure(widthMeasureSpec, |
Selim Cinek | 06a24ebd | 2015-04-22 14:22:05 -0700 | [diff] [blame] | 114 | MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY)); |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 115 | maxChildHeight = Math.max(maxChildHeight, mContractedChild.getMeasuredHeight()); |
| 116 | } |
| 117 | if (mExpandedChild != null) { |
| 118 | int size = maxSize; |
| 119 | ViewGroup.LayoutParams layoutParams = mExpandedChild.getLayoutParams(); |
| 120 | if (layoutParams.height >= 0) { |
| 121 | // An actual height is set |
| 122 | size = Math.min(maxSize, layoutParams.height); |
| 123 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 124 | int spec = size == Integer.MAX_VALUE |
| 125 | ? MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED) |
| 126 | : MeasureSpec.makeMeasureSpec(size, MeasureSpec.AT_MOST); |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 127 | mExpandedChild.measure(widthMeasureSpec, spec); |
| 128 | maxChildHeight = Math.max(maxChildHeight, mExpandedChild.getMeasuredHeight()); |
| 129 | } |
| 130 | if (mHeadsUpChild != null) { |
| 131 | int size = Math.min(maxSize, mHeadsUpHeight); |
| 132 | ViewGroup.LayoutParams layoutParams = mHeadsUpChild.getLayoutParams(); |
| 133 | if (layoutParams.height >= 0) { |
| 134 | // An actual height is set |
| 135 | size = Math.min(maxSize, layoutParams.height); |
| 136 | } |
| 137 | mHeadsUpChild.measure(widthMeasureSpec, |
| 138 | MeasureSpec.makeMeasureSpec(size, MeasureSpec.AT_MOST)); |
| 139 | maxChildHeight = Math.max(maxChildHeight, mHeadsUpChild.getMeasuredHeight()); |
| 140 | } |
| 141 | int ownHeight = Math.min(maxChildHeight, maxSize); |
| 142 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 143 | setMeasuredDimension(width, ownHeight); |
| 144 | } |
| 145 | |
| 146 | @Override |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 147 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 148 | super.onLayout(changed, left, top, right, bottom); |
| 149 | updateClipping(); |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 150 | invalidateOutline(); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Selim Cinek | bb3d1cf | 2014-10-31 00:12:56 +0100 | [diff] [blame] | 153 | @Override |
Selim Cinek | 7b8157e | 2014-11-20 16:00:32 +0100 | [diff] [blame] | 154 | protected void onAttachedToWindow() { |
| 155 | super.onAttachedToWindow(); |
Selim Cinek | bb3d1cf | 2014-10-31 00:12:56 +0100 | [diff] [blame] | 156 | updateVisibility(); |
| 157 | } |
| 158 | |
Selim Cinek | 1a521f3 | 2014-11-03 17:39:29 +0100 | [diff] [blame] | 159 | public void reset(boolean resetActualHeight) { |
Jorim Jaggi | 0d9f35d | 2014-08-20 17:06:55 +0200 | [diff] [blame] | 160 | if (mContractedChild != null) { |
| 161 | mContractedChild.animate().cancel(); |
| 162 | } |
| 163 | if (mExpandedChild != null) { |
| 164 | mExpandedChild.animate().cancel(); |
| 165 | } |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 166 | if (mHeadsUpChild != null) { |
| 167 | mHeadsUpChild.animate().cancel(); |
| 168 | } |
Christoph Studer | a7fe631 | 2014-06-27 19:32:44 +0200 | [diff] [blame] | 169 | removeAllViews(); |
| 170 | mContractedChild = null; |
| 171 | mExpandedChild = null; |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 172 | mHeadsUpChild = null; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 173 | mVisibleType = VISIBLE_TYPE_CONTRACTED; |
Selim Cinek | 1a521f3 | 2014-11-03 17:39:29 +0100 | [diff] [blame] | 174 | if (resetActualHeight) { |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 175 | mContentHeight = mSmallHeight; |
Selim Cinek | 1a521f3 | 2014-11-03 17:39:29 +0100 | [diff] [blame] | 176 | } |
Christoph Studer | a7fe631 | 2014-06-27 19:32:44 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Selim Cinek | cab4a60 | 2014-09-03 14:47:57 +0200 | [diff] [blame] | 179 | public View getContractedChild() { |
| 180 | return mContractedChild; |
| 181 | } |
| 182 | |
| 183 | public View getExpandedChild() { |
| 184 | return mExpandedChild; |
| 185 | } |
| 186 | |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 187 | public View getHeadsUpChild() { |
| 188 | return mHeadsUpChild; |
| 189 | } |
| 190 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 191 | public void setContractedChild(View child) { |
| 192 | if (mContractedChild != null) { |
Jorim Jaggi | 0d9f35d | 2014-08-20 17:06:55 +0200 | [diff] [blame] | 193 | mContractedChild.animate().cancel(); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 194 | removeView(mContractedChild); |
| 195 | } |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 196 | addView(child); |
| 197 | mContractedChild = child; |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 198 | mContractedWrapper = NotificationViewWrapper.wrap(getContext(), child); |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 199 | selectLayout(false /* animate */, true /* force */); |
Jorim Jaggi | 10ad761 | 2014-12-08 18:41:11 +0100 | [diff] [blame] | 200 | mContractedWrapper.setDark(mDark, false /* animate */, 0 /* delay */); |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 201 | updateRoundRectClipping(); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | public void setExpandedChild(View child) { |
| 205 | if (mExpandedChild != null) { |
Jorim Jaggi | 0d9f35d | 2014-08-20 17:06:55 +0200 | [diff] [blame] | 206 | mExpandedChild.animate().cancel(); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 207 | removeView(mExpandedChild); |
| 208 | } |
| 209 | addView(child); |
| 210 | mExpandedChild = child; |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 211 | mExpandedWrapper = NotificationViewWrapper.wrap(getContext(), child); |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 212 | selectLayout(false /* animate */, true /* force */); |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 213 | updateRoundRectClipping(); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 216 | public void setHeadsUpChild(View child) { |
| 217 | if (mHeadsUpChild != null) { |
| 218 | mHeadsUpChild.animate().cancel(); |
| 219 | removeView(mHeadsUpChild); |
| 220 | } |
| 221 | addView(child); |
| 222 | mHeadsUpChild = child; |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 223 | mHeadsUpWrapper = NotificationViewWrapper.wrap(getContext(), child); |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 224 | selectLayout(false /* animate */, true /* force */); |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 225 | updateRoundRectClipping(); |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Selim Cinek | bb3d1cf | 2014-10-31 00:12:56 +0100 | [diff] [blame] | 228 | @Override |
| 229 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 230 | super.onVisibilityChanged(changedView, visibility); |
| 231 | updateVisibility(); |
| 232 | } |
| 233 | |
| 234 | private void updateVisibility() { |
| 235 | setVisible(isShown()); |
| 236 | } |
| 237 | |
| 238 | private void setVisible(final boolean isVisible) { |
| 239 | if (isVisible) { |
| 240 | |
| 241 | // We only animate if we are drawn at least once, otherwise the view might animate when |
| 242 | // it's shown the first time |
| 243 | getViewTreeObserver().addOnPreDrawListener(mEnableAnimationPredrawListener); |
| 244 | } else { |
| 245 | getViewTreeObserver().removeOnPreDrawListener(mEnableAnimationPredrawListener); |
| 246 | mAnimate = false; |
| 247 | } |
| 248 | } |
| 249 | |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 250 | public void setContentHeight(int contentHeight) { |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 251 | mContentHeight = Math.max(Math.min(contentHeight, getHeight()), getMinHeight());; |
| 252 | mUnrestrictedContentHeight = Math.max(contentHeight, getMinHeight()); |
Selim Cinek | bb3d1cf | 2014-10-31 00:12:56 +0100 | [diff] [blame] | 253 | selectLayout(mAnimate /* animate */, false /* force */); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 254 | updateClipping(); |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 255 | invalidateOutline(); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 258 | public int getContentHeight() { |
| 259 | return mContentHeight; |
| 260 | } |
| 261 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 262 | public int getMaxHeight() { |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 263 | if (mIsHeadsUp && mHeadsUpChild != null) { |
| 264 | return mHeadsUpChild.getHeight(); |
| 265 | } else if (mExpandedChild != null) { |
| 266 | return mExpandedChild.getHeight(); |
| 267 | } |
| 268 | return mSmallHeight; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 271 | public int getMinHeight() { |
| 272 | return mSmallHeight; |
| 273 | } |
| 274 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 275 | public void setClipTopAmount(int clipTopAmount) { |
Selim Cinek | c9c00ae | 2014-05-20 03:33:40 +0200 | [diff] [blame] | 276 | mClipTopAmount = clipTopAmount; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 277 | updateClipping(); |
| 278 | } |
| 279 | |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 280 | private void updateRoundRectClipping() { |
| 281 | boolean enabled = needsRoundRectClipping(); |
| 282 | setClipToOutline(enabled); |
| 283 | } |
| 284 | |
| 285 | private boolean needsRoundRectClipping() { |
| 286 | if (!mRoundRectClippingEnabled) { |
| 287 | return false; |
| 288 | } |
| 289 | boolean needsForContracted = mContractedChild != null |
| 290 | && mContractedChild.getVisibility() == View.VISIBLE |
| 291 | && mContractedWrapper.needsRoundRectClipping(); |
| 292 | boolean needsForExpanded = mExpandedChild != null |
| 293 | && mExpandedChild.getVisibility() == View.VISIBLE |
| 294 | && mExpandedWrapper.needsRoundRectClipping(); |
| 295 | boolean needsForHeadsUp = mExpandedChild != null |
| 296 | && mExpandedChild.getVisibility() == View.VISIBLE |
| 297 | && mExpandedWrapper.needsRoundRectClipping(); |
| 298 | return needsForContracted || needsForExpanded || needsForHeadsUp; |
| 299 | } |
| 300 | |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 301 | private void updateClipping() { |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 302 | mClipBounds.set(0, mClipTopAmount, getWidth(), mContentHeight); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 303 | setClipBounds(mClipBounds); |
| 304 | } |
| 305 | |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 306 | private void selectLayout(boolean animate, boolean force) { |
| 307 | if (mContractedChild == null) { |
| 308 | return; |
| 309 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 310 | int visibleType = calculateVisibleType(); |
| 311 | if (visibleType != mVisibleType || force) { |
| 312 | if (animate && (visibleType == VISIBLE_TYPE_EXPANDED && mExpandedChild != null) |
| 313 | || (visibleType == VISIBLE_TYPE_HEADSUP && mHeadsUpChild != null) |
| 314 | || visibleType == VISIBLE_TYPE_CONTRACTED) { |
| 315 | runSwitchAnimation(visibleType); |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 316 | } else { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 317 | updateViewVisibilities(visibleType); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 318 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 319 | mVisibleType = visibleType; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 320 | } |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 323 | private void updateViewVisibilities(int visibleType) { |
| 324 | boolean contractedVisible = visibleType == VISIBLE_TYPE_CONTRACTED; |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 325 | mContractedChild.setVisibility(contractedVisible ? View.VISIBLE : View.INVISIBLE); |
| 326 | mContractedChild.setAlpha(contractedVisible ? 1f : 0f); |
| 327 | mContractedChild.setLayerType(LAYER_TYPE_NONE, null); |
| 328 | if (mExpandedChild != null) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 329 | boolean expandedVisible = visibleType == VISIBLE_TYPE_EXPANDED; |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 330 | mExpandedChild.setVisibility(expandedVisible ? View.VISIBLE : View.INVISIBLE); |
| 331 | mExpandedChild.setAlpha(expandedVisible ? 1f : 0f); |
| 332 | mExpandedChild.setLayerType(LAYER_TYPE_NONE, null); |
| 333 | } |
| 334 | if (mHeadsUpChild != null) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 335 | boolean headsUpVisible = visibleType == VISIBLE_TYPE_HEADSUP; |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 336 | mHeadsUpChild.setVisibility(headsUpVisible ? View.VISIBLE : View.INVISIBLE); |
| 337 | mHeadsUpChild.setAlpha(headsUpVisible ? 1f : 0f); |
| 338 | mHeadsUpChild.setLayerType(LAYER_TYPE_NONE, null); |
| 339 | } |
| 340 | setLayerType(LAYER_TYPE_NONE, null); |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 341 | updateRoundRectClipping(); |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 344 | private void runSwitchAnimation(int visibleType) { |
| 345 | View shownView = getViewForVisibleType(visibleType); |
| 346 | View hiddenView = getViewForVisibleType(mVisibleType); |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 347 | shownView.setVisibility(View.VISIBLE); |
| 348 | hiddenView.setVisibility(View.VISIBLE); |
| 349 | shownView.setLayerType(LAYER_TYPE_HARDWARE, mFadePaint); |
| 350 | hiddenView.setLayerType(LAYER_TYPE_HARDWARE, mFadePaint); |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 351 | setLayerType(LAYER_TYPE_HARDWARE, null); |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 352 | hiddenView.animate() |
| 353 | .alpha(0f) |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 354 | .setDuration(ANIMATION_DURATION_LENGTH) |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 355 | .setInterpolator(mLinearInterpolator) |
| 356 | .withEndAction(null); // In case we have multiple changes in one frame. |
| 357 | shownView.animate() |
| 358 | .alpha(1f) |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 359 | .setDuration(ANIMATION_DURATION_LENGTH) |
| 360 | .setInterpolator(mLinearInterpolator) |
| 361 | .withEndAction(new Runnable() { |
| 362 | @Override |
| 363 | public void run() { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 364 | updateViewVisibilities(mVisibleType); |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 365 | } |
| 366 | }); |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 367 | updateRoundRectClipping(); |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 368 | } |
| 369 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 370 | /** |
| 371 | * @param visibleType one of the static enum types in this view |
| 372 | * @return the corresponding view according to the given visible type |
| 373 | */ |
| 374 | private View getViewForVisibleType(int visibleType) { |
| 375 | switch (visibleType) { |
| 376 | case VISIBLE_TYPE_EXPANDED: |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 377 | return mExpandedChild; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 378 | case VISIBLE_TYPE_HEADSUP: |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 379 | return mHeadsUpChild; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 380 | default: |
| 381 | return mContractedChild; |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 382 | } |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 385 | /** |
| 386 | * @return one of the static enum types in this view, calculated form the current state |
| 387 | */ |
| 388 | private int calculateVisibleType() { |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 389 | boolean noExpandedChild = mExpandedChild == null; |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 390 | if (mIsHeadsUp && mHeadsUpChild != null) { |
| 391 | if (mContentHeight <= mHeadsUpChild.getHeight() || noExpandedChild) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 392 | return VISIBLE_TYPE_HEADSUP; |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 393 | } else { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 394 | return VISIBLE_TYPE_EXPANDED; |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 395 | } |
| 396 | } else { |
| 397 | if (mContentHeight <= mSmallHeight || noExpandedChild) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 398 | return VISIBLE_TYPE_CONTRACTED; |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 399 | } else { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 400 | return VISIBLE_TYPE_EXPANDED; |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | public void notifyContentUpdated() { |
Jorim Jaggi | 1129883 | 2014-05-24 16:18:38 +0200 | [diff] [blame] | 406 | selectLayout(false /* animate */, true /* force */); |
Jorim Jaggi | 394a5d6 | 2014-11-26 23:07:13 +0100 | [diff] [blame] | 407 | if (mContractedChild != null) { |
Jorim Jaggi | dacc924 | 2014-12-08 19:21:26 +0100 | [diff] [blame] | 408 | mContractedWrapper.notifyContentUpdated(); |
Jorim Jaggi | 394a5d6 | 2014-11-26 23:07:13 +0100 | [diff] [blame] | 409 | mContractedWrapper.setDark(mDark, false /* animate */, 0 /* delay */); |
| 410 | } |
Jorim Jaggi | be4116a | 2015-05-20 20:04:08 -0700 | [diff] [blame^] | 411 | if (mExpandedChild != null) { |
| 412 | mExpandedWrapper.notifyContentUpdated(); |
| 413 | } |
| 414 | updateRoundRectClipping(); |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 415 | } |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 416 | |
Jorim Jaggi | 4222d9a | 2014-04-23 16:13:15 +0200 | [diff] [blame] | 417 | public boolean isContentExpandable() { |
| 418 | return mExpandedChild != null; |
| 419 | } |
John Spurlock | e15452b | 2014-08-21 09:44:39 -0400 | [diff] [blame] | 420 | |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 421 | public void setDark(boolean dark, boolean fade, long delay) { |
John Spurlock | c3dfd32 | 2014-08-27 14:55:54 -0400 | [diff] [blame] | 422 | if (mDark == dark || mContractedChild == null) return; |
John Spurlock | e15452b | 2014-08-21 09:44:39 -0400 | [diff] [blame] | 423 | mDark = dark; |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 424 | mContractedWrapper.setDark(dark, fade, delay); |
John Spurlock | e15452b | 2014-08-21 09:44:39 -0400 | [diff] [blame] | 425 | } |
| 426 | |
Selim Cinek | 8d490d4 | 2015-04-10 00:05:50 -0700 | [diff] [blame] | 427 | public void setHeadsUp(boolean headsUp) { |
| 428 | mIsHeadsUp = headsUp; |
| 429 | selectLayout(false /* animate */, true /* force */); |
| 430 | } |
| 431 | |
Jorim Jaggi | aa92ffb | 2014-09-10 23:29:28 +0200 | [diff] [blame] | 432 | @Override |
| 433 | public boolean hasOverlappingRendering() { |
| 434 | |
| 435 | // This is not really true, but good enough when fading from the contracted to the expanded |
| 436 | // layout, and saves us some layers. |
| 437 | return false; |
| 438 | } |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 439 | } |