1 /* 2 * Copyright (C) 2013 Samsung System LSI 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 package com.android.bluetooth.map; 16 17 import java.io.IOException; 18 import java.io.StringWriter; 19 20 import java.text.SimpleDateFormat; 21 import java.util.Date; 22 23 import org.xmlpull.v1.XmlSerializer; 24 25 import android.telephony.PhoneNumberUtils; 26 import android.util.Log; 27 import android.util.Xml; 28 29 import com.android.bluetooth.map.BluetoothMapUtils.TYPE; 30 import com.android.bluetooth.util.Interop; 31 32 public class BluetoothMapMessageListingElement 33 implements Comparable<BluetoothMapMessageListingElement> { 34 35 private static final String TAG = "BluetoothMapMessageListingElement"; 36 private static final boolean D = false; 37 private static final boolean V = false; 38 39 private long mCpHandle = 0; /* The content provider handle - without type information */ 40 private String mSubject = null; 41 private long mDateTime = 0; 42 private String mSenderName = null; 43 private String mSenderAddressing = null; 44 private String mReplytoAddressing = null; 45 private String mRecipientName = null; 46 private String mRecipientAddressing = null; 47 private TYPE mType = null; 48 private boolean mMsgTypeAppParamSet = false; 49 private int mSize = -1; 50 private String mText = null; 51 private String mReceptionStatus = null; 52 private String mDeliveryStatus = null; 53 private int mAttachmentSize = -1; 54 private String mPriority = null; 55 private boolean mRead = false; 56 private String mSent = null; 57 private String mProtect = null; 58 private String mFolderType = null; 59 private String mThreadId = null; 60 private String mThreadName = null; 61 private String mAttachmentMimeTypes = null; 62 63 private boolean mReportRead = false; 64 private int mCursorIndex = 0; 65 66 public int getCursorIndex() { 67 return mCursorIndex; 68 } 69 70 public void setCursorIndex(int cursorIndex) { 71 this.mCursorIndex = cursorIndex; 72 } 73 74 public long getHandle() { 75 return mCpHandle; 76 } 77 78 public void setHandle(long handle) { 79 this.mCpHandle = handle; 80 } 81 82 public long getDateTime() { 83 return mDateTime; 84 } 85 86 public String getDateTimeString() { 87 /* TODO: if the feature bit mask of the client supports it, add the time-zone 88 * (as for MSETime) */ 89 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss"); 90 Date date = new Date(mDateTime); 91 return format.format(date); // Format to YYYYMMDDTHHMMSS local time 92 } 93 94 public void setDateTime(long dateTime) { 95 this.mDateTime = dateTime; 96 } 97 98 public String getSubject() { 99 return mSubject; 100 } 101 102 public void setSubject(String subject) { 103 this.mSubject = subject; 104 } 105 106 public String getSenderName() { 107 return mSenderName; 108 } 109 110 public void setSenderName(String senderName) { 111 this.mSenderName = senderName; 112 } 113 114 public String getSenderAddressing() { 115 return mSenderAddressing; 116 } 117 118 public void setSenderAddressing(String senderAddressing) { 119 this.mSenderAddressing = senderAddressing; 120 } 121 122 public String getReplyToAddressing() { 123 return mReplytoAddressing; 124 } 125 126 public void setReplytoAddressing(String replytoAddressing) { 127 this.mReplytoAddressing = replytoAddressing; 128 } 129 130 public String getRecipientName() { 131 return mRecipientName; 132 } 133 134 public void setRecipientName(String recipientName) { 135 this.mRecipientName = recipientName; 136 } 137 138 public String getRecipientAddressing() { 139 return mRecipientAddressing; 140 } 141 142 public void setRecipientAddressing(String recipientAddressing) { 143 this.mRecipientAddressing = recipientAddressing; 144 } 145 146 public TYPE getType() { 147 return mType; 148 } 149 150 public void setType(TYPE type, boolean appParamSet) { 151 this.mMsgTypeAppParamSet = appParamSet; 152 this.mType = type; 153 } 154 155 public int getSize() { 156 return mSize; 157 } 158 159 public void setSize(int size) { 160 this.mSize = size; 161 } 162 163 public String getText() { 164 return mText; 165 } 166 167 public void setText(String text) { 168 this.mText = text; 169 } 170 171 public String getReceptionStatus() { 172 return mReceptionStatus; 173 } 174 175 public void setReceptionStatus(String receptionStatus) { 176 this.mReceptionStatus = receptionStatus; 177 } 178 179 public String getDeliveryStatus() { 180 return mDeliveryStatus; 181 } 182 183 public void setDeliveryStatus(String deliveryStatus) { 184 this.mDeliveryStatus = deliveryStatus; 185 } 186 187 public int getAttachmentSize() { 188 return mAttachmentSize; 189 } 190 191 public void setAttachmentSize(int attachmentSize) { 192 this.mAttachmentSize = attachmentSize; 193 } 194 195 public String getAttachmentMimeTypes() { 196 return mAttachmentMimeTypes; 197 } 198 199 public void setAttachmentMimeTypes(String attachmentMimeTypes) { 200 this.mAttachmentMimeTypes = attachmentMimeTypes; 201 } 202 203 public String getPriority() { 204 return mPriority; 205 } 206 207 public void setPriority(String priority) { 208 this.mPriority = priority; 209 } 210 211 public String getRead() { 212 return (mRead?"yes":"no"); 213 } 214 public boolean getReadBool() { 215 return mRead; 216 } 217 public void setRead(boolean read, boolean reportRead) { 218 this.mRead = read; 219 this.mReportRead = reportRead; 220 } 221 222 public String getSent() { 223 return mSent; 224 } 225 226 public void setSent(String sent) { 227 this.mSent = sent; 228 } 229 230 public String getProtect() { 231 return mProtect; 232 } 233 234 public void setProtect(String protect) { 235 this.mProtect = protect; 236 } 237 238 public void setThreadId(long threadId, TYPE type) { 239 if(threadId != -1) { 240 this.mThreadId = BluetoothMapUtils.getMapConvoHandle(threadId, type); 241 } 242 } 243 244 public String getThreadName() { 245 return mThreadName; 246 } 247 248 public void setThreadName(String name) { 249 this.mThreadName = name; 250 } 251 252 public String getFolderType() { 253 return mFolderType; 254 } 255 256 public void setFolderType(String folderType) { 257 this.mFolderType = folderType; 258 } 259 260 public int compareTo(BluetoothMapMessageListingElement e) { 261 if (this.mDateTime < e.mDateTime) { 262 return 1; 263 } else if (this.mDateTime > e.mDateTime) { 264 return -1; 265 } else { 266 return 0; 267 } 268 } 269 270 /* Encode the MapMessageListingElement into the StringBuilder reference. 271 * */ 272 public void encode(XmlSerializer xmlMsgElement, boolean includeThreadId) 273 throws IllegalArgumentException, IllegalStateException, IOException 274 { 275 // contruct the XML tag for a single msg in the msglisting 276 xmlMsgElement.startTag(null, "msg"); 277 xmlMsgElement.attribute(null, "handle", 278 BluetoothMapUtils.getMapHandle(mCpHandle, mType)); 279 if(mSubject != null){ 280 String stripped = BluetoothMapUtils.stripInvalidChars(mSubject); 281 282 if (Interop.matchByAddress(Interop.INTEROP_MAP_ASCIIONLY, 283 BluetoothMapService.getRemoteDevice().getAddress())) { 284 stripped = stripped.replaceAll("[\\P{ASCII}&\"><]", ""); 285 if (stripped.isEmpty()) stripped = "---"; 286 } 287 288 xmlMsgElement.attribute(null, "subject", 289 stripped.substring(0, stripped.length() < 256 ? stripped.length() : 256)); 290 } 291 292 if(mDateTime != 0) 293 xmlMsgElement.attribute(null, "datetime", this.getDateTimeString()); 294 if(mSenderName != null) 295 xmlMsgElement.attribute(null, "sender_name", 296 BluetoothMapUtils.stripInvalidChars(mSenderName)); 297 if(mSenderAddressing != null) 298 xmlMsgElement.attribute(null, "sender_addressing", mSenderAddressing); 299 if(mReplytoAddressing != null) 300 xmlMsgElement.attribute(null, "replyto_addressing",mReplytoAddressing); 301 if(mRecipientName != null) 302 xmlMsgElement.attribute(null, "recipient_name", 303 BluetoothMapUtils.stripInvalidChars(mRecipientName)); 304 if(mRecipientAddressing != null) 305 xmlMsgElement.attribute(null, "recipient_addressing", mRecipientAddressing); 306 /* Avoid NPE for possible "null" value of mType */ 307 if(mMsgTypeAppParamSet == true && mType != null) 308 xmlMsgElement.attribute(null, "type", mType.name()); 309 if(mSize != -1) 310 xmlMsgElement.attribute(null, "size", Integer.toString(mSize)); 311 if(mText != null) 312 xmlMsgElement.attribute(null, "text", mText); 313 if(mReceptionStatus != null) 314 xmlMsgElement.attribute(null, "reception_status", mReceptionStatus); 315 if(mDeliveryStatus != null) 316 xmlMsgElement.attribute(null, "delivery_status", mDeliveryStatus); 317 if(mAttachmentSize != -1) 318 xmlMsgElement.attribute(null, "attachment_size", 319 Integer.toString(mAttachmentSize)); 320 if(mAttachmentMimeTypes != null) 321 xmlMsgElement.attribute(null, "attachment_mime_types", mAttachmentMimeTypes); 322 if(mPriority != null) 323 xmlMsgElement.attribute(null, "priority", mPriority); 324 if(mReportRead) 325 xmlMsgElement.attribute(null, "read", getRead()); 326 if(mSent != null) 327 xmlMsgElement.attribute(null, "sent", mSent); 328 if(mProtect != null) 329 xmlMsgElement.attribute(null, "protected", mProtect); 330 if(mThreadId != null && includeThreadId == true) 331 xmlMsgElement.attribute(null, "conversation_id", mThreadId); 332 if(mThreadName != null && includeThreadId == true) 333 xmlMsgElement.attribute(null, "conversation_name", mThreadName); 334 if(mFolderType != null ) 335 xmlMsgElement.attribute(null, "folder_type", mFolderType); 336 xmlMsgElement.endTag(null, "msg"); 337 338 } 339 } 340 341 342