Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [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 | |
Selim Cinek | 0ffbda6 | 2016-01-01 20:29:12 +0100 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification; |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 18 | |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 19 | import android.animation.ValueAnimator; |
| 20 | import android.content.Context; |
| 21 | import android.graphics.Color; |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 22 | import android.service.notification.StatusBarNotification; |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 23 | import android.view.View; |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 24 | import android.widget.ImageView; |
Selim Cinek | 75fe38c | 2015-11-20 12:47:59 -0800 | [diff] [blame] | 25 | import android.widget.ProgressBar; |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 26 | import android.widget.TextView; |
| 27 | |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 28 | import com.android.systemui.Interpolators; |
Selim Cinek | fd3e262 | 2016-01-12 16:02:42 -0800 | [diff] [blame] | 29 | import com.android.systemui.statusbar.CrossFadeHelper; |
Selim Cinek | 0ffbda6 | 2016-01-01 20:29:12 +0100 | [diff] [blame] | 30 | import com.android.systemui.statusbar.TransformableView; |
Selim Cinek | fd3e262 | 2016-01-12 16:02:42 -0800 | [diff] [blame] | 31 | import com.android.systemui.statusbar.ViewTransformationHelper; |
Selim Cinek | 9969559 | 2016-01-12 17:51:35 -0800 | [diff] [blame] | 32 | import com.android.systemui.statusbar.stack.StackStateAnimator; |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 33 | |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 34 | /** |
| 35 | * Wraps a notification view inflated from a template. |
| 36 | */ |
Selim Cinek | 9c7712d | 2015-12-08 19:19:48 -0800 | [diff] [blame] | 37 | public class NotificationTemplateViewWrapper extends NotificationHeaderViewWrapper { |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 38 | |
Selim Cinek | 9c7712d | 2015-12-08 19:19:48 -0800 | [diff] [blame] | 39 | private static final int mDarkProgressTint = 0xffffffff; |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 40 | |
Jorim Jaggi | dacc924 | 2014-12-08 19:21:26 +0100 | [diff] [blame] | 41 | protected ImageView mPicture; |
Selim Cinek | 75fe38c | 2015-11-20 12:47:59 -0800 | [diff] [blame] | 42 | private ProgressBar mProgressBar; |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 43 | private TextView mTitle; |
| 44 | private TextView mText; |
Jorim Jaggi | 92df1f2 | 2014-12-16 19:44:41 +0100 | [diff] [blame] | 45 | |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 46 | protected NotificationTemplateViewWrapper(Context ctx, View view) { |
Selim Cinek | 9c7712d | 2015-12-08 19:19:48 -0800 | [diff] [blame] | 47 | super(ctx, view); |
Selim Cinek | fd3e262 | 2016-01-12 16:02:42 -0800 | [diff] [blame] | 48 | mTransformationHelper.setCustomTransformation( |
| 49 | new ViewTransformationHelper.CustomTransformation() { |
| 50 | @Override |
| 51 | public boolean transformTo(TransformState ownState, |
Selim Cinek | 8f2f6a6 | 2016-02-23 19:56:31 -0800 | [diff] [blame] | 52 | TransformableView notification, final float transformationAmount) { |
Selim Cinek | fd3e262 | 2016-01-12 16:02:42 -0800 | [diff] [blame] | 53 | if (!(notification instanceof HybridNotificationView)) { |
| 54 | return false; |
| 55 | } |
| 56 | TransformState otherState = notification.getCurrentState( |
| 57 | TRANSFORMING_VIEW_TITLE); |
Selim Cinek | b66685b | 2016-02-02 17:16:08 -0800 | [diff] [blame] | 58 | final View text = ownState.getTransformedView(); |
Selim Cinek | 8f2f6a6 | 2016-02-23 19:56:31 -0800 | [diff] [blame] | 59 | CrossFadeHelper.fadeOut(text, transformationAmount); |
Selim Cinek | fd3e262 | 2016-01-12 16:02:42 -0800 | [diff] [blame] | 60 | if (otherState != null) { |
Selim Cinek | 8f2f6a6 | 2016-02-23 19:56:31 -0800 | [diff] [blame] | 61 | ownState.transformViewVerticalTo(otherState, this, |
| 62 | transformationAmount); |
Selim Cinek | fd3e262 | 2016-01-12 16:02:42 -0800 | [diff] [blame] | 63 | otherState.recycle(); |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | @Override |
Selim Cinek | 8f2f6a6 | 2016-02-23 19:56:31 -0800 | [diff] [blame] | 69 | public boolean customTransformTarget(TransformState ownState, |
| 70 | TransformState otherState) { |
| 71 | float endY = getTransformationY(ownState, otherState); |
| 72 | ownState.setTransformationEndY(endY); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | @Override |
Selim Cinek | fd3e262 | 2016-01-12 16:02:42 -0800 | [diff] [blame] | 77 | public boolean transformFrom(TransformState ownState, |
Selim Cinek | 8f2f6a6 | 2016-02-23 19:56:31 -0800 | [diff] [blame] | 78 | TransformableView notification, float transformationAmount) { |
Selim Cinek | fd3e262 | 2016-01-12 16:02:42 -0800 | [diff] [blame] | 79 | if (!(notification instanceof HybridNotificationView)) { |
| 80 | return false; |
| 81 | } |
| 82 | TransformState otherState = notification.getCurrentState( |
| 83 | TRANSFORMING_VIEW_TITLE); |
Selim Cinek | b66685b | 2016-02-02 17:16:08 -0800 | [diff] [blame] | 84 | final View text = ownState.getTransformedView(); |
Selim Cinek | 8f2f6a6 | 2016-02-23 19:56:31 -0800 | [diff] [blame] | 85 | CrossFadeHelper.fadeIn(text, transformationAmount); |
Selim Cinek | fd3e262 | 2016-01-12 16:02:42 -0800 | [diff] [blame] | 86 | if (otherState != null) { |
Selim Cinek | 8f2f6a6 | 2016-02-23 19:56:31 -0800 | [diff] [blame] | 87 | ownState.transformViewVerticalFrom(otherState, this, |
| 88 | transformationAmount); |
Selim Cinek | fd3e262 | 2016-01-12 16:02:42 -0800 | [diff] [blame] | 89 | otherState.recycle(); |
| 90 | } |
| 91 | return true; |
| 92 | } |
Selim Cinek | 8f2f6a6 | 2016-02-23 19:56:31 -0800 | [diff] [blame] | 93 | |
| 94 | @Override |
| 95 | public boolean initTransformation(TransformState ownState, |
| 96 | TransformState otherState) { |
| 97 | float startY = getTransformationY(ownState, otherState); |
| 98 | ownState.setTransformationStartY(startY); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | private float getTransformationY(TransformState ownState, |
| 103 | TransformState otherState) { |
| 104 | int[] otherStablePosition = otherState.getLaidOutLocationOnScreen(); |
| 105 | int[] ownStablePosition = ownState.getLaidOutLocationOnScreen(); |
| 106 | return (otherStablePosition[1] |
| 107 | + otherState.getTransformedView().getHeight() |
| 108 | - ownStablePosition[1]) * 0.33f; |
| 109 | } |
| 110 | |
Selim Cinek | fd3e262 | 2016-01-12 16:02:42 -0800 | [diff] [blame] | 111 | }, TRANSFORMING_VIEW_TEXT); |
Jorim Jaggi | dacc924 | 2014-12-08 19:21:26 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 114 | private void resolveTemplateViews(StatusBarNotification notification) { |
Selim Cinek | 65b2e7c | 2015-10-26 14:11:31 -0700 | [diff] [blame] | 115 | mPicture = (ImageView) mView.findViewById(com.android.internal.R.id.right_icon); |
Selim Cinek | 0ffbda6 | 2016-01-01 20:29:12 +0100 | [diff] [blame] | 116 | mPicture.setTag(ImageTransformState.ICON_TAG, |
| 117 | notification.getNotification().getLargeIcon()); |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 118 | mTitle = (TextView) mView.findViewById(com.android.internal.R.id.title); |
| 119 | mText = (TextView) mView.findViewById(com.android.internal.R.id.text); |
Selim Cinek | 75fe38c | 2015-11-20 12:47:59 -0800 | [diff] [blame] | 120 | final View progress = mView.findViewById(com.android.internal.R.id.progress); |
| 121 | if (progress instanceof ProgressBar) { |
| 122 | mProgressBar = (ProgressBar) progress; |
| 123 | } else { |
| 124 | // It's still a viewstub |
| 125 | mProgressBar = null; |
| 126 | } |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void notifyContentUpdated(StatusBarNotification notification) { |
Selim Cinek | 646d205 | 2016-01-03 14:42:02 +0800 | [diff] [blame] | 131 | // Reinspect the notification. Before the super call, because the super call also updates |
| 132 | // the transformation types and we need to have our values set by then. |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 133 | resolveTemplateViews(notification); |
| 134 | super.notifyContentUpdated(notification); |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | protected void updateInvertHelper() { |
| 139 | super.updateInvertHelper(); |
| 140 | View mainColumn = mView.findViewById(com.android.internal.R.id.notification_main_column); |
Selim Cinek | 75fe38c | 2015-11-20 12:47:59 -0800 | [diff] [blame] | 141 | if (mainColumn != null) { |
Selim Cinek | 9c7712d | 2015-12-08 19:19:48 -0800 | [diff] [blame] | 142 | mInvertHelper.addTarget(mainColumn); |
Selim Cinek | 75fe38c | 2015-11-20 12:47:59 -0800 | [diff] [blame] | 143 | } |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 147 | protected void updateTransformedTypes() { |
| 148 | // This also clears the existing types |
| 149 | super.updateTransformedTypes(); |
| 150 | if (mTitle != null) { |
Selim Cinek | 0ffbda6 | 2016-01-01 20:29:12 +0100 | [diff] [blame] | 151 | mTransformationHelper.addTransformedView(TransformableView.TRANSFORMING_VIEW_TITLE, mTitle); |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 152 | } |
| 153 | if (mText != null) { |
Selim Cinek | 0ffbda6 | 2016-01-01 20:29:12 +0100 | [diff] [blame] | 154 | mTransformationHelper.addTransformedView(TransformableView.TRANSFORMING_VIEW_TEXT, mText); |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 155 | } |
| 156 | if (mPicture != null) { |
Selim Cinek | 0ffbda6 | 2016-01-01 20:29:12 +0100 | [diff] [blame] | 157 | mTransformationHelper.addTransformedView(TransformableView.TRANSFORMING_VIEW_IMAGE, mPicture); |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 158 | } |
| 159 | if (mProgressBar != null) { |
Selim Cinek | 0ffbda6 | 2016-01-01 20:29:12 +0100 | [diff] [blame] | 160 | mTransformationHelper.addTransformedView(TransformableView.TRANSFORMING_VIEW_PROGRESS, mProgressBar); |
Selim Cinek | 4ffd636 | 2015-12-29 15:12:23 +0100 | [diff] [blame] | 161 | } |
Jorim Jaggi | dacc924 | 2014-12-08 19:21:26 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 165 | public void setDark(boolean dark, boolean fade, long delay) { |
Selim Cinek | b88b984 | 2016-02-26 09:25:33 -0800 | [diff] [blame] | 166 | if (dark == mDark && mDarkInitialized) { |
Selim Cinek | 19ba705 | 2016-01-27 20:04:27 -0800 | [diff] [blame] | 167 | return; |
| 168 | } |
Selim Cinek | 9c7712d | 2015-12-08 19:19:48 -0800 | [diff] [blame] | 169 | super.setDark(dark, fade, delay); |
Jorim Jaggi | 394a5d6 | 2014-11-26 23:07:13 +0100 | [diff] [blame] | 170 | setPictureGrayscale(dark, fade, delay); |
Selim Cinek | 75fe38c | 2015-11-20 12:47:59 -0800 | [diff] [blame] | 171 | setProgressBarDark(dark, fade, delay); |
| 172 | } |
| 173 | |
| 174 | private void setProgressBarDark(boolean dark, boolean fade, long delay) { |
| 175 | if (mProgressBar != null) { |
| 176 | if (fade) { |
| 177 | fadeProgressDark(mProgressBar, dark, delay); |
| 178 | } else { |
| 179 | updateProgressDark(mProgressBar, dark); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | private void fadeProgressDark(final ProgressBar target, final boolean dark, long delay) { |
| 185 | startIntensityAnimation(new ValueAnimator.AnimatorUpdateListener() { |
| 186 | @Override |
| 187 | public void onAnimationUpdate(ValueAnimator animation) { |
| 188 | float t = (float) animation.getAnimatedValue(); |
| 189 | updateProgressDark(target, t); |
| 190 | } |
| 191 | }, dark, delay, null /* listener */); |
| 192 | } |
| 193 | |
| 194 | private void updateProgressDark(ProgressBar target, float intensity) { |
| 195 | int color = interpolateColor(mColor, mDarkProgressTint, intensity); |
| 196 | target.getIndeterminateDrawable().mutate().setTint(color); |
| 197 | target.getProgressDrawable().mutate().setTint(color); |
| 198 | } |
| 199 | |
| 200 | private void updateProgressDark(ProgressBar target, boolean dark) { |
| 201 | updateProgressDark(target, dark ? 1f : 0f); |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | protected void setPictureGrayscale(boolean grayscale, boolean fade, long delay) { |
| 205 | if (mPicture != null) { |
| 206 | if (fade) { |
| 207 | fadeGrayscale(mPicture, grayscale, delay); |
| 208 | } else { |
| 209 | updateGrayscale(mPicture, grayscale); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 214 | private static int interpolateColor(int source, int target, float t) { |
| 215 | int aSource = Color.alpha(source); |
| 216 | int rSource = Color.red(source); |
| 217 | int gSource = Color.green(source); |
| 218 | int bSource = Color.blue(source); |
| 219 | int aTarget = Color.alpha(target); |
| 220 | int rTarget = Color.red(target); |
| 221 | int gTarget = Color.green(target); |
| 222 | int bTarget = Color.blue(target); |
| 223 | return Color.argb( |
| 224 | (int) (aSource * (1f - t) + aTarget * t), |
| 225 | (int) (rSource * (1f - t) + rTarget * t), |
| 226 | (int) (gSource * (1f - t) + gTarget * t), |
| 227 | (int) (bSource * (1f - t) + bTarget * t)); |
| 228 | } |
Jorim Jaggi | 4e857f4 | 2014-11-17 19:14:04 +0100 | [diff] [blame] | 229 | } |