"""Offline deterministic verifier for Android CH01 (tutorial-contents)."""
from __future__ import annotations
import json
import xml.etree.ElementTree as ET
from collections import deque
ANDROID_NS = "{http://schemas.android.com/apk/res/android}"
def case_curriculum_module_catalog() -> dict:
modules = [
("M01_env_and_project", "环境搭建与工程基础", 14, []),
("M02_ui_layouts_widgets", "UI 布局与常用控件", 40, ["M01_env_and_project"]),
("M03_events_and_async", "事件处理与异步消息", 8, ["M02_ui_layouts_widgets"]),
("M04_four_components", "四大组件与 Intent", 12, ["M03_events_and_async"]),
("M05_fragment", "Fragment 模块化界面", 6, ["M04_four_components"]),
("M06_data_storage", "本地数据存储与 SQLite", 4, ["M04_four_components"]),
("M07_network_webview_socket", "网络通信与 WebView/Socket", 20, ["M03_events_and_async", "M06_data_storage"]),
("M08_graphics_and_animation", "图形绘制与动画机制", 27, ["M02_ui_layouts_widgets"]),
("M09_multimedia", "多媒体音视频与相机", 4, ["M04_four_components"]),
("M10_system_services_sensors", "系统服务、传感器与 GPS", 14, ["M04_four_components"]),
("M11_capstone_summary", "全课程总结与实战进阶", 1, ["M05_fragment", "M07_network_webview_socket", "M08_graphics_and_animation", "M09_multimedia", "M10_system_services_sensors"]),
]
indeg = {m[0]: len(m[3]) for m in modules}
children: dict[str, list[str]] = {m[0]: [] for m in modules}
for mid, _, _, deps in modules:
for d in deps:
children[d].append(mid)
q = deque(sorted(mid for mid, deg in indeg.items() if deg == 0))
topo = []
while q:
cur = q.popleft()
topo.append(cur)
for nxt in sorted(children[cur]):
indeg[nxt] -= 1
if indeg[nxt] == 0:
q.append(nxt)
actual = {
"module_count": len(modules),
"listed_toc_entries": sum(m[2] for m in modules),
"dag_acyclic": len(topo) == len(modules),
"first_module": topo[0],
"last_module": topo[-1],
}
expected = {
"module_count": 11,
"listed_toc_entries": 150,
"dag_acyclic": True,
"first_module": "M01_env_and_project",
"last_module": "M11_capstone_summary",
}
return {
"name": "curriculum_module_catalog_integrity",
"summary": "校验 11 大模块共 150 项目录条目的依赖 DAG 无环性与学习先后路径",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_manifest_four_components() -> dict:
manifest_xml = """<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.mbsky.android.roadmap">
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="RoadmapApp">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".SyncService" android:exported="false" />
<receiver android:name=".BootReceiver" android:exported="false" />
<provider android:name=".NoteProvider" android:authorities="org.mbsky.notes" android:exported="false" />
</application>
</manifest>"""
root = ET.fromstring(manifest_xml)
app = root.find("application")
assert app is not None
counts = {tag: len(app.findall(tag)) for tag in ("activity", "service", "receiver", "provider")}
launcher = None
for act in app.findall("activity"):
for flt in act.findall("intent-filter"):
actions = {a.attrib.get(f"{ANDROID_NS}name") for a in flt.findall("action")}
cats = {c.attrib.get(f"{ANDROID_NS}name") for c in flt.findall("category")}
if "android.intent.action.MAIN" in actions and "android.intent.category.LAUNCHER" in cats:
launcher = act.attrib.get(f"{ANDROID_NS}name")
actual = {
"package": root.attrib["package"],
"component_counts": counts,
"launcher_activity": launcher,
}
expected = {
"package": "org.mbsky.android.roadmap",
"component_counts": {"activity": 1, "service": 1, "receiver": 1, "provider": 1},
"launcher_activity": ".MainActivity",
}
return {
"name": "android_manifest_and_component_taxonomy",
"summary": "解析包含四大组件声明的 AndroidManifest.xml 并定位 LAUNCHER 入口",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_dp_sp_density_scaling() -> dict:
buckets = [
("ldpi", 120),
("mdpi", 160),
("hdpi", 240),
("xhdpi", 320),
("xxhdpi", 480),
("xxxhdpi", 640),
]
touch_target_dp = 48
actual = {name: int(round(touch_target_dp * (dpi / 160.0))) for name, dpi in buckets}
expected = {
"ldpi": 36,
"mdpi": 48,
"hdpi": 72,
"xhdpi": 96,
"xxhdpi": 144,
"xxxhdpi": 192,
}
return {
"name": "dp_sp_density_bucket_scaling",
"summary": "验证 48dp 标准触控目标在 ldpi 至 xxxhdpi 六档屏幕密度下的物理像素换算",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_modern_toolchain_migration() -> dict:
migration = {
"Eclipse_ADT": "Android Studio + Gradle Kotlin DSL",
"Dalvik_VM": "ART (AOT/PGO)",
"Support_v4_v7": "AndroidX / Jetpack",
"ListView_BaseAdapter": "RecyclerView / Jetpack Compose LazyColumn",
"AsyncTask": "Kotlin Coroutines / WorkManager",
"Apache_HttpClient": "OkHttp / Retrofit",
}
actual = {
"tracked_transitions": len(migration),
"ide_target": migration["Eclipse_ADT"],
"async_target": migration["AsyncTask"],
"http_target": migration["Apache_HttpClient"],
}
expected = {
"tracked_transitions": 6,
"ide_target": "Android Studio + Gradle Kotlin DSL",
"async_target": "Kotlin Coroutines / WorkManager",
"http_target": "OkHttp / Retrofit",
}
return {
"name": "modern_android_toolchain_migration_map",
"summary": "核对 2015 基线教程主题向现代 AndroidX/Gradle/Jetpack 生态的演进映射",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main() -> None:
cases = [
case_curriculum_module_catalog(),
case_manifest_four_components(),
case_dp_sp_density_scaling(),
case_modern_toolchain_migration(),
]
payload = {
"version": "CPython 3.12.14 offline Android XML/layout/manifest verifier (Android SDK/emulator absent)",
"cases": cases,
}
print(json.dumps(payload, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/android-tutorial-contents-lab/explore-tutorial-contents.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-contents.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1862.zip