• 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.phone.vvm.omtp.sms;
17 
18 import android.util.ArrayMap;
19 import android.util.Log;
20 
21 import com.android.phone.vvm.omtp.OmtpConstants;
22 
23 import java.util.Map;
24 
25 /**
26  * OMTP SMS parser interface, for parsing SYNC and STATUS SMS sent by OMTP visual voicemail server.
27  */
28 public class OmtpSmsParser {
29     private static String TAG = "OmtpSmsParser";
30     /**
31      * Parses the supplied SMS body and returns back a structured OMTP message.
32      * Returns null if unable to parse the SMS body.
33      */
parse(String smsBody)34     public static WrappedMessageData parse(String smsBody) {
35         if (smsBody == null) {
36             return null;
37         }
38 
39         WrappedMessageData messageData = null;
40         if (smsBody.startsWith(OmtpConstants.SYNC_SMS_PREFIX)) {
41             messageData = new WrappedMessageData(OmtpConstants.SYNC_SMS_PREFIX,
42                     parseSmsBody(smsBody.substring(OmtpConstants.SYNC_SMS_PREFIX.length())));
43             // Check for a mandatory field.
44             String triggerEvent = messageData.extractString(OmtpConstants.SYNC_TRIGGER_EVENT);
45             if (triggerEvent == null) {
46                 Log.e(TAG, "Missing mandatory field: " + OmtpConstants.SYNC_TRIGGER_EVENT);
47                 return null;
48             }
49         } else if (smsBody.startsWith(OmtpConstants.STATUS_SMS_PREFIX)) {
50             messageData = new WrappedMessageData(OmtpConstants.STATUS_SMS_PREFIX,
51                     parseSmsBody(smsBody.substring(OmtpConstants.STATUS_SMS_PREFIX.length())));
52         }
53 
54         return messageData;
55     }
56 
57     /**
58      * Converts a String of key/value pairs into a Map object. The WrappedMessageData object
59      * contains helper functions to retrieve the values.
60      *
61      * e.g. "//VVM:STATUS:st=R;rc=0;srv=1;dn=1;ipt=1;spt=0;u=eg@example.com;pw=1"
62      * => "WrappedMessageData [mFields={st=R, ipt=1, srv=1, dn=1, u=eg@example.com, pw=1, rc=0}]"
63      *
64      * @param message The sms string with the prefix removed.
65      * @return A WrappedMessageData object containing the map.
66      */
parseSmsBody(String message)67     private static Map<String, String> parseSmsBody(String message) {
68         Map<String, String> keyValues = new ArrayMap<String, String>();
69         String[] entries = message.split(OmtpConstants.SMS_FIELD_SEPARATOR);
70         for (String entry : entries) {
71             String[] keyValue = entry.split(OmtpConstants.SMS_KEY_VALUE_SEPARATOR);
72             if (keyValue.length != 2) {
73                 continue;
74             }
75             keyValues.put(keyValue[0].trim(), keyValue[1].trim());
76         }
77 
78         return keyValues;
79     }
80 }