Skip to content

Commit

Permalink
Fixed delay bug + vibrate bug
Browse files Browse the repository at this point in the history
Fixed TimerHandler Delay getDelay()
Fixed vibrate not stopping sometimes
Revert back to ~1000ms on notification when app not visible
  • Loading branch information
josephnglynn committed Mar 11, 2024
1 parent b294b55 commit 7ea4ec1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ class MainActivity : FlutterActivity() {
}
}

override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
timerService?.apply {
mainActivityVisible = hasFocus
updateTimerNotificationRefreshRate()
}
}

override fun onDestroy() {
super.onDestroy()
applicationContext.unregisterReceiver(tickReceiver)
Expand Down
26 changes: 22 additions & 4 deletions android/app/src/main/kotlin/com/presley/flexify/TimerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class TimerService : Service() {
private var vibrator: Vibrator? = null
private val binder = LocalBinder()
private var currentDescription = ""
var mainActivityVisible = true
var flexifyTimer: FlexifyTimer = FlexifyTimer.emptyTimer()


Expand Down Expand Up @@ -62,6 +63,9 @@ class TimerService : Service() {
object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("TimerService", "Received add broadcast intent")
mediaPlayer?.stop()
vibrator?.cancel()

val timeStamp = intent?.getLongExtra("timeStamp", 0)
if (flexifyTimer.isExpired()) return startTimer(
FlexifyTimer.ONE_MINUTE_MILLI,
Expand All @@ -70,8 +74,7 @@ class TimerService : Service() {

flexifyTimer.increaseDuration(applicationContext, FlexifyTimer.ONE_MINUTE_MILLI)
updateNotification(flexifyTimer.getRemainingSeconds())
mediaPlayer?.stop()
vibrator?.cancel()

if (intent != null && intent.action == ADD_BROADCAST_INTERNAL) updateAppUI()
}
}
Expand Down Expand Up @@ -120,6 +123,20 @@ class TimerService : Service() {
updateAppUI()
}

fun updateTimerNotificationRefreshRate() {
timerRunnable?.let {
timerHandler.removeCallbacks(it)
timerHandler.postDelayed(it, getDelay(SystemClock.elapsedRealtime()))
}
}

private fun getDelay(startTime: Long): Long {
if (mainActivityVisible) return 20

val delay = flexifyTimer.getRemainingMillis() % 1000
return if (SystemClock.elapsedRealtime() - startTime + delay > 980) 20 else delay
}

private fun startTimer(msDuration: Long, timeStamp: Long) {
timerRunnable?.let { timerHandler.removeCallbacks(it) }

Expand All @@ -145,12 +162,13 @@ class TimerService : Service() {

timerRunnable = object : Runnable {
override fun run() {
val startTime = SystemClock.elapsedRealtime()
if (flexifyTimer.isExpired()) return
if (flexifyTimer.hasSecondsUpdated()) updateNotification(flexifyTimer.getRemainingSeconds())
timerHandler.postDelayed(this, flexifyTimer.getRemainingMillis() % 20)
timerHandler.postDelayed(this, getDelay(startTime))
}
}
timerHandler.postDelayed(timerRunnable!!, 20)
timerHandler.postDelayed(timerRunnable!!, getDelay(SystemClock.elapsedRealtime()))

if (timeStamp == 0.toLong()) updateAppUI()
}
Expand Down

0 comments on commit 7ea4ec1

Please sign in to comment.