@@ -887,25 +887,30 @@ class KeepAwakeService : Service(), SharedPrefsObserver, ServiceStatusObserver {
887887 *
888888 * @param status [ServiceStatus] The current status of the KeepAwakeService.
889889 * @param caffeinateApplication [CaffeinateApplication] The application context.
890+ * @param wrapAround [Boolean] Whether to wrap around to the first timeout if the last timeout is reached.
890891 *
891892 * @return [KeepAwakeServiceState] The next [KeepAwakeServiceState] to transition to.
892893 */
893- private fun debounce (status : ServiceStatus .Running , caffeinateApplication : CaffeinateApplication ) = caffeinateApplication.run {
894- fun nextTimeout (): KeepAwakeServiceState {
895- timeout = nextTimeout
896- return when (prevTimeout) {
897- lastTimeout -> STATE_STOP
898- else -> STATE_START_DELAYED
894+ private fun debounce (status : ServiceStatus .Running , caffeinateApplication : CaffeinateApplication , wrapAround : Boolean = false) =
895+ caffeinateApplication.run {
896+ fun nextTimeout (): KeepAwakeServiceState {
897+ timeout = nextTimeout
898+
899+ if (wrapAround) return STATE_START_DELAYED
900+ return when (prevTimeout) {
901+ lastTimeout -> STATE_STOP
902+ else -> STATE_START_DELAYED
903+ }
904+ }
905+
906+ val keepAwakeServiceState = when {
907+ wrapAround -> nextTimeout()
908+ status.isCountingDown -> STATE_STOP
909+ else -> nextTimeout()
910+ }
911+
912+ toggleState(this , keepAwakeServiceState)
899913 }
900- }
901-
902- val keepAwakeServiceState = when {
903- status.isCountingDown -> STATE_STOP
904- else -> nextTimeout()
905- }
906-
907- toggleState(this , keepAwakeServiceState)
908- }
909914
910915 /* *
911916 * Starts the KeepAwakeService with debouncing.
@@ -915,11 +920,12 @@ class KeepAwakeService : Service(), SharedPrefsObserver, ServiceStatusObserver {
915920 * before starting the new timeout.
916921 *
917922 * @param caffeinateApplication [CaffeinateApplication] The application context.
923+ * @param wrapAround [Boolean] Whether to wrap around to the first timeout if the last timeout is reached.
918924 */
919- private fun startWithDebounce (caffeinateApplication : CaffeinateApplication ) = caffeinateApplication.run {
925+ private fun startWithDebounce (caffeinateApplication : CaffeinateApplication , wrapAround : Boolean = false ) = caffeinateApplication.run {
920926 when (val status = lastStatusUpdate) {
921927 is ServiceStatus .Stopped -> toggleState(this , STATE_START_DELAYED )
922- is ServiceStatus .Running -> debounce(status, this )
928+ is ServiceStatus .Running -> debounce(status, this , wrapAround )
923929 }
924930 }
925931
@@ -966,14 +972,16 @@ class KeepAwakeService : Service(), SharedPrefsObserver, ServiceStatusObserver {
966972 * @param caffeinateApplication [CaffeinateApplication] The application context.
967973 * @param debounce [Boolean] If `true`, the service will debounce the next timeout by waiting for [DEBOUNCE_DURATION]
968974 * before starting the new timeout. If `false`, the service will start the new timeout immediately.
975+ * @param wrapAround [Boolean] If `true`, the service will wrap around to the first timeout if the last timeout is reached.
969976 */
970- fun startNextTimeout (caffeinateApplication : CaffeinateApplication , debounce : Boolean = true) = caffeinateApplication.run {
971- when {
972- timeoutCheckBoxes.size == 1 -> startSingleTimeout(this )
973- debounce -> startWithDebounce(this )
974- else -> startWithoutDebounce(this )
975- }
976- }
977+ fun startNextTimeout (caffeinateApplication : CaffeinateApplication , debounce : Boolean = true, wrapAround : Boolean = false) =
978+ caffeinateApplication.run {
979+ when {
980+ timeoutCheckBoxes.size == 1 -> startSingleTimeout(this )
981+ debounce -> startWithDebounce(this , wrapAround)
982+ else -> startWithoutDebounce(this )
983+ }
984+ }
977985
978986 /* *
979987 * Starts the KeepAwakeService indefinitely.
@@ -1030,6 +1038,7 @@ class KeepAwakeService : Service(), SharedPrefsObserver, ServiceStatusObserver {
10301038 startTimeout : Duration ? = null
10311039 ): Unit = caffeinateApplication.run {
10321040 Log .d(" newState: $newKeepAwakeServiceState " )
1041+
10331042 val start = when (newKeepAwakeServiceState) {
10341043 STATE_START -> true
10351044 STATE_START_DELAYED -> true
@@ -1043,7 +1052,7 @@ class KeepAwakeService : Service(), SharedPrefsObserver, ServiceStatusObserver {
10431052 }
10441053 }
10451054
1046- startTimeout?.run { timeout = this }
1055+ startTimeout?.let { timeout = it }
10471056
10481057 when {
10491058 Build .VERSION .SDK_INT >= Build .VERSION_CODES .O -> localizedApplicationContext.startForegroundService(intent)
0 commit comments