Home | History | Annotate | Download | only in notification
      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;
     18 
     19 import android.app.Notification;
     20 import android.content.Context;
     21 import android.text.BidiFormatter;
     22 import android.text.Spannable;
     23 import android.text.SpannableStringBuilder;
     24 import android.text.TextUtils;
     25 import android.text.style.TextAppearanceSpan;
     26 import android.view.LayoutInflater;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.widget.TextView;
     30 
     31 import com.android.systemui.R;
     32 import com.android.systemui.statusbar.ExpandableNotificationRow;
     33 
     34 import java.util.List;
     35 
     36 /**
     37  * A class managing hybrid groups that include {@link HybridNotificationView} and the notification
     38  * group overflow.
     39  */
     40 public class HybridGroupManager {
     41 
     42     private final Context mContext;
     43     private ViewGroup mParent;
     44     private int mOverflowNumberColor;
     45 
     46     public HybridGroupManager(Context ctx, ViewGroup parent) {
     47         mContext = ctx;
     48         mParent = parent;
     49     }
     50 
     51     private HybridNotificationView inflateHybridView() {
     52         LayoutInflater inflater = mContext.getSystemService(LayoutInflater.class);
     53         HybridNotificationView hybrid = (HybridNotificationView) inflater.inflate(
     54                 R.layout.hybrid_notification, mParent, false);
     55         mParent.addView(hybrid);
     56         return hybrid;
     57     }
     58 
     59     private TextView inflateOverflowNumber() {
     60         LayoutInflater inflater = mContext.getSystemService(LayoutInflater.class);
     61         TextView numberView = (TextView) inflater.inflate(
     62                 R.layout.hybrid_overflow_number, mParent, false);
     63         mParent.addView(numberView);
     64         updateOverFlowNumberColor(numberView);
     65         return numberView;
     66     }
     67 
     68     private void updateOverFlowNumberColor(TextView numberView) {
     69         numberView.setTextColor(mOverflowNumberColor);
     70     }
     71 
     72     public void setOverflowNumberColor(TextView numberView, int overflowNumberColor) {
     73         mOverflowNumberColor = overflowNumberColor;
     74         if (numberView != null) {
     75             updateOverFlowNumberColor(numberView);
     76         }
     77     }
     78 
     79     public HybridNotificationView bindFromNotification(HybridNotificationView reusableView,
     80             Notification notification) {
     81         if (reusableView == null) {
     82             reusableView = inflateHybridView();
     83         }
     84         CharSequence titleText = resolveTitle(notification);
     85         CharSequence contentText = resolveText(notification);
     86         reusableView.bind(titleText, contentText);
     87         return reusableView;
     88     }
     89 
     90     private CharSequence resolveText(Notification notification) {
     91         CharSequence contentText = notification.extras.getCharSequence(Notification.EXTRA_TEXT);
     92         if (contentText == null) {
     93             contentText = notification.extras.getCharSequence(Notification.EXTRA_BIG_TEXT);
     94         }
     95         return contentText;
     96     }
     97 
     98     private CharSequence resolveTitle(Notification notification) {
     99         CharSequence titleText = notification.extras.getCharSequence(Notification.EXTRA_TITLE);
    100         if (titleText == null) {
    101             titleText = notification.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
    102         }
    103         return titleText;
    104     }
    105 
    106     public TextView bindOverflowNumber(TextView reusableView, int number) {
    107         if (reusableView == null) {
    108             reusableView = inflateOverflowNumber();
    109         }
    110         String text = mContext.getResources().getString(
    111                 R.string.notification_group_overflow_indicator, number);
    112         if (!text.equals(reusableView.getText())) {
    113             reusableView.setText(text);
    114         }
    115         String contentDescription = String.format(mContext.getResources().getQuantityString(
    116                 R.plurals.notification_group_overflow_description, number), number);
    117 
    118         reusableView.setContentDescription(contentDescription);
    119         return reusableView;
    120     }
    121 }
    122