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