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