Skip to content

Commit 629b638

Browse files
committed
created new custom CircularDurationView for PiP mode
Change-Id: Icaa919c6fb31025f40e02436132e15835113892c Signed-off-by: AbdAlMoniem AlHifnawy <hifnawy_moniem@hotmail.com>
1 parent 4b8ce31 commit 629b638

File tree

9 files changed

+648
-92
lines changed

9 files changed

+648
-92
lines changed

app/src/main/java/com/hifnawy/caffeinate/controller/KeepAwakeService.kt

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

app/src/main/java/com/hifnawy/caffeinate/controller/PictureInPictureActionsReceiver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class PictureInPictureActionsReceiver(
9898

9999
when (intent.action) {
100100
RESTART.name -> KeepAwakeService.restart(caffeinateApplication)
101-
NEXT_TIMEOUT.name -> KeepAwakeService.startNextTimeout(caffeinateApplication, debounce = false)
101+
NEXT_TIMEOUT.name -> KeepAwakeService.startNextTimeout(caffeinateApplication, wrapAround = true)
102102
TOGGLE.name -> when (caffeinateApplication.lastStatusUpdate) {
103103
is ServiceStatus.Stopped -> KeepAwakeService.startNextTimeout(caffeinateApplication)
104104
is ServiceStatus.Running -> KeepAwakeService.toggleState(caffeinateApplication, KeepAwakeServiceState.STATE_STOP)

app/src/main/java/com/hifnawy/caffeinate/utils/DurationExtensionFunctions.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.hifnawy.caffeinate.utils
33
import android.content.Context
44
import com.hifnawy.caffeinate.CaffeinateApplication
55
import com.hifnawy.caffeinate.R
6+
import java.util.Locale
67
import kotlin.time.Duration
78

89
/**
@@ -27,13 +28,13 @@ object DurationExtensionFunctions {
2728
isInfinite() -> ""
2829
else -> {
2930
val format = when {
30-
hideLegend || CaffeinateApplication.isRTL -> if (hours == 0L) "%02d:%02d" else "%02d:%02d:%02d"
31-
else -> if (hours == 0L) "%02dm %02ds" else "%02dh %02dm %02ds"
31+
hideLegend -> if (hours == 0L) "%02d:%02d" else "%02d:%02d:%02d"
32+
else -> if (hours == 0L) "%02dm %02ds" else "%02dh %02dm %02ds"
3233
}
3334

3435
when (hours) {
35-
0L -> String.format(CaffeinateApplication.applicationLocale, format, minutes, seconds)
36-
else -> String.format(CaffeinateApplication.applicationLocale, format, hours, minutes, seconds)
36+
0L -> String.format(Locale.ENGLISH, format, minutes, seconds)
37+
else -> String.format(Locale.ENGLISH, format, hours, minutes, seconds)
3738
}
3839
}
3940
}

0 commit comments

Comments
 (0)