Skip to content

About

No description, website, or topics provided.

Resources

Security policy

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

ADA Smart Alarm

Destination-Aware Commute Alarm & Dynamic Journey Tracking Platform

Android Native Cross-Platform Mobile Shared Core FastAPI Backend PostgreSQL Redis Firebase Auth Tests Passing License


A mission-critical, destination-aware smart alarm and live journey monitoring platform.
Schedule when you must arrive; ADA determines when you need to wake up and leave, monitors road traffic in real time, begins GPS tracking before departure, adapts dynamically to unexpected delays, alerts you as you approach, and confirms your arrival.


πŸ“– Table of Contents


🎯 The Core Problem ADA Solves

Traditional alarm clocks suffer from a fatal design flaw: they are blind to the real world.

If you need to reach an exam, flight, or critical meeting by 9:00 AM:

  1. A conventional alarm wakes you at a fixed time (e.g. 7:00 AM) even if an overnight accident doubled your commute.
  2. If traffic is unusually light, you waste hours sitting idle.
  3. If traffic builds up, you arrive late.
  4. Once you step out of the door, traditional alarms do nothingβ€”they cannot warn you if you take a wrong turn or alert you before your stop.

The 5 Questions ADA Answers:

  1. When must I reach my destination? (Your required arrival deadline)
  2. How long will the journey take today? (Live traffic and weather-aware commute estimation)
  3. When should I leave? (Departure time dynamically adjusted for safety buffers)
  4. When should I wake up? (Wake-up time adjusted for your personal morning routine)
  5. Is my planned arrival still achievable right now? (Continuous GPS tracking, route deviation detection, and arrival alerts)

βš™οΈ How ADA Works

The Calculation Contract

ADA's scheduling engine works backwards from your arrival deadline through deterministic mathematical formulas:

[Required Arrival Time]  (e.g., 9:00 AM)
         β”‚
         β–Ό  minus Safety Buffer Margin (e.g., 15 min)
[Planned Arrival Time]   (8:45 AM)
         β”‚
         β–Ό  minus Live Commute Travel Duration (e.g., 45 min live traffic)
[Recommended Leave Time] (8:00 AM)
         β”‚
         β–Ό  minus Morning Routine Preparation Time (e.g., 30 min)
[Recommended Wake Time]  (7:30 AM) ──> Alarm Manager Schedules Wake-Up Ring

$$\text{PlannedArrivalTime} = \text{RequiredArrivalTime} - \text{SafetyBuffer}$$ $$\text{RecommendedLeaveTime} = \text{PlannedArrivalTime} - \text{TravelDuration}_{\text{traffic}}$$ $$\text{RecommendedWakeTime} = \text{RecommendedLeaveTime} - \text{PreparationDuration}$$


Journey State Machine

From initial creation through confirmed arrival, ADA transitions through a validated deterministic state machine:

stateDiagram-v2
    [*] --> SCHEDULED: Create Journey
    SCHEDULED --> READY_TO_TRACK: T - 15 min (Lead Time Alarm)
    READY_TO_TRACK --> ACTIVE_TRACKING: Start Tracking (Auto / Manual)
    
    state ACTIVE_TRACKING {
        [*] --> ValidatingFix
        ValidatingFix --> CheckDeviation: Fix Valid (Acc <= 50m, Age <= 60s)
        ValidatingFix --> TRACKING_DEGRADED: Fix Stale or Inaccurate
        TRACKING_DEGRADED --> ValidatingFix: Fresh Fix Received
        
        CheckDeviation --> RECALCULATING: Off Route (> 250m)
        RECALCULATING --> CheckDeviation: New Route Computed
    }
    
    ACTIVE_TRACKING --> APPROACHING: Distance <= 1,000m (Alert Fired Once)
    APPROACHING --> ARRIVED: Distance <= 100m (2 Consecutive Accurate Fixes)
    ACTIVE_TRACKING --> ARRIVED: Distance <= 100m (2 Consecutive Accurate Fixes)
    
    ACTIVE_TRACKING --> CANCELLED: User Stops / Cancels
    APPROACHING --> CANCELLED: User Cancels
    ARRIVED --> [*]: Tracking Terminated & Cleanup
    CANCELLED --> [*]
Loading

Dynamic ETA & Rerouting Rules

ADA implements strict invariants ported across native Kotlin (DynamicEtaEngine.kt) and pure TypeScript (DynamicEtaEngine.ts):

  1. Zero Double-Counting of Travel Time: $$\text{PredictedArrivalEpochMs} = t_{\text{fix}} + (\text{RemainingDurationSeconds} \times 1,000)$$ Elapsed journey time is never added twice, and static stop penalties are never superimposed onto live provider traffic durations.
  2. GPS Velocity Jump Protection: Rejects unrealistic GPS jumps ($v &gt; 45\text{ m/s} \approx 162\text{ km/h}$) unless accuracy is exceptionally high ($&lt; 15\text{m}$), shielding against urban GPS reflections.
  3. Route Recalculation Hysteresis:
    • Provider request cooldown: Minimum 30 seconds between requests.
    • Immediate recalculation when cross-track corridor deviation exceeds 250 meters.
    • Routine recalculation when displacement $\ge 200\text{m}$ and elapsed time $\ge 180\text{s}$, or if previous route is stale ($&gt; 300\text{s}$).
  4. Multi-Stage Proximity Alerts:
    • Approaching Alert: Triggers once when remaining distance $\le 1,000\text{ meters}$.
    • Arrival Confirmation: Confirmed when remaining distance $\le 100\text{ meters}$ across 2 consecutive accurate fixes ($\le 50\text{m}$ accuracy). Cleanly stops tracking upon arrival.

