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