1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.bluetooth.client.map;
18import android.bluetooth.client.map.utils.ObexTime;
19
20import org.json.JSONException;
21import org.json.JSONObject;
22
23import java.math.BigInteger;
24import java.util.Date;
25import java.util.HashMap;
26
27/**
28 * Object representation of message received in messages listing
29 * <p>
30 * This object will be received in
31 * {@link BluetoothMasClient#EVENT_GET_MESSAGES_LISTING} callback message.
32 */
33public class BluetoothMapMessage {
34
35    private final String mHandle;
36
37    private final String mSubject;
38
39    private final Date mDateTime;
40
41    private final String mSenderName;
42
43    private final String mSenderAddressing;
44
45    private final String mReplytoAddressing;
46
47    private final String mRecipientName;
48
49    private final String mRecipientAddressing;
50
51    private final Type mType;
52
53    private final int mSize;
54
55    private final boolean mText;
56
57    private final ReceptionStatus mReceptionStatus;
58
59    private final int mAttachmentSize;
60
61    private final boolean mPriority;
62
63    private final boolean mRead;
64
65    private final boolean mSent;
66
67    private final boolean mProtected;
68
69    public enum Type {
70        UNKNOWN, EMAIL, SMS_GSM, SMS_CDMA, MMS
71    };
72
73    public enum ReceptionStatus {
74        UNKNOWN, COMPLETE, FRACTIONED, NOTIFICATION
75    }
76
77    BluetoothMapMessage(HashMap<String, String> attrs) throws IllegalArgumentException {
78        int size;
79
80        try {
81            /* just to validate */
82            new BigInteger(attrs.get("handle"), 16);
83
84            mHandle = attrs.get("handle");
85        } catch (NumberFormatException e) {
86            /*
87             * handle MUST have proper value, if it does not then throw
88             * something here
89             */
90            throw new IllegalArgumentException(e);
91        }
92
93        mSubject = attrs.get("subject");
94
95        mDateTime = (new ObexTime(attrs.get("datetime"))).getTime();
96
97        mSenderName = attrs.get("sender_name");
98
99        mSenderAddressing = attrs.get("sender_addressing");
100
101        mReplytoAddressing = attrs.get("replyto_addressing");
102
103        mRecipientName = attrs.get("recipient_name");
104
105        mRecipientAddressing = attrs.get("recipient_addressing");
106
107        mType = strToType(attrs.get("type"));
108
109        try {
110            size = Integer.parseInt(attrs.get("size"));
111        } catch (NumberFormatException e) {
112            size = 0;
113        }
114
115        mSize = size;
116
117        mText = yesnoToBoolean(attrs.get("text"));
118
119        mReceptionStatus = strToReceptionStatus(attrs.get("reception_status"));
120
121        try {
122            size = Integer.parseInt(attrs.get("attachment_size"));
123        } catch (NumberFormatException e) {
124            size = 0;
125        }
126
127        mAttachmentSize = size;
128
129        mPriority = yesnoToBoolean(attrs.get("priority"));
130
131        mRead = yesnoToBoolean(attrs.get("read"));
132
133        mSent = yesnoToBoolean(attrs.get("sent"));
134
135        mProtected = yesnoToBoolean(attrs.get("protected"));
136    }
137
138    private boolean yesnoToBoolean(String yesno) {
139        return "yes".equals(yesno);
140    }
141
142    private Type strToType(String s) {
143        if ("EMAIL".equals(s)) {
144            return Type.EMAIL;
145        } else if ("SMS_GSM".equals(s)) {
146            return Type.SMS_GSM;
147        } else if ("SMS_CDMA".equals(s)) {
148            return Type.SMS_CDMA;
149        } else if ("MMS".equals(s)) {
150            return Type.MMS;
151        }
152
153        return Type.UNKNOWN;
154    }
155
156    private ReceptionStatus strToReceptionStatus(String s) {
157        if ("complete".equals(s)) {
158            return ReceptionStatus.COMPLETE;
159        } else if ("fractioned".equals(s)) {
160            return ReceptionStatus.FRACTIONED;
161        } else if ("notification".equals(s)) {
162            return ReceptionStatus.NOTIFICATION;
163        }
164
165        return ReceptionStatus.UNKNOWN;
166    }
167
168    @Override
169    public String toString() {
170        JSONObject json = new JSONObject();
171
172        try {
173            json.put("handle", mHandle);
174            json.put("subject", mSubject);
175            json.put("datetime", mDateTime);
176            json.put("sender_name", mSenderName);
177            json.put("sender_addressing", mSenderAddressing);
178            json.put("replyto_addressing", mReplytoAddressing);
179            json.put("recipient_name", mRecipientName);
180            json.put("recipient_addressing", mRecipientAddressing);
181            json.put("type", mType);
182            json.put("size", mSize);
183            json.put("text", mText);
184            json.put("reception_status", mReceptionStatus);
185            json.put("attachment_size", mAttachmentSize);
186            json.put("priority", mPriority);
187            json.put("read", mRead);
188            json.put("sent", mSent);
189            json.put("protected", mProtected);
190        } catch (JSONException e) {
191            // do nothing
192        }
193
194        return json.toString();
195    }
196
197    /**
198     * @return value corresponding to <code>handle</code> parameter in MAP
199     *         specification
200     */
201    public String getHandle() {
202        return mHandle;
203    }
204
205    /**
206     * @return value corresponding to <code>subject</code> parameter in MAP
207     *         specification
208     */
209    public String getSubject() {
210        return mSubject;
211    }
212
213    /**
214     * @return <code>Date</code> object corresponding to <code>datetime</code>
215     *         parameter in MAP specification
216     */
217    public Date getDateTime() {
218        return mDateTime;
219    }
220
221    /**
222     * @return value corresponding to <code>sender_name</code> parameter in MAP
223     *         specification
224     */
225    public String getSenderName() {
226        return mSenderName;
227    }
228
229    /**
230     * @return value corresponding to <code>sender_addressing</code> parameter
231     *         in MAP specification
232     */
233    public String getSenderAddressing() {
234        return mSenderAddressing;
235    }
236
237    /**
238     * @return value corresponding to <code>replyto_addressing</code> parameter
239     *         in MAP specification
240     */
241    public String getReplytoAddressing() {
242        return mReplytoAddressing;
243    }
244
245    /**
246     * @return value corresponding to <code>recipient_name</code> parameter in
247     *         MAP specification
248     */
249    public String getRecipientName() {
250        return mRecipientName;
251    }
252
253    /**
254     * @return value corresponding to <code>recipient_addressing</code>
255     *         parameter in MAP specification
256     */
257    public String getRecipientAddressing() {
258        return mRecipientAddressing;
259    }
260
261    /**
262     * @return {@link Type} object corresponding to <code>type</code> parameter
263     *         in MAP specification
264     */
265    public Type getType() {
266        return mType;
267    }
268
269    /**
270     * @return value corresponding to <code>size</code> parameter in MAP
271     *         specification
272     */
273    public int getSize() {
274        return mSize;
275    }
276
277    /**
278     * @return {@link .ReceptionStatus} object corresponding to
279     *         <code>reception_status</code> parameter in MAP specification
280     */
281    public ReceptionStatus getReceptionStatus() {
282        return mReceptionStatus;
283    }
284
285    /**
286     * @return value corresponding to <code>attachment_size</code> parameter in
287     *         MAP specification
288     */
289    public int getAttachmentSize() {
290        return mAttachmentSize;
291    }
292
293    /**
294     * @return value corresponding to <code>text</code> parameter in MAP
295     *         specification
296     */
297    public boolean isText() {
298        return mText;
299    }
300
301    /**
302     * @return value corresponding to <code>priority</code> parameter in MAP
303     *         specification
304     */
305    public boolean isPriority() {
306        return mPriority;
307    }
308
309    /**
310     * @return value corresponding to <code>read</code> parameter in MAP
311     *         specification
312     */
313    public boolean isRead() {
314        return mRead;
315    }
316
317    /**
318     * @return value corresponding to <code>sent</code> parameter in MAP
319     *         specification
320     */
321    public boolean isSent() {
322        return mSent;
323    }
324
325    /**
326     * @return value corresponding to <code>protected</code> parameter in MAP
327     *         specification
328     */
329    public boolean isProtected() {
330        return mProtected;
331    }
332}
333