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 package com.android.car.notification.template;
17 
18 import android.annotation.CallSuper;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.UserHandle;
22 import android.provider.Settings;
23 import android.view.View;
24 import android.widget.Button;
25 import android.widget.TextView;
26 
27 import androidx.recyclerview.widget.RecyclerView;
28 
29 import com.android.car.notification.CarNotificationItemController;
30 import com.android.car.notification.NotificationClickHandlerFactory;
31 import com.android.car.notification.NotificationUtils;
32 import com.android.car.notification.R;
33 
34 /**
35  * Header template for the notification shade. This templates supports the clear all button with id
36  * clear_all_button, a header text with id notification_header_text when the notification list is
37  * not empty and a secondary header text with id empty_notification_header_text when notification
38  * list is empty.
39  */
40 public class CarNotificationHeaderViewHolder extends RecyclerView.ViewHolder {
41     private final Context mContext;
42     private final TextView mNotificationHeaderText;
43     private final Button mClearAllButton;
44     private final CarNotificationItemController mNotificationItemController;
45     private final boolean mShowHeader;
46     private final boolean mShowRecentsAndOlderHeaders;
47     private final NotificationClickHandlerFactory mClickHandlerFactory;
48     private final boolean mCollapsePanelAfterManageButton;
49 
CarNotificationHeaderViewHolder(Context context, View view, CarNotificationItemController notificationItemController, NotificationClickHandlerFactory notificationClickHandlerFactory)50     public CarNotificationHeaderViewHolder(Context context, View view,
51             CarNotificationItemController notificationItemController,
52             NotificationClickHandlerFactory notificationClickHandlerFactory) {
53         super(view);
54 
55         if (notificationItemController == null) {
56             throw new IllegalArgumentException(
57                     "com.android.car.notification.template.CarNotificationHeaderViewHolder did not "
58                             + "receive NotificationItemController from the Adapter.");
59         }
60 
61         mContext = context;
62         mClickHandlerFactory = notificationClickHandlerFactory;
63         mNotificationHeaderText = view.findViewById(R.id.notification_header_text);
64         mClearAllButton = view.findViewById(R.id.clear_all_button);
65         mShowHeader = context.getResources().getBoolean(R.bool.config_showHeaderForNotifications);
66         mNotificationItemController = notificationItemController;
67         mShowRecentsAndOlderHeaders =
68                 context.getResources().getBoolean(R.bool.config_showRecentAndOldHeaders);
69         mCollapsePanelAfterManageButton = context.getResources().getBoolean(
70                 R.bool.config_collapseShadePanelAfterManageButtonPress);
71     }
72 
73     @CallSuper
bind(boolean containsNotification)74     public void bind(boolean containsNotification) {
75         if (containsNotification && mShowHeader) {
76             mNotificationHeaderText.setVisibility(View.VISIBLE);
77 
78             if (mClearAllButton == null) {
79                 return;
80             }
81 
82             mClearAllButton.setVisibility(View.VISIBLE);
83             if (mShowRecentsAndOlderHeaders) {
84                 mClearAllButton.setText(R.string.manage_text);
85                 if (!mClearAllButton.hasOnClickListeners()) {
86                     mClearAllButton.setOnClickListener(this::manageButtonOnClickListener);
87                 }
88             } else {
89                 if (!mClearAllButton.hasOnClickListeners()) {
90                     mClearAllButton.setOnClickListener(view -> {
91                         mNotificationItemController.clearAllNotifications();
92                     });
93                 }
94             }
95             return;
96         }
97 
98         mNotificationHeaderText.setVisibility(View.GONE);
99 
100         if (mClearAllButton != null) {
101             mClearAllButton.setVisibility(View.GONE);
102         }
103     }
104 
manageButtonOnClickListener(View v)105     private void manageButtonOnClickListener(View v) {
106         Intent intent = new Intent(Settings.ACTION_NOTIFICATION_SETTINGS);
107         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
108         intent.addCategory(Intent.CATEGORY_DEFAULT);
109         mContext.startActivityAsUser(intent,
110                 UserHandle.of(NotificationUtils.getCurrentUser(mContext)));
111 
112         if (mClickHandlerFactory != null && mCollapsePanelAfterManageButton) {
113             mClickHandlerFactory.collapsePanel();
114         }
115     }
116 }
117