From 89ba9c4492f105053ce671011ed9f87680816777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Delmas?= Date: Thu, 21 Dec 2023 19:53:34 +0100 Subject: [PATCH] fix intent conflict --- .../main/java/com/rooster/rooster/Alarm.kt | 4 +- .../java/com/rooster/rooster/AlarmDbHelper.kt | 14 +-- .../java/com/rooster/rooster/AlarmHandler.kt | 117 +++++------------- .../com/rooster/rooster/AlarmclockReceiver.kt | 28 +---- 4 files changed, 50 insertions(+), 113 deletions(-) diff --git a/app/src/main/java/com/rooster/rooster/Alarm.kt b/app/src/main/java/com/rooster/rooster/Alarm.kt index 17783f9..8b537dd 100644 --- a/app/src/main/java/com/rooster/rooster/Alarm.kt +++ b/app/src/main/java/com/rooster/rooster/Alarm.kt @@ -49,7 +49,9 @@ class Alarm( } fun getDayEnabled(d: String): Boolean { - return when (d) { + val day = d.toLowerCase() // Convert input string to lowercase + + return when (day) { "monday" -> this.monday "tuesday" -> this.tuesday "wednesday" -> this.wednesday diff --git a/app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt b/app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt index e8360b2..e4b4359 100644 --- a/app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt +++ b/app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt @@ -111,6 +111,12 @@ class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", nu fun updateAlarm(alarm: Alarm) { val db = writableDatabase // Calculate the alarm time + if (alarm.relative1 != "Pick Time") { + alarm.time1 = getRelativeTime(alarm.relative1) + } + if (alarm.relative2 != "Pick Time") { + alarm.time2 = getRelativeTime(alarm.relative2) + } alarm.calculatedTime = calculateTime(alarm) Log.e("Update Alarm", alarm.calculatedTime.toString()) val values = ContentValues().apply { @@ -130,14 +136,8 @@ class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", nu put("saturday", alarm.saturday) put("sunday", alarm.sunday) } - db.update("alarms", values, "id = ?", arrayOf(alarm.id.toString())) - if (alarm.enabled) { - alarmHandler.unsetAlarm(context, alarm) - alarmHandler.setAlarm(context, alarm) - } else { - alarmHandler.unsetAlarm(context, alarm) - } + alarmHandler.setNextAlarm(context) } private fun calculateTime(alarm: Alarm): Long { diff --git a/app/src/main/java/com/rooster/rooster/AlarmHandler.kt b/app/src/main/java/com/rooster/rooster/AlarmHandler.kt index 22f3f7d..e71668b 100644 --- a/app/src/main/java/com/rooster/rooster/AlarmHandler.kt +++ b/app/src/main/java/com/rooster/rooster/AlarmHandler.kt @@ -2,6 +2,7 @@ package com.rooster.rooster import android.app.AlarmManager import android.app.PendingIntent +import android.content.ContentValues.TAG import android.content.Context import android.content.Intent import android.icu.text.SimpleDateFormat @@ -15,6 +16,7 @@ import android.os.VibrationEffect import android.os.Vibrator import android.util.Log import android.view.WindowManager +import java.util.Locale class AlarmHandler { fun setAlarm(context: Context, alarm: Alarm) { @@ -44,27 +46,6 @@ class AlarmHandler { } } - fun setNextIntent(context: Context?, alarmId: String) { - val alarmDbHelper = context?.let { AlarmDbHelper(it) } - Log.e("SetNextIntent", "Intent Alarm: $alarmId") - var alarm = alarmDbHelper?.getAlarm(alarmId.toLong()) - if (alarm != null) { - // Disable alarm if no repeats - if (alarm.monday || alarm.tuesday || alarm.wednesday || - alarm.thursday || alarm.friday || alarm.saturday || alarm.sunday) { - - } else { - alarm.enabled = false - if (alarmDbHelper != null) { - alarmDbHelper.updateAlarm(alarm) - } - } - } - } - - - - fun unsetAlarm(context: Context, alarm: Alarm) { unsetAlarmById(context, alarm.id) } @@ -79,74 +60,44 @@ class AlarmHandler { am.cancel(PendingIntent.getBroadcast( context, id.toInt(), - intent, - PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_MUTABLE + intent,PendingIntent.FLAG_MUTABLE )) } - fun ring(context: Context) { -// Wake Phone - val powerManager: PowerManager = context?.getSystemService(Context.POWER_SERVICE) as PowerManager - @Suppress("DEPRECATION") val wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP or PowerManager.ON_AFTER_RELEASE or PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "rooster:wakelock") - wakeLock.acquire() - - val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - val gentlePattern = createWaveformVibrationPattern(longArrayOf(0, 1000, 2000, 3000), repeat = -1) - - var vibrationEffect1 = VibrationEffect.createWaveform(gentlePattern, 3) - - // it is safe to cancel other vibrations currently taking place - vibrator.cancel() - val soundUri = Uri.parse("android.resource://${context.packageName}/raw/alarmclock") - val mediaPlayer = MediaPlayer().apply { - setDataSource(context, soundUri) - setAudioAttributes( - AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_ALARM) - .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) - .build() - ) - prepare() - } - // Start a loop that checks a SharedPreferences for a flag to stop the vibration - Thread { - vibrator.vibrate(vibrationEffect1) - while (true) { - Log.w("Rooster Alarm", "Ring") - val sharedPreferences = context.getSharedPreferences("RoosterSharedPrefs", Context.MODE_PRIVATE) - val isAlarmStopped = sharedPreferences.getBoolean("StoppedAlarm", false) - - if (isAlarmStopped) { - vibrator.cancel() - mediaPlayer.stop() - mediaPlayer.release() - wakeLock.release() - break - } else { - mediaPlayer.start() - Thread.sleep(500) - //vibrator.vibrate(vibrationEffect1) - } - Thread.sleep(1000) + fun setNextAlarm(context: Context) { + Log.i(TAG, "Setting Next Alarm") + val alarmDbHelper = AlarmDbHelper(context) + val alarms = alarmDbHelper.getAllAlarms() + + val currentTime = Calendar.getInstance() + val currentMillis = currentTime.timeInMillis + + var closestAlarm: Alarm? = null + var timeDifference: Long = Long.MAX_VALUE + val dayFormat = SimpleDateFormat("EEEE", Locale.getDefault()) + val today: String = dayFormat.format(currentTime.time) + + for (alarm in alarms) { + if (alarm.enabled && + (alarm.getDayEnabled(today) || + (!alarm.monday && !alarm.tuesday && !alarm.wednesday && !alarm.thursday && !alarm.friday && !alarm.saturday && !alarm.sunday))) { + val alarmTime = Calendar.getInstance() + alarmTime.timeInMillis = alarm.calculatedTime + val alarmMillis = alarmTime.timeInMillis + + // Calculate the time difference between current time and alarm time + val diff = alarmMillis - currentMillis + + // If the alarm time is in the future and closer than the previous closest alarm, update closestAlarm and timeDifference + if (diff > 0 && diff < timeDifference) { + closestAlarm = alarm + timeDifference = diff } - }.start() - } - } - - private fun createWaveformVibrationPattern(timings: LongArray, repeat: Int = -1): LongArray { - var pattern = LongArray(timings.size + 1) - pattern[0] = 0L - for (i in 1 until pattern.size) { - pattern[i] = pattern[i - 1] + timings[i - 1] + } } - if (repeat != -1) { - val repeatIndex = pattern[repeat] - val repeatPattern = pattern.copyOfRange(repeatIndex.toInt(), pattern.size) - pattern = pattern.copyOfRange(0, repeatIndex.toInt()) + repeatPattern + closestAlarm?.let { + setAlarm(context, it) } - - return pattern } } \ No newline at end of file diff --git a/app/src/main/java/com/rooster/rooster/AlarmclockReceiver.kt b/app/src/main/java/com/rooster/rooster/AlarmclockReceiver.kt index 4de8288..60d50cb 100644 --- a/app/src/main/java/com/rooster/rooster/AlarmclockReceiver.kt +++ b/app/src/main/java/com/rooster/rooster/AlarmclockReceiver.kt @@ -12,6 +12,7 @@ import androidx.core.app.NotificationCompat import androidx.core.os.postDelayed class AlarmclockReceiver : BroadcastReceiver() { + private var alarmHandler = AlarmHandler() override fun onReceive(context: Context, intent: Intent) { Log.e("RECEIVER", "RECEIVED WELL") if (intent != null && "com.rooster.alarmmanager" == intent.action) { @@ -57,30 +58,13 @@ class AlarmclockReceiver : BroadcastReceiver() { notificationManager.notify(1, notification) context.applicationContext.startActivity(alarmActivityIntent) val handler = Handler() - val delay = 3 * 60 * 1000L + val delay = 30 * 1000L handler.postDelayed({ - val alarmDbHelper = AlarmDbHelper(context) - val alarm = alarmDbHelper.getAlarm(alarmId) - if (alarm != null) { - alarm.calculatedTime = 0 - alarmDbHelper.updateAlarm(alarm) - } + alarmHandler.setNextAlarm(context) }, delay) } else if (intent != null && "android.intent.action.BOOT_COMPLETED" == intent.action) { -// Retrieve saved alarms from persistent storage - var alarmHandler = AlarmHandler() - val alarmDbHelper = AlarmDbHelper(context) - val alarms = alarmDbHelper.getAllAlarms() - - // Re-set alarms using AlarmManager - for (alarm in alarms) { - if (alarm.enabled) { - alarmHandler.unsetAlarm(context, alarm) - alarmHandler.setAlarm(context, alarm) - } else { - alarmHandler.unsetAlarm(context, alarm) - } - - } } + // Retrieve saved alarms from persistent storage + alarmHandler.setNextAlarm(context) + } } } \ No newline at end of file