Skip to content

Commit

Permalink
catch and show exceptions thrown at querying alarms
Browse files Browse the repository at this point in the history
  • Loading branch information
tibbi committed Mar 14, 2018
1 parent 64305e1 commit fe11e1a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
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
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fe11e1a

Please sign in to comment.