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