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