πŸ›οΈ System Architecture

ADA is engineered as a production-grade multi-platform system:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     Native Android (Kotlin + Compose)   β”‚  β”‚   Cross-Platform Mobile (React Native)   β”‚
β”‚            [android/]                   β”‚  β”‚              [apps/mobile/]              β”‚
β”‚   Package: com.thinkx.ada (Preserved)   β”‚  β”‚    Bundle ID: com.thinkx.ada.mobile      β”‚
β”‚   β€’ AlarmManager.setAlarmClock()        β”‚  β”‚    β€’ iOS Background Location Tracking    β”‚
β”‚   β€’ Foreground Location Service         β”‚  β”‚    β€’ Hardware Keystore (expo-secure-store)β”‚
β”‚   β€’ Room SQLite Database v3             β”‚  β”‚    β€’ Native Local Notifications          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚                                             β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                            β”‚
                                            β–Ό
                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚       Shared TypeScript Domain Core         β”‚
                     β”‚             [packages/shared/]              β”‚
                     β”‚   β€’ Pure DynamicEtaEngine (1:1 with Kotlin) β”‚
                     β”‚   β€’ Polyline decoder & Haversine distance   β”‚
                     β”‚   β€’ Journey state machine & typed DTOs      β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                            β”‚
                                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                              FastAPI Backend Gateway [backend/]                        β”‚
β”‚   β€’ Google Maps & Mapbox live traffic routing proxy with Redis 5-minute caching        β”‚
β”‚   β€’ Offline Great-Circle heuristic fallback routing                                    β”‚
β”‚   β€’ Firebase Admin token verification & user claims                                    β”‚
β”‚   β€’ PostgreSQL 15 + SQLAlchemy 2.0 with idempotent sync (X-Idempotency-Key)            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Repository Layout

β”œβ”€β”€ android/            # Native Kotlin + Jetpack Compose Android app (com.thinkx.ada)
β”œβ”€β”€ apps/
β”‚   └── mobile/         # Cross-platform React Native + Expo app (com.thinkx.ada.mobile)
β”œβ”€β”€ packages/
β”‚   └── shared/         # Reusable pure TypeScript domain math, models, and state machine
β”œβ”€β”€ backend/            # FastAPI, PostgreSQL, Redis, routing proxy, and Celery workers
β”œβ”€β”€ frontend/           # React 19 + Vite web dashboard
└── docs/               # Architecture runbooks, iOS setup, test plans, and release guides

🎬 Step-by-Step Demo Guide

Experience how ADA handles a real-world commute from Ahmedabad to Charusat University (Anand) with an arrival deadline of 9:00 AM:

[07:00 AM] Scheduled Departure Planned
   β”‚
   β”œβ”€β”€ [07:15 AM] Alarm Wake-Up: ADA sounds alarm based on 45m commute + 15m safety buffer + 45m prep
   β”œβ”€β”€ [07:45 AM] (T - 15m): ADA automatically wakes device; initiates Background Location Tracking
   β”œβ”€β”€ [08:00 AM] Departure: User begins drive; persistent notification displays live dynamic ETA
   β”œβ”€β”€ [08:20 AM] Road Incident: Heavy traffic on NH48; ETA dynamically recalculates from 8:45 AM to 8:58 AM
   β”œβ”€β”€ [08:35 AM] Detour Taken: User turns off route; deviation > 250m detected; instant reroute triggered
   β”œβ”€β”€ [08:52 AM] Approaching: User reaches 1,000m perimeter; "Approaching Destination" alert sounds
   └── [08:58 AM] Arrival: User enters 100m geofence (2 consecutive GPS fixes); "Arrived!" alert sounds,
                          tracking automatically terminates, releasing GPS & battery resources.

Trying the Interactive Demo via Code:

  1. Shared Math Engine: Run vitest run packages/shared/__tests__ to see all 14 mathematical invariants tested against the canonical Charusat scenario.
  2. Mobile Tracking Engine: Run vitest run apps/mobile/__tests__ to observe simulated GPS fixes triggering approaching alerts and 2-hit arrival confirmations.
  3. Backend Proxy: Run python -m pytest backend/tests/test_location_and_routing.py to see route resolution and traffic duration separation.

πŸ› οΈ Installation & Setup Guide

1. FastAPI Backend & Database Setup

Works identically on macOS, Linux, and Windows:

Prerequisites:

  • Python 3.10+
  • Docker & Docker Compose (for PostgreSQL and Redis)
