1 /*
2  * Copyright (C) 2019 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.stack;
18 
19 import android.annotation.ColorInt;
20 import android.annotation.Nullable;
21 import android.annotation.StringRes;
22 import android.content.Context;
23 import android.content.res.ColorStateList;
24 import android.util.AttributeSet;
25 import android.view.MotionEvent;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.ImageView;
29 import android.widget.TextView;
30 
31 import com.android.systemui.res.R;
32 import com.android.systemui.statusbar.notification.row.StackScrollerDecorView;
33 
34 /**
35  * Header displayed above a notification section in the shade. Currently used for Alerting and
36  * Silent sections.
37  */
38 public class SectionHeaderView extends StackScrollerDecorView {
39 
40     private ViewGroup mContents;
41     private TextView mLabelView;
42     private ImageView mClearAllButton;
43     @StringRes @Nullable private Integer mLabelTextId;
44     @Nullable private View.OnClickListener mLabelClickListener = null;
45     @Nullable private View.OnClickListener mOnClearClickListener = null;
46 
SectionHeaderView(Context context, AttributeSet attrs)47     public SectionHeaderView(Context context, AttributeSet attrs) {
48         super(context, attrs);
49     }
50 
51     @Override
onFinishInflate()52     protected void onFinishInflate() {
53         mContents = requireViewById(R.id.content);
54         bindContents();
55         super.onFinishInflate();
56         setVisible(true /* visible */, false /* animate */);
57     }
58 
bindContents()59     private void bindContents() {
60         mLabelView = requireViewById(R.id.header_label);
61         mClearAllButton = requireViewById(R.id.btn_clear_all);
62         if (mOnClearClickListener != null) {
63             mClearAllButton.setOnClickListener(mOnClearClickListener);
64         }
65         if (mLabelClickListener != null) {
66             mLabelView.setOnClickListener(mLabelClickListener);
67         }
68         if (mLabelTextId != null) {
69             mLabelView.setText(mLabelTextId);
70         }
71     }
72 
73     @Override
findContentView()74     protected View findContentView() {
75         return mContents;
76     }
77 
78     @Override
findSecondaryView()79     protected View findSecondaryView() {
80         return null;
81     }
82 
83     @Override
isTransparent()84     public boolean isTransparent() {
85         return true;
86     }
87 
88     /**
89      * Show the clear section [X] button
90      * @param enabled
91      */
setClearSectionButtonEnabled(boolean enabled)92     public void setClearSectionButtonEnabled(boolean enabled) {
93         mClearAllButton.setVisibility(enabled ? View.VISIBLE : View.GONE);
94     }
95 
96     @Override
onInterceptTouchEvent(MotionEvent ev)97     public boolean onInterceptTouchEvent(MotionEvent ev) {
98         return super.onInterceptTouchEvent(ev);
99     }
100 
101     /**
102      * Fired whenever the user clicks on the body of the header (e.g. no sub-buttons or anything).
103      */
setOnHeaderClickListener(View.OnClickListener listener)104     public void setOnHeaderClickListener(View.OnClickListener listener) {
105         mLabelClickListener = listener;
106         mLabelView.setOnClickListener(listener);
107     }
108 
109     @Override
applyContentTransformation(float contentAlpha, float translationY)110     protected void applyContentTransformation(float contentAlpha, float translationY) {
111         super.applyContentTransformation(contentAlpha, translationY);
112         mLabelView.setAlpha(contentAlpha);
113         mLabelView.setTranslationY(translationY);
114         mClearAllButton.setAlpha(contentAlpha);
115         mClearAllButton.setTranslationY(translationY);
116     }
117 
118     /** Fired when the user clicks on the "X" button on the far right of the header. */
setOnClearAllClickListener(View.OnClickListener listener)119     public void setOnClearAllClickListener(View.OnClickListener listener) {
120         mOnClearClickListener = listener;
121         mClearAllButton.setOnClickListener(listener);
122     }
123 
124     @Override
needsClippingToShelf()125     public boolean needsClippingToShelf() {
126         return true;
127     }
128 
129     /** Sets text to be displayed in the header */
setHeaderText(@tringRes int resId)130     public void setHeaderText(@StringRes int resId) {
131         mLabelTextId = resId;
132         mLabelView.setText(resId);
133     }
134 
setForegroundColors(@olorInt int onSurface, @ColorInt int onSurfaceVariant)135     void setForegroundColors(@ColorInt int onSurface, @ColorInt int onSurfaceVariant) {
136         mLabelView.setTextColor(onSurface);
137         mClearAllButton.setImageTintList(ColorStateList.valueOf(onSurfaceVariant));
138     }
139 }
140