【笔记】CarrierConfig 配置参数的解析规则

配置方法

CarrierConfig应用中,可以使用mccmnc搭配各种mvno参数进行匹配,其中关于imsi正则匹配的案例如下:

    <carrier_config mcc="206" mnc="01" imsi="^206018014.*">
        <boolean name="carrier_name_override_bool" value="true" />
        <string name="carrier_name_string">UPC AT</string>
    </carrier_config>

 

carrier_config中,通过使用imsi属性的正则表达式"^206018014.*"来匹配以206018014为前缀的IMSI(国际移动用户识别码)。

这里的"^"符号表示匹配IMSI字符串的开头,"206018014"是具体的前缀值。而".*"表示匹配前缀后的任意字符。

所以,当设备的IMSI以206018014开头时,该carrier_config的配置将被匹配到,相应的carrier_name_string会被设置为"UPC AT",而carrier_name_override_bool将被设置为true。

Note:carrier_config是在设备的SIM卡插入时进行匹配的。如果IMSI不符合任何一个carrier_config的配置,则会使用默认的运营商配置。

代码逻辑

/packages/apps/CarrierConfig/

com/android/carrierconfig/DefaultCarrierConfigService.java

DefaultCarrierConfigService.java - OpenGrok cross reference for /packages/apps/CarrierConfig/src/com/android/carrierconfig/DefaultCarrierConfigService.javaicon-default.png?t=N7T8http://aospxref.com/android-14.0.0_r2/xref/packages/apps/CarrierConfig/src/com/android/carrierconfig/DefaultCarrierConfigService.java#295

    /**
     * Checks to see if an XML node matches carrier filters.
     *
     * <p>This iterates over the attributes of the current tag pointed to by {@code parser} and
     * checks each one against {@code id} or {@link Build.DEVICE} or {@link R.string#sku_filter} or
     * {@link Build.BOARD}. Attributes that are not specified in the node will not be checked, so a
     * node with no attributes will always return true. The supported filter attributes are,
     * <ul>
     *   <li>mcc: {@link CarrierIdentifier#getMcc}</li>
     *   <li>mnc: {@link CarrierIdentifier#getMnc}</li>
     *   <li>gid1: {@link CarrierIdentifier#getGid1}</li>
     *   <li>gid2: {@link CarrierIdentifier#getGid2}</li>
     *   <li>spn: {@link CarrierIdentifier#getSpn}</li>
     *   <li>imsi: {@link CarrierIdentifier#getImsi}</li>
     *   <li>device: {@link Build.DEVICE}</li>
     *   <li>vendorSku: {@link SystemConfig.VENDOR_SKU_PROPERTY}</li>
     *   <li>hardwareSku: {@link SystemConfig.SKU_PROPERTY}</li>
     *   <li>board: {@link Build.BOARD}</li>
     *   <li>cid: {@link CarrierIdentifier#getCarrierId()}
     *   or {@link CarrierIdentifier#getSpecificCarrierId()}</li>
     *   <li>sku: {@link R.string#sku_filter} "sku_filter" that OEM customizable filter</li>
     * </ul>
     * </p>
     *
     * <p>
     * The attributes imsi and spn can be expressed as regexp to filter on patterns.
     * The spn attribute can be set to the string "null" to allow matching against a SIM
     * with no spn set.
     * </p>
     *
     * @param parser an XmlPullParser pointing at a START_TAG with the attributes to check.
     * @param id the carrier details to check against.
     * @param sku a filter to be customizable.
     * @return false if any XML attribute does not match the corresponding value.
     */
    static boolean checkFilters(XmlPullParser parser, @Nullable CarrierIdentifier id, String sku) {
        String vendorSkuProperty = SystemProperties.get(
            "ro.boot.product.vendor.sku", "");
        String hardwareSkuProperty = SystemProperties.get(
            "ro.boot.product.hardware.sku", "");
        for (int i = 0; i < parser.getAttributeCount(); ++i) {
            boolean result = true;
            String attribute = parser.getAttributeName(i);
            String value = parser.getAttributeValue(i);
            switch (attribute) {

                case "imsi":
                    result = (id == null) || matchOnImsi(value, id);
                //可以在这里自定义过滤key值,如:
                //case "project":
                //    result = result && value.equalsIgnoreCase(Build.PROJECTKEY);
                //    break;
                default:
                    Log.e(TAG, "Unknown attribute " + attribute + "=" + value);
                    result = false;
                    break;
            }

            if (!result) {
                return false;
            }
        }
        return true;
    }


    /**
     * Check to see if the IMSI expression from the XML matches the IMSI of the
     * Carrier.
     *
     * @param xmlImsi IMSI expression fetched from the resource XML
     * @param id Id of the evaluated CarrierIdentifier
     * @return true if the XML IMSI matches the IMSI of CarrierIdentifier, false
     *         otherwise.
     */
    static boolean matchOnImsi(String xmlImsi, CarrierIdentifier id) {
        boolean matchFound = false;

        String currentImsi = id.getImsi();
        // If we were able to retrieve current IMSI, see if it matches.
        if (currentImsi != null) {
            Pattern imsiPattern = Pattern.compile(xmlImsi, Pattern.CASE_INSENSITIVE);
            Matcher matcher = imsiPattern.matcher(currentImsi);
            matchFound = matcher.matches();
        }
        return matchFound;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值