|
| 1 | +# BuildInfo |
| 2 | + |
| 3 | +BuildInfo is a singleton class that provides access to OS version and build information. It stores the release version and API level (SDK version) that identifies the MicroPythonOS build. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +BuildInfo centralizes OS version and build information in a single class with nested version attributes. This provides: |
| 8 | + |
| 9 | +- **Version Information** - Access the human-readable release version |
| 10 | +- **API Level** - Get the SDK API level for compatibility checks |
| 11 | +- **Build Identification** - Identify the exact OS build |
| 12 | +- **Android-Inspired** - Follows Android's Build.VERSION pattern |
| 13 | +- **Singleton Pattern** - Single source of truth for OS version |
| 14 | + |
| 15 | +## Architecture |
| 16 | + |
| 17 | +BuildInfo is implemented as a singleton with a nested `version` class containing version attributes: |
| 18 | + |
| 19 | +```python |
| 20 | +class BuildInfo: |
| 21 | + class version: |
| 22 | + """Version information.""" |
| 23 | + release = "0.7.0" # Human-readable version |
| 24 | + sdk_int = 0 # API level |
| 25 | +``` |
| 26 | + |
| 27 | +All version information is accessed through class attributes - no instance creation is needed. |
| 28 | + |
| 29 | +## Usage |
| 30 | + |
| 31 | +### Getting OS Version |
| 32 | + |
| 33 | +```python |
| 34 | +from mpos import BuildInfo |
| 35 | + |
| 36 | +# Get human-readable version |
| 37 | +version = BuildInfo.version.release |
| 38 | +# Returns: "0.7.0" |
| 39 | + |
| 40 | +# Get API level |
| 41 | +api_level = BuildInfo.version.sdk_int |
| 42 | +# Returns: 0 |
| 43 | +``` |
| 44 | + |
| 45 | +### Version Checking |
| 46 | + |
| 47 | +```python |
| 48 | +from mpos import BuildInfo |
| 49 | + |
| 50 | +# Check if running specific version |
| 51 | +if BuildInfo.version.release == "0.7.0": |
| 52 | + print("Running MicroPythonOS 0.7.0") |
| 53 | + |
| 54 | +# Check API level |
| 55 | +if BuildInfo.version.sdk_int >= 1: |
| 56 | + print("API level 1 or higher") |
| 57 | +``` |
| 58 | + |
| 59 | +### Version Comparison |
| 60 | + |
| 61 | +```python |
| 62 | +from mpos import BuildInfo |
| 63 | + |
| 64 | +def parse_version(version_str): |
| 65 | + """Parse version string into tuple.""" |
| 66 | + return tuple(map(int, version_str.split('.'))) |
| 67 | + |
| 68 | +# Compare versions |
| 69 | +current = parse_version(BuildInfo.version.release) |
| 70 | +required = parse_version("0.7.0") |
| 71 | + |
| 72 | +if current >= required: |
| 73 | + print("OS version meets requirements") |
| 74 | +else: |
| 75 | + print("OS version too old") |
| 76 | +``` |
| 77 | + |
| 78 | +## API Reference |
| 79 | + |
| 80 | +### Class Attributes |
| 81 | + |
| 82 | +#### `BuildInfo.version.release` |
| 83 | +Human-readable OS version string. |
| 84 | + |
| 85 | +**Type:** str |
| 86 | + |
| 87 | +**Example:** |
| 88 | +```python |
| 89 | +version = BuildInfo.version.release |
| 90 | +# Returns: "0.7.0" |
| 91 | +``` |
| 92 | + |
| 93 | +#### `BuildInfo.version.sdk_int` |
| 94 | +API level (SDK version) as an integer. |
| 95 | + |
| 96 | +**Type:** int |
| 97 | + |
| 98 | +**Example:** |
| 99 | +```python |
| 100 | +api_level = BuildInfo.version.sdk_int |
| 101 | +# Returns: 0 |
| 102 | +``` |
| 103 | + |
| 104 | +## Practical Examples |
| 105 | + |
| 106 | +### Version Display in About Screen |
| 107 | + |
| 108 | +```python |
| 109 | +from mpos import Activity, BuildInfo |
| 110 | +import lvgl as lv |
| 111 | + |
| 112 | +class AboutActivity(Activity): |
| 113 | + def onCreate(self): |
| 114 | + screen = lv.obj() |
| 115 | + |
| 116 | + # Display version information |
| 117 | + version_label = lv.label(screen) |
| 118 | + version_label.set_text(f"Version: {BuildInfo.version.release}") |
| 119 | + |
| 120 | + api_label = lv.label(screen) |
| 121 | + api_label.set_text(f"API Level: {BuildInfo.version.sdk_int}") |
| 122 | + |
| 123 | + self.setContentView(screen) |
| 124 | +``` |
| 125 | + |
| 126 | +### Feature Availability Based on API Level |
| 127 | + |
| 128 | +```python |
| 129 | +from mpos import BuildInfo |
| 130 | + |
| 131 | +class FeatureManager: |
| 132 | + @staticmethod |
| 133 | + def supports_feature(feature_name): |
| 134 | + """Check if feature is available in current API level.""" |
| 135 | + api_level = BuildInfo.version.sdk_int |
| 136 | + |
| 137 | + features = { |
| 138 | + "camera": 1, |
| 139 | + "wifi": 0, |
| 140 | + "bluetooth": 2, |
| 141 | + "nfc": 3 |
| 142 | + } |
| 143 | + |
| 144 | + required_api = features.get(feature_name, float('inf')) |
| 145 | + return api_level >= required_api |
| 146 | + |
| 147 | +# Usage |
| 148 | +if FeatureManager.supports_feature("camera"): |
| 149 | + # Enable camera features |
| 150 | + pass |
| 151 | +``` |
| 152 | + |
| 153 | +### Version-Specific Behavior |
| 154 | + |
| 155 | +```python |
| 156 | +from mpos import BuildInfo |
| 157 | + |
| 158 | +def get_ui_style(): |
| 159 | + """Get UI style based on OS version.""" |
| 160 | + version = BuildInfo.version.release |
| 161 | + |
| 162 | + if version.startswith("0.7"): |
| 163 | + return "modern" |
| 164 | + elif version.startswith("0.6"): |
| 165 | + return "classic" |
| 166 | + else: |
| 167 | + return "legacy" |
| 168 | +``` |
| 169 | + |
| 170 | +### Compatibility Checking |
| 171 | + |
| 172 | +```python |
| 173 | +from mpos import BuildInfo |
| 174 | + |
| 175 | +class AppCompatibility: |
| 176 | + MIN_VERSION = "0.7.0" |
| 177 | + MIN_API_LEVEL = 0 |
| 178 | + |
| 179 | + @classmethod |
| 180 | + def is_compatible(cls): |
| 181 | + """Check if app is compatible with current OS.""" |
| 182 | + version = BuildInfo.version.release |
| 183 | + api_level = BuildInfo.version.sdk_int |
| 184 | + |
| 185 | + # Check API level |
| 186 | + if api_level < cls.MIN_API_LEVEL: |
| 187 | + return False |
| 188 | + |
| 189 | + # Check version |
| 190 | + current = tuple(map(int, version.split('.'))) |
| 191 | + required = tuple(map(int, cls.MIN_VERSION.split('.'))) |
| 192 | + |
| 193 | + return current >= required |
| 194 | + |
| 195 | +# Usage |
| 196 | +if AppCompatibility.is_compatible(): |
| 197 | + print("App is compatible with this OS") |
| 198 | +else: |
| 199 | + print("App requires newer OS version") |
| 200 | +``` |
| 201 | + |
| 202 | +### Build Information Logging |
| 203 | + |
| 204 | +```python |
| 205 | +from mpos import BuildInfo |
| 206 | +import logging |
| 207 | + |
| 208 | +def setup_logging(): |
| 209 | + """Setup logging with build information.""" |
| 210 | + logger = logging.getLogger(__name__) |
| 211 | + |
| 212 | + # Log build information |
| 213 | + logger.info(f"MicroPythonOS {BuildInfo.version.release} (API {BuildInfo.version.sdk_int})") |
| 214 | + |
| 215 | + return logger |
| 216 | +``` |
| 217 | + |
| 218 | +### Version-Specific Workarounds |
| 219 | + |
| 220 | +```python |
| 221 | +from mpos import BuildInfo |
| 222 | + |
| 223 | +def apply_workarounds(): |
| 224 | + """Apply version-specific workarounds.""" |
| 225 | + version = BuildInfo.version.release |
| 226 | + |
| 227 | + if version == "0.7.0": |
| 228 | + # Workaround for known issue in 0.7.0 |
| 229 | + import gc |
| 230 | + gc.collect() |
| 231 | + |
| 232 | + if BuildInfo.version.sdk_int < 1: |
| 233 | + # Workaround for older API |
| 234 | + print("Using legacy API compatibility mode") |
| 235 | +``` |
| 236 | + |
| 237 | +### Telemetry with Version Info |
| 238 | + |
| 239 | +```python |
| 240 | +from mpos import BuildInfo |
| 241 | +import time |
| 242 | + |
| 243 | +class Telemetry: |
| 244 | + @staticmethod |
| 245 | + def get_system_context(): |
| 246 | + """Get system context for telemetry.""" |
| 247 | + return { |
| 248 | + "os_version": BuildInfo.version.release, |
| 249 | + "api_level": BuildInfo.version.sdk_int, |
| 250 | + "timestamp": time.time() |
| 251 | + } |
| 252 | +``` |
| 253 | + |
| 254 | +## Implementation Details |
| 255 | + |
| 256 | +### File Structure |
| 257 | + |
| 258 | +``` |
| 259 | +mpos/ |
| 260 | +├── build_info.py # Core BuildInfo class |
| 261 | +└── __init__.py # Exports BuildInfo |
| 262 | +``` |
| 263 | + |
| 264 | +### Version Attributes |
| 265 | + |
| 266 | +The `BuildInfo.version` class contains: |
| 267 | + |
| 268 | +```python |
| 269 | +class BuildInfo: |
| 270 | + class version: |
| 271 | + release = "0.7.0" # Human-readable version: "major.minor.patch" |
| 272 | + sdk_int = 0 # API level: integer for compatibility checks |
| 273 | +``` |
| 274 | + |
| 275 | +### Version Numbering |
| 276 | + |
| 277 | +- **release**: Semantic versioning (e.g., "0.7.0", "1.0.0") |
| 278 | + - Major: Breaking changes |
| 279 | + - Minor: New features |
| 280 | + - Patch: Bug fixes |
| 281 | + |
| 282 | +- **sdk_int**: API level for feature availability |
| 283 | + - Incremented when new APIs are added |
| 284 | + - Used for compatibility checks |
| 285 | + |
| 286 | +## Design Patterns |
| 287 | + |
| 288 | +### Nested Class Pattern |
| 289 | + |
| 290 | +BuildInfo uses a nested `version` class to organize version-related attributes: |
| 291 | + |
| 292 | +```python |
| 293 | +class BuildInfo: |
| 294 | + class version: |
| 295 | + release = "0.7.0" |
| 296 | + sdk_int = 0 |
| 297 | +``` |
| 298 | + |
| 299 | +This pattern groups related information and provides a clear namespace. |
| 300 | + |
| 301 | +### Class Attribute Access |
| 302 | + |
| 303 | +All attributes are class attributes, accessed directly without instantiation: |
| 304 | + |
| 305 | +```python |
| 306 | +# No need for BuildInfo() or BuildInfo.get() |
| 307 | +version = BuildInfo.version.release # Direct class attribute access |
| 308 | +``` |
| 309 | + |
| 310 | +## Related Frameworks |
| 311 | + |
| 312 | +- **[DeviceInfo](device-info.md)** - Device hardware information |
| 313 | +- **[TimeZone](time-zone.md)** - Timezone conversion and management |
| 314 | +- **[DisplayMetrics](display-metrics.md)** - Display properties and metrics |
| 315 | + |
| 316 | +## See Also |
| 317 | + |
| 318 | +- [Architecture Overview](../architecture/overview.md) |
| 319 | +- [Frameworks](../architecture/frameworks.md) |
| 320 | +- [Creating Apps](../apps/creating-apps.md) |
0 commit comments