"""Offline Android system-service verifier for Ch 136: TelephonyManager."""
import json
import xml.etree.ElementTree as ET
VERSION = "CPython 3.12.14 offline Android graphics/animation/media/service verifier (Android SDK/emulator absent)"
PHONE_INFO_XML = """<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<TextView android:id="@+id/tv_phone1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" />
<TextView android:id="@+id/tv_phone2" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" />
<TextView android:id="@+id/tv_phone3" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" />
<TextView android:id="@+id/tv_phone4" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" />
<TextView android:id="@+id/tv_phone5" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" />
<TextView android:id="@+id/tv_phone6" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" />
<TextView android:id="@+id/tv_phone7" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" />
<TextView android:id="@+id/tv_phone8" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" />
<TextView android:id="@+id/tv_phone9" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" />
</LinearLayout>
"""
PHONE_TYPES = {0: "PHONE_TYPE_NONE", 1: "PHONE_TYPE_GSM", 2: "PHONE_TYPE_CDMA", 3: "PHONE_TYPE_SIP"}
SIM_STATES = {
0: "SIM_STATE_UNKNOWN",
1: "SIM_STATE_ABSENT",
2: "SIM_STATE_PIN_REQUIRED",
3: "SIM_STATE_PUK_REQUIRED",
4: "SIM_STATE_NETWORK_LOCKED",
5: "SIM_STATE_READY",
}
NETWORK_TYPES = {1: "GPRS", 2: "EDGE", 3: "UMTS", 13: "LTE", 20: "NR"}
def gsm_asu_to_dbm(asu: int) -> int | None:
# 99 indicates unknown or undetectable GSM signal strength in 3GPP TS 27.007
if asu == 99 or asu < 0 or asu > 31:
return None
return -113 + 2 * asu
def main() -> None:
# Case 1: Dialer Intent URI & 9-TextView info layout + SIM/PhoneType/NetworkType constant maps
root = ET.fromstring(PHONE_INFO_XML)
dial_uri = f"tel:{'10086'}"
c1_actual = {
"dial_action": "android.intent.action.DIAL",
"dial_uri": dial_uri,
"dial_requires_call_phone_perm": False,
"info_textview_count": len(root.findall("TextView")),
"phone_type_1": PHONE_TYPES[1],
"sim_state_5": SIM_STATES[5],
"network_type_1_and_13": [NETWORK_TYPES[1], NETWORK_TYPES[13]],
}
c1_expected = {
"dial_action": "android.intent.action.DIAL",
"dial_uri": "tel:10086",
"dial_requires_call_phone_perm": False,
"info_textview_count": 9,
"phone_type_1": "PHONE_TYPE_GSM",
"sim_state_5": "SIM_STATE_READY",
"network_type_1_and_13": ["GPRS", "LTE"],
}
# Case 2: GSM SignalStrength ASU -> dBm formula (-113 + 2*asu) & unknown=99 handling
# asu=31 -> -51 dBm, asu=10 -> -93 dBm, asu=0 -> -113 dBm, asu=99 -> None
c2_actual = {
"asu_31_dbm": gsm_asu_to_dbm(31),
"asu_10_dbm": gsm_asu_to_dbm(10),
"asu_0_dbm": gsm_asu_to_dbm(0),
"asu_99_unknown": gsm_asu_to_dbm(99),
"listen_mask_290_has_signal_bit_256": bool(290 & 256),
}
c2_expected = {
"asu_31_dbm": -51,
"asu_10_dbm": -93,
"asu_0_dbm": -113,
"asu_99_unknown": None,
"listen_mask_290_has_signal_bit_256": True,
}
# Case 3: Call state transitions (IDLE=0, RINGING=1, OFFHOOK=2) & blacklist matching
blacklist = {"13800138000"}
events = [
("IDLE", ""),
("RINGING", "13800138000"),
("RINGING", "13900139000"),
("OFFHOOK", "13900139000"),
]
logged: list[str] = []
blocked: list[str] = []
for st, num in events:
if st == "RINGING":
logged.append(num)
if num in blacklist:
blocked.append(num)
c3_actual = {
"logged_ringing_numbers": logged,
"blocked_numbers": blocked,
}
c3_expected = {
"logged_ringing_numbers": ["13800138000", "13900139000"],
"blocked_numbers": ["13800138000"],
}
# Case 4: Android 6/9/10/12 permission & hidden-API evolution matrix
c4_actual = {
"runtime_permissions_api23": ["READ_PHONE_STATE", "ACCESS_COARSE_LOCATION", "CALL_PHONE"],
"incoming_number_extra_perm_api28": "READ_CALL_LOG",
"device_id_restricted_api29": True,
"aidl_endcall_replacement": "CallScreeningService / TelecomManager.endCall()",
"listener_replacement_api31": "TelephonyCallback",
}
c4_expected = {
"runtime_permissions_api23": ["READ_PHONE_STATE", "ACCESS_COARSE_LOCATION", "CALL_PHONE"],
"incoming_number_extra_perm_api28": "READ_CALL_LOG",
"device_id_restricted_api29": True,
"aidl_endcall_replacement": "CallScreeningService / TelecomManager.endCall()",
"listener_replacement_api31": "TelephonyCallback",
}
cases = [
{
"name": "telephony_dial_uri_and_info_constants",
"summary": "核验 ACTION_DIAL 拨号 URI、9 个信息项布局及 PhoneType/SimState/NetworkType 常量区分",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "gsm_signal_strength_asu_to_dbm",
"summary": "核验 GSM 信号公式 dBm = -113 + 2*asu 在 0/10/31 与未知值 99 下的边界换算",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "call_state_logging_and_blacklist_match",
"summary": "核验 CALL_STATE_RINGING 来电日志记录与黑名单号码匹配拦截逻辑",
"expected": c3_expected,
"actual": c3_actual,
"passed": c3_actual == c3_expected,
},
{
"name": "telephony_runtime_permissions_and_modern_apis",
"summary": "核验 API 23/28/29/31 下设备号限制、READ_CALL_LOG、CallScreeningService 与 TelephonyCallback 演进",
"expected": c4_expected,
"actual": c4_actual,
"passed": c4_actual == c4_expected,
},
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/android-tutorial-telephonymanager-lab/explore-tutorial-telephonymanager.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-telephonymanager.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1957.zip