Skip to content

Commit cec01e1

Browse files
authored
Fix null-safety crashes in location/weather services (#932)
* Implement a graceful UI fallback when system ringtones are unavailable on iOS * fix(android): remove null-unsafe unwraps in location/weather services Resolve crash-prone null assertions in LocationFetcherService and WeatherFetcherService with guarded parsing, logging, and graceful exits.\n\nCloses #931 * fix(android): make foreground start API-safe for minSdk 24 Use SDK-gated foreground service startup to avoid 3-arg startForeground on older Android versions.\n\nRefs #933
1 parent 5091527 commit cec01e1

3 files changed

Lines changed: 44 additions & 2 deletions

File tree

android/app/src/main/kotlin/com/ccextractor/ultimate_alarm_clock/ultimate_alarm_clock/Utilities/LocationFetcherService.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.content.Intent
1010
import android.content.pm.ServiceInfo
1111
import android.content.SharedPreferences
1212
import android.hardware.display.DisplayManager
13+
import android.os.Build
1314
import android.os.IBinder
1415
import android.os.SystemClock.sleep
1516
import android.util.Log
@@ -40,7 +41,7 @@ class LocationFetcherService : Service() {
4041
displayManager = getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
4142

4243
createNotificationChannel()
43-
startForeground(notificationId, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION)
44+
startForegroundCompat()
4445
var fetchLocationDeffered = async {
4546
fetchLocation()
4647
}
@@ -150,6 +151,15 @@ class LocationFetcherService : Service() {
150151
return START_STICKY
151152
}
152153

154+
private fun startForegroundCompat() {
155+
val notification = getNotification()
156+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
157+
startForeground(notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION)
158+
} else {
159+
startForeground(notificationId, notification)
160+
}
161+
}
162+
153163
private fun getNotification(): Notification {
154164
val intent = Intent(this, MainActivity::class.java) // Replace with your main activity
155165
val pendingIntent =

android/app/src/main/kotlin/com/ccextractor/ultimate_alarm_clock/ultimate_alarm_clock/Utilities/WeatherFetcherService.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.app.PendingIntent
77
import android.app.Service
88
import android.content.Context
99
import android.content.Intent
10+
import android.os.Build
1011
import android.content.SharedPreferences
1112
import android.hardware.display.DisplayManager
1213
import android.os.IBinder
@@ -179,10 +180,19 @@ class WeatherFetcherService() : Service() {
179180

180181

181182
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
182-
startForeground(notificationId, getNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION)
183+
startForegroundCompat()
183184
return START_STICKY
184185
}
185186

187+
private fun startForegroundCompat() {
188+
val notification = getNotification()
189+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
190+
startForeground(notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION)
191+
} else {
192+
startForeground(notificationId, notification)
193+
}
194+
}
195+
186196
private fun getNotification(): Notification {
187197
val intent = Intent(this, MainActivity::class.java) // Replace with your main activity
188198
val pendingIntent =

lib/app/modules/addOrUpdateAlarm/views/ringtone_selection_page.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,29 @@ class RingtoneSelectionPage extends GetView<AddOrUpdateAlarmController> {
159159
);
160160
}
161161

162+
// Widget _buildSystemRingtonesTab() {
163+
// return SystemRingtonePicker(
164+
// isFullScreen: true,
165+
// );
166+
// }
162167
Widget _buildSystemRingtonesTab() {
168+
if (GetPlatform.isIOS) {
169+
return Center(
170+
child: Padding(
171+
padding: const EdgeInsets.all(32.0),
172+
child: Text(
173+
'System ringtones are currently only supported on Android.\n\nPlease upload a custom ringtone to use this feature.',
174+
textAlign: TextAlign.center,
175+
style: TextStyle(
176+
color: themeController.primaryTextColor.value.withOpacity(0.7),
177+
fontSize: 16,
178+
height: 1.5,
179+
),
180+
),
181+
),
182+
);
183+
}
184+
163185
return SystemRingtonePicker(
164186
isFullScreen: true,
165187
);

0 commit comments

Comments
 (0)