Allow restoration over cellular

This commit is contained in:
Michelle Tang
2024-12-05 13:02:36 -05:00
committed by Greyson Parrelli
parent 0c86ff1f84
commit a7d7c1da8d
11 changed files with 128 additions and 10 deletions

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2024 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.thoughtcrime.securesms.jobmanager.impl
import android.app.Application
import android.app.job.JobInfo
import android.content.Context
import org.thoughtcrime.securesms.jobmanager.Constraint
import org.thoughtcrime.securesms.keyvalue.SignalStore
/**
* Constraint that, when added, means that a job cannot be performed unless the user either has Wifi or, if they enabled it, cellular
*/
class RestoreAttachmentConstraint(private val application: Application) : Constraint {
companion object {
const val KEY = "RestoreAttachmentConstraint"
fun isMet(context: Context): Boolean {
if (SignalStore.backup.restoreWithCellular) {
return NetworkConstraint.isMet(context)
}
return WifiConstraint.isMet(context)
}
}
override fun isMet(): Boolean {
return isMet(application)
}
override fun getFactoryKey(): String = KEY
override fun applyToJobInfo(jobInfoBuilder: JobInfo.Builder) = Unit
class Factory(val application: Application) : Constraint.Factory<RestoreAttachmentConstraint> {
override fun create(): RestoreAttachmentConstraint {
return RestoreAttachmentConstraint(application)
}
}
}

View File

@@ -0,0 +1,28 @@
package org.thoughtcrime.securesms.jobmanager.impl
import org.thoughtcrime.securesms.dependencies.AppDependencies
import org.thoughtcrime.securesms.jobmanager.ConstraintObserver
/**
* An observer for the [RestoreAttachmentConstraint]. This is called
* when users change whether or not restoring is allowed via cellular
*/
object RestoreAttachmentConstraintObserver : ConstraintObserver {
private const val REASON = "RestoreAttachmentConstraint"
private var notifier: ConstraintObserver.Notifier? = null
override fun register(notifier: ConstraintObserver.Notifier) {
this.notifier = notifier
}
/**
* Let the observer know that the restore using cellular flag has changed.
*/
fun onChange() {
if (RestoreAttachmentConstraint.isMet(AppDependencies.application)) {
notifier?.onConstraintMet(REASON)
}
}
}