# Clone the repository
git clone https://github.com/buildwithneel/ADA-Smart-Alarm.git
cd ADA-Smart-Alarm

# 1. Start PostgreSQL 15 and Redis 7 in the background
docker-compose up -d postgres redis

# 2. Configure Backend Environment
cd backend
cp .env.example .env

# 3. Create Virtual Environment & Install Dependencies
# On macOS / Linux:
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

# On Windows (PowerShell):
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt

# 4. Run Database Migrations
alembic upgrade head

# 5. Start the FastAPI Development Server
python -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

API Swagger Documentation will be available at http://localhost:8000/docs.


2. Native Android Application Setup (android/)

The native Kotlin + Jetpack Compose application (com.thinkx.ada).

Prerequisites across OSes:

  • macOS / Linux / Windows: JDK 17, Android Studio (Ladybug or newer), Android SDK 36, Build-Tools 36.0.0.
cd android

# On macOS / Linux:
./gradlew testDebugUnitTest
./gradlew assembleDebug

# On Windows (PowerShell):
.\gradlew.bat testDebugUnitTest
.\gradlew.bat assembleDebug

Installing to Physical Phone or Emulator:

# Ensure device is connected via USB with USB Debugging enabled
adb devices

# Install debug APK
# On macOS / Linux:
adb install -r app/build/outputs/apk/debug/app-debug.apk

# On Windows:
adb install -r app\build\outputs\apk\debug\app-debug.apk

Networking Notice: When testing on the Android Emulator, the backend is reachable at http://10.0.2.2:8000/api/v1. On a physical phone, connect to the same Wi-Fi network and use your host computer's LAN IP (e.g. http://192.168.1.50:8000/api/v1).


3. Cross-Platform Mobile Application Setup (apps/mobile/)

The React Native + TypeScript Expo application (com.thinkx.ada.mobile).

Prerequisites:

  • Node.js 20+ or 24+
  • npm or yarn
cd apps/mobile

# Install dependencies
npm install

# Run automated tests
npm test

# Start the Expo development server
npx expo start

Running by Operating System:

OS Android Development iOS Development
macOS βœ… Android Emulator or physical device via npx expo run:android βœ… Full iOS Simulator & physical iPhone builds via npx expo run:ios and Xcode
Windows βœ… Android Emulator or physical device via npx expo run:android ⚠️ UI & logic testable in Expo Go; native iOS Mach-O compilation requires macOS/Xcode
Linux βœ… Android Emulator or physical device via npx expo run:android ⚠️ UI & logic testable in Expo Go; native iOS Mach-O compilation requires macOS/Xcode

πŸ§ͺ Testing & Quality Assurance

ADA adheres to an uncompromising testing pyramid. All 280 automated tests are passing:

# 1. Backend Regression Suite (131 tests)
cd backend
python -m pytest

# 2. Native Android Unit Test Suite (97 tests)
cd android
./gradlew testDebugUnitTest  # (or .\gradlew.bat testDebugUnitTest on Windows)

# 3. Web Client Vitest Suite (29 tests)
cd frontend
npm test

# 4. Shared Core & Mobile Unit Tests (23 tests)
# From project root:
.\frontend\node_modules\.bin\vitest.cmd run --config vitest.config.ts apps/mobile/__tests__ packages/shared/__tests__

πŸ”’ Security, Privacy & Background Policy

  • Zero Hardcoded Secrets: Private keys, keystores, and Firebase service accounts are never checked into git.
  • Hardware Keystore Storage: Mobile authentication tokens are stored exclusively in hardware keystores (expo-secure-store using iOS Keychain and Android KeyStore). Plaintext AsyncStorage is banned.
  • Location Privacy: Location fixes are collected only during user-authorized active journeys. Reverse geocoding in FastAPI automatically fuzzes coordinates to $\sim 11\text{m}$ resolution.
  • Truthful Battery & OS Constraints:
    • On Android: Runs under FOREGROUND_SERVICE_LOCATION with a persistent notification.
    • On iOS: Uses kCLLocationAccuracyBestForNavigation with showsBackgroundLocationIndicator. ADA explicitly informs users that force-closing the app from the iOS App Switcher halts tracking per Apple operating system policy.

πŸ“š Documentation Index

For comprehensive technical specifications, consult our detailed documentation in docs/:

  1. docs/CROSS_PLATFORM_ARCHITECTURE.md β€” Multi-platform architecture, directory layout, and endpoint reuse map.
  2. docs/IOS_BACKGROUND_LOCATION.md β€” Apple Core Location capabilities, Info.plist disclosures, and background lifecycle.
  3. docs/MOBILE_DEVELOPMENT_SETUP.md β€” Comprehensive developer machine setup and networking configuration.
  4. docs/MOBILE_TEST_PLAN.md β€” Complete 5-tier verification matrix.
  5. docs/IOS_RELEASE_CHECKLIST.md β€” Apple developer provisioning, privacy nutrition disclosures, and TestFlight release gates.

πŸ“„ License

This project is licensed under the MIT License β€” see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Security policy

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages