• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.notification.row;
18 
19 import static android.app.Notification.COLOR_INVALID;
20 
21 import android.annotation.Nullable;
22 import android.content.Context;
23 import android.content.res.TypedArray;
24 import android.text.TextUtils;
25 import android.util.AttributeSet;
26 import android.view.View;
27 import android.widget.TextView;
28 
29 import androidx.annotation.ColorInt;
30 
31 import com.android.keyguard.AlphaOptimizedLinearLayout;
32 import com.android.systemui.res.R;
33 import com.android.systemui.statusbar.CrossFadeHelper;
34 import com.android.systemui.statusbar.TransformableView;
35 import com.android.systemui.statusbar.ViewTransformationHelper;
36 import com.android.systemui.statusbar.notification.NotificationFadeAware;
37 import com.android.systemui.statusbar.notification.TransformState;
38 
39 /**
40  * A hybrid view which may contain information about one ore more notifications.
41  */
42 public class HybridNotificationView extends AlphaOptimizedLinearLayout
43         implements TransformableView, NotificationFadeAware {
44 
45     protected final ViewTransformationHelper mTransformationHelper = new ViewTransformationHelper();
46     protected TextView mTitleView;
47     protected TextView mTextView;
48     protected int mPrimaryTextColor = COLOR_INVALID;
49     protected int mSecondaryTextColor = COLOR_INVALID;
50 
HybridNotificationView(Context context)51     public HybridNotificationView(Context context) {
52         this(context, null);
53     }
54 
HybridNotificationView(Context context, @Nullable AttributeSet attrs)55     public HybridNotificationView(Context context, @Nullable AttributeSet attrs) {
56         this(context, attrs, 0);
57     }
58 
HybridNotificationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)59     public HybridNotificationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
60         this(context, attrs, defStyleAttr, 0);
61     }
62 
HybridNotificationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)63     public HybridNotificationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
64             int defStyleRes) {
65         super(context, attrs, defStyleAttr, defStyleRes);
66     }
67 
getTitleView()68     public TextView getTitleView() {
69         return mTitleView;
70     }
71 
getTextView()72     public TextView getTextView() {
73         return mTextView;
74     }
75 
76     @Override
onFinishInflate()77     protected void onFinishInflate() {
78         super.onFinishInflate();
79         resolveThemeTextColors();
80         mTitleView = findViewById(R.id.notification_title);
81         mTextView = findViewById(R.id.notification_text);
82         applyTextColor(mTitleView, mPrimaryTextColor);
83         applyTextColor(mTextView, mSecondaryTextColor);
84         mTransformationHelper.setCustomTransformation(
85                 new FadeOutAndDownWithTitleTransformation(mTextView),
86                 TRANSFORMING_VIEW_TEXT);
87         mTransformationHelper.addTransformedView(TRANSFORMING_VIEW_TITLE, mTitleView);
88         mTransformationHelper.addTransformedView(TRANSFORMING_VIEW_TEXT, mTextView);
89     }
90 
applyTextColor(TextView textView, @ColorInt int textColor)91     protected void applyTextColor(TextView textView, @ColorInt int textColor) {
92         if (textColor != COLOR_INVALID) {
93             textView.setTextColor(textColor);
94         }
95     }
96 
resolveThemeTextColors()97     private void resolveThemeTextColors() {
98         try (TypedArray ta = mContext.getTheme().obtainStyledAttributes(
99                 android.R.style.Theme_DeviceDefault_DayNight, new int[]{
100                         com.android.internal.R.attr.materialColorOnSurface,
101                         com.android.internal.R.attr.materialColorOnSurfaceVariant
102                 })) {
103             if (ta != null) {
104                 mPrimaryTextColor = ta.getColor(0, mPrimaryTextColor);
105                 mSecondaryTextColor = ta.getColor(1, mSecondaryTextColor);
106             }
107         }
108     }
109 
bind(@ullable CharSequence title, @Nullable CharSequence text, @Nullable View contentView)110     public void bind(@Nullable CharSequence title, @Nullable CharSequence text,
111             @Nullable View contentView) {
112         mTitleView.setText(title != null ? title.toString() : title);
113         mTitleView.setVisibility(TextUtils.isEmpty(title) ? GONE : VISIBLE);
114         if (TextUtils.isEmpty(text)) {
115             mTextView.setVisibility(GONE);
116             mTextView.setText(null);
117         } else {
118             mTextView.setVisibility(VISIBLE);
119             mTextView.setText(text.toString());
120         }
121         requestLayout();
122     }
123 
124     @Override
getCurrentState(int fadingView)125     public TransformState getCurrentState(int fadingView) {
126         return mTransformationHelper.getCurrentState(fadingView);
127     }
128 
129     @Override
transformTo(TransformableView notification, Runnable endRunnable)130     public void transformTo(TransformableView notification, Runnable endRunnable) {
131         mTransformationHelper.transformTo(notification, endRunnable);
132     }
133 
134     @Override
transformTo(TransformableView notification, float transformationAmount)135     public void transformTo(TransformableView notification, float transformationAmount) {
136         mTransformationHelper.transformTo(notification, transformationAmount);
137     }
138 
139     @Override
transformFrom(TransformableView notification)140     public void transformFrom(TransformableView notification) {
141         mTransformationHelper.transformFrom(notification);
142     }
143 
144     @Override
transformFrom(TransformableView notification, float transformationAmount)145     public void transformFrom(TransformableView notification, float transformationAmount) {
146         mTransformationHelper.transformFrom(notification, transformationAmount);
147     }
148 
149     @Override
setVisible(boolean visible)150     public void setVisible(boolean visible) {
151         setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
152         mTransformationHelper.setVisible(visible);
153     }
154 
155     @Override
setNotificationFaded(boolean faded)156     public void setNotificationFaded(boolean faded) {}
157 
158     protected static class FadeOutAndDownWithTitleTransformation extends
159             ViewTransformationHelper.CustomTransformation {
160 
161         private final View mView;
162 
FadeOutAndDownWithTitleTransformation(View view)163         public FadeOutAndDownWithTitleTransformation(View view) {
164             mView = view;
165         }
166 
167         @Override
transformTo(TransformState ownState, TransformableView notification, float transformationAmount)168         public boolean transformTo(TransformState ownState, TransformableView notification,
169                 float transformationAmount) {
170             // We want to transform to the same y location as the title
171             TransformState otherState = notification.getCurrentState(TRANSFORMING_VIEW_TITLE);
172             CrossFadeHelper.fadeOut(mView, transformationAmount);
173             if (otherState != null) {
174                 ownState.transformViewVerticalTo(otherState, transformationAmount);
175                 otherState.recycle();
176             }
177             return true;
178         }
179 
180         @Override
transformFrom(TransformState ownState, TransformableView notification, float transformationAmount)181         public boolean transformFrom(TransformState ownState,
182                 TransformableView notification, float transformationAmount) {
183             // We want to transform from the same y location as the title
184             TransformState otherState = notification.getCurrentState(TRANSFORMING_VIEW_TITLE);
185             CrossFadeHelper.fadeIn(mView, transformationAmount, true /* remap */);
186             if (otherState != null) {
187                 ownState.transformViewVerticalFrom(otherState, transformationAmount);
188                 otherState.recycle();
189             }
190             return true;
191         }
192     }
193 }
194