Skip to content

Commit dcae1c1

Browse files
Fix crash.
Disable ringtone if not available. Information on UI will be displayed about the alarm. Attempt to invoke virtual method 'void android.media.Ringtone.setVolume(float)' on a null object reference
1 parent c73f009 commit dcae1c1

File tree

1 file changed

+14
-10
lines changed
  • profile_prx/src/main/java/no/nordicsemi/android/prx/repository

1 file changed

+14
-10
lines changed

profile_prx/src/main/java/no/nordicsemi/android/prx/repository/AlarmHandler.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ internal class AlarmHandler @Inject constructor(
1212
private val context: Context
1313
) {
1414

15-
private val highLevelRingtone = RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)).apply {
16-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
17-
volume = 1f
15+
private val highLevelRingtone = RingtoneManager
16+
.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM))
17+
?.apply {
18+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
19+
volume = 1f
20+
}
1821
}
19-
}
2022

21-
private val mediumLevelRingtone = RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)).apply {
22-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
23-
volume = 0.5f
23+
private val mediumLevelRingtone = RingtoneManager
24+
.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE))
25+
?.apply {
26+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
27+
volume = 0.5f
28+
}
2429
}
25-
}
2630

2731
fun playAlarm(alarmLevel: AlarmLevel) {
2832
val ringtone = when (alarmLevel) {
@@ -34,7 +38,7 @@ internal class AlarmHandler @Inject constructor(
3438
}
3539

3640
fun pauseAlarm() {
37-
highLevelRingtone.takeIf { it.isPlaying }?.stop()
38-
mediumLevelRingtone.takeIf { it.isPlaying }?.stop()
41+
highLevelRingtone?.takeIf { it.isPlaying }?.stop()
42+
mediumLevelRingtone?.takeIf { it.isPlaying }?.stop()
3943
}
4044
}

0 commit comments

Comments
 (0)