Advanced Android Flashlight HAL & State Sync Engine
A high-performance, industrial-grade Android Flashlight application built with Site Reliability Engineering (SRE) principles. This project focuses on extreme stability, sub-millisecond responsiveness, and seamless state synchronization between the System Quick Settings Tile and the main Application.
- Advanced HAL (Hardware Abstraction Layer): Custom implementation using Camera2 API with support for high-level torch strength (API 33+).
- SSOT State Management: Uses Kotlin Coroutines & Flow to maintain a "Single Source of Truth," ensuring the Quick Settings Tile and App UI are always in sync.
- SRE Guardrails: * Mutex-Locked Hardware Access: Prevents race conditions and hardware deadlocks during rapid switching.
- Memory Leak Protection: Singleton-based service scope tied to the Application lifecycle.
- Self-Healing Logic: Automatic coroutine scope recovery and hardware error interception.
- Special Modes: Includes precision-timed SOS (Morse Code) and Breathing Light effects.
Standard Camera2 implementations suffer from a 300ms+ delay during cameraIdList enumeration. BaptizingLight implements an optimistic UI pattern:
-
Async Validation: Triggers a background hardware check to verify the ID while the light is already being toggled.
-
Visual First: Updates the UI/Tile state on the first frame of the click event.
The project utilizes a custom CoroutineScope with a SupervisorJob and Dispatchers.Main.immediate.
- Atomic Mode Switching: Every mode change follows a "Cancel-and-Join" pattern to ensure no two hardware tasks (e.g., SOS and Manual) ever compete for the Camera HAL.
- Hardware Mutex: A
kotlinx.coroutines.sync.Mutexensures that rapid-fire clicks from the user are linearized.
The App utilizes a bi-directional sync strategy:
-
Tile โ Service:
TileServiceacts as a thin client, dispatching intents to the singletonFlashlightService. -
Service โ App UI: The
MainActivityobserves aStateFlowfrom the service, ensuring the slider and toggle buttons reflect reality, even if the light was turned off via the notification shade.
- Minimum SDK: Android 7.0 (API 24)
- Target SDK: Android 16 (API 36)
- Language: 100% Kotlin
- Asynchronous: Kotlin Coroutines & Flow
- Build System: Gradle (Kotlin DSL)
| Light Theme | Dark Theme |
|---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Contributions are welcome! Please submit an issue or pull request to contribute to this project.
This project is licensed under the terms of the MIT License. See the LICENSE file for the full license text and copyright notice.












