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