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