1 /*
2  * Copyright (C) 2015 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.row;
18 
19 import static com.android.systemui.util.PluralMessageFormaterKt.icuMessageFormat;
20 
21 import android.annotation.Nullable;
22 import android.app.Notification;
23 import android.content.Context;
24 import android.content.res.Resources;
25 import android.os.Trace;
26 import android.service.notification.StatusBarNotification;
27 import android.util.TypedValue;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.TextView;
32 
33 import com.android.internal.widget.ConversationLayout;
34 import com.android.systemui.R;
35 
36 /**
37  * A class managing hybrid groups that include {@link HybridNotificationView} and the notification
38  * group overflow.
39  */
40 public class HybridGroupManager {
41 
42     private final Context mContext;
43 
44     private float mOverflowNumberSize;
45     private int mOverflowNumberPadding;
46 
47     private int mOverflowNumberColor;
48 
HybridGroupManager(Context ctx)49     public HybridGroupManager(Context ctx) {
50         mContext = ctx;
51         initDimens();
52     }
53 
initDimens()54     public void initDimens() {
55         Resources res = mContext.getResources();
56         mOverflowNumberSize = res.getDimensionPixelSize(R.dimen.group_overflow_number_size);
57         mOverflowNumberPadding = res.getDimensionPixelSize(R.dimen.group_overflow_number_padding);
58     }
59 
inflateHybridView(View contentView, ViewGroup parent)60     private HybridNotificationView inflateHybridView(View contentView, ViewGroup parent) {
61         Trace.beginSection("HybridGroupManager#inflateHybridView");
62         LayoutInflater inflater = LayoutInflater.from(mContext);
63         int layout = contentView instanceof ConversationLayout
64                 ? R.layout.hybrid_conversation_notification
65                 : R.layout.hybrid_notification;
66         HybridNotificationView hybrid = (HybridNotificationView)
67                 inflater.inflate(layout, parent, false);
68         parent.addView(hybrid);
69         Trace.endSection();
70         return hybrid;
71     }
72 
inflateOverflowNumber(ViewGroup parent)73     private TextView inflateOverflowNumber(ViewGroup parent) {
74         LayoutInflater inflater = mContext.getSystemService(LayoutInflater.class);
75         TextView numberView = (TextView) inflater.inflate(
76                 R.layout.hybrid_overflow_number, parent, false);
77         parent.addView(numberView);
78         updateOverFlowNumberColor(numberView);
79         return numberView;
80     }
81 
updateOverFlowNumberColor(TextView numberView)82     private void updateOverFlowNumberColor(TextView numberView) {
83         numberView.setTextColor(mOverflowNumberColor);
84     }
85 
setOverflowNumberColor(TextView numberView, int colorRegular)86     public void setOverflowNumberColor(TextView numberView, int colorRegular) {
87         mOverflowNumberColor = colorRegular;
88         if (numberView != null) {
89             updateOverFlowNumberColor(numberView);
90         }
91     }
92 
bindFromNotification(HybridNotificationView reusableView, View contentView, StatusBarNotification notification, ViewGroup parent)93     public HybridNotificationView bindFromNotification(HybridNotificationView reusableView,
94             View contentView, StatusBarNotification notification,
95             ViewGroup parent) {
96         boolean isNewView = false;
97         if (reusableView == null) {
98             Trace.beginSection("HybridGroupManager#bindFromNotification");
99             reusableView = inflateHybridView(contentView, parent);
100             isNewView = true;
101         }
102         CharSequence titleText = resolveTitle(notification.getNotification());
103         CharSequence contentText = resolveText(notification.getNotification());
104         reusableView.bind(titleText, contentText, contentView);
105         if (isNewView) {
106             Trace.endSection();
107         }
108         return reusableView;
109     }
110 
111     @Nullable
resolveText(Notification notification)112     public static CharSequence resolveText(Notification notification) {
113         CharSequence contentText = notification.extras.getCharSequence(Notification.EXTRA_TEXT);
114         if (contentText == null) {
115             contentText = notification.extras.getCharSequence(Notification.EXTRA_BIG_TEXT);
116         }
117         return contentText;
118     }
119 
120     @Nullable
resolveTitle(Notification notification)121     public static CharSequence resolveTitle(Notification notification) {
122         CharSequence titleText = notification.extras.getCharSequence(Notification.EXTRA_TITLE);
123         if (titleText == null) {
124             titleText = notification.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
125         }
126         return titleText;
127     }
128 
bindOverflowNumber(TextView reusableView, int number, ViewGroup parent)129     public TextView bindOverflowNumber(TextView reusableView, int number,
130             ViewGroup parent) {
131         if (reusableView == null) {
132             reusableView = inflateOverflowNumber(parent);
133         }
134         String text = mContext.getResources().getString(
135                 R.string.notification_group_overflow_indicator, number);
136         if (!text.equals(reusableView.getText())) {
137             reusableView.setText(text);
138         }
139         String contentDescription = icuMessageFormat(mContext.getResources(),
140                 R.string.notification_group_overflow_description, number);
141 
142         reusableView.setContentDescription(contentDescription);
143         reusableView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mOverflowNumberSize);
144         reusableView.setPaddingRelative(reusableView.getPaddingStart(),
145                 reusableView.getPaddingTop(), mOverflowNumberPadding,
146                 reusableView.getPaddingBottom());
147         updateOverFlowNumberColor(reusableView);
148         return reusableView;
149     }
150 }
151