Home | History | Annotate | Download | only in browse
      1 package com.android.mail.browse;
      2 
      3 import android.content.Context;
      4 import android.graphics.drawable.Drawable;
      5 import android.text.Spannable;
      6 import android.text.SpannableString;
      7 import android.text.style.ImageSpan;
      8 import android.util.AttributeSet;
      9 import android.widget.TextView;
     10 
     11 import com.android.emailcommon.mail.Address;
     12 import com.android.mail.R;
     13 import com.android.mail.providers.Message;
     14 import com.android.mail.providers.UIProvider;
     15 import com.android.mail.utils.Utils;
     16 
     17 public class SpamWarningView extends TextView {
     18     // Prefix added to the Spam warning text so that ImageSpan overrides it to
     19     // display the alert icon.
     20     private static final String SPANNABLE_SPAM_WARNING_PREFIX = ".";
     21 
     22     private final int mHighWarningColor;
     23     private final int mLowWarningColor;
     24     private final int mHighWarningBackgroundColor;
     25     private final int mLowWarningBackgroundColor;
     26 
     27     public SpamWarningView(Context context) {
     28         this(context, null);
     29     }
     30 
     31     public SpamWarningView(Context context, AttributeSet attrs) {
     32         super(context, attrs);
     33         mHighWarningColor = getResources().getColor(R.color.high_spam_color);
     34         mHighWarningBackgroundColor = getResources().getColor(
     35             R.color.high_spam_warning_background_color);
     36         mLowWarningColor = getResources().getColor(R.color.low_spam_color);
     37         mLowWarningBackgroundColor = getResources().getColor(
     38             R.color.low_spam_warning_background_color);
     39     }
     40 
     41     public void showSpamWarning(Message message, Address sender) {
     42         setVisibility(VISIBLE);
     43 
     44         // Sets the text and adds any necessary formatting
     45         // to enable the proper display.
     46         final String senderAddress = sender.getAddress();
     47         final String senderDomain = senderAddress.substring(senderAddress.indexOf('@')+1);
     48         final String spamWarningText = Utils.convertHtmlToPlainText(String.format(
     49                 message.spamWarningString, senderAddress, senderDomain));
     50         final int alertIconResourceId;
     51         if (message.spamWarningLevel == UIProvider.SpamWarningLevel.HIGH_WARNING) {
     52             setBackgroundColor(mHighWarningBackgroundColor);
     53             setTextColor(mHighWarningColor);
     54             alertIconResourceId = R.drawable.ic_warning_white;
     55         } else {
     56             setBackgroundColor(mLowWarningBackgroundColor);
     57             setTextColor(mLowWarningColor);
     58             alertIconResourceId = R.drawable.ic_warning_gray;
     59         }
     60         final Drawable alertIcon = getResources().getDrawable(alertIconResourceId);
     61         alertIcon.setBounds(0, 0, alertIcon.getIntrinsicWidth(), alertIcon.getIntrinsicHeight());
     62         ImageSpan imageSpan = new ImageSpan(alertIcon, ImageSpan.ALIGN_BASELINE);
     63 
     64         // Spam warning format: <Alert Icon><space><warning message>
     65         SpannableString ss = new SpannableString(
     66             SPANNABLE_SPAM_WARNING_PREFIX + " " + spamWarningText);
     67         ss.setSpan(imageSpan, 0, SPANNABLE_SPAM_WARNING_PREFIX.length(),
     68             Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     69         setText(ss);
     70     }
     71 }
     72