Make alarms created by Intent single shot if created without UI

This commit is contained in:
Ensar Sarajčić 2023-08-31 15:57:15 +02:00
parent fe490f6683
commit d69d25907b
8 changed files with 56 additions and 10 deletions

View file

@ -142,6 +142,7 @@ class IntentHandlerActivity : SimpleActivity() {
} else {
TOMORROW_BIT
}
newAlarm.oneShot = true
}
ensureBackgroundThread {

View file

@ -275,8 +275,20 @@ class ReminderActivity : SimpleActivity() {
}
private fun finishActivity() {
if (!wasAlarmSnoozed && alarm != null && alarm!!.days > 0) {
scheduleNextAlarm(alarm!!, false)
if (!wasAlarmSnoozed && alarm != null) {
cancelAlarmClock(alarm!!)
if (alarm!!.days > 0) {
scheduleNextAlarm(alarm!!, false)
}
if (alarm!!.days < 0) {
if (alarm!!.oneShot) {
alarm!!.isEnabled = false
dbHelper.deleteAlarms(arrayListOf(alarm!!))
} else {
dbHelper.updateAlarmEnabledState(alarm!!.id, false)
}
updateWidgets()
}
}
destroyEffects()

View file

@ -157,6 +157,7 @@ class EditAlarmDialog(val activity: SimpleActivity, val alarm: Alarm, val onDism
alarm.label = binding.editAlarm.value
alarm.isEnabled = true
alarm.oneShot = false
var alarmId = alarm.id
activity.handleFullScreenNotificationsPermission { granted ->

View file

@ -87,14 +87,22 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
}
context?.getEnabledAlarms { enabledAlarms ->
if (enabledAlarms.isNullOrEmpty()) {
val removedAlarms = mutableListOf<Alarm>()
alarms.forEach {
if (it.days == TODAY_BIT && it.isEnabled && it.timeInMinutes <= getCurrentDayMinutes()) {
it.isEnabled = false
ensureBackgroundThread {
context?.dbHelper?.updateAlarmEnabledState(it.id, false)
if (it.oneShot) {
it.isEnabled = false
context?.dbHelper?.deleteAlarms(arrayListOf(it))
removedAlarms.add(it)
} else {
context?.dbHelper?.updateAlarmEnabledState(it.id, false)
}
}
}
}
alarms.removeAll(removedAlarms)
}
}
@ -131,6 +139,10 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
val alarm = alarms.firstOrNull { it.id == id } ?: return@handleFullScreenNotificationsPermission
alarm.isEnabled = isEnabled
checkAlarmState(alarm)
if (!alarm.isEnabled && alarm.oneShot) {
requireContext().dbHelper.deleteAlarms(arrayListOf(alarm))
setupAlarms()
}
} else {
requireActivity().toast(com.simplemobiletools.commons.R.string.unknown_error_occurred)
}

View file

@ -23,11 +23,12 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private val COL_SOUND_TITLE = "sound_title"
private val COL_SOUND_URI = "sound_uri"
private val COL_LABEL = "label"
private val COL_ONE_SHOT = "one_shot"
private val mDb = writableDatabase
companion object {
private const val DB_VERSION = 1
private const val DB_VERSION = 2
const val DB_NAME = "alarms.db"
var dbInstance: DBHelper? = null
@ -47,7 +48,13 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
insertInitialAlarms(db)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
var version = oldVersion
if (version == 1 && newVersion > version) {
db.execSQL("ALTER TABLE $ALARMS_TABLE_NAME ADD COLUMN $COL_ONE_SHOT INTEGER NOT NULL DEFAULT 0")
version++
}
}
private fun insertInitialAlarms(db: SQLiteDatabase) {
val weekDays = MONDAY_BIT or TUESDAY_BIT or WEDNESDAY_BIT or THURSDAY_BIT or FRIDAY_BIT
@ -102,6 +109,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
put(COL_SOUND_TITLE, alarm.soundTitle)
put(COL_SOUND_URI, alarm.soundUri)
put(COL_LABEL, alarm.label)
put(COL_ONE_SHOT, alarm.oneShot)
}
}
@ -109,7 +117,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
fun getAlarms(): ArrayList<Alarm> {
val alarms = ArrayList<Alarm>()
val cols = arrayOf(COL_ID, COL_TIME_IN_MINUTES, COL_DAYS, COL_IS_ENABLED, COL_VIBRATE, COL_SOUND_TITLE, COL_SOUND_URI, COL_LABEL)
val cols = arrayOf(COL_ID, COL_TIME_IN_MINUTES, COL_DAYS, COL_IS_ENABLED, COL_VIBRATE, COL_SOUND_TITLE, COL_SOUND_URI, COL_LABEL, COL_ONE_SHOT)
var cursor: Cursor? = null
try {
cursor = mDb.query(ALARMS_TABLE_NAME, cols, null, null, null, null, null)
@ -124,8 +132,9 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val soundTitle = cursor.getStringValue(COL_SOUND_TITLE)
val soundUri = cursor.getStringValue(COL_SOUND_URI)
val label = cursor.getStringValue(COL_LABEL)
val oneShot = cursor.getIntValue(COL_ONE_SHOT) == 1
val alarm = Alarm(id, timeInMinutes, days, isEnabled, vibrate, soundTitle, soundUri, label)
val alarm = Alarm(id, timeInMinutes, days, isEnabled, vibrate, soundTitle, soundUri, label, oneShot)
alarms.add(alarm)
} catch (e: Exception) {
continue

View file

@ -8,5 +8,6 @@ data class Alarm(
var vibrate: Boolean,
var soundTitle: String,
var soundUri: String,
var label: String
var label: String,
var oneShot: Boolean = false
)

View file

@ -27,7 +27,12 @@ class DismissAlarmReceiver : BroadcastReceiver() {
context.cancelAlarmClock(alarm)
scheduleNextAlarm(alarm, context)
if (alarm.days < 0) {
context.dbHelper.updateAlarmEnabledState(alarm.id, false)
if (alarm.oneShot) {
alarm.isEnabled = false
context.dbHelper.deleteAlarms(arrayListOf(alarm))
} else {
context.dbHelper.updateAlarmEnabledState(alarm.id, false)
}
context.updateWidgets()
}
}

View file

@ -21,7 +21,12 @@ class HideAlarmReceiver : BroadcastReceiver() {
ensureBackgroundThread {
val alarm = context.dbHelper.getAlarmWithId(id)
if (alarm != null && alarm.days < 0) {
context.dbHelper.updateAlarmEnabledState(alarm.id, false)
if (alarm.oneShot) {
alarm.isEnabled = false
context.dbHelper.deleteAlarms(arrayListOf(alarm))
} else {
context.dbHelper.updateAlarmEnabledState(alarm.id, false)
}
context.updateWidgets()
}
}