Skip to content

Commit

Permalink
Ensure timer thread is quit onDestroy of service
Browse files Browse the repository at this point in the history
  • Loading branch information
arburk committed Jun 24, 2023
1 parent 243905d commit ed1fdf1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,23 @@ class RoundSettingsListViewAdapter(
updateView()
}

private fun getIncreaseStep(it: Int): Int {
return if (it < 50) {
1
} else if (it < 100) {
5
} else if (it < 500) {
10
} else if (it < 1000) {
50
} else 100
}

private fun decreaseBlind(currentBlind: LiveData<Blind>) {
currentBlind.value!!.small.also {
timerService.updateBlind(it, it - getIncreaseStep(it))
}
updateView()
}

private fun getIncreaseStep(it: Int): Int {
return when {
it < 50 -> 1
it < 100 -> 5
it < 500 -> 10
it < 1000 -> 50
else -> 100
}
}

private fun formattedNumberOfCurrentRound(position: Int) =
(position + 1).toString().padStart(calculatePadLength(), '0')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.arburk.vscp.app.model

data class ConfigModel(

var minPerRound: Int,
var minPerWarning: Int,
var rounds: Array<Blind>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.github.arburk.vscp.app.service

import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Binder
import android.os.HandlerThread
import android.os.IBinder
import android.os.Process.THREAD_PRIORITY_FOREGROUND
import android.util.Log
import androidx.preference.PreferenceManager
import com.github.arburk.vscp.app.activity.PokerTimerViewModel
import com.github.arburk.vscp.app.model.Blind
import com.github.arburk.vscp.app.model.ConfigModel
Expand All @@ -31,6 +31,7 @@ class TimerService : Service(), SharedPreferences.OnSharedPreferenceChangeListen
private var timerTask: TimerTask? = null

private var viewModels: List<PokerTimerViewModel> = arrayListOf()
private val timerServiceThread: HandlerThread = HandlerThread(TimerService::class.simpleName, THREAD_PRIORITY_FOREGROUND)

inner class TimerServiceBinder : Binder() {
fun getService(): TimerService = this@TimerService
Expand All @@ -52,13 +53,25 @@ class TimerService : Service(), SharedPreferences.OnSharedPreferenceChangeListen
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
initConfig()
timerServiceThread.start()
}

override fun onDestroy() {
super.onDestroy()
timerServiceThread.quit()
}

private fun initConfig() {
val sharedPreferences = this.getSharedPreferences(this.packageName + "_preferences", Context.MODE_PRIVATE)
sharedPreferences.registerOnSharedPreferenceChangeListener(this)
val minPerRound = sharedPreferences.getString(pref_key_min_per_round, "12")!!.toInt()
val minutePerWarning = sharedPreferences.getString(pref_key_min_per_warning, "1")!!.toInt()

initConfig(sharedPreferences)
HandlerThread("TimerService", THREAD_PRIORITY_FOREGROUND).apply {
start()
}
// TODO: init vscpConfig from saved state if available
config = ConfigModel(minPerRound, minutePerWarning, readBlindConfigFromDevice())
resetTimer()
Log.v("TimerService", "initConfig conducted $config")
}

fun getCurrentBlind(): Blind = config.rounds[currentRound]
Expand Down Expand Up @@ -154,16 +167,6 @@ class TimerService : Service(), SharedPreferences.OnSharedPreferenceChangeListen
Log.v("TimerService", "onSharedPreferenceChanged changed config $config")
}

private fun initConfig(sharedPreferences: SharedPreferences) {
val minPerRound = sharedPreferences.getString(pref_key_min_per_round, "12")!!.toInt()
val minutePerWarning = sharedPreferences.getString(pref_key_min_per_warning, "1")!!.toInt()

// TODO: init vscpConfig from saved state if available
config = ConfigModel(minPerRound, minutePerWarning, readBlindConfigFromDevice())
resetTimer()
Log.v("TimerService", "initConfig conducted $config")
}

/**
* TODO: read it from file before if available, return default if not found
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.arburk.vscp.app.model

import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class ConfigModelTest {

@Test
fun baseFunctionTest() {
val testee = ConfigModel(5, 2, arrayOf(Blind(2), Blind(9), Blind(401)))
val testee2 = ConfigModel(5, 2, arrayOf(Blind(2), Blind(9), Blind(401)))
assertEquals(
"ConfigModel(minPerRound=5, minPerWarning=2, rounds=[Blind(small=2), Blind(small=9), Blind(small=401)])",
testee.toString()
)
assertEquals(37260, testee.hashCode())
assertTrue(testee.equals(testee))
assertTrue(testee.equals(testee2))
assertFalse(testee.equals(ConfigModel(4, 2, arrayOf(Blind(2), Blind(9), Blind(401)))))
assertFalse(testee.equals(ConfigModel(5, 1, arrayOf(Blind(2), Blind(9), Blind(401)))))
assertFalse(testee.equals(ConfigModel(5, 2, arrayOf(Blind(1), Blind(9), Blind(401)))))
assertFalse(testee.equals(ConfigModel(5, 2, arrayOf(Blind(401)))))
}
}

0 comments on commit ed1fdf1

Please sign in to comment.