Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | package com.android.contacts.interactions; |
| 17 | |
| 18 | import com.android.contacts.R; |
Walter Jang | 7ce5352 | 2014-10-29 13:26:43 -0700 | [diff] [blame] | 19 | import com.android.contacts.common.util.ContactDisplayUtils; |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 20 | |
| 21 | import android.content.ContentValues; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.graphics.drawable.Drawable; |
| 25 | import android.net.Uri; |
| 26 | import android.provider.Telephony.Sms; |
Brian Attwell | c62cc79 | 2014-10-02 12:35:07 -0700 | [diff] [blame] | 27 | import android.text.BidiFormatter; |
Walter Jang | 7ce5352 | 2014-10-29 13:26:43 -0700 | [diff] [blame] | 28 | import android.text.Spannable; |
Brian Attwell | c62cc79 | 2014-10-02 12:35:07 -0700 | [diff] [blame] | 29 | import android.text.TextDirectionHeuristics; |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Represents an sms interaction, wrapping the columns in |
| 33 | * {@link android.provider.Telephony.Sms}. |
| 34 | */ |
| 35 | public class SmsInteraction implements ContactInteraction { |
| 36 | |
| 37 | private static final String URI_TARGET_PREFIX = "smsto:"; |
| 38 | private static final int SMS_ICON_RES = R.drawable.ic_message_24dp; |
Brian Attwell | c62cc79 | 2014-10-02 12:35:07 -0700 | [diff] [blame] | 39 | private static BidiFormatter sBidiFormatter = BidiFormatter.getInstance(); |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 40 | |
| 41 | private ContentValues mValues; |
| 42 | |
| 43 | public SmsInteraction(ContentValues values) { |
| 44 | mValues = values; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public Intent getIntent() { |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 49 | String address = getAddress(); |
| 50 | return address == null ? null : new Intent(Intent.ACTION_VIEW).setData( |
| 51 | Uri.parse(URI_TARGET_PREFIX + address)); |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 55 | public long getInteractionDate() { |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 56 | Long date = getDate(); |
| 57 | return date == null ? -1 : date; |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public String getViewHeader(Context context) { |
Paul Soulos | 9f3f887 | 2014-08-20 17:02:16 -0700 | [diff] [blame] | 62 | String body = getBody(); |
| 63 | if (getType() == Sms.MESSAGE_TYPE_SENT) { |
| 64 | body = context.getResources().getString(R.string.message_from_you_prefix, body); |
| 65 | } |
| 66 | return body; |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String getViewBody(Context context) { |
| 71 | return getAddress(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public String getViewFooter(Context context) { |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 76 | Long date = getDate(); |
| 77 | return date == null ? null : ContactInteractionUtil.formatDateStringFromTimestamp( |
| 78 | date, context); |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Drawable getIcon(Context context) { |
| 83 | return context.getResources().getDrawable(SMS_ICON_RES); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Drawable getBodyIcon(Context context) { |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Drawable getFooterIcon(Context context) { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | public String getAddress() { |
Jay Shrauner | b221576 | 2014-10-22 09:02:53 -0700 | [diff] [blame] | 97 | final String address = mValues.getAsString(Sms.ADDRESS); |
| 98 | return address == null ? null : |
| 99 | sBidiFormatter.unicodeWrap(address, TextDirectionHeuristics.LTR); |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | public String getBody() { |
| 103 | return mValues.getAsString(Sms.BODY); |
| 104 | } |
| 105 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 106 | public Long getDate() { |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 107 | return mValues.getAsLong(Sms.DATE); |
| 108 | } |
| 109 | |
| 110 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 111 | public Long getDateSent() { |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 112 | return mValues.getAsLong(Sms.DATE_SENT); |
| 113 | } |
| 114 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 115 | public Integer getErrorCode() { |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 116 | return mValues.getAsInteger(Sms.ERROR_CODE); |
| 117 | } |
| 118 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 119 | public Boolean getLocked() { |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 120 | return mValues.getAsBoolean(Sms.LOCKED); |
| 121 | } |
| 122 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 123 | public Integer getPerson() { |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 124 | return mValues.getAsInteger(Sms.PERSON); |
| 125 | } |
| 126 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 127 | public Integer getProtocol() { |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 128 | return mValues.getAsInteger(Sms.PROTOCOL); |
| 129 | } |
| 130 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 131 | public Boolean getRead() { |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 132 | return mValues.getAsBoolean(Sms.READ); |
| 133 | } |
| 134 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 135 | public Boolean getReplyPathPresent() { |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 136 | return mValues.getAsBoolean(Sms.REPLY_PATH_PRESENT); |
| 137 | } |
| 138 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 139 | public Boolean getSeen() { |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 140 | return mValues.getAsBoolean(Sms.SEEN); |
| 141 | } |
| 142 | |
| 143 | public String getServiceCenter() { |
| 144 | return mValues.getAsString(Sms.SERVICE_CENTER); |
| 145 | } |
| 146 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 147 | public Integer getStatus() { |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 148 | return mValues.getAsInteger(Sms.STATUS); |
| 149 | } |
| 150 | |
| 151 | public String getSubject() { |
| 152 | return mValues.getAsString(Sms.SUBJECT); |
| 153 | } |
| 154 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 155 | public Integer getThreadId() { |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 156 | return mValues.getAsInteger(Sms.THREAD_ID); |
| 157 | } |
| 158 | |
Paul Soulos | 75d5a91 | 2014-06-24 11:22:37 -0700 | [diff] [blame] | 159 | public Integer getType() { |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 160 | return mValues.getAsInteger(Sms.TYPE); |
| 161 | } |
Paul Soulos | 23e2836 | 2014-08-29 14:57:08 -0700 | [diff] [blame] | 162 | |
| 163 | @Override |
Walter Jang | 7ce5352 | 2014-10-29 13:26:43 -0700 | [diff] [blame] | 164 | public Spannable getContentDescription(Context context) { |
| 165 | final String phoneNumber = getViewBody(context); |
| 166 | final String contentDescription = context.getResources().getString( |
| 167 | R.string.content_description_recent_sms, |
| 168 | getViewHeader(context), phoneNumber, getViewFooter(context)); |
| 169 | return ContactDisplayUtils.getTelephoneTtsSpannable(contentDescription, phoneNumber); |
Paul Soulos | 23e2836 | 2014-08-29 14:57:08 -0700 | [diff] [blame] | 170 | } |
Paul Soulos | 48290be | 2014-09-08 13:44:51 -0700 | [diff] [blame] | 171 | |
| 172 | @Override |
| 173 | public int getIconResourceId() { |
| 174 | return SMS_ICON_RES; |
| 175 | } |
Paul Soulos | b3054e5 | 2014-06-05 16:46:02 -0700 | [diff] [blame] | 176 | } |