-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
83 lines (60 loc) · 2.48 KB
/
utils.py
File metadata and controls
83 lines (60 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Platform detection and shared helpers.
This module is imported early by most other modules, so it avoids
importing platform-specific packages at module level.
"""
import os
from typing import Any, Optional
# ======================================================================
# Platform detection
# ======================================================================
_is_android: Optional[bool] = None
def _detect_android() -> bool:
env = os.environ
if "ANDROID_BOOTLOGO" in env or "ANDROID_ROOT" in env or "ANDROID_DATA" in env or "ANDROID_ARGUMENT" in env:
return True
try:
from java import jclass # noqa: F401
return True
except Exception:
pass
return False
def _ensure_platform_detection() -> None:
global _is_android
if _is_android is None:
_is_android = _detect_android()
def _get_is_android() -> bool:
_ensure_platform_detection()
assert _is_android is not None
return _is_android
IS_ANDROID: bool = _get_is_android()
# ======================================================================
# Android context management
# ======================================================================
_android_context: Any = None
_android_fragment_container: Any = None
def set_android_context(context: Any) -> None:
"""Record the current Android Activity/Context for view construction."""
global _android_context
_android_context = context
def set_android_fragment_container(container_view: Any) -> None:
"""Record the current Fragment root container ViewGroup."""
global _android_fragment_container
_android_fragment_container = container_view
def get_android_context() -> Any:
"""Return the current Android Activity/Context."""
if not IS_ANDROID:
raise RuntimeError("get_android_context() called on non-Android platform")
if _android_context is None:
raise RuntimeError(
"Android context not set. Ensure Page is initialized from an Activity before constructing views."
)
return _android_context
def get_android_fragment_container() -> Any:
"""Return the current Fragment container ViewGroup."""
if not IS_ANDROID:
raise RuntimeError("get_android_fragment_container() called on non-Android platform")
if _android_fragment_container is None:
raise RuntimeError(
"Android fragment container not set. Ensure PageFragment has been created before set_root_view."
)
return _android_fragment_container