@@ -3,6 +3,7 @@ package com.hegocre.nextcloudpasswords.api
33import android.content.Context
44import android.util.Log
55import androidx.lifecycle.MutableLiveData
6+ import androidx.work.WorkManager
67import com.hegocre.nextcloudpasswords.api.encryption.CSEv1Keychain
78import com.hegocre.nextcloudpasswords.api.exceptions.PWDv1ChallengeMasterKeyInvalidException
89import com.hegocre.nextcloudpasswords.api.exceptions.PWDv1ChallengeMasterKeyNeededException
@@ -15,6 +16,7 @@ import com.hegocre.nextcloudpasswords.data.password.NewPassword
1516import com.hegocre.nextcloudpasswords.data.password.Password
1617import com.hegocre.nextcloudpasswords.data.password.UpdatedPassword
1718import com.hegocre.nextcloudpasswords.data.user.UserController
19+ import com.hegocre.nextcloudpasswords.services.keepalive.KeepAliveWorker
1820import com.hegocre.nextcloudpasswords.utils.Error
1921import com.hegocre.nextcloudpasswords.utils.OkHttpRequest
2022import com.hegocre.nextcloudpasswords.utils.PreferencesManager
@@ -57,6 +59,8 @@ class ApiController private constructor(context: Context) {
5759 val sessionOpen: StateFlow <Boolean >
5860 get() = _sessionOpen .asStateFlow()
5961
62+ private val workManager = WorkManager .getInstance(context)
63+
6064 init {
6165 decryptCSEv1Keychain(
6266 preferencesManager.getCSEv1Keychain(),
@@ -77,22 +81,6 @@ class ApiController private constructor(context: Context) {
7781 serverSettings.postValue(settings)
7882 preferencesManager.setServerSettings(settings)
7983 preferencesManager.setInstanceColor(settings.themeColorPrimary)
80-
81- var keepAliveDelay = (settings.sessionLifetime * 3 / 4 * 1000 ).toLong()
82- while (true ) {
83- sessionCode?.let {
84- delay(keepAliveDelay)
85- keepAliveDelay = if (sessionApi.keepAlive(it)) {
86- Log .i(" KeepAlive" , " Successfully sent keep alive request" )
87- (settings.sessionLifetime * 3 / 4 * 1000 ).toLong()
88- } else {
89- Log .e(" KeepAlive" , " Error sending keep alive request" )
90- _sessionOpen .emit(false )
91- sessionCode = null
92- 5000L
93- }
94- } ? : delay(5000L )
95- }
9684 }
9785 OkHttpRequest .getInstance().allowInsecureRequests =
9886 preferencesManager.getSkipCertificateValidation()
@@ -215,6 +203,11 @@ class ApiController private constructor(context: Context) {
215203 }
216204 }
217205 sessionCode = newSessionCode
206+ serverSettings.value?.let { settings ->
207+ val keepAliveDelay = (settings.sessionLifetime * 3 / 4 * 1000 ).toLong()
208+ workManager.cancelAllWorkByTag(KeepAliveWorker .TAG )
209+ workManager.enqueue(KeepAliveWorker .getRequest(keepAliveDelay, newSessionCode))
210+ }
218211
219212 _sessionOpen .emit(true )
220213 return @withContext true
@@ -236,6 +229,11 @@ class ApiController private constructor(context: Context) {
236229 }
237230 }
238231
232+ suspend fun clearSession () {
233+ _sessionOpen .emit(false )
234+ sessionCode = null
235+ }
236+
239237 /* *
240238 * Gets a list of the user passwords via the [PasswordsApi] class. This can only be called when a
241239 * session is open, otherwise an error is thrown.
0 commit comments