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