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