Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 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 | |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification.row; |
Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 18 | |
| 19 | import android.app.Notification; |
| 20 | import android.content.Context; |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
| 22 | import android.util.TypedValue; |
| 23 | import android.view.ContextThemeWrapper; |
Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 25 | import android.view.ViewGroup; |
| 26 | import android.widget.TextView; |
| 27 | |
| 28 | import com.android.systemui.R; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 29 | import com.android.systemui.statusbar.notification.NotificationDozeHelper; |
| 30 | import com.android.systemui.statusbar.notification.NotificationUtils; |
Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * A class managing hybrid groups that include {@link HybridNotificationView} and the notification |
| 34 | * group overflow. |
| 35 | */ |
| 36 | public class HybridGroupManager { |
| 37 | |
| 38 | private final Context mContext; |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 39 | private final NotificationDozeHelper mDozer; |
| 40 | private final ViewGroup mParent; |
| 41 | |
dongwan0605.kim | 11e7dec | 2018-05-06 18:04:52 +0900 | [diff] [blame] | 42 | private float mOverflowNumberSize; |
| 43 | private int mOverflowNumberPadding; |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 44 | |
Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 45 | private int mOverflowNumberColor; |
| 46 | |
| 47 | public HybridGroupManager(Context ctx, ViewGroup parent) { |
| 48 | mContext = ctx; |
| 49 | mParent = parent; |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 50 | mDozer = new NotificationDozeHelper(); |
dongwan0605.kim | 11e7dec | 2018-05-06 18:04:52 +0900 | [diff] [blame] | 51 | initDimens(); |
| 52 | } |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 53 | |
dongwan0605.kim | 11e7dec | 2018-05-06 18:04:52 +0900 | [diff] [blame] | 54 | public void initDimens() { |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 55 | Resources res = mContext.getResources(); |
| 56 | mOverflowNumberSize = res.getDimensionPixelSize( |
| 57 | R.dimen.group_overflow_number_size); |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 58 | mOverflowNumberPadding = res.getDimensionPixelSize( |
| 59 | R.dimen.group_overflow_number_padding); |
Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 62 | private HybridNotificationView inflateHybridViewWithStyle(int style) { |
| 63 | LayoutInflater inflater = new ContextThemeWrapper(mContext, style) |
| 64 | .getSystemService(LayoutInflater.class); |
Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 65 | 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 Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 81 | numberView.setTextColor(mOverflowNumberColor); |
Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 84 | public void setOverflowNumberColor(TextView numberView, int colorRegular) { |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 85 | mOverflowNumberColor = colorRegular; |
Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 86 | if (numberView != null) { |
| 87 | updateOverFlowNumberColor(numberView); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public HybridNotificationView bindFromNotification(HybridNotificationView reusableView, |
| 92 | Notification notification) { |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 93 | 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 Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 100 | R.style.HybridNotification); |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private HybridNotificationView bindFromNotificationWithStyle( |
| 104 | HybridNotificationView reusableView, Notification notification, int style) { |
Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 105 | if (reusableView == null) { |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 106 | reusableView = inflateHybridViewWithStyle(style); |
Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 107 | } |
| 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 Cinek | ddf1b39 | 2016-05-27 16:33:10 -0700 | [diff] [blame] | 139 | String contentDescription = String.format(mContext.getResources().getQuantityString( |
| 140 | R.plurals.notification_group_overflow_description, number), number); |
| 141 | |
| 142 | reusableView.setContentDescription(contentDescription); |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 143 | reusableView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mOverflowNumberSize); |
| 144 | reusableView.setPaddingRelative(reusableView.getPaddingStart(), |
| 145 | reusableView.getPaddingTop(), mOverflowNumberPadding, |
| 146 | reusableView.getPaddingBottom()); |
| 147 | updateOverFlowNumberColor(reusableView); |
Selim Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 148 | return reusableView; |
| 149 | } |
Adrian Roos | 6f6e159 | 2017-05-02 16:22:53 -0700 | [diff] [blame] | 150 | |
Lucas Dupin | 4c797d6 | 2018-05-07 15:32:13 -0700 | [diff] [blame] | 151 | 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 Cinek | c897bd3 | 2016-03-18 17:32:31 -0700 | [diff] [blame] | 161 | } |