Skip to content

Commit

Permalink
55938-elys
Browse files Browse the repository at this point in the history
  • Loading branch information
ELY M committed Jan 29, 2025
1 parent 900f4b2 commit ec40b6e
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 182 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:versionCode="55937"
android:versionName="55937-elys">
android:versionCode="55938"
android:versionName="55938-elys">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/joshuatee/wx/common/GlobalVariables.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]"
const val HTTP_USER_AGENT = "Android $PACKAGE_NAME $EMAIL"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/joshuatee/wx/objects/PolygonWarning.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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="
Expand Down
24 changes: 15 additions & 9 deletions app/src/main/java/joshuatee/wx/objects/Sites.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand All @@ -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
}
}
Expand Down
38 changes: 19 additions & 19 deletions app/src/main/java/joshuatee/wx/objects/URL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<br>")
}
// @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, "<br>")
// }

fun getBytes(): ByteArray {
UtilityLog.download("getByte $url")
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/joshuatee/wx/util/Hazards.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/joshuatee/wx/util/UtilityDownloadNWS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
23 changes: 11 additions & 12 deletions app/src/main/java/joshuatee/wx/util/UtilityMath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 =
Expand Down
160 changes: 27 additions & 133 deletions app/src/main/java/joshuatee/wx/util/UtilityNetworkIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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, "<br>")
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, "<br>")
// }

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()
}
}
Loading

0 comments on commit ec40b6e

Please sign in to comment.