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