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