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
@@ -0,0 +1,51 @@
package de.mm20.launcher2.plugin
import android.content.ContentResolver
import android.net.Uri
import android.util.Log
import de.mm20.launcher2.plugin.config.SearchPluginConfig
import de.mm20.launcher2.plugin.config.WeatherPluginConfig
import de.mm20.launcher2.plugin.contracts.PluginContract
class PluginApi(
private val pluginAuthority: String,
private val contentResolver: ContentResolver,
) {
fun getSearchPluginConfig(): SearchPluginConfig? {
val configBundle = try {
contentResolver.call(
Uri.Builder()
.scheme("content")
.authority(pluginAuthority)
.build(),
PluginContract.Methods.GetConfig,
null,
null
) ?: return null
} catch (e: Exception) {
Log.e("MM20", "Plugin $pluginAuthority threw exception", e)
return null
}
return SearchPluginConfig(configBundle)
}
fun getWeatherPluginConfig(): WeatherPluginConfig? {
val configBundle = try {
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)
return null
}
return WeatherPluginConfig(configBundle)
}
}
@@ -8,5 +8,6 @@ fun WeatherPluginConfig(bundle: Bundle): WeatherPluginConfig {
"minUpdateInterval",
60 * 60 * 1000L
),
managedLocation = bundle.getBoolean("managedLocation", false)
)
}
@@ -481,6 +481,8 @@
<string name="preference_category_location">Location</string>
<string name="preference_automatic_location">Automatic location</string>
<string name="preference_automatic_location_summary">Use GPS and location services to determine location automatically</string>
<string name="preference_location_managed">Managed by plugin</string>
<string name="preference_location_managed_summary">The location for this provider can be configured in the plugin app</string>
<string name="preference_location">Location</string>
<string name="preference_imperial_units_summary">Use degrees Fahrenheit and miles per hour</string>
<string name="preference_imperial_units">Imperial units</string>
@@ -393,6 +393,7 @@ data class LatLon(
data class ProviderSettings(
val locationId: String? = null,
val locationName: String? = null,
val managedLocation: Boolean = false,
)
@Serializable
@@ -20,6 +20,10 @@ sealed interface WeatherLocation {
override val name: String,
val locationId: String,
) : WeatherLocation
data object Managed : WeatherLocation {
override val name: String = "Managed by plugin"
}
}
data class WeatherSettingsData(
@@ -51,6 +55,10 @@ class WeatherSettings internal constructor(
val location = launcherDataStore.data.map {
val providerSettings = it.weatherProviderSettings[it.weatherProvider]
if (providerSettings?.managedLocation == true) {
return@map WeatherLocation.Managed
}
val id = providerSettings?.locationId
val name = providerSettings?.locationName
@@ -87,6 +95,7 @@ class WeatherSettings internal constructor(
providerSettings.copy(
locationId = null,
locationName = null,
managedLocation = false,
)
)
}
@@ -104,7 +113,27 @@ class WeatherSettings internal constructor(
it.weatherProvider,
providerSettings.copy(
locationId = location.locationId,
locationName = location.name
locationName = location.name,
managedLocation = false,
)
)
}
)
}
is WeatherLocation.Managed -> {
it.copy(
weatherLocation = null,
weatherLocationName = null,
weatherAutoLocation = true,
weatherLastUpdate = 0L,
weatherProviderSettings = it.weatherProviderSettings.toMutableMap().apply {
put(
it.weatherProvider,
providerSettings.copy(
locationId = null,
locationName = null,
managedLocation = true,
)
)
}
@@ -1,7 +1,5 @@
package de.mm20.launcher2.plugin.config
import android.os.Bundle
data class WeatherPluginConfig(
/**
* Minimum time (in ms) that needs to pass before the provider can be queried again.
@@ -9,4 +7,9 @@ data class WeatherPluginConfig(
* or weather provider.
*/
val minUpdateInterval: Long = 60 * 60 * 1000L,
/**
* Whether the location is managed by the plugin. If true, the user cannot change the location
* in the launcher settings.
*/
val managedLocation: Boolean = false,
)