蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 Configuration 常量核对、横竖屏尺寸互换与 API 13+ configChanges 规则离线验证

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-tutorial-configuration.py
explore-tutorial-configuration.py178 行
"""Offline Android verifier for Ch60: Configuration properties, orientation toggle & configChanges."""
import json
from dataclasses import dataclass

ORIENTATION_PORTRAIT = 1
ORIENTATION_LANDSCAPE = 2
HARDKEYBOARDHIDDEN_UNDEFINED = 0
HARDKEYBOARDHIDDEN_NO = 1
HARDKEYBOARDHIDDEN_YES = 2
KEYBOARD_NOKEYS = 1
KEYBOARD_QWERTY = 2
KEYBOARD_12KEY = 3
NAVIGATION_NONAV = 1
TOUCHSCREEN_FINGER = 3


@dataclass
class Configuration:
    density_dpi: int
    font_scale: float
    hard_keyboard_hidden: int
    keyboard: int
    keyboard_hidden: int
    locale: str
    mcc: int
    mnc: int
    navigation: int
    orientation: int
    screen_width_dp: int
    screen_height_dp: int
    smallest_screen_width_dp: int
    touchscreen: int

    def rotate(self) -> "Configuration":
        new_ori = ORIENTATION_LANDSCAPE if self.orientation == ORIENTATION_PORTRAIT else ORIENTATION_PORTRAIT
        return Configuration(
            density_dpi=self.density_dpi,
            font_scale=self.font_scale,
            hard_keyboard_hidden=self.hard_keyboard_hidden,
            keyboard=self.keyboard,
            keyboard_hidden=self.keyboard_hidden,
            locale=self.locale,
            mcc=self.mcc,
            mnc=self.mnc,
            navigation=self.navigation,
            orientation=new_ori,
            screen_width_dp=self.screen_height_dp,
            screen_height_dp=self.screen_width_dp,
            smallest_screen_width_dp=self.smallest_screen_width_dp,
            touchscreen=self.touchscreen,
        )


def simulate_rotation_lifecycle(target_sdk: int, config_changes: set[str]) -> dict:
    """On API >= 13 (Android 3.2+), rotation triggers both 'orientation' and 'screenSize' diffs."""
    changed_flags = {"orientation", "screenSize"} if target_sdk >= 13 else {"orientation"}
    handled_in_place = changed_flags.issubset(config_changes)
    if handled_in_place:
        return {"activity_recreated": False, "onConfigurationChanged_called": True}
    return {"activity_recreated": True, "onConfigurationChanged_called": False}


def main() -> None:
    cfg = Configuration(
        density_dpi=420,
        font_scale=1.15,
        hard_keyboard_hidden=HARDKEYBOARDHIDDEN_YES,
        keyboard=KEYBOARD_NOKEYS,
        keyboard_hidden=1,
        locale="zh_CN",
        mcc=460,
        mnc=0,
        navigation=NAVIGATION_NONAV,
        orientation=ORIENTATION_PORTRAIT,
        screen_width_dp=411,
        screen_height_dp=891,
        smallest_screen_width_dp=411,
        touchscreen=TOUCHSCREEN_FINGER,
    )

    # Case 1: Configuration field reading & Android constant values
    act1 = {
        "densityDpi": cfg.density_dpi,
        "fontScale": cfg.font_scale,
        "hardKeyboardHidden_constants": [HARDKEYBOARDHIDDEN_UNDEFINED, HARDKEYBOARDHIDDEN_NO, HARDKEYBOARDHIDDEN_YES],
        "mcc_mnc_operator_pair": [cfg.mcc, cfg.mnc],
        "locale": cfg.locale,
        "smallestScreenWidthDp": cfg.smallest_screen_width_dp,
    }
    exp1 = {
        "densityDpi": 420,
        "fontScale": 1.15,
        "hardKeyboardHidden_constants": [0, 1, 2],
        "mcc_mnc_operator_pair": [460, 0],
        "locale": "zh_CN",
        "smallestScreenWidthDp": 411,
    }

    # Case 2: Portrait <-> Landscape orientation toggle & dp swap
    land = cfg.rotate()
    back_port = land.rotate()
    act2 = {
        "portrait_dims": [cfg.orientation, cfg.screen_width_dp, cfg.screen_height_dp],
        "landscape_dims": [land.orientation, land.screen_width_dp, land.screen_height_dp],
        "back_to_portrait": back_port.orientation == ORIENTATION_PORTRAIT,
        "toast_label_land": "横屏" if land.orientation == ORIENTATION_LANDSCAPE else "竖屏",
    }
    exp2 = {
        "portrait_dims": [1, 411, 891],
        "landscape_dims": [2, 891, 411],
        "back_to_portrait": True,
        "toast_label_land": "横屏",
    }

    # Case 3: API 12 vs API 13+ configChanges="orientation" vs "orientation|screenSize"
    act3 = {
        "api12_orientation_only": simulate_rotation_lifecycle(12, {"orientation"}),
        "api34_orientation_only": simulate_rotation_lifecycle(34, {"orientation"}),
        "api34_orientation_and_screenSize": simulate_rotation_lifecycle(34, {"orientation", "screenSize"}),
    }
    exp3 = {
        "api12_orientation_only": {"activity_recreated": False, "onConfigurationChanged_called": True},
        "api34_orientation_only": {"activity_recreated": True, "onConfigurationChanged_called": False},
        "api34_orientation_and_screenSize": {"activity_recreated": False, "onConfigurationChanged_called": True},
    }

    # Case 4: Density & fontScale pixel conversion (dp -> px, sp -> px)
    density = cfg.density_dpi / 160.0
    dp_16_px = round(16 * density, 2)
    sp_16_px = round(16 * density * cfg.font_scale, 2)
    act4 = {"density_factor": round(density, 4), "16dp_in_px": dp_16_px, "16sp_in_px": sp_16_px}
    exp4 = {"density_factor": 2.625, "16dp_in_px": 42.0, "16sp_in_px": 48.3}

    cases = [
        {
            "name": "configuration_fields_and_constants",
            "summary": "验证 Configuration 设备密度、字体缩放、硬键盘常量值(0/1/2)、MCC/MNC 与语言环境字段",
            "expected": exp1,
            "actual": act1,
            "passed": act1 == exp1,
        },
        {
            "name": "orientation_toggle_and_dp_dimensions",
            "summary": "验证横竖屏切换时 orientation 常量与 screenWidthDp/screenHeightDp 互换及提示文案",
            "expected": exp2,
            "actual": act2,
            "passed": act2 == exp2,
        },
        {
            "name": "config_changes_api13_screen_size_rule",
            "summary": "验证 API>=13 仅声明 orientation 会重建 Activity,声明 orientation|screenSize 才回调 onConfigurationChanged",
            "expected": exp3,
            "actual": act3,
            "passed": act3 == exp3,
        },
        {
            "name": "density_dpi_and_font_scale_conversion",
            "summary": "验证 densityDpi=420 与 fontScale=1.15 下 16dp 与 16sp 到像素的换算差异",
            "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-configuration-lab/explore-tutorial-configuration.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-tutorial-configuration.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Android

课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip

下载课程关联工程包:Android

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1859.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习