1 /* 2 * Copyright (C) 2015 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 package com.android.phone.vvm.omtp.sms; 17 18 import android.annotation.Nullable; 19 import android.os.Bundle; 20 21 import com.android.phone.NeededForTesting; 22 import com.android.phone.vvm.omtp.OmtpConstants; 23 24 import java.text.ParseException; 25 import java.text.SimpleDateFormat; 26 import java.util.Locale; 27 28 /** 29 * Structured data representation of an OMTP SYNC message. 30 * 31 * Getters will return null if the field was not set in the message body or it could not be parsed. 32 */ 33 public class SyncMessage { 34 // Sync event that triggered this message. 35 private final String mSyncTriggerEvent; 36 // Total number of new messages on the server. 37 private final int mNewMessageCount; 38 // UID of the new message. 39 private final String mMessageId; 40 // Length of the message. 41 private final int mMessageLength; 42 // Content type (voice, video, fax...) of the new message. 43 private final String mContentType; 44 // Sender of the new message. 45 private final String mSender; 46 // Timestamp (in millis) of the new message. 47 private final long mMsgTimeMillis; 48 49 @Override toString()50 public String toString() { 51 return "SyncMessage [mSyncTriggerEvent=" + mSyncTriggerEvent 52 + ", mNewMessageCount=" + mNewMessageCount 53 + ", mMessageId=" + mMessageId 54 + ", mMessageLength=" + mMessageLength 55 + ", mContentType=" + mContentType 56 + ", mSender=" + mSender 57 + ", mMsgTimeMillis=" + mMsgTimeMillis + "]"; 58 } 59 SyncMessage(Bundle wrappedData)60 public SyncMessage(Bundle wrappedData) { 61 mSyncTriggerEvent = getString(wrappedData, OmtpConstants.SYNC_TRIGGER_EVENT); 62 mMessageId = getString(wrappedData, OmtpConstants.MESSAGE_UID); 63 mMessageLength = getInt(wrappedData, OmtpConstants.MESSAGE_LENGTH); 64 mContentType = getString(wrappedData, OmtpConstants.CONTENT_TYPE); 65 mSender = getString(wrappedData, OmtpConstants.SENDER); 66 mNewMessageCount = getInt(wrappedData, OmtpConstants.NUM_MESSAGE_COUNT); 67 mMsgTimeMillis = parseTime(wrappedData.getString(OmtpConstants.TIME)); 68 } 69 parseTime(@ullable String value)70 private static long parseTime(@Nullable String value) { 71 if (value == null) { 72 return 0L; 73 } 74 try { 75 return new SimpleDateFormat( 76 OmtpConstants.DATE_TIME_FORMAT, Locale.US) 77 .parse(value).getTime(); 78 } catch (ParseException e) { 79 return 0L; 80 } 81 } 82 /** 83 * @return the event that triggered the sync message. This is a mandatory field and must always 84 * be set. 85 */ getSyncTriggerEvent()86 public String getSyncTriggerEvent() { 87 return mSyncTriggerEvent; 88 } 89 90 /** 91 * @return the number of new messages stored on the voicemail server. 92 */ 93 @NeededForTesting getNewMessageCount()94 public int getNewMessageCount() { 95 return mNewMessageCount; 96 } 97 98 /** 99 * @return the message ID of the new message. 100 * <p> 101 * Expected to be set only for 102 * {@link com.android.phone.vvm.omtp.OmtpConstants#NEW_MESSAGE} 103 */ getId()104 public String getId() { 105 return mMessageId; 106 } 107 108 /** 109 * @return the content type of the new message. 110 * <p> 111 * Expected to be set only for 112 * {@link com.android.phone.vvm.omtp.OmtpConstants#NEW_MESSAGE} 113 */ 114 @NeededForTesting getContentType()115 public String getContentType() { 116 return mContentType; 117 } 118 119 /** 120 * @return the message length of the new message. 121 * <p> 122 * Expected to be set only for 123 * {@link com.android.phone.vvm.omtp.OmtpConstants#NEW_MESSAGE} 124 */ getLength()125 public int getLength() { 126 return mMessageLength; 127 } 128 129 /** 130 * @return the sender's phone number of the new message specified as MSISDN. 131 * <p> 132 * Expected to be set only for 133 * {@link com.android.phone.vvm.omtp.OmtpConstants#NEW_MESSAGE} 134 */ getSender()135 public String getSender() { 136 return mSender; 137 } 138 139 /** 140 * @return the timestamp as milliseconds for the new message. 141 * <p> 142 * Expected to be set only for 143 * {@link com.android.phone.vvm.omtp.OmtpConstants#NEW_MESSAGE} 144 */ getTimestampMillis()145 public long getTimestampMillis() { 146 return mMsgTimeMillis; 147 } 148 getInt(Bundle wrappedData, String key)149 private static int getInt(Bundle wrappedData, String key) { 150 String value = wrappedData.getString(key); 151 if (value == null) { 152 return 0; 153 } 154 try { 155 return Integer.parseInt(value); 156 } catch (NumberFormatException e) { 157 return 0; 158 } 159 } 160 getString(Bundle wrappedData, String key)161 private static String getString(Bundle wrappedData, String key) { 162 String value = wrappedData.getString(key); 163 if (value == null) { 164 return ""; 165 } 166 return value; 167 } 168 }