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;
18import java.io.StringWriter;
19
20import java.text.SimpleDateFormat;
21import java.util.Date;
22
23import org.xmlpull.v1.XmlSerializer;
24
25import android.telephony.PhoneNumberUtils;
26import android.util.Log;
27import android.util.Xml;
28
29import com.android.bluetooth.map.BluetoothMapUtils.TYPE;
30
31public 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 cpHandle = 0; /* The content provider handle - without type information */
39    private String mapHandle = null; /* The map hex-string handle with type information */
40    private String subject = null;
41    private long dateTime = 0;
42    private String senderName = null;
43    private String senderAddressing = null;
44    private String replytoAddressing = null;
45    private String recipientName = null;
46    private String recipientAddressing = null;
47    private TYPE type = null;
48    private int size = -1;
49    private String text = null;
50    private String receptionStatus = null;
51    private int attachmentSize = -1;
52    private String priority = null;
53    private String read = null;
54    private String sent = null;
55    private String protect = null;
56    private boolean reportRead;
57    public long getHandle() {
58        return cpHandle;
59    }
60
61    public void setHandle(long handle, TYPE type) {
62        this.cpHandle = handle;
63        this.mapHandle = BluetoothMapUtils.getMapHandle(cpHandle, type);
64    }
65
66    public long getDateTime() {
67        return dateTime;
68    }
69
70    public String getDateTimeString() {
71        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
72        Date date = new Date(dateTime);
73        return format.format(date); // Format to YYYYMMDDTHHMMSS local time
74    }
75
76    public void setDateTime(long dateTime) {
77        this.dateTime = dateTime;
78    }
79
80    public String getSubject() {
81        return subject;
82    }
83
84    public void setSubject(String subject) {
85        this.subject = subject;
86    }
87
88    public String getSenderName() {
89        return senderName;
90    }
91
92    public void setSenderName(String senderName) {
93        this.senderName = senderName;
94    }
95
96    public String getSenderAddressing() {
97        return senderAddressing;
98    }
99
100    public void setSenderAddressing(String senderAddressing) {
101        /* TODO: This should depend on the type - for email, the addressing is an email address
102         * Consider removing this again - to allow strings.
103         */
104        this.senderAddressing = PhoneNumberUtils.extractNetworkPortion(senderAddressing);
105        if(this.senderAddressing == null || this.senderAddressing.length() < 2){
106            this.senderAddressing = "11"; // Ensure we have at least two digits to
107        }
108    }
109
110    public String getReplyToAddressing() {
111        return replytoAddressing;
112    }
113
114    public void setReplytoAddressing(String replytoAddressing) {
115        this.replytoAddressing = replytoAddressing;
116    }
117
118    public String getRecipientName() {
119        return recipientName;
120    }
121
122    public void setRecipientName(String recipientName) {
123        this.recipientName = recipientName;
124    }
125
126    public String getRecipientAddressing() {
127        return recipientAddressing;
128    }
129
130    public void setRecipientAddressing(String recipientAddressing) {
131        this.recipientAddressing = recipientAddressing;
132    }
133
134    public TYPE getType() {
135        return type;
136    }
137
138    public void setType(TYPE type) {
139        this.type = type;
140    }
141
142    public int getSize() {
143        return size;
144    }
145
146    public void setSize(int size) {
147        this.size = size;
148    }
149
150    public String getText() {
151        return text;
152    }
153
154    public void setText(String text) {
155        this.text = text;
156    }
157
158    public String getReceptionStatus() {
159        return receptionStatus;
160    }
161
162    public void setReceptionStatus(String receptionStatus) {
163        this.receptionStatus = receptionStatus;
164    }
165
166    public int getAttachmentSize() {
167        return attachmentSize;
168    }
169
170    public void setAttachmentSize(int attachmentSize) {
171        this.attachmentSize = attachmentSize;
172    }
173
174    public String getPriority() {
175        return priority;
176    }
177
178    public void setPriority(String priority) {
179        this.priority = priority;
180    }
181
182    public String getRead() {
183        return read;
184    }
185
186    public void setRead(String read, boolean reportRead) {
187        this.read = read;
188        this.reportRead = reportRead;
189    }
190
191    public String getSent() {
192        return sent;
193    }
194
195    public void setSent(String sent) {
196        this.sent = sent;
197    }
198
199    public String getProtect() {
200        return protect;
201    }
202
203    public void setProtect(String protect) {
204        this.protect = protect;
205    }
206
207    public int compareTo(BluetoothMapMessageListingElement e) {
208        if (this.dateTime < e.dateTime) {
209            return 1;
210        } else if (this.dateTime > e.dateTime) {
211            return -1;
212        } else {
213            return 0;
214        }
215    }
216
217    /* Encode the MapMessageListingElement into the StringBuilder reference.
218     * */
219    public void encode(XmlSerializer xmlMsgElement) throws IllegalArgumentException, IllegalStateException, IOException
220    {
221
222            // contruct the XML tag for a single msg in the msglisting
223            xmlMsgElement.startTag("", "msg");
224            xmlMsgElement.attribute("", "handle", mapHandle);
225            if(subject != null)
226                xmlMsgElement.attribute("", "subject", subject);
227            if(dateTime != 0)
228                xmlMsgElement.attribute("", "datetime", this.getDateTimeString());
229            if(senderName != null)
230                xmlMsgElement.attribute("", "sender_name", senderName);
231            if(senderAddressing != null)
232                xmlMsgElement.attribute("", "sender_addressing", senderAddressing);
233            if(replytoAddressing != null)
234                xmlMsgElement.attribute("", "replyto_addressing",replytoAddressing);
235            if(recipientName != null)
236                xmlMsgElement.attribute("", "recipient_name",recipientName);
237            if(recipientAddressing != null)
238                xmlMsgElement.attribute("", "recipient_addressing", recipientAddressing);
239            if(type != null)
240                xmlMsgElement.attribute("", "type", type.name());
241            if(size != -1)
242                xmlMsgElement.attribute("", "size", Integer.toString(size));
243            if(text != null)
244                xmlMsgElement.attribute("", "text", text);
245            if(receptionStatus != null)
246                xmlMsgElement.attribute("", "reception_status", receptionStatus);
247            if(attachmentSize != -1)
248                xmlMsgElement.attribute("", "attachment_size", Integer.toString(attachmentSize));
249            if(priority != null)
250                xmlMsgElement.attribute("", "priority", priority);
251            if(read != null && reportRead)
252                xmlMsgElement.attribute("", "read", read);
253            if(sent != null)
254                xmlMsgElement.attribute("", "sent", sent);
255            if(protect != null)
256                xmlMsgElement.attribute("", "protect", protect);
257            xmlMsgElement.endTag("", "msg");
258
259    }
260}
261
262
263