Look the external provider up once per change of the installed apps (#100)
This commit is contained in:
1 parent
b77311dff7
commit
96c94eeec3
3 files changed
+58
-2
No files matched your search
@@ -1,6 +1,9 @@
|
||||
package de.jeanlucmakiola.agendula.data.tasks
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.content.pm.PackageManager
|
||||
import androidx.core.content.ContextCompat
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
@@ -26,6 +29,9 @@ interface ProviderEnvironment {
|
||||
|
||||
/** [packageName]'s own app name, or null when it cannot be read. */
|
||||
fun appLabel(packageName: String): String?
|
||||
|
||||
/** Calls [listener] whenever an app is installed, updated or removed, which may change [packageDeclaring]'s answer. */
|
||||
fun onPackagesChanged(listener: () -> Unit) {}
|
||||
}
|
||||
|
||||
@Singleton
|
||||
@@ -43,4 +49,19 @@ class AndroidProviderEnvironment @Inject constructor(
|
||||
val pm = context.packageManager
|
||||
pm.getApplicationLabel(pm.getApplicationInfo(packageName, 0)).toString()
|
||||
}.getOrNull()
|
||||
|
||||
override fun onPackagesChanged(listener: () -> Unit) {
|
||||
val filter = IntentFilter().apply {
|
||||
addAction(Intent.ACTION_PACKAGE_ADDED)
|
||||
addAction(Intent.ACTION_PACKAGE_REMOVED)
|
||||
addAction(Intent.ACTION_PACKAGE_REPLACED)
|
||||
addAction(Intent.ACTION_PACKAGE_CHANGED)
|
||||
addDataScheme("package")
|
||||
}
|
||||
val receiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) = listener()
|
||||
}
|
||||
// System broadcasts still reach a receiver that is not exported.
|
||||
ContextCompat.registerReceiver(context, receiver, filter, ContextCompat.RECEIVER_NOT_EXPORTED)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package de.jeanlucmakiola.agendula.data.tasks
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -34,6 +36,14 @@ class ProviderResolver @Inject constructor(
|
||||
|
||||
private val modeListeners = CopyOnWriteArrayList<() -> Unit>()
|
||||
|
||||
/** Bumped by every install, update or removal; a cached lookup of an older one is stale. */
|
||||
private val packagesGeneration = AtomicLong()
|
||||
private val external = AtomicReference<Pair<Long, TaskProvider?>?>(null)
|
||||
|
||||
init {
|
||||
environment.onPackagesChanged { packagesGeneration.incrementAndGet() }
|
||||
}
|
||||
|
||||
/**
|
||||
* The user's explicit choice, or `null` while they have not made one (which is
|
||||
* the normal state — most people never open Settings). Kept as a plain field
|
||||
@@ -107,8 +117,18 @@ class ProviderResolver @Inject constructor(
|
||||
return mode to external?.authority?.takeIf { mode == StorageMode.EXTERNAL }
|
||||
}
|
||||
|
||||
/** The first installed external candidate, or `null` when none is present. */
|
||||
/**
|
||||
* The first installed external candidate, or `null` when none is present.
|
||||
* Looked up once per change of the installed apps, as every read of the
|
||||
* active store, its scope included, asks it.
|
||||
*/
|
||||
fun resolveExternal(): TaskProvider? {
|
||||
val generation = packagesGeneration.get()
|
||||
external.get()?.takeIf { it.first == generation }?.let { return it.second }
|
||||
return lookupExternal().also { external.set(generation to it) }
|
||||
}
|
||||
|
||||
private fun lookupExternal(): TaskProvider? {
|
||||
for (candidate in EXTERNAL_CANDIDATES) {
|
||||
val packageName = environment.packageDeclaring(candidate.authority) ?: continue
|
||||
return candidate.copy(packageName = packageName)
|
||||
|
||||
@@ -23,8 +23,17 @@ class ProviderScopeTest {
|
||||
}
|
||||
|
||||
/** One provider installed at a time, its permissions held. */
|
||||
private class SwitchingEnvironment(var authority: String) : ProviderEnvironment {
|
||||
private class SwitchingEnvironment(authority: String) : ProviderEnvironment {
|
||||
private val listeners = mutableListOf<() -> Unit>()
|
||||
var authority = authority
|
||||
set(value) {
|
||||
field = value
|
||||
listeners.forEach { it() }
|
||||
}
|
||||
var lookups = 0
|
||||
override fun onPackagesChanged(listener: () -> Unit) {
|
||||
listeners += listener
|
||||
}
|
||||
override fun packageDeclaring(authority: String): String? {
|
||||
lookups++
|
||||
return authority.takeIf { it == this.authority }
|
||||
@@ -81,6 +90,12 @@ class ProviderScopeTest {
|
||||
prefs.setSnoozeMinutes(5)
|
||||
environment.lookups = 0
|
||||
prefs.settings.first()
|
||||
prefs.storeScope()
|
||||
assertThat(environment.lookups).isEqualTo(0)
|
||||
// An install or removal asks once more.
|
||||
environment.authority = OPEN_TASKS
|
||||
prefs.storeScope()
|
||||
prefs.storeScope()
|
||||
assertThat(environment.lookups).isEqualTo(1)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user