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.telephony.CellBroadcastMessage; 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 /** 31 * This class manages the list item view for a single alert. 32 */ 33 public class CellBroadcastListItem extends RelativeLayout { 34 35 private CellBroadcastMessage mCbMessage; 36 37 private TextView mChannelView; 38 private TextView mMessageView; 39 private TextView mDateView; 40 private Context mContext; 41 CellBroadcastListItem(Context context, AttributeSet attrs)42 public CellBroadcastListItem(Context context, AttributeSet attrs) { 43 super(context, attrs); 44 mContext = context; 45 } 46 getMessage()47 CellBroadcastMessage getMessage() { 48 return mCbMessage; 49 } 50 51 @Override onFinishInflate()52 protected void onFinishInflate() { 53 super.onFinishInflate(); 54 55 mChannelView = (TextView) findViewById(R.id.channel); 56 mDateView = (TextView) findViewById(R.id.date); 57 mMessageView = (TextView) findViewById(R.id.message); 58 } 59 60 /** 61 * Only used for header binding. 62 * @param message the message contents to bind 63 */ bind(CellBroadcastMessage message)64 public void bind(CellBroadcastMessage message) { 65 mCbMessage = message; 66 mChannelView.setText(CellBroadcastResources.getDialogTitleResource(mContext, message)); 67 mDateView.setText(message.getDateString(getContext())); 68 mMessageView.setText(formatMessage(message)); 69 } 70 formatMessage(CellBroadcastMessage message)71 private static CharSequence formatMessage(CellBroadcastMessage message) { 72 String body = message.getMessageBody(); 73 74 SpannableStringBuilder buf = new SpannableStringBuilder(body); 75 76 // Unread messages are shown in bold 77 if (!message.isRead()) { 78 buf.setSpan(new StyleSpan(Typeface.BOLD), 0, buf.length(), 79 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 80 } 81 return buf; 82 } 83 84 @Override dispatchPopulateAccessibilityEvent(AccessibilityEvent event)85 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { 86 // Speak the date first, then channel name, then message body 87 event.getText().add(mCbMessage.getSpokenDateString(getContext())); 88 mChannelView.dispatchPopulateAccessibilityEvent(event); 89 mMessageView.dispatchPopulateAccessibilityEvent(event); 90 return true; 91 } 92 } 93