catch and show exceptions thrown at querying alarms

This commit is contained in:
tibbi 2018-03-14 20:56:34 +01:00
parent 64305e1c15
commit fe11e1aa8e
2 changed files with 31 additions and 24 deletions

View file

@ -1,7 +1,11 @@
package com.simplemobiletools.clock.extensions
import android.app.Activity
import android.media.RingtoneManager
import android.view.WindowManager
import com.simplemobiletools.clock.models.AlarmSound
import com.simplemobiletools.commons.extensions.showErrorToast
import java.util.*
fun Activity.showOverLockscreen() {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON or
@ -9,3 +13,30 @@ fun Activity.showOverLockscreen() {
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON)
}
fun Activity.getAlarms(): ArrayList<AlarmSound> {
val alarms = ArrayList<AlarmSound>()
val manager = RingtoneManager(this)
manager.setType(RingtoneManager.TYPE_ALARM)
try {
val cursor = manager.cursor
val defaultAlarm = AlarmSound(getDefaultAlarmTitle(), getDefaultAlarmUri().toString())
alarms.add(defaultAlarm)
while (cursor.moveToNext()) {
val title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX)
var uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX)
val id = cursor.getString(RingtoneManager.ID_COLUMN_INDEX)
if (!uri.endsWith(id)) {
uri += "/$id"
}
val alarmSound = AlarmSound(title, uri)
alarms.add(alarmSound)
}
} catch (e: Exception) {
showErrorToast(e)
}
return alarms
}

View file

@ -22,7 +22,6 @@ import com.simplemobiletools.clock.activities.SnoozeReminderActivity
import com.simplemobiletools.clock.activities.SplashActivity
import com.simplemobiletools.clock.helpers.*
import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.clock.models.AlarmSound
import com.simplemobiletools.clock.models.MyTimeZone
import com.simplemobiletools.clock.receivers.AlarmReceiver
import com.simplemobiletools.clock.receivers.DateTimeWidgetUpdateReceiver
@ -78,29 +77,6 @@ fun Context.getAllTimeZonesModified(): ArrayList<MyTimeZone> {
fun Context.getModifiedTimeZoneTitle(id: Int) = getAllTimeZonesModified().firstOrNull { it.id == id }?.title ?: getDefaultTimeZoneTitle(id)
fun Context.getAlarms(): ArrayList<AlarmSound> {
val manager = RingtoneManager(this)
manager.setType(RingtoneManager.TYPE_ALARM)
val cursor = manager.cursor
val alarms = ArrayList<AlarmSound>()
val defaultAlarm = AlarmSound(getDefaultAlarmTitle(), getDefaultAlarmUri().toString())
alarms.add(defaultAlarm)
while (cursor.moveToNext()) {
val title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX)
var uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX)
val id = cursor.getString(RingtoneManager.ID_COLUMN_INDEX)
if (!uri.endsWith(id)) {
uri += "/$id"
}
val alarmSound = AlarmSound(title, uri)
alarms.add(alarmSound)
}
return alarms
}
fun Context.getDefaultAlarmUri() = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
fun Context.getDefaultAlarmTitle() = RingtoneManager.getRingtone(this, getDefaultAlarmUri()).getTitle(this)