Add managed location to weather plugin API

This commit is contained in:
MM20
2024-05-03 13:02:17 +02:00
parent b0f9ffd473
commit 1d6688831c
19 changed files with 256 additions and 100 deletions
@@ -3,4 +3,5 @@ package de.mm20.launcher2.weather
data class WeatherProviderInfo(
val id: String,
val name: String,
val managedLocation: Boolean = false,
)
@@ -8,6 +8,7 @@ import de.mm20.launcher2.devicepose.DevicePoseProvider
import de.mm20.launcher2.ktx.or
import de.mm20.launcher2.permissions.PermissionGroup
import de.mm20.launcher2.permissions.PermissionsManager
import de.mm20.launcher2.plugin.PluginApi
import de.mm20.launcher2.plugin.PluginRepository
import de.mm20.launcher2.plugin.PluginType
import de.mm20.launcher2.preferences.LatLon
@@ -180,8 +181,9 @@ internal class WeatherRepositoryImpl(
}
val pluginProviders = pluginRepository.findMany(type = PluginType.Weather, enabled = true)
return pluginProviders.map {
providers + it.map {
WeatherProviderInfo(it.authority, it.label)
providers + it.mapNotNull {
val config = PluginApi(it.authority, context.contentResolver).getWeatherPluginConfig() ?: return@mapNotNull null
WeatherProviderInfo(it.authority, it.label, config.managedLocation)
}
}
}
@@ -10,6 +10,7 @@ import androidx.core.database.getIntOrNull
import androidx.core.database.getLongOrNull
import androidx.core.database.getStringOrNull
import de.mm20.launcher2.crashreporter.CrashReporter
import de.mm20.launcher2.plugin.PluginApi
import de.mm20.launcher2.plugin.config.WeatherPluginConfig
import de.mm20.launcher2.plugin.contracts.PluginContract
import de.mm20.launcher2.plugin.contracts.WeatherPluginContract
@@ -27,11 +28,14 @@ internal class PluginWeatherProvider(
private val pluginAuthority: String,
) : WeatherProvider {
override suspend fun getWeatherData(location: WeatherLocation): List<Forecast>? {
val config = getPluginConfig()
val uri = Uri.Builder()
.scheme("content")
.authority(pluginAuthority)
.path(WeatherPluginContract.Paths.Forecasts).apply {
if (location is WeatherLocation.LatLon) {
if (config?.managedLocation == true || location is WeatherLocation.Managed) {
// no parameters
} else if (location is WeatherLocation.LatLon) {
appendQueryParameter(
WeatherPluginContract.ForecastParams.Lat,
location.lat.toString()
@@ -40,14 +44,15 @@ internal class PluginWeatherProvider(
WeatherPluginContract.ForecastParams.Lon,
location.lon.toString()
)
appendQueryParameter(WeatherPluginContract.ForecastParams.LocationName, location.name)
} else if (location is WeatherLocation.Id) {
appendQueryParameter(
WeatherPluginContract.ForecastParams.Id,
location.locationId
)
appendQueryParameter(WeatherPluginContract.ForecastParams.LocationName, location.name)
}
}
.appendQueryParameter(WeatherPluginContract.ForecastParams.LocationName, location.name)
.appendQueryParameter(WeatherPluginContract.ForecastParams.Language, getLang())
.build()
@@ -307,22 +312,6 @@ internal class PluginWeatherProvider(
}
private fun getPluginConfig(): WeatherPluginConfig? {
val configBundle = try {
context.contentResolver.call(
Uri.Builder()
.scheme("content")
.authority(pluginAuthority)
.build(),
PluginContract.Methods.GetConfig,
null,
null
) ?: return null
} catch (e: Exception) {
Log.e("PluginWeatherProvider", "Plugin $pluginAuthority threw exception", e)
CrashReporter.logException(e)
return null
}
return WeatherPluginConfig(configBundle)
return PluginApi(pluginAuthority, context.contentResolver).getWeatherPluginConfig()
}
}