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.database.Cursor;
21 import android.graphics.Typeface;
22 import android.provider.Telephony;
23 import android.telephony.SmsCbMessage;
24 import android.text.Spannable;
25 import android.text.SpannableStringBuilder;
26 import android.text.format.DateUtils;
27 import android.text.style.StyleSpan;
28 import android.util.AttributeSet;
29 import android.view.accessibility.AccessibilityEvent;
30 import android.widget.RelativeLayout;
31 import android.widget.TextView;
32 
33 /**
34  * This class manages the list item view for a single alert.
35  */
36 public class CellBroadcastListItem extends RelativeLayout {
37 
38     private SmsCbMessage mCbMessage;
39 
40     private TextView mChannelView;
41     private TextView mMessageView;
42     private TextView mDateView;
43     private Context mContext;
44 
CellBroadcastListItem(Context context, AttributeSet attrs)45     public CellBroadcastListItem(Context context, AttributeSet attrs) {
46         super(context, attrs);
47         mContext = context;
48     }
49 
getMessage()50     SmsCbMessage getMessage() {
51         return mCbMessage;
52     }
53 
54     @Override
onFinishInflate()55     protected void onFinishInflate() {
56         super.onFinishInflate();
57 
58         mChannelView = (TextView) findViewById(R.id.channel);
59         mDateView = (TextView) findViewById(R.id.date);
60         mMessageView = (TextView) findViewById(R.id.message);
61     }
62 
63     /**
64      * Only used for header binding.
65      * @param message the message contents to bind
66      */
bind(SmsCbMessage message)67     public void bind(SmsCbMessage message) {
68         mCbMessage = message;
69         mChannelView.setText(CellBroadcastResources.getDialogTitleResource(mContext, message));
70         mDateView.setText(DateUtils.formatDateTime(getContext(), message.getReceivedTime(),
71                 DateUtils.FORMAT_NO_NOON_MIDNIGHT | DateUtils.FORMAT_SHOW_TIME
72                         | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_DATE
73                         | DateUtils.FORMAT_CAP_AMPM));
74 
75         SpannableStringBuilder messageText = new SpannableStringBuilder(message.getMessageBody());
76         try (Cursor cursor = mContext.getContentResolver().query(
77                 CellBroadcastContentProvider.CONTENT_URI,
78                 CellBroadcastContentProvider.QUERY_COLUMNS,
79                 Telephony.CellBroadcasts.DELIVERY_TIME + "=?",
80                 new String[] {Long.toString(message.getReceivedTime())},
81                 null)) {
82             if (cursor != null) {
83                 while (cursor.moveToNext()) {
84                     if (cursor.getInt(cursor.getColumnIndexOrThrow(
85                             Telephony.CellBroadcasts.MESSAGE_READ)) != 0) {
86                         messageText.setSpan(new StyleSpan(Typeface.BOLD), 0, messageText.length(),
87                                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
88                         break;
89                     }
90                 }
91             }
92         }
93 
94         mMessageView.setText(messageText);
95     }
96 
97     @Override
dispatchPopulateAccessibilityEvent(AccessibilityEvent event)98     public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
99         // Speak the date first, then channel name, then message body
100         int flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE;
101         String dateTime = DateUtils.formatDateTime(getContext(), mCbMessage.getReceivedTime(),
102                 flags);
103 
104         event.getText().add(dateTime);
105         mChannelView.dispatchPopulateAccessibilityEvent(event);
106         mMessageView.dispatchPopulateAccessibilityEvent(event);
107         return true;
108     }
109 }
110