1 /* 2 * Copyright (C) 2011 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.cellbroadcastreceiver; 18 19 import android.content.Context; 20 import android.graphics.Typeface; 21 import android.graphics.drawable.Drawable; 22 import android.text.Spannable; 23 import android.text.SpannableStringBuilder; 24 import android.text.style.StyleSpan; 25 import android.util.AttributeSet; 26 import android.view.accessibility.AccessibilityEvent; 27 import android.widget.RelativeLayout; 28 import android.widget.TextView; 29 30 import java.util.List; 31 32 /** 33 * This class manages the view for given conversation. 34 */ 35 public class CellBroadcastListItem extends RelativeLayout { 36 private static final String TAG = "CellBroadcastListItem"; 37 private static final boolean DEBUG = false; 38 39 private CellBroadcastMessage mCbMessage; 40 41 private TextView mChannelView; 42 private TextView mMessageView; 43 private TextView mDateView; 44 45 private static final StyleSpan STYLE_BOLD = new StyleSpan(Typeface.BOLD); 46 CellBroadcastListItem(Context context)47 public CellBroadcastListItem(Context context) { 48 super(context); 49 } 50 CellBroadcastListItem(Context context, AttributeSet attrs)51 public CellBroadcastListItem(Context context, AttributeSet attrs) { 52 super(context, attrs); 53 } 54 getMessage()55 CellBroadcastMessage getMessage() { 56 return mCbMessage; 57 } 58 59 @Override onFinishInflate()60 protected void onFinishInflate() { 61 super.onFinishInflate(); 62 63 mChannelView = (TextView) findViewById(R.id.channel); 64 mDateView = (TextView) findViewById(R.id.date); 65 mMessageView = (TextView) findViewById(R.id.message); 66 } 67 68 /** 69 * Only used for header binding. 70 * @param message the message contents to bind 71 */ bind(CellBroadcastMessage message)72 public void bind(CellBroadcastMessage message) { 73 mCbMessage = message; 74 75 Drawable background = message.isRead() ? 76 getResources().getDrawable(R.drawable.list_item_background_read) : 77 getResources().getDrawable(R.drawable.list_item_background_unread); 78 79 setBackgroundDrawable(background); 80 81 mChannelView.setText(message.getDialogTitleResource()); 82 mDateView.setText(message.getDateString(getContext())); 83 mMessageView.setText(formatMessage(message)); 84 } 85 formatMessage(CellBroadcastMessage message)86 private static CharSequence formatMessage(CellBroadcastMessage message) { 87 String body = message.getMessageBody(); 88 89 SpannableStringBuilder buf = new SpannableStringBuilder(body); 90 91 // Unread messages are shown in bold 92 if (!message.isRead()) { 93 buf.setSpan(STYLE_BOLD, 0, buf.length(), 94 Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 95 } 96 return buf; 97 } 98 99 @Override dispatchPopulateAccessibilityEvent(AccessibilityEvent event)100 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { 101 // Speak the date first, then channel name, then message body 102 event.getText().add(mCbMessage.getSpokenDateString(getContext())); 103 mChannelView.dispatchPopulateAccessibilityEvent(event); 104 mMessageView.dispatchPopulateAccessibilityEvent(event); 105 return true; 106 } 107 } 108