@@ -13,7 +13,6 @@ import android.os.Handler
13
13
import android.os.Looper
14
14
import android.os.PowerManager
15
15
import android.text.SpannableString
16
- import android.text.format.DateFormat
17
16
import android.text.style.RelativeSizeSpan
18
17
import android.widget.Toast
19
18
import androidx.core.app.AlarmManagerCompat
@@ -36,7 +35,8 @@ import org.fossify.commons.helpers.*
36
35
import java.text.SimpleDateFormat
37
36
import java.util.Calendar
38
37
import java.util.Locale
39
- import kotlin.math.pow
38
+ import kotlin.math.ceil
39
+ import kotlin.time.Duration.Companion.milliseconds
40
40
import kotlin.time.Duration.Companion.minutes
41
41
42
42
val Context .config: Config get() = Config .newInstance(applicationContext)
@@ -100,59 +100,44 @@ fun Context.createNewTimer(): Timer {
100
100
}
101
101
102
102
fun Context.scheduleNextAlarm (alarm : Alarm , showToast : Boolean ) {
103
- val calendar = Calendar .getInstance()
104
- calendar.firstDayOfWeek = Calendar .MONDAY
105
- val currentTimeInMinutes = getCurrentDayMinutes()
103
+ val triggerTimeMillis = getTimeOfNextAlarm(alarm)?.timeInMillis ? : return
104
+ setupAlarmClock(alarm = alarm, triggerTimeMillis = triggerTimeMillis)
106
105
107
- if (alarm.days == TODAY_BIT ) {
108
- val triggerInMinutes = alarm.timeInMinutes - currentTimeInMinutes
109
- setupAlarmClock(alarm, triggerInMinutes * 60 - calendar.get(Calendar .SECOND ))
110
-
111
- if (showToast) {
112
- showRemainingTimeMessage(triggerInMinutes)
113
- }
114
- } else if (alarm.days == TOMORROW_BIT ) {
115
- val triggerInMinutes = alarm.timeInMinutes - currentTimeInMinutes + DAY_MINUTES
116
- setupAlarmClock(alarm, triggerInMinutes * 60 - calendar.get(Calendar .SECOND ))
106
+ if (showToast) {
107
+ val now = Calendar .getInstance()
108
+ val triggerInMillis = triggerTimeMillis - now.timeInMillis
109
+ showRemainingTimeMessage(triggerInMillis)
110
+ }
111
+ }
117
112
118
- if (showToast) {
119
- showRemainingTimeMessage(triggerInMinutes)
120
- }
113
+ fun Context.showRemainingTimeMessage (triggerInMillis : Long ) {
114
+ val totalSeconds = triggerInMillis.milliseconds.inWholeSeconds.toInt()
115
+ val remainingTime = if (totalSeconds >= MINUTE_SECONDS ) {
116
+ val roundedMinutes = ceil(totalSeconds / MINUTE_SECONDS .toFloat()).toInt()
117
+ formatMinutesToTimeString(roundedMinutes)
121
118
} else {
122
- for (i in 0 .. 7 ) {
123
- val currentDay = (calendar.get(Calendar .DAY_OF_WEEK ) + 5 ) % 7
124
- val isCorrectDay = alarm.days and 2.0 .pow(currentDay).toInt() != 0
125
- if (isCorrectDay && (alarm.timeInMinutes > currentTimeInMinutes || i > 0 )) {
126
- val triggerInMinutes = alarm.timeInMinutes - currentTimeInMinutes + (i * DAY_MINUTES )
127
- setupAlarmClock(alarm, triggerInMinutes * 60 - calendar.get(Calendar .SECOND ))
128
-
129
- if (showToast) {
130
- showRemainingTimeMessage(triggerInMinutes)
131
- }
132
- break
133
- } else {
134
- calendar.add(Calendar .DAY_OF_MONTH , 1 )
135
- }
136
- }
119
+ formatSecondsToTimeString(totalSeconds)
137
120
}
138
- }
139
121
140
- fun Context.showRemainingTimeMessage (totalMinutes : Int ) {
141
- val fullString = String .format(getString(org.fossify.commons.R .string.time_remaining), formatMinutesToTimeString(totalMinutes))
142
- toast(fullString, Toast .LENGTH_LONG )
122
+ toast(
123
+ msg = String .format(
124
+ getString(org.fossify.commons.R .string.time_remaining), remainingTime
125
+ ),
126
+ length = Toast .LENGTH_LONG
127
+ )
143
128
}
144
129
145
- fun Context.setupAlarmClock (alarm : Alarm , triggerInSeconds : Int ) {
130
+ fun Context.setupAlarmClock (alarm : Alarm , triggerTimeMillis : Long ) {
146
131
val alarmManager = getSystemService(Context .ALARM_SERVICE ) as AlarmManager
147
- val targetMS = System .currentTimeMillis() + triggerInSeconds * 1000
132
+
148
133
try {
149
- AlarmManagerCompat .setAlarmClock(alarmManager, targetMS , getOpenAlarmTabIntent(), getAlarmIntent(alarm))
134
+ AlarmManagerCompat .setAlarmClock(alarmManager, triggerTimeMillis , getOpenAlarmTabIntent(), getAlarmIntent(alarm))
150
135
151
136
// show a notification to allow dismissing the alarm 10 minutes before it actually triggers
152
- val dismissalTriggerTime = if (targetMS - System .currentTimeMillis() < 10 .minutes.inWholeMilliseconds) {
137
+ val dismissalTriggerTime = if (triggerTimeMillis - System .currentTimeMillis() < 10 .minutes.inWholeMilliseconds) {
153
138
System .currentTimeMillis() + 500
154
139
} else {
155
- targetMS - 10 .minutes.inWholeMilliseconds
140
+ triggerTimeMillis - 10 .minutes.inWholeMilliseconds
156
141
}
157
142
AlarmManagerCompat .setExactAndAllowWhileIdle(alarmManager, 0 , dismissalTriggerTime, getEarlyAlarmDismissalIntent(alarm))
158
143
} catch (e: Exception ) {
@@ -277,35 +262,26 @@ fun Context.getClosestEnabledAlarmString(callback: (result: String) -> Unit) {
277
262
return @getEnabledAlarms
278
263
}
279
264
265
+ val now = Calendar .getInstance()
280
266
val nextAlarmList = enabledAlarms
281
- .mapNotNull { getTimeUntilNextAlarm(it.timeInMinutes, it.days) }
267
+ .mapNotNull(::getTimeOfNextAlarm)
268
+ .filter { it > now }
282
269
283
- if (nextAlarmList.isEmpty()) {
284
- callback(" " )
285
- }
286
-
287
- var closestAlarmTime = Int .MAX_VALUE
288
- nextAlarmList.forEach { time ->
289
- if (time < closestAlarmTime) {
290
- closestAlarmTime = time
291
- }
292
- }
293
-
294
- if (closestAlarmTime == Int .MAX_VALUE ) {
270
+ val closestAlarmTime = nextAlarmList.minOrNull()
271
+ if (closestAlarmTime == null ) {
295
272
callback(" " )
273
+ return @getEnabledAlarms
296
274
}
297
275
298
- val calendar = Calendar .getInstance().apply { firstDayOfWeek = Calendar .MONDAY }
299
- calendar.add(Calendar .MINUTE , closestAlarmTime)
300
- val dayOfWeekIndex = (calendar.get(Calendar .DAY_OF_WEEK ) + 5 ) % 7
276
+ val dayOfWeekIndex = (closestAlarmTime.get(Calendar .DAY_OF_WEEK ) + 5 ) % 7
301
277
val dayOfWeek = resources.getStringArray(org.fossify.commons.R .array.week_days_short)[dayOfWeekIndex]
302
278
val pattern = if (config.use24HourFormat) {
303
279
FORMAT_24H
304
280
} else {
305
281
FORMAT_12H
306
282
}
307
283
308
- val formattedTime = SimpleDateFormat (pattern, Locale .getDefault()).format(calendar .time)
284
+ val formattedTime = SimpleDateFormat (pattern, Locale .getDefault()).format(closestAlarmTime .time)
309
285
callback(" $dayOfWeek $formattedTime " )
310
286
}
311
287
}
@@ -359,7 +335,7 @@ fun Context.getTimerNotification(timer: Timer, pendingIntent: PendingIntent, add
359
335
if (isOreoPlus()) {
360
336
try {
361
337
notificationManager.deleteNotificationChannel(channelId)
362
- } catch (e : Exception ) {
338
+ } catch (_ : Exception ) {
363
339
}
364
340
365
341
val audioAttributes = AudioAttributes .Builder ()
@@ -385,10 +361,8 @@ fun Context.getTimerNotification(timer: Timer, pendingIntent: PendingIntent, add
385
361
}
386
362
}
387
363
388
- val title = if ( timer.label.isEmpty()) {
364
+ val title = timer.label.ifEmpty {
389
365
getString(R .string.timer)
390
- } else {
391
- timer.label
392
366
}
393
367
394
368
val reminderActivityIntent = getReminderActivityIntent()
0 commit comments