配置方法
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
/**
* 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;
}