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