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 = HybridNotificationView.getLayoutResource( 67 /* isConversation = */ contentView instanceof ConversationLayout); 68 HybridNotificationView hybrid = (HybridNotificationView) 69 inflater.inflate(layout, parent, false); 70 parent.addView(hybrid); 71 Trace.endSection(); 72 return hybrid; 73 } 74 inflateOverflowNumber(ViewGroup parent)75 private TextView inflateOverflowNumber(ViewGroup parent) { 76 LayoutInflater inflater = mContext.getSystemService(LayoutInflater.class); 77 TextView numberView = (TextView) inflater.inflate( 78 R.layout.hybrid_overflow_number, parent, false); 79 parent.addView(numberView); 80 updateOverFlowNumberColor(numberView); 81 return numberView; 82 } 83 updateOverFlowNumberColor(TextView numberView)84 private void updateOverFlowNumberColor(TextView numberView) { 85 numberView.setTextColor(mOverflowNumberColor); 86 } 87 setOverflowNumberColor(TextView numberView, int colorRegular)88 public void setOverflowNumberColor(TextView numberView, int colorRegular) { 89 mOverflowNumberColor = colorRegular; 90 if (numberView != null) { 91 updateOverFlowNumberColor(numberView); 92 } 93 } 94 bindFromNotification(HybridNotificationView reusableView, View contentView, StatusBarNotification notification, ViewGroup parent)95 public HybridNotificationView bindFromNotification(HybridNotificationView reusableView, 96 View contentView, StatusBarNotification notification, 97 ViewGroup parent) { 98 AsyncHybridViewInflation.assertInLegacyMode(); 99 boolean isNewView = false; 100 if (reusableView == null) { 101 Trace.beginSection("HybridGroupManager#bindFromNotification"); 102 reusableView = inflateHybridView(contentView, parent); 103 isNewView = true; 104 } 105 106 updateReusableView(reusableView, notification, contentView); 107 if (isNewView) { 108 Trace.endSection(); 109 } 110 return reusableView; 111 } 112 113 /** 114 * Update the HybridNotificationView (single-line view)'s appearance 115 */ updateReusableView(HybridNotificationView reusableView, StatusBarNotification notification, View contentView)116 public void updateReusableView(HybridNotificationView reusableView, 117 StatusBarNotification notification, View contentView) { 118 AsyncHybridViewInflation.assertInLegacyMode(); 119 final CharSequence titleText = resolveTitle(notification.getNotification()); 120 final CharSequence contentText = resolveText(notification.getNotification()); 121 if (reusableView != null) { 122 reusableView.bind(titleText, contentText, contentView); 123 } 124 } 125 126 @Nullable resolveText(Notification notification)127 public static CharSequence resolveText(Notification notification) { 128 CharSequence contentText = notification.extras.getCharSequence(Notification.EXTRA_TEXT); 129 if (contentText == null) { 130 contentText = notification.extras.getCharSequence(Notification.EXTRA_BIG_TEXT); 131 } 132 return contentText; 133 } 134 135 @Nullable resolveTitle(Notification notification)136 public static CharSequence resolveTitle(Notification notification) { 137 CharSequence titleText = notification.extras.getCharSequence(Notification.EXTRA_TITLE); 138 if (titleText == null) { 139 titleText = notification.extras.getCharSequence(Notification.EXTRA_TITLE_BIG); 140 } 141 return titleText; 142 } 143 bindOverflowNumber(TextView reusableView, int number, ViewGroup parent)144 public TextView bindOverflowNumber(TextView reusableView, int number, 145 ViewGroup parent) { 146 if (reusableView == null) { 147 reusableView = inflateOverflowNumber(parent); 148 } 149 String text = mContext.getResources().getString( 150 R.string.notification_group_overflow_indicator, number); 151 if (!text.equals(reusableView.getText())) { 152 reusableView.setText(text); 153 } 154 String contentDescription = icuMessageFormat(mContext.getResources(), 155 R.string.notification_group_overflow_description, number); 156 157 reusableView.setContentDescription(contentDescription); 158 reusableView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mOverflowNumberSize); 159 reusableView.setPaddingRelative(reusableView.getPaddingStart(), 160 reusableView.getPaddingTop(), mOverflowNumberPadding, 161 reusableView.getPaddingBottom()); 162 updateOverFlowNumberColor(reusableView); 163 return reusableView; 164 } 165 } 166