Skip to content

Commit 903984c

Browse files
authored
Alarm Reliability Improvements and Debug Screen Implementation (#769)
* you * final changes * changes * moved debug screen to app drawer * hello * improved debug screen
1 parent a45d501 commit 903984c

File tree

11 files changed

+711
-92
lines changed

11 files changed

+711
-92
lines changed

android/app/src/main/kotlin/com/ccextractor/ultimate_alarm_clock/ultimate_alarm_clock/AlarmReceiver.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class AlarmReceiver : BroadcastReceiver() {
1111
return
1212
}
1313

14-
1514

1615

16+
val logdbHelper = LogDatabaseHelper(context);
1717
val flutterIntent = Intent(context, MainActivity::class.java).apply {
1818
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
1919

@@ -38,4 +38,3 @@ class AlarmReceiver : BroadcastReceiver() {
3838
}
3939

4040
}
41-

android/app/src/main/kotlin/com/ccextractor/ultimate_alarm_clock/ultimate_alarm_clock/BootReceiver.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ class BootReceiver : BroadcastReceiver() {
2424
val profile = sharedPreferences.getString("flutter.profile", "Default")
2525

2626
val dbHelper = DatabaseHelper(context)
27+
val logdbHelper = LogDatabaseHelper(context)
2728
val db = dbHelper.readableDatabase
28-
val ringTime = getLatestAlarm(db, true, profile?:"Default")
29+
val ringTime = getLatestAlarm(db, true, profile?:"Default", context)
2930
db.close()
3031
if (ringTime != null) {
3132
scheduleAlarm(ringTime["interval"]!! as Long, context, ringTime["isActivity"]!!)
@@ -63,6 +64,7 @@ class BootReceiver : BroadcastReceiver() {
6364
}
6465
}
6566

67+
@SuppressLint("ScheduleExactAlarm")
6668
fun scheduleAlarm(milliSeconds: Long, context: Context, activityMonitor: Any) {
6769
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
6870
val intent = Intent(context, AlarmReceiver::class.java)
@@ -82,14 +84,15 @@ class BootReceiver : BroadcastReceiver() {
8284
// Schedule the alarm
8385
val tenMinutesInMilliseconds = 600000L
8486
val preTriggerTime =
85-
SystemClock.elapsedRealtime() + (milliSeconds - tenMinutesInMilliseconds)
87+
System.currentTimeMillis() + (milliSeconds - tenMinutesInMilliseconds)
8688

8789
// Schedule the alarm
88-
val triggerTime = SystemClock.elapsedRealtime() + milliSeconds
90+
val triggerTime = System.currentTimeMillis() + milliSeconds
91+
8992
if (activityMonitor == 1) {
90-
alarmManager.setExact(
91-
AlarmManager.ELAPSED_REALTIME_WAKEUP,
92-
preTriggerTime,
93+
val alarmClockInfo = AlarmManager.AlarmClockInfo(preTriggerTime, pendingIntent)
94+
alarmManager.setAlarmClock(
95+
alarmClockInfo,
9396
pendingActivityCheckIntent
9497
)
9598
} else {
@@ -101,8 +104,8 @@ class BootReceiver : BroadcastReceiver() {
101104
editor.putLong("flutter.is_screen_on", 0L)
102105
editor.apply()
103106
}
104-
105-
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerTime, pendingIntent)
107+
val clockInfo = AlarmManager.AlarmClockInfo(triggerTime, pendingIntent)
108+
alarmManager.setAlarmClock(clockInfo, pendingIntent)
106109

107110
}
108111

@@ -153,6 +156,4 @@ class BootReceiver : BroadcastReceiver() {
153156
String.format("%02d:%02d", minutes, seconds)
154157
}
155158
}
156-
157-
158-
}
159+
}

android/app/src/main/kotlin/com/ccextractor/ultimate_alarm_clock/ultimate_alarm_clock/GetLatestAlarm.kt

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import java.util.*
1212
import java.util.concurrent.TimeUnit
1313

1414

15-
fun getLatestAlarm(db: SQLiteDatabase, wantNextAlarm: Boolean, profile: String): Map<String, *>? {
15+
fun getLatestAlarm(db: SQLiteDatabase, wantNextAlarm: Boolean, profile: String,context: Context): Map<String, *>? {
1616
val now = Calendar.getInstance()
1717
var nowInMinutes = now.get(Calendar.HOUR_OF_DAY) * 60 + now.get(Calendar.MINUTE)
1818
var nowInSeconds = nowInMinutes * 60 + now.get(Calendar.SECOND)
@@ -22,8 +22,10 @@ fun getLatestAlarm(db: SQLiteDatabase, wantNextAlarm: Boolean, profile: String):
2222
}
2323
val currentDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1
2424
val currentTime = SimpleDateFormat("HH:mm", Locale.getDefault()).format(Date())
25-
Log.d("d","cd ${currentDay}")
25+
Log.d("d", "cd ${currentDay}")
2626

27+
// Initialize DatabaseHelper
28+
val logdbHelper = LogDatabaseHelper(context)
2729

2830
val cursor = db.rawQuery(
2931
"""
@@ -32,77 +34,75 @@ fun getLatestAlarm(db: SQLiteDatabase, wantNextAlarm: Boolean, profile: String):
3234
AND (profile = ? OR ringOn = 1)
3335
""", arrayOf(profile)
3436
)
35-
var selectedAlarm = null;
37+
var selectedAlarm = null
3638
Log.d("Alarm", cursor.count.toString())
3739

38-
return if (cursor.count>0) {
40+
return if (cursor.count > 0) {
3941
// Parse the cursor into an AlarmModel object
4042
cursor.moveToFirst()
4143
var alarm = AlarmModel.fromCursor(cursor)
4244
var intervaltoAlarm = Long.MAX_VALUE
43-
var setAlarm: AlarmModel? =null
44-
do {
45-
alarm = AlarmModel.fromCursor(cursor)
46-
if (alarm.ringOn == 0) {
45+
var setAlarm: AlarmModel? = null
46+
do {
47+
alarm = AlarmModel.fromCursor(cursor)
48+
if (alarm.ringOn == 0) {
4749

48-
var dayfromToday = 0
49-
var timeDif = getTimeDifferenceInMillis(alarm.alarmTime)
50-
Log.d("d","timeDiff ${timeDif}")
50+
var dayfromToday = 0
51+
var timeDif = getTimeDifferenceInMillis(alarm.alarmTime)
52+
Log.d("d", "timeDiff ${timeDif}")
53+
54+
if ((alarm.days[currentDay] == '1' || alarm.days == "0000000") && timeDif > -1L) {
55+
if (timeDif < intervaltoAlarm) {
56+
intervaltoAlarm = timeDif
57+
setAlarm = alarm
58+
}
59+
} else {
60+
dayfromToday = getDaysUntilNextAlarm(alarm.days, currentDay)
61+
if (dayfromToday == 0) {
62+
63+
if (alarm.days == "0000000") {
5164

52-
if ((alarm.days[currentDay] == '1' || alarm.days=="0000000") && timeDif > -1L) {
53-
if (timeDif < intervaltoAlarm) {
54-
intervaltoAlarm = timeDif
55-
setAlarm = alarm
56-
}
57-
} else {
58-
dayfromToday = getDaysUntilNextAlarm(alarm.days, currentDay)
59-
if (dayfromToday == 0) {
60-
61-
if(alarm.days=="0000000")
62-
{
63-
64-
var timeDif =
65-
getTimeDifferenceFromMidnight(alarm.alarmTime) + getMillisecondsUntilMidnight()
66-
if (timeDif < intervaltoAlarm && timeDif > -1L ) {
67-
intervaltoAlarm = timeDif
68-
setAlarm = alarm
69-
}
70-
}
71-
else{
72-
73-
var timeDif =
74-
getTimeDifferenceFromMidnight(alarm.alarmTime) + getMillisecondsUntilMidnight() + 86400000 * 6
75-
if (timeDif < intervaltoAlarm && timeDif > -1L ) {
76-
intervaltoAlarm = timeDif
77-
setAlarm = alarm
78-
}
79-
}
80-
} else if (dayfromToday == 1) {
8165
var timeDif =
8266
getTimeDifferenceFromMidnight(alarm.alarmTime) + getMillisecondsUntilMidnight()
83-
Log.d("d","timeDiff ${timeDif}")
84-
8567
if (timeDif < intervaltoAlarm && timeDif > -1L) {
8668
intervaltoAlarm = timeDif
8769
setAlarm = alarm
8870
}
8971
} else {
72+
9073
var timeDif =
91-
getTimeDifferenceFromMidnight(alarm.alarmTime) + getMillisecondsUntilMidnight() + 86400000 * (dayfromToday - 1)
74+
getTimeDifferenceFromMidnight(alarm.alarmTime) + getMillisecondsUntilMidnight() + 86400000 * 6
9275
if (timeDif < intervaltoAlarm && timeDif > -1L) {
9376
intervaltoAlarm = timeDif
9477
setAlarm = alarm
9578
}
9679
}
80+
} else if (dayfromToday == 1) {
81+
var timeDif =
82+
getTimeDifferenceFromMidnight(alarm.alarmTime) + getMillisecondsUntilMidnight()
83+
Log.d("d", "timeDiff ${timeDif}")
9784

85+
if (timeDif < intervaltoAlarm && timeDif > -1L) {
86+
intervaltoAlarm = timeDif
87+
setAlarm = alarm
88+
}
89+
} else {
90+
var timeDif =
91+
getTimeDifferenceFromMidnight(alarm.alarmTime) + getMillisecondsUntilMidnight() + 86400000 * (dayfromToday - 1)
92+
if (timeDif < intervaltoAlarm && timeDif > -1L) {
93+
intervaltoAlarm = timeDif
94+
setAlarm = alarm
95+
}
9896
}
99-
} else {
100-
val dayfromToday = getDaysFromCurrentDate(alarm.alarmDate)
101-
if (dayfromToday == 0L) {
102-
var timeDif = getTimeDifferenceInMillis(alarm.alarmTime)
103-
if (alarm.days[currentDay] == '1' && timeDif > -1L) {
104-
if (timeDif < intervaltoAlarm) {
105-
intervaltoAlarm = timeDif
97+
98+
}
99+
} else {
100+
val dayfromToday = getDaysFromCurrentDate(alarm.alarmDate)
101+
if (dayfromToday == 0L) {
102+
var timeDif = getTimeDifferenceInMillis(alarm.alarmTime)
103+
if (alarm.days[currentDay] == '1' && timeDif > -1L) {
104+
if (timeDif < intervaltoAlarm) {
105+
intervaltoAlarm = timeDif
106106
setAlarm = alarm
107107
}
108108
}
@@ -127,18 +127,27 @@ fun getLatestAlarm(db: SQLiteDatabase, wantNextAlarm: Boolean, profile: String):
127127

128128
} while (cursor.moveToNext())
129129
cursor.close()
130+
130131
if (setAlarm != null) {
131132
Log.d("Alarm", intervaltoAlarm.toString())
132-
val a = mapOf(
133+
134+
// Add the latest alarm details to the LOG table
135+
val logDetails = """
136+
Alarm Scheduled for ${setAlarm.alarmTime}
137+
""".trimIndent()
138+
logdbHelper.insertLog(logDetails)
139+
140+
// Return the latest alarm details
141+
val a = mapOf(
133142
"interval" to intervaltoAlarm,
134143
"isActivity" to setAlarm.activityMonitor,
135144
"isLocation" to setAlarm.isLocationEnabled,
136145
"location" to setAlarm.location,
137146
"isWeather" to setAlarm.isWeatherEnabled,
138147
"weatherTypes" to setAlarm.weatherTypes,
139-
"alarmID" to setAlarm.alarmId
148+
"alarmID" to setAlarm.alarmId
140149
)
141-
Log.d("s","sdsd ${a}")
150+
Log.d("s", "sdsd ${a}")
142151
return a
143152
}
144153
null
@@ -147,7 +156,6 @@ fun getLatestAlarm(db: SQLiteDatabase, wantNextAlarm: Boolean, profile: String):
147156
}
148157
}
149158

150-
151159
fun getTimeDifferenceInMillis(timeString: String): Long {
152160
// Define the time format
153161
val timeFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
@@ -321,4 +329,4 @@ data class AlarmModel(
321329
)
322330
}
323331
}
324-
}
332+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.ccextractor.ultimate_alarm_clock
2+
import android.content.ContentValues
3+
import android.content.Context
4+
import android.database.Cursor
5+
import android.database.sqlite.SQLiteDatabase
6+
import android.database.sqlite.SQLiteOpenHelper
7+
8+
class LogDatabaseHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
9+
10+
companion object {
11+
private const val DATABASE_NAME = "AlarmLogs.db"
12+
private const val DATABASE_VERSION = 1
13+
private const val TABLE_NAME = "LOG"
14+
private const val COLUMN_LOG_ID = "LogID"
15+
private const val COLUMN_LOG_TIME = "LogTime"
16+
private const val COLUMN_STATUS = "Status"
17+
}
18+
19+
override fun onCreate(db: SQLiteDatabase?) {
20+
// Create the LOG table
21+
val createTableQuery = """
22+
CREATE TABLE $TABLE_NAME (
23+
$COLUMN_LOG_ID INTEGER PRIMARY KEY AUTOINCREMENT,
24+
$COLUMN_LOG_TIME DATETIME NOT NULL,
25+
$COLUMN_STATUS TEXT NOT NULL
26+
)
27+
""".trimIndent()
28+
db?.execSQL(createTableQuery)
29+
}
30+
31+
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
32+
// Drop the table if it exists and recreate it
33+
db?.execSQL("DROP TABLE IF EXISTS $TABLE_NAME")
34+
onCreate(db)
35+
}
36+
37+
// Insert a log entry
38+
fun insertLog(status: String): Long {
39+
val db = writableDatabase
40+
val values = ContentValues().apply {
41+
put(COLUMN_LOG_TIME, System.currentTimeMillis()) // Store current time as milliseconds
42+
put(COLUMN_STATUS, status)
43+
}
44+
return db.insert(TABLE_NAME, null, values)
45+
}
46+
47+
// Fetch all log entries
48+
fun getLogs(): Cursor {
49+
val db = readableDatabase
50+
return db.query(TABLE_NAME, null, null, null, null, null, null)
51+
}
52+
53+
// Update a log entry
54+
fun updateLog(logId: Int, newStatus: String): Int {
55+
val db = writableDatabase
56+
val values = ContentValues().apply {
57+
put(COLUMN_STATUS, newStatus)
58+
}
59+
return db.update(TABLE_NAME, values, "$COLUMN_LOG_ID = ?", arrayOf(logId.toString()))
60+
}
61+
62+
// Delete a log entry
63+
fun deleteLog(logId: Int): Int {
64+
val db = writableDatabase
65+
return db.delete(TABLE_NAME, "$COLUMN_LOG_ID = ?", arrayOf(logId.toString()))
66+
}
67+
}

0 commit comments

Comments
 (0)