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 String dateTime = attrs.get("datetime"); 95 //Handle possible NPE when not able to retreive datetime attribute 96 if(dateTime != null){ 97 mDateTime = (new ObexTime(dateTime)).getTime(); 98 } else { 99 mDateTime = null; 100 } 101 102 103 mSenderName = attrs.get("sender_name"); 104 105 mSenderAddressing = attrs.get("sender_addressing"); 106 107 mReplytoAddressing = attrs.get("replyto_addressing"); 108 109 mRecipientName = attrs.get("recipient_name"); 110 111 mRecipientAddressing = attrs.get("recipient_addressing"); 112 113 mType = strToType(attrs.get("type")); 114 115 try { 116 size = Integer.parseInt(attrs.get("size")); 117 } catch (NumberFormatException e) { 118 size = 0; 119 } 120 121 mSize = size; 122 123 mText = yesnoToBoolean(attrs.get("text")); 124 125 mReceptionStatus = strToReceptionStatus(attrs.get("reception_status")); 126 127 try { 128 size = Integer.parseInt(attrs.get("attachment_size")); 129 } catch (NumberFormatException e) { 130 size = 0; 131 } 132 133 mAttachmentSize = size; 134 135 mPriority = yesnoToBoolean(attrs.get("priority")); 136 137 mRead = yesnoToBoolean(attrs.get("read")); 138 139 mSent = yesnoToBoolean(attrs.get("sent")); 140 141 mProtected = yesnoToBoolean(attrs.get("protected")); 142 } 143 144 private boolean yesnoToBoolean(String yesno) { 145 return "yes".equals(yesno); 146 } 147 148 private Type strToType(String s) { 149 if ("EMAIL".equals(s)) { 150 return Type.EMAIL; 151 } else if ("SMS_GSM".equals(s)) { 152 return Type.SMS_GSM; 153 } else if ("SMS_CDMA".equals(s)) { 154 return Type.SMS_CDMA; 155 } else if ("MMS".equals(s)) { 156 return Type.MMS; 157 } 158 159 return Type.UNKNOWN; 160 } 161 162 private ReceptionStatus strToReceptionStatus(String s) { 163 if ("complete".equals(s)) { 164 return ReceptionStatus.COMPLETE; 165 } else if ("fractioned".equals(s)) { 166 return ReceptionStatus.FRACTIONED; 167 } else if ("notification".equals(s)) { 168 return ReceptionStatus.NOTIFICATION; 169 } 170 171 return ReceptionStatus.UNKNOWN; 172 } 173 174 @Override 175 public String toString() { 176 JSONObject json = new JSONObject(); 177 178 try { 179 json.put("handle", mHandle); 180 json.put("subject", mSubject); 181 json.put("datetime", mDateTime); 182 json.put("sender_name", mSenderName); 183 json.put("sender_addressing", mSenderAddressing); 184 json.put("replyto_addressing", mReplytoAddressing); 185 json.put("recipient_name", mRecipientName); 186 json.put("recipient_addressing", mRecipientAddressing); 187 json.put("type", mType); 188 json.put("size", mSize); 189 json.put("text", mText); 190 json.put("reception_status", mReceptionStatus); 191 json.put("attachment_size", mAttachmentSize); 192 json.put("priority", mPriority); 193 json.put("read", mRead); 194 json.put("sent", mSent); 195 json.put("protected", mProtected); 196 } catch (JSONException e) { 197 // do nothing 198 } 199 200 return json.toString(); 201 } 202 203 /** 204 * @return value corresponding to <code>handle</code> parameter in MAP 205 * specification 206 */ 207 public String getHandle() { 208 return mHandle; 209 } 210 211 /** 212 * @return value corresponding to <code>subject</code> parameter in MAP 213 * specification 214 */ 215 public String getSubject() { 216 return mSubject; 217 } 218 219 /** 220 * @return <code>Date</code> object corresponding to <code>datetime</code> 221 * parameter in MAP specification 222 */ 223 public Date getDateTime() { 224 return mDateTime; 225 } 226 227 /** 228 * @return value corresponding to <code>sender_name</code> parameter in MAP 229 * specification 230 */ 231 public String getSenderName() { 232 return mSenderName; 233 } 234 235 /** 236 * @return value corresponding to <code>sender_addressing</code> parameter 237 * in MAP specification 238 */ 239 public String getSenderAddressing() { 240 return mSenderAddressing; 241 } 242 243 /** 244 * @return value corresponding to <code>replyto_addressing</code> parameter 245 * in MAP specification 246 */ 247 public String getReplytoAddressing() { 248 return mReplytoAddressing; 249 } 250 251 /** 252 * @return value corresponding to <code>recipient_name</code> parameter in 253 * MAP specification 254 */ 255 public String getRecipientName() { 256 return mRecipientName; 257 } 258 259 /** 260 * @return value corresponding to <code>recipient_addressing</code> 261 * parameter in MAP specification 262 */ 263 public String getRecipientAddressing() { 264 return mRecipientAddressing; 265 } 266 267 /** 268 * @return {@link Type} object corresponding to <code>type</code> parameter 269 * in MAP specification 270 */ 271 public Type getType() { 272 return mType; 273 } 274 275 /** 276 * @return value corresponding to <code>size</code> parameter in MAP 277 * specification 278 */ 279 public int getSize() { 280 return mSize; 281 } 282 283 /** 284 * @return {@link .ReceptionStatus} object corresponding to 285 * <code>reception_status</code> parameter in MAP specification 286 */ 287 public ReceptionStatus getReceptionStatus() { 288 return mReceptionStatus; 289 } 290 291 /** 292 * @return value corresponding to <code>attachment_size</code> parameter in 293 * MAP specification 294 */ 295 public int getAttachmentSize() { 296 return mAttachmentSize; 297 } 298 299 /** 300 * @return value corresponding to <code>text</code> parameter in MAP 301 * specification 302 */ 303 public boolean isText() { 304 return mText; 305 } 306 307 /** 308 * @return value corresponding to <code>priority</code> parameter in MAP 309 * specification 310 */ 311 public boolean isPriority() { 312 return mPriority; 313 } 314 315 /** 316 * @return value corresponding to <code>read</code> parameter in MAP 317 * specification 318 */ 319 public boolean isRead() { 320 return mRead; 321 } 322 323 /** 324 * @return value corresponding to <code>sent</code> parameter in MAP 325 * specification 326 */ 327 public boolean isSent() { 328 return mSent; 329 } 330 331 /** 332 * @return value corresponding to <code>protected</code> parameter in MAP 333 * specification 334 */ 335 public boolean isProtected() { 336 return mProtected; 337 } 338} 339