ui: use applicationScope helper when possible

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2023-05-11 02:57:38 +02:00
parent 675386241b
commit 254e6164d7

View File

@ -21,6 +21,7 @@ import com.wireguard.android.Application
import com.wireguard.android.BuildConfig import com.wireguard.android.BuildConfig
import com.wireguard.android.activity.MainActivity import com.wireguard.android.activity.MainActivity
import com.wireguard.android.util.UserKnobs import com.wireguard.android.util.UserKnobs
import com.wireguard.android.util.applicationScope
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@ -76,7 +77,7 @@ object Updater {
object Complete : Progress() object Complete : Progress()
class Available(val version: String) : Progress() { class Available(val version: String) : Progress() {
fun update() { fun update() {
Application.getCoroutineScope().launch { applicationScope.launch {
UserKnobs.setUpdaterNewerVersionConsented(version) UserKnobs.setUpdaterNewerVersionConsented(version)
} }
} }
@ -100,7 +101,7 @@ object Updater {
} }
fun markAsDone() { fun markAsDone() {
Application.getCoroutineScope().launch { applicationScope.launch {
if (installerActive()) if (installerActive())
return@launch return@launch
delay(7.seconds) delay(7.seconds)
@ -333,13 +334,13 @@ object Updater {
PackageInstaller.STATUS_PENDING_USER_ACTION -> { PackageInstaller.STATUS_PENDING_USER_ACTION -> {
val id = intent.getIntExtra(PackageInstaller.EXTRA_SESSION_ID, 0) val id = intent.getIntExtra(PackageInstaller.EXTRA_SESSION_ID, 0)
val userIntervention = IntentCompat.getParcelableExtra(intent, Intent.EXTRA_INTENT, Intent::class.java)!! val userIntervention = IntentCompat.getParcelableExtra(intent, Intent.EXTRA_INTENT, Intent::class.java)!!
Application.getCoroutineScope().launch { applicationScope.launch {
emitProgress(Progress.NeedsUserIntervention(userIntervention, id)) emitProgress(Progress.NeedsUserIntervention(userIntervention, id))
} }
} }
PackageInstaller.STATUS_SUCCESS -> { PackageInstaller.STATUS_SUCCESS -> {
Application.getCoroutineScope().launch { applicationScope.launch {
emitProgress(Progress.Complete) emitProgress(Progress.Complete)
} }
context.applicationContext.unregisterReceiver(this) context.applicationContext.unregisterReceiver(this)
@ -352,7 +353,7 @@ object Updater {
} catch (_: SecurityException) { } catch (_: SecurityException) {
} }
val message = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE) ?: "Installation error $status" val message = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE) ?: "Installation error $status"
Application.getCoroutineScope().launch { applicationScope.launch {
val e = Exception(message) val e = Exception(message)
Log.e(TAG, "Update failure", e) Log.e(TAG, "Update failure", e)
emitProgress(Progress.Failure(e)) emitProgress(Progress.Failure(e))
@ -414,14 +415,14 @@ object Updater {
UserKnobs.updaterNewerVersionConsented.firstOrNull()?.let { Version(it) > CURRENT_VERSION } != true UserKnobs.updaterNewerVersionConsented.firstOrNull()?.let { Version(it) > CURRENT_VERSION } != true
) )
emitProgress(Progress.Available(ver)) emitProgress(Progress.Available(ver))
}.launchIn(Application.getCoroutineScope()) }.launchIn(applicationScope)
UserKnobs.updaterNewerVersionConsented.onEach { ver -> UserKnobs.updaterNewerVersionConsented.onEach { ver ->
if (ver != null && Version(ver) > CURRENT_VERSION) if (ver != null && Version(ver) > CURRENT_VERSION)
updaterScope.launch { updaterScope.launch {
downloadAndUpdateWrapErrors() downloadAndUpdateWrapErrors()
} }
}.launchIn(Application.getCoroutineScope()) }.launchIn(applicationScope)
} }
class AppUpdatedReceiver : BroadcastReceiver() { class AppUpdatedReceiver : BroadcastReceiver() {