-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a47e390
commit 4f458a0
Showing
19 changed files
with
657 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/src/main/java/org/thoughtcrime/securesms/jobs/RefreshKbsCredentialsJob.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.thoughtcrime.securesms.jobs | ||
|
||
import org.signal.core.util.logging.Log | ||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies | ||
import org.thoughtcrime.securesms.jobmanager.Data | ||
import org.thoughtcrime.securesms.jobmanager.Job | ||
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint | ||
import org.thoughtcrime.securesms.keyvalue.SignalStore | ||
import org.thoughtcrime.securesms.pin.KbsRepository | ||
import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException | ||
import java.io.IOException | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.days | ||
|
||
/** | ||
* Refresh KBS authentication credentials for talking to KBS during re-registration. | ||
*/ | ||
class RefreshKbsCredentialsJob private constructor(parameters: Parameters) : BaseJob(parameters) { | ||
|
||
companion object { | ||
const val KEY = "RefreshKbsCredentialsJob" | ||
|
||
private val TAG = Log.tag(RefreshKbsCredentialsJob::class.java) | ||
private val FREQUENCY: Duration = 15.days | ||
|
||
@JvmStatic | ||
fun enqueueIfNecessary() { | ||
val lastTimestamp = SignalStore.kbsValues().lastRefreshAuthTimestamp | ||
if (lastTimestamp + FREQUENCY.inWholeMilliseconds < System.currentTimeMillis() || lastTimestamp > System.currentTimeMillis()) { | ||
ApplicationDependencies.getJobManager().add(RefreshKbsCredentialsJob()) | ||
} else { | ||
Log.d(TAG, "Do not need to refresh credentials. Last refresh: $lastTimestamp") | ||
} | ||
} | ||
} | ||
|
||
private constructor() : this( | ||
parameters = Parameters.Builder() | ||
.setQueue("RefreshKbsCredentials") | ||
.addConstraint(NetworkConstraint.KEY) | ||
.setMaxInstancesForQueue(2) | ||
.setLifespan(1.days.inWholeMilliseconds) | ||
.build() | ||
) | ||
|
||
override fun serialize(): Data = Data.Builder().build() | ||
|
||
override fun getFactoryKey(): String = KEY | ||
|
||
override fun onRun() { | ||
KbsRepository().refreshAuthorization() | ||
} | ||
|
||
override fun onShouldRetry(e: Exception): Boolean { | ||
return e is IOException && e !is NonSuccessfulResponseCodeException | ||
} | ||
|
||
override fun onFailure() = Unit | ||
|
||
class Factory : Job.Factory<RefreshKbsCredentialsJob> { | ||
override fun create(parameters: Parameters, data: Data): RefreshKbsCredentialsJob { | ||
return RefreshKbsCredentialsJob(parameters) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.