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