From 6282b83897de893fa0eafd228cdff0a044880ce7 Mon Sep 17 00:00:00 2001 From: ottoptj Date: Thu, 22 Aug 2024 19:11:21 +0300 Subject: [PATCH] Fixed startup crash when internet is off and weather enabled. Updated accessibility service settings location. --- .../ottop/yamlauncher/utils/WeatherSystem.kt | 81 ++++++++++--------- app/src/main/res/values/themes.xml | 9 ++- .../res/xml/screenlock_service_config.xml | 2 +- 3 files changed, 53 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/eu/ottop/yamlauncher/utils/WeatherSystem.kt b/app/src/main/java/eu/ottop/yamlauncher/utils/WeatherSystem.kt index 22e9760..a2cba8b 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/utils/WeatherSystem.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/utils/WeatherSystem.kt @@ -41,10 +41,10 @@ class WeatherSystem(private val context: Context) { { location: Location? -> if (location != null) { CoroutineScope(Dispatchers.IO).launch { - val latitude = location.latitude - val longitude = location.longitude - sharedPreferenceManager.setWeatherLocation("latitude=${latitude}&longitude=${longitude}", "Latest GPS location") - activity.updateWeatherText() + val latitude = location.latitude + val longitude = location.longitude + sharedPreferenceManager.setWeatherLocation("latitude=${latitude}&longitude=${longitude}", "Latest GPS location") + activity.updateWeatherText() } } } @@ -55,10 +55,10 @@ class WeatherSystem(private val context: Context) { fun getSearchedLocations(searchTerm: String?) : MutableList> { val foundLocations = mutableListOf>() - val url = URL("https://geocoding-api.open-meteo.com/v1/search?name=$searchTerm&count=50&language=en&format=json") - with(url.openConnection() as HttpURLConnection) { - requestMethod = "GET" - try { + val url = URL("https://geocoding-api.open-meteo.com/v1/search?name=$searchTerm&count=50&language=en&format=json") + with(url.openConnection() as HttpURLConnection) { + requestMethod = "GET" + try { inputStream.bufferedReader().use { val response = it.readText() val jsonObject = JSONObject(response) @@ -85,50 +85,57 @@ class WeatherSystem(private val context: Context) { // Run with Dispatchers.IO from the outside fun getTemp() : String { - val tempUnits = sharedPreferenceManager.getTempUnits() - var currentWeather = "" + val tempUnits = sharedPreferenceManager.getTempUnits() + var currentWeather = "" - val location = sharedPreferenceManager.getWeatherLocation() + val location = sharedPreferenceManager.getWeatherLocation() if (location != null) { if (location.isNotEmpty()) { - val url = URL("https://api.open-meteo.com/v1/forecast?$location&temperature_unit=${tempUnits}¤t=temperature_2m,weather_code") + val url = + URL("https://api.open-meteo.com/v1/forecast?$location&temperature_unit=${tempUnits}¤t=temperature_2m,weather_code") with(url.openConnection() as HttpURLConnection) { requestMethod = "GET" - inputStream.bufferedReader().use { - val response = it.readText() + try { + inputStream.bufferedReader().use { + val response = it.readText() - val jsonObject = JSONObject(response) + val jsonObject = JSONObject(response) - val currentData = jsonObject.getJSONObject("current") + val currentData = jsonObject.getJSONObject("current") - var weatherType = "" + var weatherType = "" + + when (currentData.getInt("weather_code")) { + 0, 1 -> { + weatherType = "☀\uFE0E" // Sunny + } + + 2, 3, 45, 48 -> { + weatherType = "☁\uFE0E" // Sunny + } + + 51, 53, 55, 56, 57, 61, 63, 65, 67, 80, 81, 82 -> { + weatherType = "☂\uFE0E" // Rain + } + + 71, 73, 75, 77, 85, 86 -> { + weatherType = "❄\uFE0E" // Snow + } + + 95, 96, 99 -> { + weatherType = "⛈\uFE0E" // Thunder + } - when (currentData.getInt("weather_code")) { - 0, 1 -> { - weatherType = "☀\uFE0E" // Sunny - } - 2, 3, 45, 48 -> { - weatherType = "☁\uFE0E" // Sunny - } - 51, 53, 55, 56, 57, 61, 63, 65, 67, 80, 81, 82 -> { - weatherType = "☂\uFE0E" // Rain - } - 71, 73, 75, 77, 85, 86 -> { - weatherType = "❄\uFE0E" // Snow - } - 95, 96, 99 -> { - weatherType = "⛈\uFE0E" // Thunder } + currentWeather = "$weatherType ${currentData.getInt("temperature_2m")}" + } - currentWeather = "$weatherType ${currentData.getInt("temperature_2m")}" - - } - } - } + } catch(_: Exception) {} + }} } return when (tempUnits) { diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index d9d3c7b..9c39c9a 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -14,9 +14,16 @@ -