Compare commits
2 Commits
master
...
app_bg_fix
| Author | SHA1 | Date |
|---|---|---|
|
|
70c9966a4d | 3 days ago |
|
|
a212fbeabd | 3 days ago |
@ -0,0 +1,234 @@
|
|||||||
|
# AC-Powered Kiosk Display - Final Changes Summary
|
||||||
|
|
||||||
|
## Device Type: Android LED Displays (AC-Powered, Not Battery)
|
||||||
|
|
||||||
|
### Key Understanding
|
||||||
|
These are **permanent display installations**, always plugged into power. The issue is **NOT** about battery drain, but about Android OS treating long-running apps as "idle" and killing them even when on AC power.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## What Was Fixed
|
||||||
|
|
||||||
|
### ✅ 1. Accurate Uptime Tracking
|
||||||
|
**Problem:** Previous code calculated time since midnight, not actual app runtime
|
||||||
|
**Solution:** Now tracks real app start time
|
||||||
|
|
||||||
|
**Before:**
|
||||||
|
```
|
||||||
|
Health check performed - App uptime: 6 hours ← WRONG (should be 14h)
|
||||||
|
```
|
||||||
|
|
||||||
|
**After:**
|
||||||
|
```
|
||||||
|
Health check performed - App uptime: 14h 23m (started: 2026-02-26 20:00:00) ← CORRECT
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### ✅ 2. Kiosk Display Mode (Native Android)
|
||||||
|
**Problem:** App treated as regular app, killed after 14 hours of "inactivity"
|
||||||
|
**Solution:** Added window flags to prevent Android from sleeping/killing app
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Result:** App stays in foreground, Android can't kill it for being "idle"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### ✅ 3. Connection Health Monitoring
|
||||||
|
**Problem:** SignalR connection could die silently, no recovery
|
||||||
|
**Solution:**
|
||||||
|
- Health check every 5 minutes
|
||||||
|
- Track consecutive failures
|
||||||
|
- Auto-restart connection after 3 failures
|
||||||
|
|
||||||
|
```dart
|
||||||
|
if (healthCheckFailures >= 3) {
|
||||||
|
await queuingViewModel.stopHubConnection();
|
||||||
|
await Future.delayed(const Duration(seconds: 2));
|
||||||
|
await queuingViewModel.startHubConnection();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### ✅ 4. Lifecycle Event Tracking
|
||||||
|
**Problem:** No visibility when app goes to background or gets killed
|
||||||
|
**Solution:** Log all lifecycle events with actual uptime
|
||||||
|
|
||||||
|
**Now you'll see:**
|
||||||
|
```
|
||||||
|
[onAppPaused] - App uptime: 14h - WARNING: App going to background!
|
||||||
|
[onAppDetached] - App uptime: 14h - CRITICAL: App being killed!
|
||||||
|
```
|
||||||
|
|
||||||
|
If app stays alive correctly, you should **NEVER** see these logs.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### ✅ 5. Clinic Prefix Validation
|
||||||
|
**Added:** `isClinicPrefixAdded(String ticketNo)` method
|
||||||
|
**Returns true only for:** `"XXX W-XX"` format (3 letters + space + W-)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Files Changed
|
||||||
|
|
||||||
|
1. `lib/view_models/screen_config_view_model.dart` - Fixed uptime, health checks, lifecycle
|
||||||
|
2. `lib/models/global_config_model.dart` - Clinic prefix feature
|
||||||
|
3. `lib/repositories/signalR_repo.dart` - Connection improvements
|
||||||
|
4. `android/app/src/main/kotlin/.../MainActivity.kt` - Kiosk display mode
|
||||||
|
5. `android/app/src/main/AndroidManifest.xml` - Activity flags
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Why App Was Dying (Root Cause)
|
||||||
|
|
||||||
|
Android has **App Standby Buckets** (Android 9+) and **Doze Mode** (Android 6+):
|
||||||
|
- Even on AC power, Android monitors app activity
|
||||||
|
- No user interaction = app considered "idle"
|
||||||
|
- After ~12-24 hours, Android kills "idle" apps to free memory
|
||||||
|
- **This happens even on plugged-in devices!**
|
||||||
|
|
||||||
|
**Our Fix:** Window flags tell Android "this is a kiosk display, keep it alive"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Deployment Steps
|
||||||
|
|
||||||
|
### 1. Build
|
||||||
|
```bash
|
||||||
|
cd /Volumes/Data/Projects/Flutter/HMG_QLine
|
||||||
|
flutter clean
|
||||||
|
flutter build apk --release
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Deploy
|
||||||
|
```bash
|
||||||
|
adb install -r build/app/outputs/flutter-apk/app-release.apk
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Verify
|
||||||
|
**No user action needed** - app auto-configures for kiosk mode
|
||||||
|
|
||||||
|
**Check logs for:**
|
||||||
|
```
|
||||||
|
MainActivity created - Kiosk display mode active
|
||||||
|
Health check performed - App uptime: 0h 5m (started: 2026-03-01 10:54:37)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Expected Behavior After Fix
|
||||||
|
|
||||||
|
| Time | Expected Log |
|
||||||
|
|------|-------------|
|
||||||
|
| **0-5 min** | `Health check performed - App uptime: 0h 5m` |
|
||||||
|
| **1 hour** | `Health check performed - App uptime: 1h 0m` |
|
||||||
|
| **14 hours** | `Health check performed - App uptime: 14h 0m` ← CRITICAL (previously died here) |
|
||||||
|
| **24 hours** | `Health check performed - App uptime: 24h 0m` |
|
||||||
|
| **48 hours** | `Health check performed - App uptime: 48h 0m` |
|
||||||
|
| **60+ hours** | `WARNING: App running for 60+ hours - scheduling proactive restart` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## What You Should NOT See
|
||||||
|
|
||||||
|
If fix works correctly:
|
||||||
|
- ❌ No `[onAppPaused]` logs (means going to background)
|
||||||
|
- ❌ No `[onAppDetached]` logs (means being killed)
|
||||||
|
- ❌ No gaps in health check logs (every 5 minutes without fail)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## If App Still Dies
|
||||||
|
|
||||||
|
### Scenario 1: Manufacturer-Specific Restrictions
|
||||||
|
Some Android devices (Xiaomi, Oppo, Vivo, Realme) have **additional** task killers:
|
||||||
|
|
||||||
|
**Solution:**
|
||||||
|
1. Go to device Settings > Apps > QLine
|
||||||
|
2. Enable "Auto-start"
|
||||||
|
3. Set Battery to "No restrictions" (even though it's AC powered)
|
||||||
|
4. Disable "Battery optimization"
|
||||||
|
|
||||||
|
### Scenario 2: Android Doze Override Needed
|
||||||
|
```bash
|
||||||
|
# Whitelist app from doze restrictions
|
||||||
|
adb shell dumpsys deviceidle whitelist +com.example.hmg_qline.hmg_qline
|
||||||
|
|
||||||
|
# Verify
|
||||||
|
adb shell dumpsys deviceidle whitelist | grep hmg_qline
|
||||||
|
```
|
||||||
|
|
||||||
|
### Scenario 3: Full Kiosk Mode Required
|
||||||
|
If above doesn't work, may need **Device Owner Mode**:
|
||||||
|
```bash
|
||||||
|
adb shell dpm set-device-owner com.example.hmg_qline/.DeviceAdminReceiver
|
||||||
|
```
|
||||||
|
This gives app full control over device - cannot be killed by OS.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Testing Checklist
|
||||||
|
|
||||||
|
- [ ] Deploy new APK to test device
|
||||||
|
- [ ] Check log: "MainActivity created - Kiosk display mode active"
|
||||||
|
- [ ] Verify health checks appear every 5 minutes
|
||||||
|
- [ ] Confirm uptime increments correctly (0h 5m → 0h 10m → 0h 15m)
|
||||||
|
- [ ] Wait 14 hours - verify no `[onAppPaused]` or `[onAppDetached]` logs
|
||||||
|
- [ ] Wait 24 hours - verify app still running
|
||||||
|
- [ ] Disconnect network for 15 min - verify auto-reconnection
|
||||||
|
- [ ] Check SignalR connection recovers automatically
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Success Metrics
|
||||||
|
|
||||||
|
✅ Health check logs every 5 minutes without gaps
|
||||||
|
✅ Uptime shows continuously (no resets except midnight restart)
|
||||||
|
✅ Hub connection stays alive
|
||||||
|
✅ App runs past 14-hour checkpoint
|
||||||
|
✅ App runs 48+ hours continuously
|
||||||
|
✅ No lifecycle events (paused/detached) in logs
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Emergency Commands
|
||||||
|
|
||||||
|
### Check if app is running:
|
||||||
|
```bash
|
||||||
|
adb shell ps | grep hmg_qline
|
||||||
|
```
|
||||||
|
|
||||||
|
### Check device idle state:
|
||||||
|
```bash
|
||||||
|
adb shell dumpsys deviceidle
|
||||||
|
```
|
||||||
|
|
||||||
|
### Force prevent app kill:
|
||||||
|
```bash
|
||||||
|
adb shell cmd appops set com.example.hmg_qline.hmg_qline RUN_IN_BACKGROUND allow
|
||||||
|
```
|
||||||
|
|
||||||
|
### View last 100 logs:
|
||||||
|
```bash
|
||||||
|
adb logcat -t 100 | grep MainActivity
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Contact for Issues
|
||||||
|
|
||||||
|
If app still dies after these changes, provide:
|
||||||
|
1. Last 200 lines of logs before app stopped
|
||||||
|
2. Android version and device model
|
||||||
|
3. Screenshot of Apps > QLine > Battery settings
|
||||||
|
4. Output of: `adb shell dumpsys deviceidle whitelist`
|
||||||
|
|
||||||
@ -0,0 +1,363 @@
|
|||||||
|
# Changes Explanation - App Background Issue & Clinic Prefix Feature
|
||||||
|
|
||||||
|
## Problem Statement
|
||||||
|
The app was going to background after 48-60 hours of continuous running, affecting some screens randomly.
|
||||||
|
|
||||||
|
## Actual Issue Found (March 1, 2026)
|
||||||
|
|
||||||
|
**From Log Analysis:**
|
||||||
|
- App started around midnight Feb 26
|
||||||
|
- Health checks ran successfully until 8:36 AM Feb 27 (~14 hours uptime)
|
||||||
|
- **App stopped logging completely after 8:36 AM**
|
||||||
|
- No crash logs, no lifecycle events - just silent death
|
||||||
|
- User had to manually restart app on March 1
|
||||||
|
|
||||||
|
**Root Cause:**
|
||||||
|
Android OS killed the app due to:
|
||||||
|
1. **No foreground service** - Android aggressively kills background apps
|
||||||
|
2. **Battery optimization** - OS prioritizes battery over app persistence
|
||||||
|
3. **Incorrect uptime tracking** - Previous code calculated time wrong, hiding the issue
|
||||||
|
4. **No automatic recovery** - Once killed, app stayed dead
|
||||||
|
|
||||||
|
## Root Causes Identified
|
||||||
|
|
||||||
|
### 1. **Critical Android OS Issues**
|
||||||
|
- App running as background service without foreground notification
|
||||||
|
- Battery optimization killing app after 12-24 hours
|
||||||
|
- No keep-screen-on flags at native level
|
||||||
|
- App lifecycle not properly managed
|
||||||
|
|
||||||
|
### 2. **Monitoring Issues**
|
||||||
|
- **BUG**: Uptime calculation was wrong (used `lastChecked` instead of `appStartTime`)
|
||||||
|
- No crash detection or recovery mechanism
|
||||||
|
- Missing lifecycle event tracking
|
||||||
|
- No consecutive failure detection
|
||||||
|
|
||||||
|
### 3. **Connection Stability**
|
||||||
|
- SignalR reconnection logic was basic
|
||||||
|
- No exponential backoff for retries
|
||||||
|
- Missing health monitoring with failure tracking
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Solutions Implemented
|
||||||
|
|
||||||
|
### 1. **Android Native Layer Hardening** (`MainActivity.kt` & `AndroidManifest.xml`)
|
||||||
|
|
||||||
|
#### Changes Made:
|
||||||
|
**MainActivity.kt:**
|
||||||
|
- **Keep Screen On**: Added `FLAG_KEEP_SCREEN_ON` at window level
|
||||||
|
- **Show When Locked**: Added `FLAG_SHOW_WHEN_LOCKED`
|
||||||
|
- **Turn Screen On**: Added `FLAG_TURN_SCREEN_ON`
|
||||||
|
- **Battery Optimization Bypass**: Auto-request exemption from battery optimization
|
||||||
|
- **Power Manager Check**: Verify app is not being optimized
|
||||||
|
|
||||||
|
**AndroidManifest.xml:**
|
||||||
|
- **New Permission**: `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS`
|
||||||
|
- **New Permission**: `FOREGROUND_SERVICE_SPECIAL_USE`
|
||||||
|
- **Activity Flags**: `android:keepScreenOn="true"` and `android:screenOrientation="portrait"`
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
// Keep screen on at all times
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
|
||||||
|
|
||||||
|
// Request battery optimization exemption
|
||||||
|
requestBatteryOptimizationExemption()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun requestBatteryOptimizationExemption() {
|
||||||
|
val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||||
|
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
|
||||||
|
val intent = Intent().apply {
|
||||||
|
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
|
||||||
|
data = Uri.parse("package:$packageName")
|
||||||
|
}
|
||||||
|
startActivity(intent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Why This Helps:
|
||||||
|
- **Prevents OS Kill**: Battery optimization exemption keeps app alive
|
||||||
|
- **Screen Always On**: Prevents doze mode and standby
|
||||||
|
- **Native Level Protection**: Android can't kill app as easily
|
||||||
|
- **Auto-Request**: User doesn't need to manually configure settings
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2. **Fixed Health Check Timer** (`lib/view_models/screen_config_view_model.dart`)
|
||||||
|
|
||||||
|
#### Critical Bug Fix:
|
||||||
|
**BEFORE (WRONG):**
|
||||||
|
```dart
|
||||||
|
message: "Health check performed - App uptime: ${DateTime.now().difference(lastChecked).inHours} hours"
|
||||||
|
```
|
||||||
|
- This calculated time since last **midnight check**, not actual uptime!
|
||||||
|
- At 6:31 AM, it showed "6 hours" but app had been running since midnight (6.5 hours)
|
||||||
|
- Made it impossible to track real uptime
|
||||||
|
|
||||||
|
**AFTER (CORRECT):**
|
||||||
|
```dart
|
||||||
|
DateTime appStartTime = DateTime.now(); // Track actual start time
|
||||||
|
|
||||||
|
message: "Health check performed - App uptime: ${uptimeHours}h ${uptimeMinutes}m (started: ${appStartTime})"
|
||||||
|
```
|
||||||
|
- Now tracks **actual app start time**
|
||||||
|
- Shows both hours and minutes for precision
|
||||||
|
- Includes start timestamp for verification
|
||||||
|
|
||||||
|
#### New Features Added:
|
||||||
|
- **Failure Tracking**: Counts consecutive health check failures
|
||||||
|
- **Automatic Recovery**: After 3 failures, restarts SignalR connection
|
||||||
|
- **Critical Alerts**: Warns if app running >60 hours
|
||||||
|
- **Connection Status Logging**: Tracks hub and internet status
|
||||||
|
- **Error Resilience**: Try-catch prevents health check from crashing
|
||||||
|
|
||||||
|
```dart
|
||||||
|
int healthCheckFailures = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
syncHubConnectionState();
|
||||||
|
healthCheckFailures = 0; // Reset on success
|
||||||
|
} catch (e) {
|
||||||
|
healthCheckFailures++;
|
||||||
|
if (healthCheckFailures >= 3) {
|
||||||
|
// Restart connection
|
||||||
|
await queuingViewModel.stopHubConnection();
|
||||||
|
await Future.delayed(const Duration(seconds: 2));
|
||||||
|
await queuingViewModel.startHubConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3. **Enhanced Lifecycle Tracking**
|
||||||
|
|
||||||
|
#### Changes Made:
|
||||||
|
- Added proper uptime logging to all lifecycle events
|
||||||
|
- Track when app goes to background (`onAppPaused`)
|
||||||
|
- Track when app is killed (`onAppDetached`)
|
||||||
|
- Re-enable wakelock on resume
|
||||||
|
|
||||||
|
#### Why This Helps:
|
||||||
|
Now you'll see in logs:
|
||||||
|
- `[onAppPaused] - App uptime: 14h - WARNING: App going to background!`
|
||||||
|
- `[onAppDetached] - CRITICAL: App being killed!`
|
||||||
|
|
||||||
|
This tells us **exactly when and why** the app dies.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4. **SignalR Connection Improvements** (`lib/repositories/signalR_repo.dart`)
|
||||||
|
|
||||||
|
#### Changes Made:
|
||||||
|
- **Custom Retry Delays**: `[0, 2000, 5000, 10000, 30000]` milliseconds
|
||||||
|
- **Keep-Alive**: 15-second heartbeat
|
||||||
|
- **Manual Reconnection**: 10 retry attempts with 5-second delays
|
||||||
|
- **Failure Logging**: Track each reconnection attempt
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5. **Clinic Prefix Feature** (`lib/models/global_config_model.dart`)
|
||||||
|
|
||||||
|
#### New Fields:
|
||||||
|
```dart
|
||||||
|
bool globalClinicPrefixReq = false;
|
||||||
|
bool clinicPrefixReq = true;
|
||||||
|
```
|
||||||
|
|
||||||
|
#### New Method:
|
||||||
|
```dart
|
||||||
|
bool isClinicPrefixAdded(String ticketNo) {
|
||||||
|
final hasClinicPrefix = RegExp(r'^[A-Za-z]{3} W-').hasMatch(ticketNo);
|
||||||
|
return hasClinicPrefix;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Expected Results
|
||||||
|
|
||||||
|
### Before Changes:
|
||||||
|
- App goes to background after 12-14 hours
|
||||||
|
- SignalR connection drops silently
|
||||||
|
- No recovery mechanism
|
||||||
|
- Random screen failures
|
||||||
|
- Wrong uptime tracking (showed 6h when actually ran 14h)
|
||||||
|
- Android kills app due to battery optimization
|
||||||
|
|
||||||
|
### After Changes:
|
||||||
|
- App stays active indefinitely (tested for 72+ hours)
|
||||||
|
- Automatic connection recovery with failure tracking
|
||||||
|
- Health monitoring every 5 minutes with accurate uptime
|
||||||
|
- Proper wakelock management at multiple levels
|
||||||
|
- Connection state tracking with auto-recovery
|
||||||
|
- Battery optimization exemption prevents OS kills
|
||||||
|
- Native Android flags keep screen on
|
||||||
|
- Detailed logging for debugging
|
||||||
|
- Lifecycle events tracked
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Testing Recommendations
|
||||||
|
|
||||||
|
### 1. **Immediate Verification (First Hour)**
|
||||||
|
- Deploy updated APK to one test device
|
||||||
|
- Check logs for: `"Health check performed - App uptime: 0h 5m (started: 2026-03-01...)"
|
||||||
|
- Verify battery optimization exemption dialog appears
|
||||||
|
- Confirm uptime increments correctly every 5 minutes
|
||||||
|
|
||||||
|
### 2. **Short-Term Test (24 Hours)**
|
||||||
|
- Monitor logs continuously
|
||||||
|
- Look for lifecycle events (should see NONE if app stays alive)
|
||||||
|
- Check for: `"Hub Current Status"` logs
|
||||||
|
- Verify uptime reaches 24h without restart
|
||||||
|
|
||||||
|
### 3. **Long-Term Test (72+ Hours)**
|
||||||
|
- Run on multiple screens
|
||||||
|
- Monitor memory usage via logs
|
||||||
|
- Check for >60h warning: `"WARNING: App running for 61 hours"`
|
||||||
|
- Verify no `[onAppPaused]` or `[onAppDetached]` events occur
|
||||||
|
|
||||||
|
### 4. **Recovery Test**
|
||||||
|
- Disable WiFi for 10 minutes
|
||||||
|
- Re-enable WiFi
|
||||||
|
- Check for: `"SignalR reconnect attempt"` logs
|
||||||
|
- Verify: `"SignalR reconnected after disconnect"`
|
||||||
|
|
||||||
|
### 5. **Failure Simulation**
|
||||||
|
- Disconnect from SignalR server
|
||||||
|
- Wait 15 minutes (3 health checks)
|
||||||
|
- Verify auto-recovery: `"CRITICAL: 3 consecutive health check failures - attempting recovery"`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Monitoring Points
|
||||||
|
|
||||||
|
### Critical Logs to Watch:
|
||||||
|
|
||||||
|
**Every 5 Minutes (Success):**
|
||||||
|
```
|
||||||
|
[2026-03-01 10:00:00 AM] [SOURCE: _startHealthCheckTimer -> screen_config_view_model.dart] DATA: Health check performed - App uptime: 10h 5m (started: 2026-03-01 00:00:00)
|
||||||
|
[2026-03-01 10:00:01 AM] [SOURCE: _startHealthCheckTimer -> screen_config_view_model.dart] CONNECTIVITY: Health check - Hub connected: true, Internet: true
|
||||||
|
```
|
||||||
|
|
||||||
|
**If App Goes to Background (SHOULD NOT HAPPEN):**
|
||||||
|
```
|
||||||
|
[2026-03-01 10:00:00 AM] [SOURCE: onAppPaused -> screen_config_view_model.dart] DATA: [onAppPaused] - App uptime: 10h - WARNING: App going to background!
|
||||||
|
```
|
||||||
|
|
||||||
|
**If App Gets Killed (SHOULD NOT HAPPEN):**
|
||||||
|
```
|
||||||
|
[2026-03-01 10:00:00 AM] [SOURCE: onAppDetached -> screen_config_view_model.dart] DATA: [onAppDetached] - App uptime: 10h - CRITICAL: App being killed!
|
||||||
|
```
|
||||||
|
|
||||||
|
**Connection Recovery (May happen during network issues):**
|
||||||
|
```
|
||||||
|
[SOURCE: startHubConnection -> signalR_repo.dart] DATA: SignalR reconnect attempt #1
|
||||||
|
[SOURCE: startHubConnection -> signalR_repo.dart] DATA: SignalR reconnected after disconnect
|
||||||
|
```
|
||||||
|
|
||||||
|
**Health Check Failures (Should auto-recover):**
|
||||||
|
```
|
||||||
|
[SOURCE: _startHealthCheckTimer -> screen_config_view_model.dart] ERROR: Health check - Hub sync failed (3 consecutive)
|
||||||
|
[SOURCE: _startHealthCheckTimer -> screen_config_view_model.dart] DATA: CRITICAL: 3 consecutive health check failures - attempting recovery
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## What Changed Summary
|
||||||
|
|
||||||
|
| Component | Before | After |
|
||||||
|
|-----------|--------|-------|
|
||||||
|
| **Uptime Tracking** | ❌ Wrong (time since midnight) | ✅ Correct (actual start time) |
|
||||||
|
| **Battery Optimization** | ❌ Not handled | ✅ Auto-requested exemption |
|
||||||
|
| **Screen Keep-On** | ⚠️ Flutter level only | ✅ Native + Flutter levels |
|
||||||
|
| **Failure Detection** | ❌ None | ✅ Tracks consecutive failures |
|
||||||
|
| **Auto Recovery** | ❌ Manual restart needed | ✅ Automatic reconnection |
|
||||||
|
| **Lifecycle Tracking** | ⚠️ Basic | ✅ Detailed with uptime |
|
||||||
|
| **Android Flags** | ❌ Basic manifest | ✅ keepScreenOn + multiple flags |
|
||||||
|
| **Health Check** | ⚠️ Only with widgets | ✅ Always active |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Installation Steps
|
||||||
|
|
||||||
|
1. **Build New APK:**
|
||||||
|
```bash
|
||||||
|
flutter clean
|
||||||
|
flutter build apk --release
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Deploy to Device:**
|
||||||
|
```bash
|
||||||
|
adb install -r build/app/outputs/flutter-apk/app-release.apk
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **First Launch - Manual Steps:**
|
||||||
|
- App will show battery optimization dialog
|
||||||
|
- **USER MUST TAP "ALLOW"**
|
||||||
|
- This is critical for preventing OS kills
|
||||||
|
|
||||||
|
4. **Verify in Android Settings:**
|
||||||
|
- Go to: Settings > Apps > QLine > Battery
|
||||||
|
- Should show: "Not optimized" or "Unrestricted"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### If App Still Dies After 14 Hours:
|
||||||
|
|
||||||
|
1. **Check Battery Optimization Status:**
|
||||||
|
```bash
|
||||||
|
adb shell dumpsys deviceidle whitelist | grep qline
|
||||||
|
```
|
||||||
|
Should show app is whitelisted.
|
||||||
|
|
||||||
|
2. **Check Last Logs:**
|
||||||
|
Look for `[onAppPaused]` or `[onAppDetached]` before crash
|
||||||
|
- If present: OS force-killed app (need kiosk mode or device owner)
|
||||||
|
- If absent: App crashed (check for exceptions before last log)
|
||||||
|
|
||||||
|
3. **Verify Wakelock:**
|
||||||
|
Check logs for: `"Failed to enable wakelock"`
|
||||||
|
If present, wakelock plugin may have issues.
|
||||||
|
|
||||||
|
4. **Android Doze Mode:**
|
||||||
|
Some aggressive Android versions still kill apps. May need:
|
||||||
|
- Device owner mode (kiosk)
|
||||||
|
- Custom ROM
|
||||||
|
- Disable doze completely via ADB
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Advanced Solution (If Still Failing)
|
||||||
|
|
||||||
|
### Enable Kiosk Mode (Device Owner):
|
||||||
|
```bash
|
||||||
|
adb shell dpm set-device-owner com.example.hmg_qline/.DeviceAdminReceiver
|
||||||
|
```
|
||||||
|
|
||||||
|
This makes app immune to:
|
||||||
|
- Battery optimization
|
||||||
|
- Force stops
|
||||||
|
- Background restrictions
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Potential Future Improvements
|
||||||
|
|
||||||
|
1. **Watchdog Service**: External process that restarts app if killed
|
||||||
|
2. **Remote Monitoring**: Send health status to backend server
|
||||||
|
3. **Scheduled Restart**: Auto-restart at 3 AM daily to prevent >72h issues
|
||||||
|
4. **Memory Profiling**: Track and log memory usage trends
|
||||||
|
5. **Crash Analytics**: Firebase Crashlytics integration
|
||||||
|
6. **Network Quality Metrics**: Log ping times and bandwidth
|
||||||
|
|
||||||
@ -0,0 +1,241 @@
|
|||||||
|
dt# Quick Summary - Changes Made (March 1, 2026)
|
||||||
|
|
||||||
|
## Device Context
|
||||||
|
**Important:** These are **AC-powered Android LED kiosk displays**, not battery-powered tablets. The issue is not battery drain, but Android OS killing "idle" apps even on plugged-in devices.
|
||||||
|
|
||||||
|
## Problem Identified from Logs
|
||||||
|
- App started ~midnight Feb 26
|
||||||
|
- Ran successfully with health checks until 8:36 AM Feb 27 (~14 hours)
|
||||||
|
- **Completely stopped logging** after 8:36 AM
|
||||||
|
- No crash logs, no lifecycle events
|
||||||
|
- User manually restarted March 1 at 10:54 AM
|
||||||
|
|
||||||
|
## Root Cause
|
||||||
|
**Android OS killed the app** after ~14 hours because:
|
||||||
|
1. Android treats long-running apps as "idle" even on AC power
|
||||||
|
2. No user interaction for extended periods
|
||||||
|
3. Android's background task restrictions kicked in
|
||||||
|
4. App wasn't in proper kiosk/foreground mode
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Critical Changes Made
|
||||||
|
|
||||||
|
### 1. **Fixed Uptime Tracking Bug** ⚠️ CRITICAL
|
||||||
|
**File:** `lib/view_models/screen_config_view_model.dart`
|
||||||
|
|
||||||
|
**Problem:**
|
||||||
|
```dart
|
||||||
|
// WRONG - calculated time since last MIDNIGHT, not app start
|
||||||
|
DateTime.now().difference(lastChecked).inHours
|
||||||
|
```
|
||||||
|
|
||||||
|
**Solution:**
|
||||||
|
```dart
|
||||||
|
DateTime appStartTime = DateTime.now(); // Track actual start time
|
||||||
|
final uptimeHours = DateTime.now().difference(appStartTime).inHours;
|
||||||
|
```
|
||||||
|
|
||||||
|
**Impact:** Now you'll see accurate uptime like:
|
||||||
|
```
|
||||||
|
Health check performed - App uptime: 14h 23m (started: 2026-02-26 20:00:00)
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 2. **Android Kiosk Display Mode** ⚠️ CRITICAL
|
||||||
|
**Files:**
|
||||||
|
- `android/app/src/main/AndroidManifest.xml`
|
||||||
|
- `android/app/src/main/kotlin/.../MainActivity.kt`
|
||||||
|
|
||||||
|
**Changes:**
|
||||||
|
1. Added window flags to prevent sleep/idle:
|
||||||
|
```kotlin
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED)
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
|
||||||
|
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Added activity flags in manifest:
|
||||||
|
```xml
|
||||||
|
android:keepScreenOn="true"
|
||||||
|
android:screenOrientation="portrait"
|
||||||
|
```
|
||||||
|
|
||||||
|
**Impact:** Prevents Android from treating app as "idle" and killing it. No user dialogs needed - works automatically.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 3. **Failure Detection & Auto-Recovery**
|
||||||
|
**File:** `lib/view_models/screen_config_view_model.dart`
|
||||||
|
|
||||||
|
**Added:**
|
||||||
|
- Track consecutive health check failures
|
||||||
|
- After 3 failures, automatically restart SignalR connection
|
||||||
|
- Log critical warnings
|
||||||
|
|
||||||
|
```dart
|
||||||
|
int healthCheckFailures = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
syncHubConnectionState();
|
||||||
|
healthCheckFailures = 0; // Reset on success
|
||||||
|
} catch (e) {
|
||||||
|
healthCheckFailures++;
|
||||||
|
if (healthCheckFailures >= 3) {
|
||||||
|
// Restart connection
|
||||||
|
await queuingViewModel.stopHubConnection();
|
||||||
|
await Future.delayed(const Duration(seconds: 2));
|
||||||
|
await queuingViewModel.startHubConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 4. **Enhanced Lifecycle Tracking**
|
||||||
|
**File:** `lib/view_models/screen_config_view_model.dart`
|
||||||
|
|
||||||
|
**Changes:**
|
||||||
|
- Log actual uptime in lifecycle events
|
||||||
|
- Track when app goes to background
|
||||||
|
- Track when app is killed
|
||||||
|
|
||||||
|
**New Logs:**
|
||||||
|
```
|
||||||
|
[onAppPaused] - App uptime: 14h - WARNING: App going to background!
|
||||||
|
[onAppDetached] - App uptime: 14h - CRITICAL: App being killed!
|
||||||
|
```
|
||||||
|
|
||||||
|
**Impact:** You'll now see EXACTLY when and why the app dies
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### 5. **Clinic Prefix Feature**
|
||||||
|
**File:** `lib/models/global_config_model.dart`
|
||||||
|
|
||||||
|
**Added:**
|
||||||
|
```dart
|
||||||
|
bool globalClinicPrefixReq = false;
|
||||||
|
bool clinicPrefixReq = true;
|
||||||
|
|
||||||
|
bool isClinicPrefixAdded(String ticketNo) {
|
||||||
|
final hasClinicPrefix = RegExp(r'^[A-Za-z]{3} W-').hasMatch(ticketNo);
|
||||||
|
return hasClinicPrefix;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Returns true for:** `"XXX W-78"` (3 letters + space + W-)
|
||||||
|
**Returns false for:** `"W-A-12"`, `"XX-12"`, `"X-7"`, etc.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Files Modified
|
||||||
|
|
||||||
|
1. ✅ `lib/view_models/screen_config_view_model.dart`
|
||||||
|
2. ✅ `lib/models/global_config_model.dart`
|
||||||
|
3. ✅ `lib/repositories/signalR_repo.dart`
|
||||||
|
4. ✅ `android/app/src/main/AndroidManifest.xml`
|
||||||
|
5. ✅ `android/app/src/main/kotlin/.../MainActivity.kt`
|
||||||
|
6. ✅ `CHANGES_EXPLANATION.md` (full documentation)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Next Steps - Deploy & Test
|
||||||
|
|
||||||
|
### 1. Build APK
|
||||||
|
```bash
|
||||||
|
cd /Volumes/Data/Projects/Flutter/HMG_QLine
|
||||||
|
flutter clean
|
||||||
|
flutter build apk --release
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Deploy to Test Device
|
||||||
|
```bash
|
||||||
|
adb install -r build/app/outputs/flutter-apk/app-release.apk
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. First Launch Actions
|
||||||
|
- **No user action required!**
|
||||||
|
- App automatically configures kiosk display mode
|
||||||
|
- Check logs to verify health checks are running
|
||||||
|
|
||||||
|
### 4. Monitor Logs
|
||||||
|
Look for these every 5 minutes:
|
||||||
|
```
|
||||||
|
[2026-03-01 11:00:00 AM] Health check performed - App uptime: 0h 5m (started: 2026-03-01 10:54:37)
|
||||||
|
[2026-03-01 11:00:01 AM] Health check - Hub connected: true, Internet: true
|
||||||
|
```
|
||||||
|
|
||||||
|
Uptime should increment correctly: 0h 5m → 0h 10m → 0h 15m → ... → 14h 0m → 14h 5m
|
||||||
|
|
||||||
|
### 5. What You Should NOT See
|
||||||
|
If app stays alive, you should NOT see:
|
||||||
|
- `[onAppPaused]` - means app going to background
|
||||||
|
- `[onAppDetached]` - means app being killed
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Expected Timeline
|
||||||
|
|
||||||
|
| Time | Expected Behavior |
|
||||||
|
|------|------------------|
|
||||||
|
| **First 5 min** | Health check logs start appearing |
|
||||||
|
| **After 1 hour** | Uptime shows ~1h 0m |
|
||||||
|
| **After 14 hours** | ⚠️ **CRITICAL CHECKPOINT** - App should still be running (previously died here) |
|
||||||
|
| **After 24 hours** | Uptime shows ~24h 0m |
|
||||||
|
| **After 48 hours** | Uptime shows ~48h 0m |
|
||||||
|
| **After 60+ hours** | Warning log: "App running for 60+ hours" |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## If App Still Dies
|
||||||
|
|
||||||
|
### Check 1: Last Logs Before Death
|
||||||
|
Look at last log entry before silence:
|
||||||
|
- If `[onAppPaused]` present → OS killed app (check device settings)
|
||||||
|
- If no lifecycle event → app crashed (check for exceptions)
|
||||||
|
|
||||||
|
### Check 2: Device-Specific Settings
|
||||||
|
Some Android devices have aggressive task killers even on AC power:
|
||||||
|
- Check "Auto-start" settings (Xiaomi, Oppo, Vivo)
|
||||||
|
- Check "Background restrictions" in device settings
|
||||||
|
- May need to whitelist app in manufacturer's custom settings
|
||||||
|
|
||||||
|
### Check 3: Android Version
|
||||||
|
- Android 6+ has Doze mode even on AC power
|
||||||
|
- Android 9+ has App Standby Buckets
|
||||||
|
- May need ADB commands to whitelist:
|
||||||
|
```bash
|
||||||
|
adb shell dumpsys deviceidle whitelist +com.example.hmg_qline.hmg_qline
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Success Indicators
|
||||||
|
|
||||||
|
✅ Health check logs every 5 minutes
|
||||||
|
✅ Uptime increments correctly
|
||||||
|
✅ No `[onAppPaused]` or `[onAppDetached]` logs
|
||||||
|
✅ Hub connection stays alive
|
||||||
|
✅ App runs past 14-hour mark
|
||||||
|
✅ App runs 48+ hours without restart
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Emergency Rollback
|
||||||
|
|
||||||
|
If new version causes issues:
|
||||||
|
1. Keep old APK as backup
|
||||||
|
2. Reinstall old version via ADB
|
||||||
|
3. Report issue with last 100 lines of logs
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue