1 /*
2  * Copyright (C) 2021 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 package com.android.car.notification.template;
17 
18 import android.annotation.CallSuper;
19 import android.content.Context;
20 import android.view.View;
21 import android.widget.Button;
22 import android.widget.TextView;
23 
24 import androidx.annotation.NonNull;
25 import androidx.recyclerview.widget.RecyclerView;
26 
27 import com.android.car.notification.CarNotificationItemController;
28 import com.android.car.notification.R;
29 
30 /**
31  * Older Header template for the notification shade. This templates supports the clear all button
32  * with ID clear_all_button, a header text with ID notification_older_text when the notification
33  * list is not empty and has seen notifications. Clear all button is only shown if no unseen
34  * notifications are present.
35  */
36 public class CarNotificationOlderViewHolder extends RecyclerView.ViewHolder {
37     private final TextView mNotificationOlderText;
38     private final Button mClearAllButton;
39     private final CarNotificationItemController mNotificationItemController;
40     private final boolean mShowOlder;
41     private final float mAlpha;
42 
CarNotificationOlderViewHolder(Context context, View view, @NonNull CarNotificationItemController notificationItemController)43     public CarNotificationOlderViewHolder(Context context, View view,
44             @NonNull CarNotificationItemController notificationItemController) {
45         super(view);
46 
47         mNotificationOlderText = view.findViewById(R.id.notification_older_text);
48         mClearAllButton = view.findViewById(R.id.clear_all_button);
49         mShowOlder = context.getResources().getBoolean(R.bool.config_showRecentAndOldHeaders);
50         mNotificationItemController = notificationItemController;
51         mAlpha = context.getResources().getFloat(R.dimen.config_olderNotificationsAlpha);
52 
53         if (mNotificationOlderText != null) {
54             mNotificationOlderText.setAlpha(mAlpha);
55         }
56         if (mClearAllButton != null) {
57             mClearAllButton.setOnClickListener(
58                     v -> mNotificationItemController.clearAllNotifications());
59             mClearAllButton.setAlpha(mAlpha);
60         }
61     }
62 
63     /**
64      * Sets up {@link CarNotificationOlderViewHolder}'s text and clear button functionality
65      *
66      * @param containsNotification {@code true} if notifications exist under this header
67      * @param showClearAllButton {@code true} if clear button should be visible
68      */
69     @CallSuper
bind(boolean containsNotification, boolean showClearAllButton)70     public void bind(boolean containsNotification, boolean showClearAllButton) {
71         if (containsNotification && mShowOlder) {
72             mNotificationOlderText.setVisibility(View.VISIBLE);
73 
74             if (mClearAllButton == null) {
75                 return;
76             }
77 
78             if (showClearAllButton) {
79                 mClearAllButton.setVisibility(View.VISIBLE);
80             } else {
81                 mClearAllButton.setVisibility(View.GONE);
82             }
83             return;
84         }
85 
86         mNotificationOlderText.setVisibility(View.GONE);
87         if (mClearAllButton != null) {
88             mClearAllButton.setVisibility(View.GONE);
89         }
90     }
91 }
92