"""Offline Android Activity data passing, result roundtrip, ActivityCollector, and window config verifier."""
import json
VERSION = "CPython 3.12.14 offline Android component/IPC/Fragment verifier (Android SDK/emulator absent)"
def case_activity_bundle_pass_and_result_roundtrip():
# Models ActivityTest1 (one-way Bundle) + ActivityTest2 (startActivityForResult -> setResult -> onActivityResult)
max_safe_bundle_bytes = 512 * 1024 # 0.5 MB safe threshold below 1 MB Binder transaction buffer
def send_bundle(payload: dict) -> tuple[bool, str]:
encoded = json.dumps(payload, ensure_ascii=False).encode("utf-8")
if len(encoded) >= max_safe_bundle_bytes:
return False, "TransactionTooLargeException"
return True, "OK"
one_way_bundle = {"user": "Alice", "age": 22}
ok_small, status_small = send_bundle(one_way_bundle)
ok_large, status_large = send_bundle({"blob": "x" * (600 * 1024)})
# Two-way result callback
request_code = 101
result_code = -1 # Activity.RESULT_OK
returned_bundle = {"selected_city": "Hangzhou"}
received = None
if request_code == 101 and result_code == -1:
received = returned_bundle["selected_city"]
actual = {
"small_bundle_ok": ok_small,
"small_bundle_status": status_small,
"oversize_bundle_ok": ok_large,
"oversize_bundle_status": status_large,
"result_roundtrip_city": received,
}
expected = {
"small_bundle_ok": True,
"small_bundle_status": "OK",
"oversize_bundle_ok": False,
"oversize_bundle_status": "TransactionTooLargeException",
"result_roundtrip_city": "Hangzhou",
}
return {
"name": "activity_bundle_pass_and_result_roundtrip",
"summary": "验证 Activity 单向 Bundle 传参、startActivityForResult/onActivityResult 双向回传,以及超过 0.5MB 阈值的 TransactionTooLargeException 防护。",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_activity_collector_and_double_back_exit():
class FakeActivity:
def __init__(self, cls_name: str):
self.cls_name = cls_name
self.finishing = False
def finish(self):
self.finishing = True
collector: list[FakeActivity] = []
for name in ("LoginActivity", "HomeActivity", "DetailActivity"):
collector.append(FakeActivity(name))
current_class_log = collector[-1].cls_name
finished_names = []
for act in collector:
if not act.finishing:
act.finish()
finished_names.append(act.cls_name)
collector.clear()
# Double-back exit via timestamp diff (2000 ms window)
def simulate_double_back(tap_times_ms: list[int], window_ms: int = 2000) -> list[str]:
exit_time = 0
actions = []
for t in tap_times_ms:
if (t - exit_time) > window_ms:
actions.append("TOAST_AGAIN")
exit_time = t
else:
actions.append("EXIT_APP")
return actions
actual = {
"current_class_log": current_class_log,
"finished_all": finished_names,
"remaining_after_clear": len(collector),
"double_back_within_2s": simulate_double_back([10000, 11200]),
"double_back_over_2s": simulate_double_back([10000, 12500, 13400]),
}
expected = {
"current_class_log": "DetailActivity",
"finished_all": ["LoginActivity", "HomeActivity", "DetailActivity"],
"remaining_after_clear": 0,
"double_back_within_2s": ["TOAST_AGAIN", "EXIT_APP"],
"double_back_over_2s": ["TOAST_AGAIN", "TOAST_AGAIN", "EXIT_APP"],
}
return {
"name": "activity_collector_and_double_back_exit",
"summary": "验证 BaseActivity 当前类名追踪、ActivityCollector 一键关闭全部页面,以及 2000ms 双击返回退出时间窗判定。",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_transition_animation_fullscreen_dialog_theme():
style_animation_slots = {
"activityOpenEnterAnimation": "B_enter_on_A_to_B",
"activityOpenExitAnimation": "A_exit_on_A_to_B",
"activityCloseEnterAnimation": "A_enter_on_B_back_to_A",
"activityCloseExitAnimation": "B_exit_on_B_back_to_A",
}
def check_no_title_order(calls: list[str]) -> str:
idx_req = calls.index("requestWindowFeature(FEATURE_NO_TITLE)")
idx_set = calls.index("setContentView")
return "OK" if idx_req < idx_set else "AndroidRuntimeException"
# onWindowFocusChanged(hasFocus=True) fires after view tree is measured
focus_measurements = []
for has_focus, measured_w in ((True, 1080), (False, 1080)):
if has_focus:
focus_measurements.append(measured_w)
actual = {
"animation_slot_count": len(style_animation_slots),
"open_enter_target": style_animation_slots["activityOpenEnterAnimation"],
"no_title_before_set_content": check_no_title_order(["requestWindowFeature(FEATURE_NO_TITLE)", "setContentView"]),
"no_title_after_set_content": check_no_title_order(["setContentView", "requestWindowFeature(FEATURE_NO_TITLE)"]),
"focus_measured_width": focus_measurements,
"dialog_theme": "@android:style/Theme.Dialog",
"dumpsys_cmd": "adb shell dumpsys activity",
}
expected = {
"animation_slot_count": 4,
"open_enter_target": "B_enter_on_A_to_B",
"no_title_before_set_content": "OK",
"no_title_after_set_content": "AndroidRuntimeException",
"focus_measured_width": [1080],
"dialog_theme": "@android:style/Theme.Dialog",
"dumpsys_cmd": "adb shell dumpsys activity",
}
return {
"name": "transition_animation_fullscreen_dialog_theme",
"summary": "验证 Activity 四项全局过场动画槽位、requestWindowFeature 调用时序、onWindowFocusChanged 获取测量宽度与对话框主题配置。",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_activity_bundle_pass_and_result_roundtrip(),
case_activity_collector_and_double_back_exit(),
case_transition_animation_fullscreen_dialog_theme(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Android 组件/生命周期/IPC/Fragment 确定性验证环境(本地未安装 Android SDK/Gradle/ADB/模拟器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Android 组件/生命周期/IPC/Fragment 确定性验证环境(本地未安装 Android SDK/Gradle/ADB/模拟器)。
入口文件:projects/android-tutorial-activity-start-lab/explore-tutorial-activity-start.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-activity-start.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Android 组件/生命周期/IPC/Fragment 确定性验证环境(本地未安装 Android SDK/Gradle/ADB/模拟器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1834.zip
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip