"""Offline Android verifier for Ch50: ProgressDialog, DatePickerDialog & TimePickerDialog."""
import json
class MockProgressDialog:
STYLE_SPINNER = 0
STYLE_HORIZONTAL = 1
def __init__(self):
self.title = ""
self.message = ""
self.style = self.STYLE_SPINNER
self.indeterminate = True
self.cancelable = True
self.max_val = 100
self.progress = 0
self.showing = False
@classmethod
def show_static(cls, title: str, message: str, indeterminate: bool, cancelable: bool) -> "MockProgressDialog":
pd = cls()
pd.title = title
pd.message = message
pd.indeterminate = indeterminate
pd.cancelable = cancelable
pd.showing = True
return pd
def set_progress(self, value: int) -> None:
self.progress = min(self.max_val, value)
def dismiss(self) -> None:
self.showing = False
def format_date_picker_callback(year: int, month_of_year_zero_based: int, day_of_month: int) -> str:
# Android Calendar.MONTH / DatePickerDialog monthOfYear is 0-based (0=Jan .. 11=Dec)
return f"你选择的是{year}年{month_of_year_zero_based + 1}月{day_of_month}日"
def format_time_picker_callback(hour_of_day: int, minute: int, is_24_hour: bool) -> dict:
if is_24_hour:
display = f"{hour_of_day:02d}:{minute:02d}"
else:
suffix = "PM" if hour_of_day >= 12 else "AM"
h12 = hour_of_day % 12 or 12
display = f"{h12:02d}:{minute:02d} {suffix}"
return {"raw_text": f"您选择的时间是:{hour_of_day}时{minute}分", "formatted": display}
def main() -> None:
# Case 1: ProgressDialog static show vs configured horizontal indeterminate
pd1 = MockProgressDialog.show_static("资源加载中", "资源加载中,请稍后...", False, True)
pd_ind = MockProgressDialog()
pd_ind.title = "软件更新中"
pd_ind.style = MockProgressDialog.STYLE_HORIZONTAL
pd_ind.indeterminate = True
pd_ind.showing = True
act1 = {
"static_showing": pd1.showing,
"static_cancelable": pd1.cancelable,
"horizontal_indeterminate": [pd_ind.style, pd_ind.indeterminate],
}
exp1 = {"static_showing": True, "static_cancelable": True, "horizontal_indeterminate": [1, True]}
# Case 2: Determinate horizontal ProgressDialog + Handler what=123 loop auto-dismiss
pd2 = MockProgressDialog()
pd2.max_val = 100
pd2.style = MockProgressDialog.STYLE_HORIZONTAL
pd2.indeterminate = False
pd2.cancelable = False
pd2.showing = True
progress_start = 0
add = 0
updates = 0
while progress_start < pd2.max_val:
add += 1
progress_start = 2 * add
msg_what = 123
if msg_what == 123:
pd2.set_progress(progress_start)
updates += 1
if progress_start >= pd2.max_val:
pd2.dismiss()
act2 = {"updates": updates, "final_progress": pd2.progress, "dismissed": not pd2.showing}
exp2 = {"updates": 50, "final_progress": 100, "dismissed": True}
# Case 3: DatePickerDialog 0-based monthOfYear correction
act3 = {
"jan_index_0": format_date_picker_callback(2026, 0, 15),
"oct_index_9": format_date_picker_callback(2026, 9, 1),
"dec_index_11": format_date_picker_callback(2026, 11, 31),
}
exp3 = {
"jan_index_0": "你选择的是2026年1月15日",
"oct_index_9": "你选择的是2026年10月1日",
"dec_index_11": "你选择的是2026年12月31日",
}
# Case 4: TimePickerDialog 24h vs 12h display & system clock isolation
system_clock = "2026-10-01T08:00:00"
t24 = format_time_picker_callback(18, 35, is_24_hour=True)
t12 = format_time_picker_callback(18, 35, is_24_hour=False)
act4 = {"mode_24h": t24, "mode_12h": t12, "system_clock_unchanged": system_clock == "2026-10-01T08:00:00"}
exp4 = {
"mode_24h": {"raw_text": "您选择的时间是:18时35分", "formatted": "18:35"},
"mode_12h": {"raw_text": "您选择的时间是:18时35分", "formatted": "06:35 PM"},
"system_clock_unchanged": True,
}
cases = [
{
"name": "progress_dialog_static_and_indeterminate",
"summary": "验证 ProgressDialog 静态 show() 与水平不确定模式属性配置",
"expected": exp1,
"actual": act1,
"passed": act1 == exp1,
},
{
"name": "progress_dialog_handler_auto_dismiss",
"summary": "模拟子线程按 2*add 递增经 Handler(what=123) 推进进度至 100 后自动 dismiss",
"expected": exp2,
"actual": act2,
"passed": act2 == exp2,
},
{
"name": "datepicker_zero_based_month_offset",
"summary": "验证 DatePickerDialog 回调 monthOfYear 从 0 起始(0->1月、11->12月)的加 1 换算",
"expected": exp3,
"actual": act3,
"passed": act3 == exp3,
},
{
"name": "timepicker_24h_12h_and_clock_isolation",
"summary": "验证 TimePickerDialog 的 24/12 小时制格式化且不修改系统时间",
"expected": exp4,
"actual": act4,
"passed": act4 == exp4,
},
]
print(
json.dumps(
{
"version": "CPython 3.12.14 offline Android UI/Event/Handler verifier (Android SDK/emulator absent)",
"cases": cases,
},
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-dialog-lab/explore-tutorial-dialog.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-dialog.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1868.zip