1 /* 2 * Copyright (C) 2016 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.carrierdefaultapp; 17 18 import android.content.Context; 19 import android.content.Intent; 20 import android.os.PersistableBundle; 21 import android.telephony.CarrierConfigManager; 22 import android.telephony.TelephonyManager; 23 import android.text.TextUtils; 24 import android.util.Log; 25 26 import com.android.internal.util.ArrayUtils; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 /** 32 * Default carrier app allows carrier customization. OEMs could configure a list 33 * of carrier actions defined in {@link com.android.carrierdefaultapp.CarrierActionUtils 34 * CarrierActionUtils} to act upon certain signal or even different args of the same signal. 35 * This allows different interpretations of the signal between carriers and could easily alter the 36 * app's behavior in a configurable way. This helper class loads and parses the carrier configs 37 * and return a list of predefined carrier actions for the given input signal. 38 */ 39 public class CustomConfigLoader { 40 // delimiters for parsing carrier configs of the form "arg1, arg2 : action1, action2" 41 private static final String INTRA_GROUP_DELIMITER = "\\s*,\\s*"; 42 private static final String INTER_GROUP_DELIMITER = "\\s*:\\s*"; 43 44 private static final String TAG = CustomConfigLoader.class.getSimpleName(); 45 private static final boolean VDBG = Log.isLoggable(TAG, Log.VERBOSE); 46 47 /** 48 * loads and parses the carrier config, return a list of carrier action for the given signal 49 * @param context 50 * @param intent passing signal for config match 51 * @return a list of carrier action for the given signal based on the carrier config. 52 * 53 * Example: input intent TelephonyManager.ACTION_CARRIER_SIGNAL_REQUEST_NETWORK_FAILED 54 * This intent allows fined-grained matching based on both intent type & extra values: 55 * apnType and errorCode. 56 * apnType read from passing intent is "default" and errorCode is 0x26 for example and 57 * returned carrier config from carrier_default_actions_on_redirection_string_array is 58 * { 59 * "default, 0x26:1,4", // 0x26(NETWORK_FAILURE) 60 * "default, 0x70:2,3" // 0x70(APN_TYPE_CONFLICT) 61 * } 62 * [1, 4] // 1(CARRIER_ACTION_DISABLE_METERED_APNS), 4(CARRIER_ACTION_SHOW_PORTAL_NOTIFICATION) 63 * returns as the action index list based on the matching rule. 64 */ loadCarrierActionList(Context context, Intent intent)65 public static List<Integer> loadCarrierActionList(Context context, Intent intent) { 66 CarrierConfigManager carrierConfigManager = (CarrierConfigManager) context.getSystemService( 67 Context.CARRIER_CONFIG_SERVICE); 68 // return an empty list if no match found 69 List<Integer> actionList = new ArrayList<>(); 70 if (carrierConfigManager == null) { 71 Log.e(TAG, "load carrier config failure with carrier config manager uninitialized"); 72 return actionList; 73 } 74 PersistableBundle b = carrierConfigManager.getConfig(); 75 if (b != null) { 76 String[] configs = null; 77 // used for intents which allow fine-grained interpretation based on intent extras 78 String arg1 = null; 79 String arg2 = null; 80 switch (intent.getAction()) { 81 case TelephonyManager.ACTION_CARRIER_SIGNAL_REDIRECTED: 82 configs = b.getStringArray(CarrierConfigManager 83 .KEY_CARRIER_DEFAULT_ACTIONS_ON_REDIRECTION_STRING_ARRAY); 84 break; 85 case TelephonyManager.ACTION_CARRIER_SIGNAL_REQUEST_NETWORK_FAILED: 86 configs = b.getStringArray(CarrierConfigManager 87 .KEY_CARRIER_DEFAULT_ACTIONS_ON_DCFAILURE_STRING_ARRAY); 88 arg1 = intent.getStringExtra(TelephonyManager.EXTRA_APN_TYPE); 89 arg2 = intent.getStringExtra(TelephonyManager.EXTRA_ERROR_CODE); 90 break; 91 case TelephonyManager.ACTION_CARRIER_SIGNAL_RESET: 92 configs = b.getStringArray(CarrierConfigManager 93 .KEY_CARRIER_DEFAULT_ACTIONS_ON_RESET); 94 break; 95 case TelephonyManager.ACTION_CARRIER_SIGNAL_DEFAULT_NETWORK_AVAILABLE: 96 configs = b.getStringArray(CarrierConfigManager 97 .KEY_CARRIER_DEFAULT_ACTIONS_ON_DEFAULT_NETWORK_AVAILABLE); 98 arg1 = String.valueOf(intent.getBooleanExtra(TelephonyManager 99 .EXTRA_DEFAULT_NETWORK_AVAILABLE, false)); 100 break; 101 default: 102 Log.e(TAG, "load carrier config failure with un-configured key: " 103 + intent.getAction()); 104 break; 105 } 106 if (!ArrayUtils.isEmpty(configs)) { 107 for (String config : configs) { 108 // parse each config until find the matching one 109 matchConfig(config, arg1, arg2, actionList); 110 if (!actionList.isEmpty()) { 111 // return the first match 112 if (VDBG) Log.d(TAG, "found match action list: " + actionList.toString()); 113 return actionList; 114 } 115 } 116 } 117 Log.d(TAG, "no matching entry for signal: " + intent.getAction() + "arg1: " + arg1 118 + "arg2: " + arg2); 119 } 120 return actionList; 121 } 122 123 /** 124 * Match based on the config's format and input args 125 * passing arg1, arg2 should match the format of the config 126 * case 1: config {actionIdx1, actionIdx2...} arg1 and arg2 must be null 127 * case 2: config {arg1, arg2 : actionIdx1, actionIdx2...} requires full match of non-null args 128 * case 3: config {arg1 : actionIdx1, actionIdx2...} only need to match arg1 129 * 130 * @param config action list config obtained from CarrierConfigManager 131 * @param arg1 first intent argument, set if required for config match 132 * @param arg2 second intent argument, set if required for config match 133 * @param actionList append each parsed action to the passing list 134 */ matchConfig(String config, String arg1, String arg2, List<Integer> actionList)135 private static void matchConfig(String config, String arg1, String arg2, 136 List<Integer> actionList) { 137 String[] splitStr = config.trim().split(INTER_GROUP_DELIMITER, 2); 138 String actionStr = null; 139 140 if (splitStr.length == 1 && arg1 == null && arg2 == null) { 141 // case 1 142 actionStr = splitStr[0]; 143 } else if (splitStr.length == 2 && arg1 != null && arg2 != null) { 144 // case 2 145 String[] args = splitStr[0].split(INTRA_GROUP_DELIMITER); 146 if (args.length == 2 && TextUtils.equals(arg1, args[0]) && 147 TextUtils.equals(arg2, args[1])) { 148 actionStr = splitStr[1]; 149 } 150 } else if ((splitStr.length == 2) && (arg1 != null) && (arg2 == null)) { 151 // case 3 152 String[] args = splitStr[0].split(INTRA_GROUP_DELIMITER); 153 if (args.length == 1 && TextUtils.equals(arg1, args[0])) { 154 actionStr = splitStr[1]; 155 } 156 } 157 // convert from string -> action idx list if found a matching entry 158 String[] actions = null; 159 if (!TextUtils.isEmpty(actionStr)) { 160 actions = actionStr.split(INTRA_GROUP_DELIMITER); 161 } 162 if (!ArrayUtils.isEmpty(actions)) { 163 for (String idx : actions) { 164 try { 165 actionList.add(Integer.parseInt(idx)); 166 } catch (NumberFormatException e) { 167 Log.e(TAG, "NumberFormatException(string: " + idx + " config:" + config + "): " 168 + e); 169 } 170 } 171 } 172 } 173 } 174