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  * Recents 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_recents_text when the notification
33  * list is not empty and has unseen notifications.
34  */
35 public class CarNotificationRecentsViewHolder extends RecyclerView.ViewHolder {
36     private final TextView mNotificationRecentsText;
37     private final Button mClearAllButton;
38     private final CarNotificationItemController mNotificationItemController;
39     private final boolean mShowRecents;
40 
CarNotificationRecentsViewHolder(Context context, View view, @NonNull CarNotificationItemController notificationItemController)41     public CarNotificationRecentsViewHolder(Context context, View view,
42             @NonNull CarNotificationItemController notificationItemController) {
43         super(view);
44 
45         mNotificationRecentsText = view.findViewById(R.id.notification_recents_text);
46         mClearAllButton = view.findViewById(R.id.clear_all_button);
47         mShowRecents = context.getResources().getBoolean(R.bool.config_showRecentAndOldHeaders);
48         mNotificationItemController = notificationItemController;
49     }
50 
51     /**
52      * Sets up {@link CarNotificationRecentsViewHolder}'s text and clear button functionality
53      *
54      * @param containsNotification {@code true} if notifications exist under this header
55      */
56     @CallSuper
bind(boolean containsNotification)57     public void bind(boolean containsNotification) {
58         if (containsNotification && mShowRecents) {
59             mNotificationRecentsText.setVisibility(View.VISIBLE);
60 
61             if (mClearAllButton == null) {
62                 return;
63             }
64 
65             mClearAllButton.setVisibility(View.VISIBLE);
66             if (!mClearAllButton.hasOnClickListeners()) {
67                 mClearAllButton.setOnClickListener(view -> {
68                     mNotificationItemController.clearAllNotifications();
69                 });
70             }
71             return;
72         }
73 
74         mNotificationRecentsText.setVisibility(View.GONE);
75 
76         if (mClearAllButton != null) {
77             mClearAllButton.setVisibility(View.GONE);
78         }
79     }
80 }
81