Skip to content

Commit

Permalink
Improve storing SNOOZED VpnServiceState
Browse files Browse the repository at this point in the history
  • Loading branch information
karlenDimla committed Nov 21, 2023
1 parent 2e590dd commit 9cbe5e2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class TrackerBlockingVpnService : VpnService(), CoroutineScope by MainScope(), V
val snoozeTriggerAtMillisExtra = intent.getLongExtra(ACTION_SNOOZE_VPN_EXTRA, 0L)
stopVpn(
VpnStopReason.SELF_STOP,
snoozed = snoozeTriggerAtMillisExtra != 0L,
snoozedDurationInMillis = snoozeTriggerAtMillisExtra,
)
}.await()
}
Expand Down Expand Up @@ -526,7 +526,7 @@ class TrackerBlockingVpnService : VpnService(), CoroutineScope by MainScope(), V
private suspend fun stopVpn(
reason: VpnStopReason,
hasVpnAlreadyStarted: Boolean = true,
snoozed: Boolean = false,
snoozedDurationInMillis: Long = 0L,
) = withContext(serviceDispatcher) {
logcat { "VPN log: Stopping VPN. $reason" }

Expand Down Expand Up @@ -554,7 +554,7 @@ class TrackerBlockingVpnService : VpnService(), CoroutineScope by MainScope(), V
// Set the state to DISABLED here, then call the on stop/failure callbacks
vpnServiceStateStatsDao.insert(
createVpnState(
state = if (snoozed) VpnServiceState.SNOOZED else VpnServiceState.DISABLED,
state = if (snoozedDurationInMillis != 0L) VpnServiceState.SNOOZED(snoozedDurationInMillis) else VpnServiceState.DISABLED,
stopReason = reason,
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import com.duckduckgo.mobile.android.vpn.state.VpnStateMonitor.VpnRunningState
import com.duckduckgo.mobile.android.vpn.state.VpnStateMonitor.VpnState
import com.duckduckgo.mobile.android.vpn.state.VpnStateMonitor.VpnStopReason
import com.squareup.anvil.annotations.ContributesBinding
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlinx.coroutines.flow.*
import logcat.logcat
Expand Down Expand Up @@ -111,7 +110,7 @@ class RealVpnStateMonitor @Inject constructor(
ENABLING -> VpnRunningState.ENABLING
ENABLED -> VpnRunningState.ENABLED
DISABLED -> VpnRunningState.DISABLED()
SNOOZED -> VpnRunningState.DISABLED(TimeUnit.MINUTES.toMillis(20)) // TODO - remove hardcoded time
is SNOOZED -> VpnRunningState.DISABLED((lastState.state as SNOOZED).snoozeLengthInMillis)
null, INVALID -> VpnRunningState.INVALID
}
val alwaysOnState = when (lastState?.alwaysOnState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class VpnStateMonitorService : Service() {
// check last state, if it was enabled then we store disabled state reason unknown
private fun maybeUpdateVPNState() {
val lastStateStats = vpnDatabase.vpnServiceStateDao().getLastStateStats()
if (lastStateStats?.state != DISABLED && lastStateStats?.state != SNOOZED) {
if (lastStateStats?.state != DISABLED && lastStateStats?.state !is SNOOZED) {
logcat { "VpnStateMonitorService destroyed but VPN state stored as ${lastStateStats?.state}, inserting DISABLED" }
vpnDatabase.vpnServiceStateDao().insert(VpnServiceStateStats(state = DISABLED))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,31 @@ data class VpnTracker(
val count: Int = 1,
)

enum class VpnServiceState {
ENABLING,
ENABLED,
DISABLED,
SNOOZED,
INVALID,
sealed class VpnServiceState(val name: String) {
data object ENABLING : VpnServiceState("ENABLING")
data object ENABLED : VpnServiceState("ENABLED")
data object DISABLED : VpnServiceState("DISABLED")
data class SNOOZED(val snoozeLengthInMillis: Long) : VpnServiceState("SNOOZED:$snoozeLengthInMillis")
data object INVALID : VpnServiceState("INVALID")

companion object {
fun valueOf(state: String): VpnServiceState {
val stateSplit = state.split(":")
val actualState = stateSplit[0]
val stateExtra = if (stateSplit.size == 2) stateSplit[1] else null

return if (stateExtra != null) {
SNOOZED(stateExtra.toLong())
} else {
when (actualState) {
ENABLING.name -> ENABLING
ENABLED.name -> ENABLED
DISABLED.name -> DISABLED
else -> INVALID
}
}
}
}
}

enum class VpnStoppingReason {
Expand Down

0 comments on commit 9cbe5e2

Please sign in to comment.