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.app.ActivityManager;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.UserHandle;
23 import android.provider.Settings;
24 import android.view.View;
25 import android.widget.Button;
26 import android.widget.TextView;
27 
28 import androidx.recyclerview.widget.RecyclerView;
29 
30 import com.android.car.notification.CarNotificationItemController;
31 import com.android.car.notification.NotificationClickHandlerFactory;
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 
CarNotificationHeaderViewHolder(Context context, View view, CarNotificationItemController notificationItemController, NotificationClickHandlerFactory notificationClickHandlerFactory)49     public CarNotificationHeaderViewHolder(Context context, View view,
50             CarNotificationItemController notificationItemController,
51             NotificationClickHandlerFactory notificationClickHandlerFactory) {
52         super(view);
53 
54         if (notificationItemController == null) {
55             throw new IllegalArgumentException(
56                     "com.android.car.notification.template.CarNotificationHeaderViewHolder did not "
57                             + "receive NotificationItemController from the Adapter.");
58         }
59 
60         mContext = context;
61         mClickHandlerFactory = notificationClickHandlerFactory;
62         mNotificationHeaderText = view.findViewById(R.id.notification_header_text);
63         mClearAllButton = view.findViewById(R.id.clear_all_button);
64         mShowHeader = context.getResources().getBoolean(R.bool.config_showHeaderForNotifications);
65         mNotificationItemController = notificationItemController;
66         mShowRecentsAndOlderHeaders =
67                 context.getResources().getBoolean(R.bool.config_showRecentAndOldHeaders);
68     }
69 
70     @CallSuper
bind(boolean containsNotification)71     public void bind(boolean containsNotification) {
72         if (containsNotification && mShowHeader) {
73             mNotificationHeaderText.setVisibility(View.VISIBLE);
74 
75             if (mClearAllButton == null) {
76                 return;
77             }
78 
79             mClearAllButton.setVisibility(View.VISIBLE);
80             if (mShowRecentsAndOlderHeaders) {
81                 mClearAllButton.setText(R.string.manage_text);
82                 if (!mClearAllButton.hasOnClickListeners()) {
83                     mClearAllButton.setOnClickListener(this::manageButtonOnClickListener);
84                 }
85             } else {
86                 if (!mClearAllButton.hasOnClickListeners()) {
87                     mClearAllButton.setOnClickListener(view -> {
88                         mNotificationItemController.clearAllNotifications();
89                     });
90                 }
91             }
92             return;
93         }
94 
95         mNotificationHeaderText.setVisibility(View.GONE);
96 
97         if (mClearAllButton != null) {
98             mClearAllButton.setVisibility(View.GONE);
99         }
100     }
101 
manageButtonOnClickListener(View v)102     private void manageButtonOnClickListener(View v) {
103         Intent intent = new Intent(Settings.ACTION_NOTIFICATION_SETTINGS);
104         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
105         intent.addCategory(Intent.CATEGORY_DEFAULT);
106         mContext.startActivityAsUser(intent, UserHandle.of(ActivityManager.getCurrentUser()));
107 
108         if (mClickHandlerFactory != null) mClickHandlerFactory.collapsePanel();
109     }
110 }
111