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 android.annotation.Nullable;
20 import android.app.Notification;
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.service.notification.StatusBarNotification;
24 import android.util.TypedValue;
25 import android.view.ContextThemeWrapper;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.TextView;
30 
31 import com.android.internal.widget.ConversationLayout;
32 import com.android.systemui.R;
33 
34 /**
35  * A class managing hybrid groups that include {@link HybridNotificationView} and the notification
36  * group overflow.
37  */
38 public class HybridGroupManager {
39 
40     private final Context mContext;
41 
42     private float mOverflowNumberSize;
43     private int mOverflowNumberPadding;
44 
45     private int mOverflowNumberColor;
46 
HybridGroupManager(Context ctx)47     public HybridGroupManager(Context ctx) {
48         mContext = ctx;
49         initDimens();
50     }
51 
initDimens()52     public void initDimens() {
53         Resources res = mContext.getResources();
54         mOverflowNumberSize = res.getDimensionPixelSize(R.dimen.group_overflow_number_size);
55         mOverflowNumberPadding = res.getDimensionPixelSize(R.dimen.group_overflow_number_padding);
56     }
57 
inflateHybridViewWithStyle(int style, View contentView, ViewGroup parent)58     private HybridNotificationView inflateHybridViewWithStyle(int style,
59             View contentView, ViewGroup parent) {
60         LayoutInflater inflater = new ContextThemeWrapper(mContext, style)
61                 .getSystemService(LayoutInflater.class);
62         int layout = contentView instanceof ConversationLayout
63                 ? R.layout.hybrid_conversation_notification
64                 : R.layout.hybrid_notification;
65         HybridNotificationView hybrid = (HybridNotificationView)
66                 inflater.inflate(layout, parent, false);
67         parent.addView(hybrid);
68         return hybrid;
69     }
70 
inflateOverflowNumber(ViewGroup parent)71     private TextView inflateOverflowNumber(ViewGroup parent) {
72         LayoutInflater inflater = mContext.getSystemService(LayoutInflater.class);
73         TextView numberView = (TextView) inflater.inflate(
74                 R.layout.hybrid_overflow_number, parent, false);
75         parent.addView(numberView);
76         updateOverFlowNumberColor(numberView);
77         return numberView;
78     }
79 
updateOverFlowNumberColor(TextView numberView)80     private void updateOverFlowNumberColor(TextView numberView) {
81         numberView.setTextColor(mOverflowNumberColor);
82     }
83 
setOverflowNumberColor(TextView numberView, int colorRegular)84     public void setOverflowNumberColor(TextView numberView, int colorRegular) {
85         mOverflowNumberColor = colorRegular;
86         if (numberView != null) {
87             updateOverFlowNumberColor(numberView);
88         }
89     }
90 
bindFromNotification(HybridNotificationView reusableView, View contentView, StatusBarNotification notification, ViewGroup parent)91     public HybridNotificationView bindFromNotification(HybridNotificationView reusableView,
92             View contentView, StatusBarNotification notification,
93             ViewGroup parent) {
94         return bindFromNotificationWithStyle(reusableView, contentView, notification,
95                 R.style.HybridNotification, parent);
96     }
97 
bindFromNotificationWithStyle( HybridNotificationView reusableView, View contentView, StatusBarNotification notification, int style, ViewGroup parent)98     private HybridNotificationView bindFromNotificationWithStyle(
99             HybridNotificationView reusableView, View contentView,
100             StatusBarNotification notification,
101             int style, ViewGroup parent) {
102         if (reusableView == null) {
103             reusableView = inflateHybridViewWithStyle(style, contentView, parent);
104         }
105         CharSequence titleText = resolveTitle(notification.getNotification());
106         CharSequence contentText = resolveText(notification.getNotification());
107         reusableView.bind(titleText, contentText, contentView);
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 = String.format(mContext.getResources().getQuantityString(
140                 R.plurals.notification_group_overflow_description, number), 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