summaryrefslogtreecommitdiffstats
path: root/src/com/android/bluetooth/map/BluetoothMapMessageListingElement.java
blob: 2d84a049adc2d090ee0bd319dd88bc92837e6dbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright (C) 2013 Samsung System LSI
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.bluetooth.map;

import java.io.IOException;
import java.io.StringWriter;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.xmlpull.v1.XmlSerializer;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import android.util.Xml;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ByteArrayInputStream;
import java.io.BufferedReader;
import java.io.StringWriter;
import java.util.List;
import java.nio.charset.Charset;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import android.util.Xml;

import com.android.bluetooth.map.BluetoothMapUtils.TYPE;

public class BluetoothMapMessageListingElement
    implements Comparable<BluetoothMapMessageListingElement> {

    private static final String TAG = "BluetoothMapMessageListingElement";
    private static final boolean D = BluetoothMapService.DEBUG;
    private static final boolean V = Log.isLoggable(BluetoothMapService.LOG_TAG, Log.VERBOSE) ? true : false;

    private long cpHandle = 0; /* The content provider handle - without type information */
    private String mapHandle = null; /* The map hex-string handle with type information */
    private String subject = null;
    private long dateTime = 0;
    private String senderName = null;
    private String senderAddressing = null;
    private String replytoAddressing = null;
    private String recipientName = null;
    private String recipientAddressing = null;
    private TYPE type = null;
    private int size = -1;
    private String text = null;
    private String receptionStatus = null;
    private int attachmentSize = -1;
    private String priority = null;
    private String read = null;
    private String sent = null;
    private String protect = null;
    private boolean reportRead;
    public long getHandle() {
        return cpHandle;
    }

    public void setHandle(long handle, TYPE type) {
        this.cpHandle = handle;
        this.mapHandle = BluetoothMapUtils.getMapHandle(cpHandle, type);
    }

    public long getDateTime() {
        return dateTime;
    }

    public String getDateTimeString() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
        Date date = new Date(dateTime);
        return format.format(date); // Format to YYYYMMDDTHHMMSS local time
    }

    public void setDateTime(long dateTime) {
        this.dateTime = dateTime;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getSenderName() {
        return senderName;
    }

    public void setSenderName(String senderName) {
        this.senderName = senderName;
    }

    public String getSenderAddressing() {
        return senderAddressing;
    }

    public void setEmailSenderAddressing(String senderAddressing) {
       this.senderAddressing = senderAddressing;
    }

    public void setSenderAddressing(String senderAddressing) {
        /* TODO: This should depend on the type - for email, the addressing is an email address
         * Consider removing this again - to allow strings.
         */
        this.senderAddressing = PhoneNumberUtils.extractNetworkPortion(senderAddressing);
        if(this.senderAddressing == null || this.senderAddressing.length() < 2){
            this.senderAddressing = ""; // Set empty value
        }
    }

    public String getReplyToAddressing() {
        return replytoAddressing;
    }

    public void setReplytoAddressing(String replytoAddressing) {
        this.replytoAddressing = replytoAddressing;
    }

    public String getRecipientName() {
        return recipientName;
    }

    public void setRecipientName(String recipientName) {
        this.recipientName = recipientName;
    }

    public String getRecipientAddressing() {
        return recipientAddressing;
    }

    public void setRecipientAddressing(String recipientAddressing) {
        this.recipientAddressing = recipientAddressing;
    }

    public TYPE getType() {
        return type;
    }

    public void setType(TYPE type) {
        this.type = type;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getReceptionStatus() {
        return receptionStatus;
    }

    public void setReceptionStatus(String receptionStatus) {
        this.receptionStatus = receptionStatus;
    }

    public int getAttachmentSize() {
        return attachmentSize;
    }

    public void setAttachmentSize(int attachmentSize) {
        this.attachmentSize = attachmentSize;
    }

    public String getPriority() {
        return priority;
    }

    public void setPriority(String priority) {
        this.priority = priority;
    }

    public String getRead() {
        return read;
    }

    public void setRead(String read, boolean reportRead) {
        this.read = read;
        this.reportRead = reportRead;
    }

    public String getSent() {
        return sent;
    }

    public void setSent(String sent) {
        this.sent = sent;
    }

    public String getProtect() {
        return protect;
    }

    public void setProtect(String protect) {
        this.protect = protect;
    }

    public int compareTo(BluetoothMapMessageListingElement e) {
        if (this.dateTime < e.dateTime) {
            return 1;
        } else if (this.dateTime > e.dateTime) {
            return -1;
        } else {
            return 0;
        }
    }

    /* Encode the MapMessageListingElement into the StringBuilder reference.
     * */
    public void encode(XmlSerializer xmlMsgElement) throws IllegalArgumentException, IllegalStateException, IOException
    {
            Log.d(TAG, "Inside encode");
            // contruct the XML tag for a single msg in the msglisting
            xmlMsgElement.startTag(null, "msg");
            xmlMsgElement.attribute(null, "handle", mapHandle);
            if(subject != null) {
               InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(subject.getBytes(Charset.forName("UTF-8"))), Charset.forName("UTF-8"));
               BufferedReader reader = new BufferedReader(isr);
               StringBuilder sb = new StringBuilder();
               String line = null;
               while ((line = reader.readLine()) != null) {
                     sb.append(line);
               }
               xmlMsgElement.attribute(null, "subject", sb.toString());
            }

            if(dateTime != 0)
                xmlMsgElement.attribute(null, "datetime", this.getDateTimeString());
            if(senderName != null) {
               InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(senderName.getBytes(Charset.forName("UTF-8"))), Charset.forName("UTF-8"));
               BufferedReader reader = new BufferedReader(isr);
               StringBuilder sb = new StringBuilder();
               String line = null;
               while ((line = reader.readLine()) != null) {
                     sb.append(line);
               }
               xmlMsgElement.attribute(null, "sender_name", sb.toString());
            }
            if(senderAddressing != null)
                xmlMsgElement.attribute(null, "sender_addressing", senderAddressing);
            if(replytoAddressing != null)
                xmlMsgElement.attribute(null, "replyto_addressing",replytoAddressing);
            if(recipientName != null)
                xmlMsgElement.attribute(null, "recipient_name",recipientName);
            if(recipientAddressing != null)
                xmlMsgElement.attribute(null, "recipient_addressing", recipientAddressing);
            if(type != null)
                xmlMsgElement.attribute(null, "type", type.name());
            if(size != -1)
                xmlMsgElement.attribute(null, "size", Integer.toString(size));
            if(text != null)
                xmlMsgElement.attribute(null, "text", text);
            if(receptionStatus != null)
                xmlMsgElement.attribute(null, "reception_status", receptionStatus);
            if(attachmentSize != -1)
                xmlMsgElement.attribute(null, "attachment_size", Integer.toString(attachmentSize));
            if(priority != null)
                xmlMsgElement.attribute(null, "priority", priority);
            if(read != null && reportRead)
                xmlMsgElement.attribute(null, "read", read);
            if(sent != null)
                xmlMsgElement.attribute(null, "sent", sent);
            if(protect != null)
                xmlMsgElement.attribute(null, "protected", protect);
            xmlMsgElement.endTag(null, "msg");
            Log.d(TAG, "Exiting encode");

    }
}