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 com.android.internal.util.Preconditions.checkNotNull; 20 21 import android.annotation.Nullable; 22 import android.content.Context; 23 import android.graphics.RectF; 24 import android.util.AttributeSet; 25 import android.view.LayoutInflater; 26 import android.view.MotionEvent; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.ImageView; 30 import android.widget.TextView; 31 32 import com.android.systemui.R; 33 import com.android.systemui.statusbar.notification.row.ActivatableNotificationView; 34 35 /** 36 * Similar in size and appearance to the NotificationShelf, appears at the beginning of some 37 * notification sections. Currently only used for gentle notifications. 38 */ 39 public class SectionHeaderView extends ActivatableNotificationView { 40 private ViewGroup mContents; 41 private TextView mLabelView; 42 private ImageView mClearAllButton; 43 @Nullable private View.OnClickListener mOnClearClickListener = null; 44 45 private final RectF mTmpRect = new RectF(); 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 super.onFinishInflate(); 54 mContents = checkNotNull(findViewById(R.id.content)); 55 bindContents(); 56 } 57 bindContents()58 private void bindContents() { 59 mLabelView = checkNotNull(findViewById(R.id.header_label)); 60 mClearAllButton = checkNotNull(findViewById(R.id.btn_clear_all)); 61 if (mOnClearClickListener != null) { 62 mClearAllButton.setOnClickListener(mOnClearClickListener); 63 } 64 } 65 66 @Override getContentView()67 protected View getContentView() { 68 return mContents; 69 } 70 71 /** 72 * Destroys and reinflates the visible contents of the section header. For use on configuration 73 * changes or any other time that layout values might need to be re-evaluated. 74 * 75 * Does not reinflate the base content view itself ({@link #getContentView()} or any of the 76 * decorator views, such as the background view or shadow view. 77 */ reinflateContents()78 void reinflateContents() { 79 mContents.removeAllViews(); 80 LayoutInflater.from(getContext()).inflate( 81 R.layout.status_bar_notification_section_header_contents, 82 mContents); 83 bindContents(); 84 } 85 86 @Override isTransparent()87 public boolean isTransparent() { 88 return true; 89 } 90 91 /** Must be called whenever the UI mode changes (i.e. when we enter night mode). */ onUiModeChanged()92 void onUiModeChanged() { 93 updateBackgroundColors(); 94 mLabelView.setTextColor( 95 getContext().getColor(R.color.notification_section_header_label_color)); 96 mClearAllButton.setImageResource( 97 R.drawable.status_bar_notification_section_header_clear_btn); 98 } 99 setAreThereDismissableGentleNotifs(boolean areThereDismissableGentleNotifs)100 void setAreThereDismissableGentleNotifs(boolean areThereDismissableGentleNotifs) { 101 mClearAllButton.setVisibility(areThereDismissableGentleNotifs ? View.VISIBLE : View.GONE); 102 } 103 104 @Override disallowSingleClick(MotionEvent event)105 protected boolean disallowSingleClick(MotionEvent event) { 106 // Disallow single click on lockscreen if user is tapping on clear all button 107 mTmpRect.set( 108 mClearAllButton.getLeft(), 109 mClearAllButton.getTop(), 110 mClearAllButton.getLeft() + mClearAllButton.getWidth(), 111 mClearAllButton.getTop() + mClearAllButton.getHeight()); 112 return mTmpRect.contains(event.getX(), event.getY()); 113 } 114 115 /** 116 * Fired whenever the user clicks on the body of the header (e.g. no sub-buttons or anything). 117 */ setOnHeaderClickListener(View.OnClickListener listener)118 void setOnHeaderClickListener(View.OnClickListener listener) { 119 mContents.setOnClickListener(listener); 120 } 121 122 /** Fired when the user clicks on the "X" button on the far right of the header. */ setOnClearAllClickListener(View.OnClickListener listener)123 void setOnClearAllClickListener(View.OnClickListener listener) { 124 mOnClearClickListener = listener; 125 mClearAllButton.setOnClickListener(listener); 126 } 127 } 128