Fix SQL crash in backup restore by preventing job from running until restore complete.

This commit is contained in:
Cody Henthorne
2023-06-14 10:28:34 -04:00
committed by GitHub
parent 9c0c25ef99
commit 305edf1928
6 changed files with 78 additions and 1 deletions

View File

@@ -0,0 +1,34 @@
package org.thoughtcrime.securesms.jobmanager.impl
import android.app.job.JobInfo
import org.thoughtcrime.securesms.jobmanager.Constraint
/**
* Constraint that, when added, means that a job cannot be performed while a backup restore or device transfer
* is occurring.
*/
object DataRestoreConstraint : Constraint {
const val KEY = "DataRestoreConstraint"
@JvmStatic
var isRestoringData: Boolean = false
set(value) {
field = value
DataRestoreConstraintObserver.onChange()
}
override fun isMet(): Boolean {
return !isRestoringData
}
override fun getFactoryKey(): String = KEY
override fun applyToJobInfo(jobInfoBuilder: JobInfo.Builder) = Unit
class Factory : Constraint.Factory<DataRestoreConstraint> {
override fun create(): DataRestoreConstraint {
return DataRestoreConstraint
}
}
}

View File

@@ -0,0 +1,27 @@
package org.thoughtcrime.securesms.jobmanager.impl
import org.thoughtcrime.securesms.jobmanager.ConstraintObserver
/**
* An observer for the [DataRestoreConstraint]. This class expects to be told when a change happens,
* since the points at which it happens are triggered by application code.
*/
object DataRestoreConstraintObserver : ConstraintObserver {
private const val REASON = "DataRestoreConstraint"
private var notifier: ConstraintObserver.Notifier? = null
override fun register(notifier: ConstraintObserver.Notifier) {
this.notifier = notifier
}
/**
* Let the observer know that the change number state has changed.
*/
fun onChange() {
if (DataRestoreConstraint.isMet) {
notifier?.onConstraintMet(REASON)
}
}
}