SongFerngWang | 154bc96 | 2018-10-23 16:47:40 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | package com.android.phone; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.XmlResourceParser; |
| 21 | import android.telephony.TelephonyManager; |
| 22 | import android.text.TextUtils; |
| 23 | import android.util.Log; |
| 24 | |
| 25 | import org.xmlpull.v1.XmlPullParser; |
| 26 | import org.xmlpull.v1.XmlPullParserException; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.HashMap; |
| 31 | import java.util.Map; |
| 32 | import java.util.Vector; |
| 33 | import java.util.regex.Matcher; |
| 34 | import java.util.regex.Pattern; |
| 35 | |
| 36 | /** |
| 37 | * CarrierXmlParser is a xml parser. It parses the carrier's ussd format from carrier_ss_string.xml. |
| 38 | * The carrier_ss_string.xml defines carrier's ussd structure and meaning in res/xml folder. |
| 39 | * Some carrier has specific ussd structure ,developer can add new xml and xml is named |
| 40 | * carrier_ss_string_carrierId.xml. The carrierId is a number and is defined in |
| 41 | * <a href="https://android.googlesource.com/platform/packages/providers/TelephonyProvider/+/master/assets/carrier_list.textpb">here</a> |
| 42 | * For example: carrier_ss_string_850.xml |
| 43 | * <p> |
| 44 | * How do use CarrierXmlParser? |
| 45 | * For example: |
| 46 | * @see CallForwardEditPreference |
| 47 | * TelephonyManager telephonyManager = new TelephonyManager(getContext(),phone.getSubId()); |
| 48 | * CarrierXmlParser = new CarrierXmlParser(getContext(), telephonyManager.getSimCarrierId()); |
| 49 | * |
| 50 | * //make a ussd command |
| 51 | * String newUssdCommand = mCarrierXmlParser.getFeature( |
| 52 | * CarrierXmlParser.FEATURE_CALL_FORWARDING).makeCommand(inputAction, inputCfInfo); |
| 53 | * //analyze ussd result |
| 54 | * HashMap<String, String> analysisResult = mCarrierXmlParser.getFeature( |
| 55 | * CarrierXmlParser.FEATURE_CALL_FORWARDING) |
| 56 | * .getResponseSet(mSsAction, response.toString()); |
| 57 | */ |
| 58 | public class CarrierXmlParser { |
| 59 | public static final String LOG_TAG = "CarrierXmlParser"; |
| 60 | private static final boolean DEBUG = true; |
| 61 | |
| 62 | private static final String STAR_SIGN = "*"; |
| 63 | private static final String POUND_SIGN = "#"; |
| 64 | |
| 65 | private static final String TAG_SIGN = "tag_"; |
| 66 | |
| 67 | // To define feature's item name in xml |
| 68 | public static final String FEATURE_CALL_FORWARDING = "callforwarding"; |
| 69 | public static final String FEATURE_CALLER_ID = "callerid"; |
| 70 | |
| 71 | // COMMAND_NAME is xml's command name. |
| 72 | public static final String TAG_COMMAND_NAME_QUERY = "query"; |
| 73 | public static final String TAG_COMMAND_NAME_ACTIVATE = "activate"; |
| 74 | public static final String TAG_COMMAND_NAME_DEACTIVATE = "deactivate"; |
| 75 | |
| 76 | // To define string level in xml. |
| 77 | // level 1 |
| 78 | private static final String TAG_FEATURE = "feature"; |
| 79 | private static final String TAG_REGULAR_PARSER = "regular_parser"; |
| 80 | // level 2 |
| 81 | private static final String TAG_COMMAND = "command"; |
| 82 | // level 3 |
| 83 | private static final String TAG_SERVICE_CODE = "service_code"; |
| 84 | private static final String TAG_ACTION_CODE = "action_code"; |
| 85 | private static final String TAG_PARAMETER = "parameter"; |
| 86 | private static final String TAG_RESPONSE_FORMAT = "response_format"; |
| 87 | private static final String TAG_COMMAND_RESULT = "command_result"; |
| 88 | // level 4 |
| 89 | private static final String TAG_ENTRY = "entry"; |
| 90 | |
| 91 | private static final String ATTR_NAME = "name"; |
| 92 | private static final String ATTR_PARAMETER_NUM = "number"; |
| 93 | private static final String ATTR_POSITION = "position"; |
| 94 | private static final String ATTR_RESULT_KEY = "key"; |
| 95 | private static final String ATTR_DEFINITION_KEY = "definition"; |
| 96 | |
| 97 | HashMap<String, SsFeature> mFeatureMaps; |
| 98 | private static String sParserFormat = ""; |
| 99 | |
| 100 | // TAG_ENTRY_NUMBER and TAG_ENTRY_TIME is xml's entry value. |
| 101 | // This is mapping user's input value. For example: number,time ... |
| 102 | // When UI makes command ,it will map the value and insert this value at position location. |
| 103 | // How to use it? |
| 104 | // The UI calls CarrierXmlParser's makeCommand function and inputs the hashmap which |
| 105 | // includes tag_name and user's input value. |
| 106 | // For example: User calls CarrierXmlParser's makeCommand in call forwarding , and inputs |
| 107 | // the hashmap {<TAG_ENTRY_NUMBER,0123456789>,<TAG_ENTRY_TIME,20>} |
| 108 | // If developer wants to add new one, xml string should the same as hashmap's name. |
| 109 | public static final String TAG_ENTRY_NUMBER = "tag_number"; |
| 110 | public static final String TAG_ENTRY_TIME = "tag_time"; |
| 111 | |
| 112 | // "response_format" key |
| 113 | // The key of "response_format" should define as below in xml. |
| 114 | // The UI will use it to define value from the response command |
| 115 | // and use the data show the screen. |
| 116 | public static final String TAG_RESPONSE_STATUS = "status_code"; |
| 117 | public static final String TAG_RESPONSE_STATUS_ERROR = "RESPONSE_ERROR"; |
| 118 | public static final String TAG_RESPONSE_NUMBER = "number"; |
| 119 | public static final String TAG_RESPONSE_TIME = "time"; |
| 120 | |
| 121 | // This is the definition for the entry's key in response_format. |
| 122 | // Xml's COMMAND_RESULT_DEFINITION should same as below. |
| 123 | public static final String TAG_COMMAND_RESULT_DEFINITION_ACTIVATE = "activate"; |
| 124 | public static final String TAG_COMMAND_RESULT_DEFINITION_DEACTIVATE = "deactivate"; |
| 125 | public static final String TAG_COMMAND_RESULT_DEFINITION_UNREGISTER = "unregister"; |
| 126 | public static final String TAG_COMMAND_RESULT_DEFINITION_OK = "ok"; |
| 127 | public static final String TAG_COMMAND_RESULT_DEFINITION_FAIL = "fail"; |
| 128 | |
| 129 | /** |
| 130 | * UssdParser is a string parser. It parses the USSD response message. |
| 131 | */ |
| 132 | public static class UssdParser { |
| 133 | private Vector<String> mParserStr = new Vector<String>(); |
| 134 | private Pattern mPatternSuppServiceResponse; |
| 135 | |
| 136 | public UssdParser(String inputParserFormat) { |
| 137 | mPatternSuppServiceResponse = Pattern.compile(inputParserFormat); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * This function is a parser and analyzes the USSD responses message. |
| 142 | * |
| 143 | * @param responseString The USSD responses message. |
| 144 | */ |
| 145 | public void newFromResponseString(String responseString) { |
| 146 | Matcher m; |
| 147 | m = mPatternSuppServiceResponse.matcher(responseString); |
| 148 | if (m.matches()) { |
| 149 | mParserStr.clear(); |
| 150 | int groupSize = m.groupCount(); |
| 151 | for (int i = 0; i <= groupSize; i++) { |
| 152 | if (!TextUtils.isEmpty(m.group(i))) { |
| 153 | mParserStr.add(m.group(i)); |
SongFerngWang | 45c6ad5 | 2018-11-29 17:29:05 +0800 | [diff] [blame] | 154 | } else { |
| 155 | mParserStr.add(""); |
SongFerngWang | 154bc96 | 2018-10-23 16:47:40 +0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * To get the UssdParser result. |
| 163 | */ |
| 164 | public Vector<String> getResult() { |
| 165 | return mParserStr; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * CarrierXmlParser parses command from xml and saves in SsEntry class. |
| 171 | */ |
| 172 | public static class SsEntry { |
| 173 | public enum SSAction { |
| 174 | UNKNOWN, |
| 175 | QUERY, |
| 176 | UPDATE_ACTIVATE, |
| 177 | UPDATE_DEACTIVATE |
| 178 | } |
| 179 | |
| 180 | public String serviceCode; |
| 181 | public SSAction ssAction = SSAction.UNKNOWN; |
| 182 | public String actionCode; |
| 183 | public HashMap<Integer, String> commandParameter = new HashMap<Integer, String>(); |
| 184 | public HashMap<Integer, String> responseFormat = new HashMap<Integer, String>(); |
| 185 | |
| 186 | public SsEntry(String action) { |
| 187 | if (action.equals(TAG_COMMAND_NAME_QUERY)) { |
| 188 | ssAction = SSAction.QUERY; |
| 189 | } else if (action.equals(TAG_COMMAND_NAME_ACTIVATE)) { |
| 190 | ssAction = SSAction.UPDATE_ACTIVATE; |
| 191 | } else if (action.equals(TAG_COMMAND_NAME_DEACTIVATE)) { |
| 192 | ssAction = SSAction.UPDATE_DEACTIVATE; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public String toString() { |
| 198 | return "SsEntry serviceCode:" + serviceCode |
| 199 | + ", ssAction:" + ssAction |
| 200 | + ", actionCode:" + actionCode |
| 201 | + ", commandParameter:" + commandParameter.toString() |
| 202 | + ", responseFormat:" + responseFormat.toString(); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * To get the caller id command by xml's structure. |
| 207 | */ |
| 208 | public String getCommandStructure() { |
| 209 | String result = actionCode + serviceCode; |
| 210 | int mapSize = commandParameter.size(); |
| 211 | int parameterIndex = 0; |
| 212 | while (parameterIndex < mapSize) { |
| 213 | parameterIndex++; |
| 214 | if (commandParameter.containsKey(parameterIndex)) { |
| 215 | if (commandParameter.get(parameterIndex) != null) { |
| 216 | result = result + STAR_SIGN + commandParameter.get(parameterIndex); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | result = result + POUND_SIGN; |
| 221 | Log.d(LOG_TAG, "getCommandStructure result:" + result); |
| 222 | return result; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * To make ussd command by xml's structure. |
| 227 | * |
| 228 | * @param inputInformationSet This is a map which includes parameters from UI. |
| 229 | * The name of map is mapping parameter's key of entry in xml. |
| 230 | */ |
| 231 | public String makeCommand(Map<String, String> inputInformationSet) { |
| 232 | String result = actionCode + serviceCode; |
| 233 | int mapSize = commandParameter.size(); |
| 234 | int parameterIndex = 0; |
| 235 | int counter = 1; |
| 236 | Map<String, String> informationSet = inputInformationSet; |
| 237 | while (parameterIndex < mapSize) { |
| 238 | if (commandParameter.containsKey(counter)) { |
| 239 | String getInputValue = ""; |
| 240 | // need to handle tag_XXXX |
| 241 | if (informationSet != null && informationSet.size() > 0 |
| 242 | && informationSet.containsKey(commandParameter.get(counter))) { |
| 243 | getInputValue = informationSet.get(commandParameter.get(counter)); |
| 244 | } |
| 245 | if (TextUtils.isEmpty(getInputValue)) { |
| 246 | result = result + STAR_SIGN + commandParameter.get(counter); |
| 247 | } else { |
| 248 | result = result + STAR_SIGN + informationSet.get( |
| 249 | commandParameter.get(counter)); |
| 250 | } |
| 251 | parameterIndex++; |
| 252 | } else { |
| 253 | result = result + STAR_SIGN; |
| 254 | } |
| 255 | counter++; |
| 256 | } |
| 257 | result = result + POUND_SIGN; |
| 258 | return result; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * To parse the specific key and value from response message. |
| 263 | * |
| 264 | * @param inputResponse This is a ussd response message from network. |
| 265 | * @param responseDefine This is the definition for "command_result" in xml. |
| 266 | */ |
| 267 | public HashMap<String, String> getResponseSet(String inputResponse, |
| 268 | HashMap<String, ArrayList<SsResultEntry>> responseDefine) { |
| 269 | HashMap<String, String> responseSet = new HashMap<String, String>(); |
| 270 | if (TextUtils.isEmpty(sParserFormat)) { |
| 271 | return responseSet; |
| 272 | } |
| 273 | UssdParser parserResult = new UssdParser(sParserFormat); |
| 274 | parserResult.newFromResponseString(inputResponse); |
| 275 | if (parserResult == null) { |
| 276 | return responseSet; |
| 277 | } |
| 278 | |
| 279 | Vector<String> result = parserResult.getResult(); |
| 280 | |
| 281 | if (result == null) { |
| 282 | return responseSet; |
| 283 | } |
| 284 | for (int i = 0; i < result.size(); i++) { |
| 285 | if (responseFormat.containsKey(i)) { |
| 286 | String defineString = ""; |
| 287 | if (responseDefine.containsKey(responseFormat.get(i))) { |
| 288 | for (int x = 0; x < responseDefine.get(responseFormat.get(i)).size(); x++) { |
| 289 | defineString = ((SsResultEntry) responseDefine.get( |
| 290 | responseFormat.get(i)).get(x)).getDefinitionByCompareValue( |
| 291 | result.get(i)); |
| 292 | if (!TextUtils.isEmpty(defineString)) { |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | // if status_code do not match definition value, we will set command error. |
| 297 | if (TAG_RESPONSE_STATUS.equals(responseFormat.get(i))) { |
| 298 | if (TextUtils.isEmpty(defineString)) { |
| 299 | responseSet.put(TAG_RESPONSE_STATUS_ERROR, |
| 300 | TAG_RESPONSE_STATUS_ERROR); |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | if (TextUtils.isEmpty(defineString)) { |
| 305 | responseSet.put(responseFormat.get(i), result.get(i)); |
| 306 | } else { |
| 307 | responseSet.put(responseFormat.get(i), defineString); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | return responseSet; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * CarrierXmlParser parses command_result from xml and saves in SsResultEntry class. |
| 317 | */ |
| 318 | public static class SsResultEntry { |
| 319 | String mDefinition; |
| 320 | String mCompareValue; |
| 321 | |
| 322 | public SsResultEntry() { |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public String toString() { |
| 327 | return "SsResultEntry mDefinition:" + mDefinition |
| 328 | + ", mCompareValue:" + mCompareValue; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * If mCompareValue item is the same as compare value,it will return the mDefinition. |
| 333 | * |
| 334 | * @param inputValue This is the entry of response command's value. |
| 335 | * @return mDefinition or null. |
| 336 | */ |
| 337 | public String getDefinitionByCompareValue(String inputValue) { |
| 338 | if (mCompareValue.equals(inputValue)) { |
| 339 | return mDefinition; |
| 340 | } |
| 341 | return null; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * CarrierXmlParser parses feature from xml and saves in SsFeature class. |
| 347 | */ |
| 348 | public class SsFeature { |
| 349 | public HashMap<SsEntry.SSAction, SsEntry> ssEntryHashMap = |
| 350 | new HashMap<SsEntry.SSAction, SsEntry>(); |
| 351 | public HashMap<String, ArrayList<SsResultEntry>> responseCode = |
| 352 | new HashMap<String, ArrayList<SsResultEntry>>(); |
| 353 | |
| 354 | public SsFeature() { |
| 355 | } |
| 356 | |
| 357 | private String getResponseCodeString() { |
| 358 | String result = ""; |
| 359 | for (Map.Entry<String, ArrayList<SsResultEntry>> entry : responseCode.entrySet()) { |
| 360 | ArrayList<SsResultEntry> values = entry.getValue(); |
| 361 | for (int i = 0; i < values.size(); i++) { |
| 362 | result += "value of i is " + ((SsResultEntry) values.get(i)).toString(); |
| 363 | } |
| 364 | } |
| 365 | return result; |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public String toString() { |
| 370 | return getResponseCodeString(); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * To get the caller id command by xml's structure. |
| 375 | * |
| 376 | * @param inputAction This is action_code of command item from xml. |
| 377 | */ |
| 378 | public String getCommandStructure(SsEntry.SSAction inputAction) { |
| 379 | SsEntry entry = ssEntryHashMap.get(inputAction); |
| 380 | return entry.getCommandStructure(); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * To make the ussd command by xml structure |
| 385 | * |
| 386 | * @param inputAction This is action_code of command item from xml. |
| 387 | * @param inputInformationSet This is for parameter of command. |
| 388 | * @return The ussd command string. |
| 389 | */ |
| 390 | public String makeCommand(SsEntry.SSAction inputAction, |
| 391 | Map<String, String> inputInformationSet) { |
| 392 | SsEntry entry = ssEntryHashMap.get(inputAction); |
| 393 | return entry.makeCommand(inputInformationSet); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * To parse the special key and value from response message. |
| 398 | * |
| 399 | * @param inputAction This is action_code of command item from xml. |
| 400 | * @param inputResponse This is response message from network. |
| 401 | * @return The set includes specific key and value. |
| 402 | */ |
| 403 | public HashMap<String, String> getResponseSet(SsEntry.SSAction inputAction, |
| 404 | String inputResponse) { |
| 405 | SsEntry entry = ssEntryHashMap.get(inputAction); |
| 406 | return entry.getResponseSet(inputResponse, responseCode); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * @param context context to get res's xml |
| 412 | * @param carrierId carrier id of the current subscription. The carrier ID is an Android |
| 413 | * platform-wide identifier for a carrier. AOSP maintains carrier ID assignments in |
| 414 | * <a href="https://android.googlesource.com/platform/packages/providers/TelephonyProvider/+/master/assets/carrier_list.textpb">here</a> |
| 415 | */ |
| 416 | public CarrierXmlParser(Context context, int carrierId) { |
| 417 | try { |
| 418 | int xmlResId = 0; |
| 419 | if (carrierId != TelephonyManager.UNKNOWN_CARRIER_ID) { |
| 420 | String xmlResIdName = "carrier_ss_string" + "_" + carrierId; |
| 421 | xmlResId = context.getResources().getIdentifier(xmlResIdName, "xml", |
| 422 | context.getPackageName()); |
| 423 | } |
| 424 | if (xmlResId == 0) { |
| 425 | xmlResId = R.xml.carrier_ss_string; |
| 426 | } |
| 427 | Log.d(LOG_TAG, "carrierId: " + carrierId); |
| 428 | |
| 429 | XmlResourceParser parser = context.getResources().getXml(xmlResId); |
| 430 | mFeatureMaps = parseXml(parser); |
| 431 | } catch (Exception e) { |
| 432 | Log.d(LOG_TAG, "Error parsing XML " + e.toString()); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | private HashMap<String, SsFeature> parseXml(XmlResourceParser parser) throws IOException { |
| 437 | HashMap<String, SsFeature> features = new HashMap<String, SsFeature>(); |
| 438 | try { |
| 439 | int eventType = parser.getEventType(); |
| 440 | while (eventType != XmlPullParser.END_DOCUMENT) { |
| 441 | if (eventType == XmlPullParser.START_TAG) { |
| 442 | if (TAG_REGULAR_PARSER.equals(parser.getName())) { |
| 443 | sParserFormat = readText(parser); |
| 444 | Log.d(LOG_TAG, "sParserFormat " + sParserFormat); |
| 445 | } else if (TAG_FEATURE.equals(parser.getName())) { |
| 446 | String featureName = getSpecificAttributeValue(parser, ATTR_NAME); |
| 447 | if (!TextUtils.isEmpty(featureName)) { |
| 448 | SsFeature feature = generateFeatureList(parser); |
| 449 | features.put(featureName, feature); |
| 450 | Log.d(LOG_TAG, "add " + featureName + " to map:" + feature.toString()); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | parser.next(); |
| 455 | eventType = parser.getEventType(); |
| 456 | } |
| 457 | } catch (XmlPullParserException e) { |
| 458 | e.printStackTrace(); |
| 459 | } |
| 460 | return features; |
| 461 | } |
| 462 | |
| 463 | private SsFeature generateFeatureList(XmlResourceParser parser) |
| 464 | throws XmlPullParserException, IOException { |
| 465 | SsFeature ssfeature = new SsFeature(); |
| 466 | int outerDepth = parser.getDepth(); |
| 467 | |
| 468 | Log.d(LOG_TAG, "generateFeatureList outerDepth" + outerDepth); |
| 469 | |
| 470 | while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 471 | Log.d(LOG_TAG, "generateFeatureList parser.getDepth()" + parser.getDepth()); |
| 472 | |
| 473 | int eventType = parser.getEventType(); |
| 474 | if (eventType == XmlPullParser.END_TAG |
| 475 | && outerDepth == parser.getDepth()) { |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | if (eventType != XmlPullParser.START_TAG) { |
| 480 | continue; |
| 481 | } |
| 482 | String name = parser.getName(); |
| 483 | // Starts by looking for the command tag. |
| 484 | if (TAG_COMMAND.equals(name)) { |
| 485 | SsEntry entry = readCommandEntry(parser); |
| 486 | ssfeature.ssEntryHashMap.put(entry.ssAction, entry); |
| 487 | } else if (TAG_COMMAND_RESULT.equals(name)) { |
| 488 | readCommandResultEntry(parser, ssfeature); |
| 489 | } |
| 490 | } |
| 491 | return ssfeature; |
| 492 | } |
| 493 | |
| 494 | private void readCommandResultEntry(XmlResourceParser parser, SsFeature ssFeature) |
| 495 | throws XmlPullParserException, IOException { |
| 496 | while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 497 | int eventType = parser.getEventType(); |
| 498 | if (eventType == XmlPullParser.END_TAG |
| 499 | && TAG_COMMAND_RESULT.equals(parser.getName())) { |
| 500 | break; |
| 501 | } |
| 502 | if (eventType == XmlPullParser.START_TAG |
| 503 | && TAG_ENTRY.equals(parser.getName())) { |
| 504 | String key = getSpecificAttributeValue(parser, ATTR_RESULT_KEY); |
| 505 | if (!TextUtils.isEmpty(key)) { |
| 506 | SsResultEntry entry = new SsResultEntry(); |
| 507 | entry.mDefinition = getSpecificAttributeValue(parser, ATTR_DEFINITION_KEY); |
| 508 | entry.mCompareValue = readText(parser); |
| 509 | if (ssFeature.responseCode.containsKey(key)) { |
| 510 | ssFeature.responseCode.get(key).add(entry); |
| 511 | } else { |
| 512 | ArrayList<SsResultEntry> arrayList = new ArrayList<>(); |
| 513 | arrayList.add(entry); |
| 514 | ssFeature.responseCode.put(key, arrayList); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | private SsEntry readCommandEntry(XmlResourceParser parser) |
| 522 | throws XmlPullParserException, IOException { |
| 523 | int outerDepth = parser.getDepth(); |
| 524 | String command_action = getSpecificAttributeValue(parser, ATTR_NAME); |
| 525 | SsEntry entry = new SsEntry(command_action); |
| 526 | |
| 527 | while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 528 | int eventType = parser.getEventType(); |
| 529 | if (eventType == XmlPullParser.END_TAG |
| 530 | && outerDepth == parser.getDepth()) { |
| 531 | break; |
| 532 | } |
| 533 | |
| 534 | if (eventType != XmlPullParser.START_TAG) { |
| 535 | continue; |
| 536 | } |
| 537 | |
| 538 | String name = parser.getName(); |
| 539 | if (TAG_SERVICE_CODE.equals(name)) { |
| 540 | entry.serviceCode = readText(parser); |
| 541 | } else if (TAG_ACTION_CODE.equals(name)) { |
| 542 | entry.actionCode = readText(parser); |
| 543 | } else if (TAG_PARAMETER.equals(name)) { |
| 544 | String number = getSpecificAttributeValue(parser, ATTR_PARAMETER_NUM); |
| 545 | if (!TextUtils.isEmpty(number)) { |
| 546 | readParameters(parser, entry, Integer.valueOf(number), TAG_PARAMETER); |
| 547 | } |
| 548 | } else if (TAG_RESPONSE_FORMAT.equals(name)) { |
| 549 | String number = getSpecificAttributeValue(parser, ATTR_PARAMETER_NUM); |
| 550 | if (!TextUtils.isEmpty(number)) { |
| 551 | readParameters(parser, entry, Integer.valueOf(number), TAG_RESPONSE_FORMAT); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | Log.d(LOG_TAG, "ssEntry:" + entry.toString()); |
| 556 | return entry; |
| 557 | } |
| 558 | |
| 559 | private void readParameters(XmlResourceParser parser, SsEntry entry, int num, String parentTag) |
| 560 | throws IOException, XmlPullParserException { |
| 561 | Log.d(LOG_TAG, "readParameters() nume:" + num); |
| 562 | int i = 0; |
| 563 | while (i < num) { |
| 564 | if (parser.next() == XmlPullParser.START_TAG) { |
| 565 | String name = parser.getName(); |
| 566 | if (TAG_ENTRY.equals(name)) { |
| 567 | String position = getSpecificAttributeValue(parser, ATTR_POSITION); |
| 568 | if (!TextUtils.isEmpty(position)) { |
| 569 | if (TAG_PARAMETER.equals(parentTag)) { |
| 570 | String value = readText(parser); |
| 571 | if (!TextUtils.isEmpty(value)) { |
| 572 | entry.commandParameter.put(Integer.valueOf(position), value); |
| 573 | } |
| 574 | } else if (TAG_RESPONSE_FORMAT.equals(parentTag)) { |
| 575 | String key = getSpecificAttributeValue(parser, ATTR_RESULT_KEY); |
| 576 | if (!TextUtils.isEmpty(key)) { |
| 577 | entry.responseFormat.put(Integer.valueOf(position), key); |
| 578 | } |
| 579 | } |
| 580 | i++; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | private String getSpecificAttributeValue(XmlResourceParser parser, String attrTag) { |
| 588 | String value = ""; |
| 589 | for (int i = 0; i < parser.getAttributeCount(); i++) { |
| 590 | if (attrTag.equals(parser.getAttributeName(i))) { |
| 591 | value = parser.getAttributeValue(i); |
| 592 | } |
| 593 | } |
| 594 | return value; |
| 595 | } |
| 596 | |
| 597 | private String readText(XmlResourceParser parser) throws IOException, XmlPullParserException { |
| 598 | String result = ""; |
| 599 | if (parser.next() == XmlPullParser.TEXT) { |
| 600 | result = parser.getText(); |
| 601 | parser.nextTag(); |
| 602 | } |
| 603 | return result; |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * CarrierXmlParser parses the xml and saves in mFeatureMap. |
| 608 | * To use this function get feature from the mFeatureMaps. |
| 609 | * |
| 610 | * @param inputFeatureName This is feature's name from xml. |
| 611 | */ |
| 612 | public SsFeature getFeature(String inputFeatureName) { |
| 613 | return mFeatureMaps.get(inputFeatureName); |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * To check the command which is dialed by user is caller id command. |
| 618 | * <p> |
| 619 | * If it is a caller id command which sets to activate, return the {@code |
| 620 | * SsEntry.SSAction.UPDATE_ACTIVATE}. |
| 621 | * If it is a caller id command which sets to deactivate, return the {@code |
| 622 | * SsEntry.SSAction.UPDATE_DEACTIVATE}. |
| 623 | * If it is not a caller id command, return the {@code SsEntry.SSAction.UNKNOWN}. |
| 624 | * |
| 625 | * @param inputCommand This is caller id's ussd command which is dialed by user. |
| 626 | * @return {@link SsEntry.SSAction} |
| 627 | */ |
| 628 | public SsEntry.SSAction getCallerIdUssdCommandAction(String inputCommand) { |
| 629 | if (isCallerIdActivate(inputCommand)) { |
| 630 | return SsEntry.SSAction.UPDATE_ACTIVATE; |
| 631 | } |
| 632 | if (isCallerIdDeactivate(inputCommand)) { |
| 633 | return SsEntry.SSAction.UPDATE_DEACTIVATE; |
| 634 | } |
| 635 | return SsEntry.SSAction.UNKNOWN; |
| 636 | } |
| 637 | |
| 638 | private String getCallerIdActivateCommandFromXml() { |
| 639 | return getFeature(FEATURE_CALLER_ID).getCommandStructure(SsEntry.SSAction.UPDATE_ACTIVATE); |
| 640 | } |
| 641 | |
| 642 | private String getCallerIdDeactivateCommandFromXml() { |
| 643 | return getFeature(FEATURE_CALLER_ID).getCommandStructure( |
| 644 | SsEntry.SSAction.UPDATE_DEACTIVATE); |
| 645 | } |
| 646 | |
| 647 | private boolean isCallerIdActivate(String inputStr) { |
| 648 | String activateStr = getCallerIdActivateCommandFromXml(); |
| 649 | return compareCommand(activateStr, inputStr); |
| 650 | } |
| 651 | |
| 652 | private boolean isCallerIdDeactivate(String inputStr) { |
| 653 | String activateStr = getCallerIdDeactivateCommandFromXml(); |
| 654 | return compareCommand(activateStr, inputStr); |
| 655 | } |
| 656 | |
| 657 | private boolean compareCommand(String activateStr, String inputStr) { |
| 658 | String[] activateArray = activateStr.split("\\" + STAR_SIGN); |
| 659 | String[] inputArray = inputStr.split("\\" + STAR_SIGN); |
| 660 | |
| 661 | if (activateArray.length == 0 || inputArray.length == 0) { |
| 662 | return false; |
| 663 | } |
| 664 | for (int i = 0; i < activateArray.length; i++) { |
| 665 | if (activateArray[i].startsWith(TAG_SIGN)) { |
| 666 | continue; |
| 667 | } |
| 668 | if (!activateArray[i].equals(inputArray[i])) { |
| 669 | Log.d(LOG_TAG, "compare fails:" + activateStr + "," + inputStr); |
| 670 | return false; |
| 671 | } |
| 672 | } |
| 673 | Log.d(LOG_TAG, "compare success"); |
| 674 | return true; |
| 675 | } |
| 676 | } |