From ec40b6e5d8fbe9d1be5a742eb7dbfd7f19f7416f Mon Sep 17 00:00:00 2001 From: ELY M <> Date: Wed, 29 Jan 2025 12:26:20 -0600 Subject: [PATCH] 55938-elys --- app/src/main/AndroidManifest.xml | 4 +- .../joshuatee/wx/common/GlobalVariables.kt | 3 +- .../joshuatee/wx/objects/PolygonWarning.kt | 2 +- .../main/java/joshuatee/wx/objects/Sites.kt | 24 ++- app/src/main/java/joshuatee/wx/objects/URL.kt | 38 ++--- .../main/java/joshuatee/wx/util/Hazards.kt | 4 +- .../joshuatee/wx/util/UtilityDownloadNWS.kt | 2 +- .../java/joshuatee/wx/util/UtilityMath.kt | 23 ++- .../joshuatee/wx/util/UtilityNetworkIO.kt | 160 +++--------------- doc/ChangeLog.md | 6 + 10 files changed, 84 insertions(+), 182 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a1078969..d728a05b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -22,8 +22,8 @@ + android:versionCode="55938" + android:versionName="55938-elys"> diff --git a/app/src/main/java/joshuatee/wx/common/GlobalVariables.kt b/app/src/main/java/joshuatee/wx/common/GlobalVariables.kt index 9257c6c5..3eaddb28 100644 --- a/app/src/main/java/joshuatee/wx/common/GlobalVariables.kt +++ b/app/src/main/java/joshuatee/wx/common/GlobalVariables.kt @@ -30,8 +30,7 @@ object GlobalVariables { val newline: String = System.lineSeparator() - const val PACKAGE_NAME = "joshuatee.wx" - + const val PACKAGE_NAME = "elys.joshuatee.wx" // const val PACKAGE_NAME_FILE = "joshuatee_wx" const val EMAIL = "elymbmx@gmail.com" const val HTTP_USER_AGENT = "Android $PACKAGE_NAME $EMAIL" diff --git a/app/src/main/java/joshuatee/wx/objects/PolygonWarning.kt b/app/src/main/java/joshuatee/wx/objects/PolygonWarning.kt index 7ce2ed18..e4cbf421 100644 --- a/app/src/main/java/joshuatee/wx/objects/PolygonWarning.kt +++ b/app/src/main/java/joshuatee/wx/objects/PolygonWarning.kt @@ -26,7 +26,6 @@ import android.graphics.Color import joshuatee.wx.radar.Warnings import joshuatee.wx.settings.RadarPreferences import joshuatee.wx.util.Utility -import joshuatee.wx.util.UtilityDownloadNws import joshuatee.wx.util.UtilityNetworkIO class PolygonWarning(val context: Context, val type: PolygonWarningType) { @@ -118,6 +117,7 @@ class PolygonWarning(val context: Context, val type: PolygonWarningType) { PolygonWarningType.TornadoWarning to "Tornado%20Warning", PolygonWarningType.ThunderstormWarning to "Severe%20Thunderstorm%20Warning", PolygonWarningType.FlashFloodWarning to "Flash%20Flood%20Warning", +// PolygonWarningType.FlashFloodWarning to "Flood%20Warning", // use for testing when no tst/tor/ffw ) const val BASE_URL = "https://api.weather.gov/alerts/active?event=" diff --git a/app/src/main/java/joshuatee/wx/objects/Sites.kt b/app/src/main/java/joshuatee/wx/objects/Sites.kt index adbed18d..844a092b 100644 --- a/app/src/main/java/joshuatee/wx/objects/Sites.kt +++ b/app/src/main/java/joshuatee/wx/objects/Sites.kt @@ -70,18 +70,22 @@ class Sites( } fun getNearest(latLon: LatLon): String { - for (site in sites) { - site.distance = LatLon.distance(latLon, site.latLon).toInt() + synchronized(sites) { + for (site in sites) { + site.distance = LatLon.distance(latLon, site.latLon).toInt() + } + sites.sortBy { it.distance } } - sites.sortBy { it.distance } return sites[0].codeName } fun getNearestSite(latLon: LatLon, order: Int = 0): Site { - for (site in sites) { - site.distance = LatLon.distance(latLon, site.latLon).toInt() + synchronized(sites) { + for (site in sites) { + site.distance = LatLon.distance(latLon, site.latLon).toInt() + } + sites.sortBy { it.distance } } - sites.sortBy { it.distance } return sites[order] } @@ -96,10 +100,12 @@ class Sites( // } fun getNearestInMiles(latLon: LatLon): Int { - for (site in sites) { - site.distance = LatLon.distance(latLon, site.latLon).toInt() + synchronized(sites) { + for (site in sites) { + site.distance = LatLon.distance(latLon, site.latLon).toInt() + } + sites.sortBy { it.distance } } - sites.sortBy { it.distance } return sites[0].distance } } diff --git a/app/src/main/java/joshuatee/wx/objects/URL.kt b/app/src/main/java/joshuatee/wx/objects/URL.kt index f27e41f7..c045a416 100644 --- a/app/src/main/java/joshuatee/wx/objects/URL.kt +++ b/app/src/main/java/joshuatee/wx/objects/URL.kt @@ -27,25 +27,25 @@ class URL(val url: String) { return out.toString() } - @Suppress("unused") - fun getTextXmlAcceptHeader(): String { - UtilityLog.download("getStringFromUrlBaseNoHeader: $url") - val breakStr = "ABC123_456ZZ" - val out = StringBuilder(5000) - try { - val request = Request.Builder() - .url(url) - .header("User-Agent", GlobalVariables.HTTP_USER_AGENT) - //.addHeader("Accept", "application/vnd.noaa.dwml+xml;version=1") - .addHeader("Accept", "application/atom+xml") - .build() - val response = MyApplication.httpClient.newCall(request).execute() - return response.body.string() - } catch (e: Exception) { - UtilityLog.handleException(e) - } - return out.toString().replace(breakStr, "
") - } +// @Suppress("unused") +// fun getTextXmlAcceptHeader(): String { +// UtilityLog.download("getStringFromUrlBaseNoHeader: $url") +// val breakStr = "ABC123_456ZZ" +// val out = StringBuilder(5000) +// try { +// val request = Request.Builder() +// .url(url) +// .header("User-Agent", GlobalVariables.HTTP_USER_AGENT) +// //.addHeader("Accept", "application/vnd.noaa.dwml+xml;version=1") +// .addHeader("Accept", "application/atom+xml") +// .build() +// val response = MyApplication.httpClient.newCall(request).execute() +// return response.body.string() +// } catch (e: Exception) { +// UtilityLog.handleException(e) +// } +// return out.toString().replace(breakStr, "
") +// } fun getBytes(): ByteArray { UtilityLog.download("getByte $url") diff --git a/app/src/main/java/joshuatee/wx/util/Hazards.kt b/app/src/main/java/joshuatee/wx/util/Hazards.kt index ff92c3c7..66468782 100644 --- a/app/src/main/java/joshuatee/wx/util/Hazards.kt +++ b/app/src/main/java/joshuatee/wx/util/Hazards.kt @@ -58,8 +58,6 @@ class Hazards { companion object { fun getHtml(latLon: LatLon): String = - ("https://api.weather.gov/alerts?point=" + UtilityMath.latLonFix(latLon.latString) + "," + UtilityMath.latLonFix( - latLon.lonString - ) + "&active=1").getNwsHtml() + ("https://api.weather.gov/alerts?point=" + latLon.latForNws + "," + latLon.lonForNws + "&active=1").getNwsHtml() } } diff --git a/app/src/main/java/joshuatee/wx/util/UtilityDownloadNWS.kt b/app/src/main/java/joshuatee/wx/util/UtilityDownloadNWS.kt index 5eb1a969..ff63a88d 100644 --- a/app/src/main/java/joshuatee/wx/util/UtilityDownloadNWS.kt +++ b/app/src/main/java/joshuatee/wx/util/UtilityDownloadNWS.kt @@ -68,5 +68,5 @@ object UtilityDownloadNws { } private fun getLocationPointData(latLon: LatLon): String = - (GlobalVariables.NWS_API_URL + "/points/" + latLon.latString + "," + latLon.lonString).getNwsHtml() + (GlobalVariables.NWS_API_URL + "/points/" + latLon.latForNws + "," + latLon.lonForNws).getNwsHtml() } diff --git a/app/src/main/java/joshuatee/wx/util/UtilityMath.kt b/app/src/main/java/joshuatee/wx/util/UtilityMath.kt index 0c695686..324fa89d 100644 --- a/app/src/main/java/joshuatee/wx/util/UtilityMath.kt +++ b/app/src/main/java/joshuatee/wx/util/UtilityMath.kt @@ -23,7 +23,6 @@ package joshuatee.wx.util import joshuatee.wx.common.GlobalVariables import joshuatee.wx.settings.UIPreferences -import java.util.Locale import kotlin.math.* object UtilityMath { @@ -69,17 +68,17 @@ object UtilityMath { listOf(x0 + fraction * (x1 - x0), y0 + fraction * (y1 - y0)) // 42.98888 to 42.99 - fun latLonFix(x: String): String { - val dblX = To.double(x) - var newX = "0.0" - try { - newX = String.format(Locale.US, "%.2f", dblX).replace("00$".toRegex(), "") - .replace("0$".toRegex(), "").replace("\\.$".toRegex(), "") - } catch (e: Exception) { - UtilityLog.handleException(e) - } - return newX - } +// fun latLonFix(x: String): String { +// val dblX = To.double(x) +// var newX = "0.0" +// try { +// newX = String.format(Locale.US, "%.2f", dblX).replace("00$".toRegex(), "") +// .replace("0$".toRegex(), "").replace("\\.$".toRegex(), "") +// } catch (e: Exception) { +// UtilityLog.handleException(e) +// } +// return newX +// } // convert polar cords to rect fun toRect(r: Float, t: Float): FloatArray = diff --git a/app/src/main/java/joshuatee/wx/util/UtilityNetworkIO.kt b/app/src/main/java/joshuatee/wx/util/UtilityNetworkIO.kt index cdeda00e..3e30ec73 100644 --- a/app/src/main/java/joshuatee/wx/util/UtilityNetworkIO.kt +++ b/app/src/main/java/joshuatee/wx/util/UtilityNetworkIO.kt @@ -41,31 +41,8 @@ object UtilityNetworkIO { private fun getStringFromUrlNew(url: String, withNewLine: Boolean): String { UtilityLog.download("getStringFromUrlNew $withNewLine: $url") - val out = StringBuilder(5000) - try { - val request = - Request.Builder().url(url).header("User-Agent", GlobalVariables.HTTP_USER_AGENT) - .build() - val response = MyApplication.httpClient.newCall(request).execute() -// val response = MyApplication.httpClientUnsafe.newCall(request).execute() - val inputStream = BufferedInputStream(response.body.byteStream()) - val bufferedReader = BufferedReader(InputStreamReader(inputStream)) - var line: String? = bufferedReader.readLine() - while (line != null) { - if (withNewLine) { - out.append(line + GlobalVariables.newline) - } else { - out.append(line) - } - line = bufferedReader.readLine() - } - bufferedReader.close() - } catch (e: Exception) { - UtilityLog.handleException(e) - } catch (e: OutOfMemoryError) { - UtilityLog.handleException(e) - } - return out.toString() + val request = Request.Builder().url(url).header("User-Agent", USER_AGENT).build() + return requestToString(request, withNewLine) } // String.getHtml() @@ -78,8 +55,7 @@ object UtilityNetworkIO { // String.getImage() fun getBitmapFromUrl(url: String): Bitmap = try { UtilityLog.download("getBitmapFromUrl: $url") - val request = - Request.Builder().url(url).header("User-Agent", GlobalVariables.HTTP_USER_AGENT).build() + val request = Request.Builder().url(url).header("User-Agent", USER_AGENT).build() val response = MyApplication.httpClient.newCall(request).execute() if (url.contains("hazards_d8_14_contours.png")) { val options = BitmapFactory.Options() @@ -103,10 +79,8 @@ object UtilityNetworkIO { // raw downloads - nexrad radar files, etc fun getInputStreamFromUrl(url: String): InputStream? = try { UtilityLog.download("getInputStreamFromUrl: $url") - val request = - Request.Builder().url(url).header("User-Agent", GlobalVariables.HTTP_USER_AGENT).build() + val request = Request.Builder().url(url).header("User-Agent", USER_AGENT).build() val response = MyApplication.httpClient.newCall(request).execute() -// val response = MyApplication.httpClientUnsafe.newCall(request).execute() response.body.byteStream() } catch (e: IOException) { UtilityLog.handleException(e) @@ -115,138 +89,58 @@ object UtilityNetworkIO { // used for CapAlert (XML) fun getStringFromUrlXml(url: String): String { - UtilityLog.download("getStringFromURLBase: $url") - val out = StringBuilder(5000) - try { - val request = Request.Builder() - .url(url) - .header("User-Agent", USER_AGENT) - .addHeader("Accept", "application/atom+xml") - .build() - val response = MyApplication.httpClient.newCall(request).execute() - val inputStream = BufferedInputStream(response.body.byteStream()) - val bufferedReader = BufferedReader(InputStreamReader(inputStream)) - var line: String? = bufferedReader.readLine() - while (line != null) { - out.append(line) - line = bufferedReader.readLine() - } - bufferedReader.close() - } catch (e: Exception) { - UtilityLog.handleException(e) - } - return out.toString() + UtilityLog.download("getStringFromUrlXml: $url") + val request = Request.Builder().url(url).header("User-Agent", USER_AGENT) + .addHeader("Accept", "application/atom+xml").build() + return requestToString(request) } // target for String.getNwsHtml() fun getStringFromUrlBaseNoAcceptHeader1(url: String): String { UtilityLog.download("getStringFromUrlBaseNoAcceptHeader1 getNwsHtml: $url") - val out = StringBuilder(5000) - try { - val request = Request.Builder() - .url(url) - .header("User-Agent", USER_AGENT) - .build() - val response = MyApplication.httpClient.newCall(request).execute() - val inputStream = BufferedInputStream(response.body.byteStream()) - val bufferedReader = BufferedReader(InputStreamReader(inputStream)) - var line: String? = bufferedReader.readLine() - while (line != null) { - out.append(line) - line = bufferedReader.readLine() - } - bufferedReader.close() - } catch (e: Exception) { - UtilityLog.handleException(e) - } - return out.toString() + val request = Request.Builder().url(url).header("User-Agent", USER_AGENT).build() + return requestToString(request) } // PolygonWarning.kt // FYI - this is probably not needed and could use getStringFromUrlBaseNoAcceptHeader1 instead fun getStringFromUrlBaseNoHeader1(url: String): String { UtilityLog.download("getStringFromUrlBaseNoHeader1: $url") - val out = StringBuilder(5000) - try { - val request = Request.Builder() - .url(url) - .header("User-Agent", USER_AGENT) - .addHeader("Accept", ACCEPT_HEADER) - .build() - val response = MyApplication.httpClient.newCall(request).execute() - val inputStream = BufferedInputStream(response.body.byteStream()) - val bufferedReader = BufferedReader(InputStreamReader(inputStream)) - var line: String? = bufferedReader.readLine() - while (line != null) { - out.append(line) - line = bufferedReader.readLine() - } - bufferedReader.close() - } catch (e: Exception) { - UtilityLog.handleException(e) - } - return out.toString() + val request = Request.Builder().url(url).header("User-Agent", USER_AGENT) + .addHeader("Accept", ACCEPT_HEADER).build() + return requestToString(request) } // used by CapAlert.kt fun getStringFromUrlSep(url: String): String { UtilityLog.download("getStringFromUrlSep: $url") - val breakStr = "ABC123_456ZZ" - val out = StringBuilder(5000) - val request = Request.Builder() - .url(url) - .header("User-Agent", USER_AGENT) - .addHeader( - "Accept", - "application/vnd.noaa.dwml+xml;version=1" - ).build() - requestToString(request, breakStr, out) - return out.toString().replace(breakStr, "
") + val request = Request.Builder().url(url).header("User-Agent", USER_AGENT) + .addHeader("Accept", "application/vnd.noaa.dwml+xml;version=1").build() + return requestToString(request) } -// fun getStringFromUrlSep(url: String): String { -// UtilityLog.download("getStringFromUrlSep: $url") -// val breakStr = "ABC123_456ZZ" -// val out = StringBuilder(5000) -// try { -// val request = Request.Builder() -// .url(url) -// .header("User-Agent", USER_AGENT) -// .addHeader( -// "Accept", -// "application/vnd.noaa.dwml+xml;version=1" -// ) // TODO FIXME, not valid defaulting to application/geo+json -// .build() -// val response = MyApplication.httpClient.newCall(request).execute() -// val inputStream = BufferedInputStream(response.body.byteStream()) -// val bufferedReader = BufferedReader(InputStreamReader(inputStream)) -// var line: String? = bufferedReader.readLine() -// while (line != null) { -// out.append(line) -// line = bufferedReader.readLine() -// } -// out.append(breakStr) -// bufferedReader.close() -// } catch (e: Exception) { -// UtilityLog.handleException(e) -// } -// return out.toString().replace(breakStr, "
") -// } - - private fun requestToString(request: Request, breakString: String, output: StringBuilder) { + private fun requestToString(request: Request, withNewLine: Boolean = false): String { + val output = StringBuilder(5000) try { val response = MyApplication.httpClient.newCall(request).execute() val inputStream = BufferedInputStream(response.body.byteStream()) val bufferedReader = BufferedReader(InputStreamReader(inputStream)) var line: String? = bufferedReader.readLine() while (line != null) { - output.append(line) + if (withNewLine) { + output.append(line + GlobalVariables.newline) + } else { + output.append(line) + } line = bufferedReader.readLine() } - output.append(breakString) +// output.append(breakString) bufferedReader.close() } catch (e: Exception) { UtilityLog.handleException(e) + } catch (e: OutOfMemoryError) { + UtilityLog.handleException(e) } + return output.toString() } } diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 7ecfa4ce..ef0b95b8 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -2,9 +2,15 @@ # Developer ChangeLog +## 55938 2025_01_29 + +* [ADD] + ## 55937 2025_01_29 * [ADD] Start consolidation in UtilityNetworkIO.kt with requestToString +* [ADD] Correct Hazards to use proper lat/lon format for NWS API +* [FIX] in Sites.kt use sync ## 55936 2025_01_29