blob: 90ff4a728915993675c10d9d5691f666e7e39e0c [file] [log] [blame]
Selim Cinekc897bd32016-03-18 17:32:31 -07001/*
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
Rohan Shah20790b82018-07-02 17:21:04 -070017package com.android.systemui.statusbar.notification.row;
Selim Cinekc897bd32016-03-18 17:32:31 -070018
19import android.app.Notification;
20import android.content.Context;
Adrian Roos6f6e1592017-05-02 16:22:53 -070021import android.content.res.Resources;
22import android.util.TypedValue;
23import android.view.ContextThemeWrapper;
Selim Cinekc897bd32016-03-18 17:32:31 -070024import android.view.LayoutInflater;
Selim Cinekc897bd32016-03-18 17:32:31 -070025import android.view.ViewGroup;
26import android.widget.TextView;
27
28import com.android.systemui.R;
Rohan Shah20790b82018-07-02 17:21:04 -070029import com.android.systemui.statusbar.notification.NotificationDozeHelper;
30import com.android.systemui.statusbar.notification.NotificationUtils;
Selim Cinekc897bd32016-03-18 17:32:31 -070031
32/**
33 * A class managing hybrid groups that include {@link HybridNotificationView} and the notification
34 * group overflow.
35 */
36public class HybridGroupManager {
37
38 private final Context mContext;
Adrian Roos6f6e1592017-05-02 16:22:53 -070039 private final NotificationDozeHelper mDozer;
40 private final ViewGroup mParent;
41
dongwan0605.kim11e7dec2018-05-06 18:04:52 +090042 private float mOverflowNumberSize;
43 private int mOverflowNumberPadding;
Adrian Roos6f6e1592017-05-02 16:22:53 -070044
Selim Cinekc897bd32016-03-18 17:32:31 -070045 private int mOverflowNumberColor;
46
47 public HybridGroupManager(Context ctx, ViewGroup parent) {
48 mContext = ctx;
49 mParent = parent;
Adrian Roos6f6e1592017-05-02 16:22:53 -070050 mDozer = new NotificationDozeHelper();
dongwan0605.kim11e7dec2018-05-06 18:04:52 +090051 initDimens();
52 }
Adrian Roos6f6e1592017-05-02 16:22:53 -070053
dongwan0605.kim11e7dec2018-05-06 18:04:52 +090054 public void initDimens() {
Adrian Roos6f6e1592017-05-02 16:22:53 -070055 Resources res = mContext.getResources();
56 mOverflowNumberSize = res.getDimensionPixelSize(
57 R.dimen.group_overflow_number_size);
Adrian Roos6f6e1592017-05-02 16:22:53 -070058 mOverflowNumberPadding = res.getDimensionPixelSize(
59 R.dimen.group_overflow_number_padding);
Selim Cinekc897bd32016-03-18 17:32:31 -070060 }
61
Adrian Roos6f6e1592017-05-02 16:22:53 -070062 private HybridNotificationView inflateHybridViewWithStyle(int style) {
63 LayoutInflater inflater = new ContextThemeWrapper(mContext, style)
64 .getSystemService(LayoutInflater.class);
Selim Cinekc897bd32016-03-18 17:32:31 -070065 HybridNotificationView hybrid = (HybridNotificationView) inflater.inflate(
66 R.layout.hybrid_notification, mParent, false);
67 mParent.addView(hybrid);
68 return hybrid;
69 }
70
71 private TextView inflateOverflowNumber() {
72 LayoutInflater inflater = mContext.getSystemService(LayoutInflater.class);
73 TextView numberView = (TextView) inflater.inflate(
74 R.layout.hybrid_overflow_number, mParent, false);
75 mParent.addView(numberView);
76 updateOverFlowNumberColor(numberView);
77 return numberView;
78 }
79
80 private void updateOverFlowNumberColor(TextView numberView) {
Lucas Dupin00be88f2019-01-03 17:50:52 -080081 numberView.setTextColor(mOverflowNumberColor);
Selim Cinekc897bd32016-03-18 17:32:31 -070082 }
83
Lucas Dupin00be88f2019-01-03 17:50:52 -080084 public void setOverflowNumberColor(TextView numberView, int colorRegular) {
Adrian Roos6f6e1592017-05-02 16:22:53 -070085 mOverflowNumberColor = colorRegular;
Selim Cinekc897bd32016-03-18 17:32:31 -070086 if (numberView != null) {
87 updateOverFlowNumberColor(numberView);
88 }
89 }
90
91 public HybridNotificationView bindFromNotification(HybridNotificationView reusableView,
92 Notification notification) {
Adrian Roos6f6e1592017-05-02 16:22:53 -070093 return bindFromNotificationWithStyle(reusableView, notification,
94 R.style.HybridNotification);
95 }
96
97 public HybridNotificationView bindAmbientFromNotification(HybridNotificationView reusableView,
98 Notification notification) {
99 return bindFromNotificationWithStyle(reusableView, notification,
Lucas Dupin00be88f2019-01-03 17:50:52 -0800100 R.style.HybridNotification);
Adrian Roos6f6e1592017-05-02 16:22:53 -0700101 }
102
103 private HybridNotificationView bindFromNotificationWithStyle(
104 HybridNotificationView reusableView, Notification notification, int style) {
Selim Cinekc897bd32016-03-18 17:32:31 -0700105 if (reusableView == null) {
Adrian Roos6f6e1592017-05-02 16:22:53 -0700106 reusableView = inflateHybridViewWithStyle(style);
Selim Cinekc897bd32016-03-18 17:32:31 -0700107 }
108 CharSequence titleText = resolveTitle(notification);
109 CharSequence contentText = resolveText(notification);
110 reusableView.bind(titleText, contentText);
111 return reusableView;
112 }
113
114 private CharSequence resolveText(Notification notification) {
115 CharSequence contentText = notification.extras.getCharSequence(Notification.EXTRA_TEXT);
116 if (contentText == null) {
117 contentText = notification.extras.getCharSequence(Notification.EXTRA_BIG_TEXT);
118 }
119 return contentText;
120 }
121
122 private CharSequence resolveTitle(Notification notification) {
123 CharSequence titleText = notification.extras.getCharSequence(Notification.EXTRA_TITLE);
124 if (titleText == null) {
125 titleText = notification.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
126 }
127 return titleText;
128 }
129
130 public TextView bindOverflowNumber(TextView reusableView, int number) {
131 if (reusableView == null) {
132 reusableView = inflateOverflowNumber();
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 }
Selim Cinekddf1b392016-05-27 16:33:10 -0700139 String contentDescription = String.format(mContext.getResources().getQuantityString(
140 R.plurals.notification_group_overflow_description, number), number);
141
142 reusableView.setContentDescription(contentDescription);
Lucas Dupin00be88f2019-01-03 17:50:52 -0800143 reusableView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mOverflowNumberSize);
144 reusableView.setPaddingRelative(reusableView.getPaddingStart(),
145 reusableView.getPaddingTop(), mOverflowNumberPadding,
146 reusableView.getPaddingBottom());
147 updateOverFlowNumberColor(reusableView);
Selim Cinekc897bd32016-03-18 17:32:31 -0700148 return reusableView;
149 }
Adrian Roos6f6e1592017-05-02 16:22:53 -0700150
Lucas Dupin4c797d62018-05-07 15:32:13 -0700151 public TextView bindOverflowNumberAmbient(TextView titleView, Notification notification,
152 int number) {
153 String text = mContext.getResources().getString(
154 R.string.notification_group_overflow_indicator_ambient,
155 resolveTitle(notification), number);
156 if (!text.equals(titleView.getText())) {
157 titleView.setText(text);
158 }
159 return titleView;
160 }
Selim Cinekc897bd32016-03-18 17:32:31 -0700161}