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