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 int mSize = -1;
48     private String mText = null;
49     private String mReceptionStatus = null;
50     private int mAttachmentSize = -1;
51     private String mPriority = null;
52     private boolean mRead = false;
53     private String mSent = null;
54     private String mProtect = null;
55     private String mThreadId = null;
56     private boolean mReportRead = false;
57     private int mCursorIndex = 0;
58 
getCursorIndex()59     public int getCursorIndex() {
60         return mCursorIndex;
61     }
62 
setCursorIndex(int cursorIndex)63     public void setCursorIndex(int cursorIndex) {
64         this.mCursorIndex = cursorIndex;
65     }
66 
getHandle()67     public long getHandle() {
68         return mCpHandle;
69     }
70 
setHandle(long handle)71     public void setHandle(long handle) {
72         this.mCpHandle = handle;
73     }
74 
getDateTime()75     public long getDateTime() {
76         return mDateTime;
77     }
78 
getDateTimeString()79     public String getDateTimeString() {
80         SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
81         Date date = new Date(mDateTime);
82         return format.format(date); // Format to YYYYMMDDTHHMMSS local time
83     }
84 
setDateTime(long dateTime)85     public void setDateTime(long dateTime) {
86         this.mDateTime = dateTime;
87     }
88 
getSubject()89     public String getSubject() {
90         return mSubject;
91     }
92 
setSubject(String subject)93     public void setSubject(String subject) {
94         this.mSubject = subject;
95     }
96 
getSenderName()97     public String getSenderName() {
98         return mSenderName;
99     }
100 
setSenderName(String senderName)101     public void setSenderName(String senderName) {
102         this.mSenderName = senderName;
103     }
104 
getSenderAddressing()105     public String getSenderAddressing() {
106         return mSenderAddressing;
107     }
108 
setSenderAddressing(String senderAddressing)109     public void setSenderAddressing(String senderAddressing) {
110         this.mSenderAddressing = senderAddressing;
111     }
112 
getReplyToAddressing()113     public String getReplyToAddressing() {
114         return mReplytoAddressing;
115     }
116 
setReplytoAddressing(String replytoAddressing)117     public void setReplytoAddressing(String replytoAddressing) {
118         this.mReplytoAddressing = replytoAddressing;
119     }
120 
getRecipientName()121     public String getRecipientName() {
122         return mRecipientName;
123     }
124 
setRecipientName(String recipientName)125     public void setRecipientName(String recipientName) {
126         this.mRecipientName = recipientName;
127     }
128 
getRecipientAddressing()129     public String getRecipientAddressing() {
130         return mRecipientAddressing;
131     }
132 
setRecipientAddressing(String recipientAddressing)133     public void setRecipientAddressing(String recipientAddressing) {
134         this.mRecipientAddressing = recipientAddressing;
135     }
136 
getType()137     public TYPE getType() {
138         return mType;
139     }
140 
setType(TYPE type)141     public void setType(TYPE type) {
142         this.mType = type;
143     }
144 
getSize()145     public int getSize() {
146         return mSize;
147     }
148 
setSize(int size)149     public void setSize(int size) {
150         this.mSize = size;
151     }
152 
getText()153     public String getText() {
154         return mText;
155     }
156 
setText(String text)157     public void setText(String text) {
158         this.mText = text;
159     }
160 
getReceptionStatus()161     public String getReceptionStatus() {
162         return mReceptionStatus;
163     }
164 
setReceptionStatus(String receptionStatus)165     public void setReceptionStatus(String receptionStatus) {
166         this.mReceptionStatus = receptionStatus;
167     }
168 
getAttachmentSize()169     public int getAttachmentSize() {
170         return mAttachmentSize;
171     }
172 
setAttachmentSize(int attachmentSize)173     public void setAttachmentSize(int attachmentSize) {
174         this.mAttachmentSize = attachmentSize;
175     }
176 
getPriority()177     public String getPriority() {
178         return mPriority;
179     }
180 
setPriority(String priority)181     public void setPriority(String priority) {
182         this.mPriority = priority;
183     }
184 
getRead()185     public String getRead() {
186         return (mRead?"yes":"no");
187     }
getReadBool()188     public boolean getReadBool() {
189         return mRead;
190     }
191 
setRead(boolean read, boolean reportRead)192     public void setRead(boolean read, boolean reportRead) {
193         this.mRead = read;
194         this.mReportRead = reportRead;
195     }
196 
getSent()197     public String getSent() {
198         return mSent;
199     }
200 
setSent(String sent)201     public void setSent(String sent) {
202         this.mSent = sent;
203     }
204 
getProtect()205     public String getProtect() {
206         return mProtect;
207     }
208 
setProtect(String protect)209     public void setProtect(String protect) {
210         this.mProtect = protect;
211     }
212 
setThreadId(long threadId)213     public void setThreadId(long threadId) {
214         if(threadId != -1) {
215             this.mThreadId = BluetoothMapUtils.getLongAsString(threadId);
216         }
217     }
218 
compareTo(BluetoothMapMessageListingElement e)219     public int compareTo(BluetoothMapMessageListingElement e) {
220         if (this.mDateTime < e.mDateTime) {
221             return 1;
222         } else if (this.mDateTime > e.mDateTime) {
223             return -1;
224         } else {
225             return 0;
226         }
227     }
228 
229     /**
230      * Strip away any illegal XML characters, that would otherwise cause the
231      * xml serializer to throw an exception.
232      * Examples of such characters are the emojis used on Android.
233      * @param text The string to validate
234      * @return the same string if valid, otherwise a new String stripped for
235      * any illegal characters
236      */
stripInvalidChars(String text)237     private static String stripInvalidChars(String text) {
238         char out[] = new char[text.length()];
239         int i, o, l;
240         for(i=0, o=0, l=text.length(); i<l; i++){
241             char c = text.charAt(i);
242             if((c >= 0x20 && c <= 0xd7ff) || (c >= 0xe000 && c <= 0xfffd)) {
243                 out[o++] = c;
244             } // Else we skip the character
245         }
246 
247         if(i==o) {
248             return text;
249         } else { // We removed some characters, create the new string
250             return new String(out,0,o);
251         }
252     }
253 
254     /* Encode the MapMessageListingElement into the StringBuilder reference.
255      * */
encode(XmlSerializer xmlMsgElement, boolean includeThreadId)256     public void encode(XmlSerializer xmlMsgElement, boolean includeThreadId) throws IllegalArgumentException, IllegalStateException, IOException
257     {
258 
259             // contruct the XML tag for a single msg in the msglisting
260             xmlMsgElement.startTag(null, "msg");
261             xmlMsgElement.attribute(null, "handle", BluetoothMapUtils.getMapHandle(mCpHandle, mType));
262             if(mSubject != null)
263                 xmlMsgElement.attribute(null, "subject", stripInvalidChars(mSubject));
264             if(mDateTime != 0)
265                 xmlMsgElement.attribute(null, "datetime", this.getDateTimeString());
266             if(mSenderName != null)
267                 xmlMsgElement.attribute(null, "sender_name", stripInvalidChars(mSenderName));
268             if(mSenderAddressing != null)
269                 xmlMsgElement.attribute(null, "sender_addressing", mSenderAddressing);
270             if(mReplytoAddressing != null)
271                 xmlMsgElement.attribute(null, "replyto_addressing",mReplytoAddressing);
272             if(mRecipientName != null)
273                 xmlMsgElement.attribute(null, "recipient_name", stripInvalidChars(mRecipientName));
274             if(mRecipientAddressing != null)
275                 xmlMsgElement.attribute(null, "recipient_addressing", mRecipientAddressing);
276             if(mType != null)
277                 xmlMsgElement.attribute(null, "type", mType.name());
278             if(mSize != -1)
279                 xmlMsgElement.attribute(null, "size", Integer.toString(mSize));
280             if(mText != null)
281                 xmlMsgElement.attribute(null, "text", mText);
282             if(mReceptionStatus != null)
283                 xmlMsgElement.attribute(null, "reception_status", mReceptionStatus);
284             if(mAttachmentSize != -1)
285                 xmlMsgElement.attribute(null, "attachment_size", Integer.toString(mAttachmentSize));
286             if(mPriority != null)
287                 xmlMsgElement.attribute(null, "priority", mPriority);
288             if(mReportRead)
289                 xmlMsgElement.attribute(null, "read", getRead());
290             if(mSent != null)
291                 xmlMsgElement.attribute(null, "sent", mSent);
292             if(mProtect != null)
293                 xmlMsgElement.attribute(null, "protected", mProtect);
294             if(mThreadId != null && includeThreadId == true)
295                 xmlMsgElement.attribute(null, "thread_id", mThreadId);
296             xmlMsgElement.endTag(null, "msg");
297 
298     }
299 }
300 
301 
302