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.text.TextUtils;
19 import android.util.ArrayMap;
20 import android.util.Log;
21 
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 import java.util.Map;
28 
29 /**
30  * Class wrapping the raw OMTP message data, internally represented as as map of all key-value pairs
31  * found in the SMS body.
32  * <p>
33  * Provides convenience methods to extract parse fields of different types.
34  * <p>
35  * All the methods return null if either the field was not present or it could not be parsed.
36  */
37 public class WrappedMessageData {
38     private final String TAG = "WrappedMessageData";
39     private final String mPrefix;
40     private final Map<String, String> mFields;
41 
42     @Override
toString()43     public String toString() {
44         return "WrappedMessageData [mFields=" + mFields + "]";
45     }
46 
WrappedMessageData(String prefix, Map<String, String> keyValues)47     WrappedMessageData(String prefix, Map<String, String> keyValues) {
48         mPrefix = prefix;
49         mFields = new ArrayMap<String, String>();
50         mFields.putAll(keyValues);
51     }
52 
53     /**
54      * @return The String prefix of the message, designating whether this is the message data of a
55      * STATUS or SYNC sms.
56      */
getPrefix()57     String getPrefix() {
58         return mPrefix;
59     }
60 
61     /**
62      * Extracts the requested field from underlying data and returns the String value as is.
63      *
64      * @param field The requested field.
65      * @return the parsed string value, or null if the field was not present or not valid.
66      */
extractString(final String field)67     String extractString(final String field) {
68         String value = mFields.get(field);
69         if (value == null) {
70             return null;
71         }
72 
73         String[] possibleValues = OmtpConstants.possibleValuesMap.get(field);
74         if (possibleValues == null) {
75             return value;
76         }
77         for (int i = 0; i < possibleValues.length; i++) {
78             if (TextUtils.equals(value, possibleValues[i])) {
79                 return value;
80             }
81         }
82         Log.e(TAG, "extractString - value \"" + value +
83                 "\" of field \"" + field + "\" is not allowed.");
84         return null;
85     }
86 
87     /**
88      * Extracts the requested field from underlying data and parses it as an {@link Integer}.
89      *
90      * @param field The requested field.
91      * @return the parsed integer value, or null if the field was not present.
92      */
extractInteger(final String field)93     Integer extractInteger(final String field) {
94         String value = mFields.get(field);
95         if (value == null) {
96             return null;
97         }
98 
99         try {
100             return Integer.decode(value);
101         } catch (NumberFormatException e) {
102             Log.e(TAG, "extractInteger - could not parse integer: " + value);
103             return null;
104         }
105     }
106 
107     /**
108      * Extracts the requested field from underlying data and parses it as a date/time represented in
109      * {@link OmtpConstants#DATE_TIME_FORMAT} format.
110      *
111      * @param field The requested field.
112      * @return the parsed string value, or null if the field was not present.
113      */
extractTime(final String field)114     Long extractTime(final String field) {
115         String value = mFields.get(field);
116         if (value == null) {
117             return null;
118         }
119 
120         try {
121             return new SimpleDateFormat(
122                     OmtpConstants.DATE_TIME_FORMAT, Locale.US).parse(value).getTime();
123         } catch (ParseException e) {
124             Log.e(TAG, "extractTime - could not parse time: " + value);
125             return null;
126         }
127     }
128 }