summaryrefslogtreecommitdiff
path: root/src/android/bluetooth/client/map/BluetoothMapMessage.java
blob: 5ce6c4be57500c0d3330d019fc6d929bb6c79bd3 (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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 android.bluetooth.client.map;
import android.bluetooth.client.map.utils.ObexTime;

import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;

/**
 * Object representation of message received in messages listing
 * <p>
 * This object will be received in
 * {@link BluetoothMasClient#EVENT_GET_MESSAGES_LISTING} callback message.
 */
public class BluetoothMapMessage {

    private final String mHandle;

    private final String mSubject;

    private final Date mDateTime;

    private final String mSenderName;

    private final String mSenderAddressing;

    private final String mReplytoAddressing;

    private final String mRecipientName;

    private final String mRecipientAddressing;

    private final Type mType;

    private final int mSize;

    private final boolean mText;

    private final ReceptionStatus mReceptionStatus;

    private final int mAttachmentSize;

    private final boolean mPriority;

    private final boolean mRead;

    private final boolean mSent;

    private final boolean mProtected;

    public enum Type {
        UNKNOWN, EMAIL, SMS_GSM, SMS_CDMA, MMS
    };

    public enum ReceptionStatus {
        UNKNOWN, COMPLETE, FRACTIONED, NOTIFICATION
    }

    BluetoothMapMessage(HashMap<String, String> attrs) throws IllegalArgumentException {
        int size;

        try {
            /* just to validate */
            new BigInteger(attrs.get("handle"), 16);

            mHandle = attrs.get("handle");
        } catch (NumberFormatException e) {
            /*
             * handle MUST have proper value, if it does not then throw
             * something here
             */
            throw new IllegalArgumentException(e);
        }

        mSubject = attrs.get("subject");
        String dateTime = attrs.get("datetime");
        //Handle possible NPE when not able to retreive datetime attribute
        if(dateTime != null){
            mDateTime = (new ObexTime(dateTime)).getTime();
        } else {
            mDateTime = null;
        }


        mSenderName = attrs.get("sender_name");

        mSenderAddressing = attrs.get("sender_addressing");

        mReplytoAddressing = attrs.get("replyto_addressing");

        mRecipientName = attrs.get("recipient_name");

        mRecipientAddressing = attrs.get("recipient_addressing");

        mType = strToType(attrs.get("type"));

        try {
            size = Integer.parseInt(attrs.get("size"));
        } catch (NumberFormatException e) {
            size = 0;
        }

        mSize = size;

        mText = yesnoToBoolean(attrs.get("text"));

        mReceptionStatus = strToReceptionStatus(attrs.get("reception_status"));

        try {
            size = Integer.parseInt(attrs.get("attachment_size"));
        } catch (NumberFormatException e) {
            size = 0;
        }

        mAttachmentSize = size;

        mPriority = yesnoToBoolean(attrs.get("priority"));

        mRead = yesnoToBoolean(attrs.get("read"));

        mSent = yesnoToBoolean(attrs.get("sent"));

        mProtected = yesnoToBoolean(attrs.get("protected"));
    }

    private boolean yesnoToBoolean(String yesno) {
        return "yes".equals(yesno);
    }

    private Type strToType(String s) {
        if ("EMAIL".equals(s)) {
            return Type.EMAIL;
        } else if ("SMS_GSM".equals(s)) {
            return Type.SMS_GSM;
        } else if ("SMS_CDMA".equals(s)) {
            return Type.SMS_CDMA;
        } else if ("MMS".equals(s)) {
            return Type.MMS;
        }

        return Type.UNKNOWN;
    }

    private ReceptionStatus strToReceptionStatus(String s) {
        if ("complete".equals(s)) {
            return ReceptionStatus.COMPLETE;
        } else if ("fractioned".equals(s)) {
            return ReceptionStatus.FRACTIONED;
        } else if ("notification".equals(s)) {
            return ReceptionStatus.NOTIFICATION;
        }

        return ReceptionStatus.UNKNOWN;
    }

    @Override
    public String toString() {
        JSONObject json = new JSONObject();

        try {
            json.put("handle", mHandle);
            json.put("subject", mSubject);
            json.put("datetime", mDateTime);
            json.put("sender_name", mSenderName);
            json.put("sender_addressing", mSenderAddressing);
            json.put("replyto_addressing", mReplytoAddressing);
            json.put("recipient_name", mRecipientName);
            json.put("recipient_addressing", mRecipientAddressing);
            json.put("type", mType);
            json.put("size", mSize);
            json.put("text", mText);
            json.put("reception_status", mReceptionStatus);
            json.put("attachment_size", mAttachmentSize);
            json.put("priority", mPriority);
            json.put("read", mRead);
            json.put("sent", mSent);
            json.put("protected", mProtected);
        } catch (JSONException e) {
            // do nothing
        }

        return json.toString();
    }

    /**
     * @return value corresponding to <code>handle</code> parameter in MAP
     *         specification
     */
    public String getHandle() {
        return mHandle;
    }

    /**
     * @return value corresponding to <code>subject</code> parameter in MAP
     *         specification
     */
    public String getSubject() {
        return mSubject;
    }

    /**
     * @return <code>Date</code> object corresponding to <code>datetime</code>
     *         parameter in MAP specification
     */
    public Date getDateTime() {
        return mDateTime;
    }

    /**
     * @return value corresponding to <code>sender_name</code> parameter in MAP
     *         specification
     */
    public String getSenderName() {
        return mSenderName;
    }

    /**
     * @return value corresponding to <code>sender_addressing</code> parameter
     *         in MAP specification
     */
    public String getSenderAddressing() {
        return mSenderAddressing;
    }

    /**
     * @return value corresponding to <code>replyto_addressing</code> parameter
     *         in MAP specification
     */
    public String getReplytoAddressing() {
        return mReplytoAddressing;
    }

    /**
     * @return value corresponding to <code>recipient_name</code> parameter in
     *         MAP specification
     */
    public String getRecipientName() {
        return mRecipientName;
    }

    /**
     * @return value corresponding to <code>recipient_addressing</code>
     *         parameter in MAP specification
     */
    public String getRecipientAddressing() {
        return mRecipientAddressing;
    }

    /**
     * @return {@link Type} object corresponding to <code>type</code> parameter
     *         in MAP specification
     */
    public Type getType() {
        return mType;
    }

    /**
     * @return value corresponding to <code>size</code> parameter in MAP
     *         specification
     */
    public int getSize() {
        return mSize;
    }

    /**
     * @return {@link .ReceptionStatus} object corresponding to
     *         <code>reception_status</code> parameter in MAP specification
     */
    public ReceptionStatus getReceptionStatus() {
        return mReceptionStatus;
    }

    /**
     * @return value corresponding to <code>attachment_size</code> parameter in
     *         MAP specification
     */
    public int getAttachmentSize() {
        return mAttachmentSize;
    }

    /**
     * @return value corresponding to <code>text</code> parameter in MAP
     *         specification
     */
    public boolean isText() {
        return mText;
    }

    /**
     * @return value corresponding to <code>priority</code> parameter in MAP
     *         specification
     */
    public boolean isPriority() {
        return mPriority;
    }

    /**
     * @return value corresponding to <code>read</code> parameter in MAP
     *         specification
     */
    public boolean isRead() {
        return mRead;
    }

    /**
     * @return value corresponding to <code>sent</code> parameter in MAP
     *         specification
     */
    public boolean isSent() {
        return mSent;
    }

    /**
     * @return value corresponding to <code>protected</code> parameter in MAP
     *         specification
     */
    public boolean isProtected() {
        return mProtected;
    }
}