Skip to content

Commit 24ea7ec

Browse files
committed
fix: Android 9 crash - Implement getForegroundInfo() for WorkManager Expedited Work (Issue #15)
This commit fixes the critical crash on Android 9 (API 28) that occurred when using WorkManager Expedited Work for background sync operations. ## Root Cause When setExpedited() is used in WorkManager, the CoroutineWorker must implement getForegroundInfo() to return a ForegroundInfo object with a Foreground Service notification. On Android 9-11, WorkManager calls this method, but the default implementation throws: IllegalStateException: Not implemented ## Solution - Implemented getForegroundInfo() in SyncWorker - Returns ForegroundInfo with sync progress notification - Android 10+: Sets FOREGROUND_SERVICE_TYPE_DATA_SYNC for proper service typing - Added required Foreground Service permissions to AndroidManifest.xml ## Technical Changes - SyncWorker.kt: Added getForegroundInfo() override - NotificationHelper.kt: Added createSyncProgressNotification() factory method - strings.xml: Added sync_in_progress UI strings (EN + DE) - AndroidManifest.xml: Added FOREGROUND_SERVICE permissions - Version updated to 1.7.1 (versionCode 18) ## Previously Fixed (in this release) - Kernel-VPN compatibility (Wireguard interface detection) - HTTP connection lifecycle optimization (SafeSardineWrapper) - Stability improvements for sync sessions ## Testing - Tested on Android 9 (API 28) - No crash on second app start - Tested on Android 15 (API 35) - No regressions - WiFi-connect sync working correctly - Expedited work notifications display properly Fixes #15 Thanks to @roughnecks for detailed bug report and testing!
1 parent df4ee4b commit 24ea7ec

10 files changed

Lines changed: 119 additions & 28 deletions

File tree

CHANGELOG.de.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,43 @@ Das Format basiert auf [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
---
1010

11-
## [1.7.1] - 2026-01-30
11+
## [1.7.1] - 2026-02-02
1212

1313
### 🐛 Kritische Fehlerbehebungen
1414

15-
- **App-Absturz auf Android 9 nach längerer Nutzung behoben** ([ref #15](https://github.com/inventory69/simple-notes-sync/issues/15))
16-
- Ressourcenerschöpfung durch nicht geschlossene HTTP-Verbindungen behoben
17-
- App konnte nach ~30-45 Minuten Nutzung durch angesammelte Connection-Leaks abstürzen
18-
- Danke an [@roughnecks] für den detaillierten Fehlerbericht!
15+
#### Android 9 App-Absturz Fix ([#15](https://github.com/inventory69/simple-notes-sync/issues/15))
1916

20-
- **VPN-Kompatibilitäts-Regression behoben** ([ref #11](https://github.com/inventory69/simple-notes-sync/issues/11))
21-
- WiFi Socket-Binding erkennt jetzt korrekt Wireguard VPN-Interfaces (tun*, wg*, *-wg-*)
22-
- Traffic wird korrekt durch VPN-Tunnel geleitet statt direkt über WiFi
23-
- Behebt "Verbindungs-Timeout" beim Sync zu externen Servern über VPN
17+
**Problem:** App stürzte auf Android 9 (API 28) ab wenn WorkManager Expedited Work für Hintergrund-Sync verwendet wurde.
18+
19+
**Root Cause:** Wenn `setExpedited()` in WorkManager verwendet wird, muss die `CoroutineWorker` die Methode `getForegroundInfo()` implementieren um eine Foreground Service Notification zurückzugeben. Auf Android 9-11 ruft WorkManager diese Methode auf, aber die Standard-Implementierung wirft `IllegalStateException: Not implemented`.
20+
21+
**Lösung:** `getForegroundInfo()` in `SyncWorker` implementiert um eine korrekte `ForegroundInfo` mit Sync-Progress-Notification zurückzugeben.
22+
23+
**Details:**
24+
- `ForegroundInfo` mit Sync-Progress-Notification für Android 9-11 hinzugefügt
25+
- Android 10+: Setzt `FOREGROUND_SERVICE_TYPE_DATA_SYNC` für korrekte Service-Typisierung
26+
- Foreground Service Permissions in AndroidManifest.xml hinzugefügt
27+
- Notification zeigt Sync-Progress mit indeterminiertem Progress Bar
28+
- Danke an [@roughnecks](https://github.com/roughnecks) für das detaillierte Debugging!
29+
30+
#### VPN-Kompatibilitäts-Fix ([#11](https://github.com/inventory69/simple-notes-sync/issues/11))
31+
32+
- WiFi Socket-Binding erkennt jetzt korrekt Wireguard VPN-Interfaces (tun*, wg*, *-wg-*)
33+
- Traffic wird korrekt durch VPN-Tunnel geleitet statt direkt über WiFi
34+
- Behebt "Verbindungs-Timeout" beim Sync zu externen Servern über VPN
2435

2536
### 🔧 Technische Änderungen
2637

2738
- Neue `SafeSardineWrapper` Klasse stellt korrektes HTTP-Connection-Cleanup sicher
2839
- Weniger unnötige 401-Authentifizierungs-Challenges durch preemptive Auth-Header
2940
- ProGuard-Regel hinzugefügt um harmlose TextInclusionStrategy-Warnungen zu unterdrücken
3041
- VPN-Interface-Erkennung via `NetworkInterface.getNetworkInterfaces()` Pattern-Matching
42+
- Foreground Service Erkennung und Notification-System für Hintergrund-Sync-Tasks
3143

3244
### 🌍 Lokalisierung
3345

3446
- Hardcodierte deutsche Fehlermeldungen behoben - jetzt String-Resources für korrekte Lokalisierung
47+
- Deutsche und englische Strings für Sync-Progress-Notifications hinzugefügt
3548

3649
---
3750

CHANGELOG.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,43 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
88

99
---
1010

11-
## [1.7.1] - 2026-01-30
11+
## [1.7.1] - 2026-02-02
1212

1313
### 🐛 Critical Bug Fixes
1414

15-
- **Fixed app crash on Android 9 after extended use** ([ref #15](https://github.com/inventory69/simple-notes-sync/issues/15))
16-
- Fixed resource exhaustion caused by unclosed HTTP connections
17-
- App could crash after ~30-45 minutes of use due to accumulated connection leaks
18-
- Thanks to [@roughnecks] for the detailed bug report!
15+
#### Android 9 App Crash Fix ([#15](https://github.com/inventory69/simple-notes-sync/issues/15))
1916

20-
- **Fixed VPN compatibility regression** ([ref #11](https://github.com/inventory69/simple-notes-sync/issues/11))
21-
- WiFi socket binding now correctly detects Wireguard VPN interfaces (tun*, wg*, *-wg-*)
22-
- Traffic routes through VPN tunnel instead of bypassing it directly to WiFi
23-
- Fixes "Connection timeout" when syncing to external servers via VPN
17+
**Problem:** App crashed on Android 9 (API 28) when using WorkManager Expedited Work for background sync.
18+
19+
**Root Cause:** When `setExpedited()` is used in WorkManager, the `CoroutineWorker` must implement `getForegroundInfo()` to return a Foreground Service notification. On Android 9-11, WorkManager calls this method, but the default implementation throws `IllegalStateException: Not implemented`.
20+
21+
**Solution:** Implemented `getForegroundInfo()` in `SyncWorker` to return a proper `ForegroundInfo` with sync progress notification.
22+
23+
**Details:**
24+
- Added `ForegroundInfo` with sync progress notification for Android 9-11
25+
- Android 10+: Sets `FOREGROUND_SERVICE_TYPE_DATA_SYNC` for proper service typing
26+
- Added Foreground Service permissions to AndroidManifest.xml
27+
- Notification shows sync progress with indeterminate progress bar
28+
- Thanks to [@roughnecks](https://github.com/roughnecks) for the detailed debugging!
29+
30+
#### VPN Compatibility Fix ([#11](https://github.com/inventory69/simple-notes-sync/issues/11))
31+
32+
- WiFi socket binding now correctly detects Wireguard VPN interfaces (tun*, wg*, *-wg-*)
33+
- Traffic routes through VPN tunnel instead of bypassing it directly to WiFi
34+
- Fixes "Connection timeout" when syncing to external servers via VPN
2435

2536
### 🔧 Technical Changes
2637

2738
- New `SafeSardineWrapper` class ensures proper HTTP connection cleanup
2839
- Reduced unnecessary 401 authentication challenges with preemptive auth headers
2940
- Added ProGuard rule to suppress harmless TextInclusionStrategy warnings on older Android versions
3041
- VPN interface detection via `NetworkInterface.getNetworkInterfaces()` pattern matching
42+
- Foreground Service detection and notification system for background sync tasks
3143

3244
### 🌍 Localization
3345

3446
- Fixed hardcoded German error messages - now uses string resources for proper localization
47+
- Added German and English strings for sync progress notifications
3548

3649
---
3750

android/app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ android {
2020
applicationId = "dev.dettmer.simplenotes"
2121
minSdk = 24
2222
targetSdk = 36
23-
versionCode = 18 // 🔧 v1.7.1: Connection Leak Fix (Issue #15)
24-
versionName = "1.7.1" // 🔧 v1.7.1: Connection Leak Fix
23+
versionCode = 18 // 🔧 v1.7.1: Android 9 getForegroundInfo Fix (Issue #15)
24+
versionName = "1.7.1" // 🔧 v1.7.1: Android 9 getForegroundInfo Fix
2525

2626
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2727
}

android/app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
<!-- Battery Optimization (for WorkManager background sync) -->
1313
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
1414

15+
<!-- v1.7.2: Foreground Service for Expedited Work (Android 9-11) -->
16+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
17+
<!-- v1.7.2: Foreground Service Type for Android 10+ -->
18+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
19+
1520
<!-- BOOT Permission - CRITICAL für Auto-Sync nach Neustart! -->
1621
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
1722

android/app/src/main/java/dev/dettmer/simplenotes/sync/SyncWorker.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ package dev.dettmer.simplenotes.sync
55
import android.app.ActivityManager
66
import android.content.Context
77
import android.content.Intent
8+
import android.content.pm.ServiceInfo
9+
import android.os.Build
810
import androidx.localbroadcastmanager.content.LocalBroadcastManager
911
import androidx.work.CoroutineWorker
12+
import androidx.work.ForegroundInfo
1013
import androidx.work.WorkerParameters
1114
import dev.dettmer.simplenotes.BuildConfig
1215
import dev.dettmer.simplenotes.utils.Constants
@@ -26,6 +29,35 @@ class SyncWorker(
2629
const val ACTION_SYNC_COMPLETED = "dev.dettmer.simplenotes.SYNC_COMPLETED"
2730
}
2831

32+
/**
33+
* 🔧 v1.7.2: Required for expedited work on Android 9-11
34+
*
35+
* WorkManager ruft diese Methode auf um die Foreground-Notification zu erstellen
36+
* wenn der Worker als Expedited Work gestartet wird.
37+
*
38+
* Ab Android 12+ wird diese Methode NICHT aufgerufen (neue Expedited API).
39+
* Auf Android 9-11 MUSS diese Methode implementiert sein!
40+
*
41+
* @see https://developer.android.com/develop/background-work/background-tasks/persistent/getting-started/define-work#foregroundinfo
42+
*/
43+
override suspend fun getForegroundInfo(): ForegroundInfo {
44+
val notification = NotificationHelper.createSyncProgressNotification(applicationContext)
45+
46+
// Android 10+ benötigt foregroundServiceType
47+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
48+
ForegroundInfo(
49+
NotificationHelper.SYNC_PROGRESS_NOTIFICATION_ID,
50+
notification,
51+
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
52+
)
53+
} else {
54+
ForegroundInfo(
55+
NotificationHelper.SYNC_PROGRESS_NOTIFICATION_ID,
56+
notification
57+
)
58+
}
59+
}
60+
2961
/**
3062
* Prüft ob die App im Vordergrund ist.
3163
* Wenn ja, brauchen wir keine Benachrichtigung - die UI zeigt die Änderungen direkt.

android/app/src/main/java/dev/dettmer/simplenotes/utils/NotificationHelper.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ object NotificationHelper {
1919
private const val CHANNEL_ID = "notes_sync_channel"
2020
private const val NOTIFICATION_ID = 1001
2121
private const val SYNC_NOTIFICATION_ID = 2
22+
const val SYNC_PROGRESS_NOTIFICATION_ID = 1003 // v1.7.2: For expedited work foreground notification
2223
private const val AUTO_CANCEL_TIMEOUT_MS = 30_000L
2324

2425
/**
@@ -54,6 +55,26 @@ object NotificationHelper {
5455
Logger.d(TAG, "🗑️ Cleared old sync notifications")
5556
}
5657

58+
/**
59+
* 🔧 v1.7.2: Erstellt Notification für Sync-Progress (Expedited Work)
60+
*
61+
* Wird von SyncWorker.getForegroundInfo() aufgerufen auf Android 9-11.
62+
* Muss eine gültige, sichtbare Notification zurückgeben.
63+
*
64+
* @return Notification (nicht anzeigen, nur erstellen)
65+
*/
66+
fun createSyncProgressNotification(context: Context): android.app.Notification {
67+
return NotificationCompat.Builder(context, CHANNEL_ID)
68+
.setSmallIcon(android.R.drawable.stat_notify_sync)
69+
.setContentTitle(context.getString(R.string.sync_in_progress))
70+
.setContentText(context.getString(R.string.sync_in_progress_text))
71+
.setPriority(NotificationCompat.PRIORITY_LOW)
72+
.setOngoing(true)
73+
.setProgress(0, 0, true) // Indeterminate progress
74+
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
75+
.build()
76+
}
77+
5778
/**
5879
* Zeigt Erfolgs-Notification nach Sync
5980
*/

android/app/src/main/res/values-de/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@
438438
<!-- ============================= -->
439439
<string name="notification_channel_name">Notizen Synchronisierung</string>
440440
<string name="notification_channel_desc">Benachrichtigungen über Sync-Status</string>
441+
<string name="sync_in_progress">Synchronisierung läuft</string>
442+
<string name="sync_in_progress_text">Notizen werden synchronisiert…</string>
441443
<string name="notification_sync_success_title">Sync erfolgreich</string>
442444
<string name="notification_sync_success_message">%d Notiz(en) synchronisiert</string>
443445
<string name="notification_sync_failed_title">Sync fehlgeschlagen</string>

android/app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@
438438
<!-- ============================= -->
439439
<string name="notification_channel_name">Notes Synchronization</string>
440440
<string name="notification_channel_desc">Notifications about sync status</string>
441+
<string name="sync_in_progress">Syncing</string>
442+
<string name="sync_in_progress_text">Syncing notes…</string>
441443
<string name="notification_sync_success_title">Sync successful</string>
442444
<string name="notification_sync_success_message">%d note(s) synchronized</string>
443445
<string name="notification_sync_failed_title">Sync failed</string>
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
• Behoben: App-Absturz auf Android 9 - Danke an @roughnecks
2-
• Behoben: Kernel-VPN-Kompatibilität (Wireguard)
3-
• Verbessert: Stabilität der Sync-Sessions
4-
• Technisch: Optimierte Verbindungsverwaltung
1+
• Behoben: App-Absturz auf Android 9 (Issue #15) - Danke an @roughnecks
2+
- WorkManager Expedited Work Kompatibilität (getForegroundInfo)
3+
- Kernel-VPN-Kompatibilität (Wireguard tun/wg Interfaces)
4+
• Verbessert: Stabilität und Verbindungsverwaltung
5+
• Technisch: Optimierter HTTP-Connection-Lebenszyklus
6+
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
• Fixed: App crash on Android 9 - Thanks to @roughnecks
2-
• Fixed: Kernel-VPN compatibility (Wireguard)
3-
• Improved: Stability at sync sessions
4-
• Technical: Optimized connection management
1+
• Fixed: App crash on Android 9 (Issue #15) - Thanks to @roughnecks
2+
- WorkManager expedited work compatibility (getForegroundInfo)
3+
- Kernel-VPN compatibility (Wireguard tun/wg interfaces)
4+
• Improved: Stability and connection management
5+
• Technical: Optimized HTTP connection lifecycle

0 commit comments

Comments
 (0)