Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f38ea7dcc |
@@ -11,8 +11,8 @@ android {
|
||||
applicationId = "com.expapp.trigger"
|
||||
minSdk = 24
|
||||
targetSdk = 34
|
||||
versionCode = 13
|
||||
versionName = "1.1.9"
|
||||
versionCode = 14
|
||||
versionName = "1.2.0"
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
vectorDrawables {
|
||||
|
||||
@@ -181,7 +181,7 @@ class TriggerClient(
|
||||
val baseStatusUrl = device.statusUrl.trim()
|
||||
if (baseStatusUrl.isBlank()) {
|
||||
runOnMain {
|
||||
callback(NodeStatus(isOnline = false, statusText = "NO STATUS URL"))
|
||||
callback(NodeStatus(isOnline = false, statusText = "NO STATUS URL", isReachable = false))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -190,7 +190,7 @@ class TriggerClient(
|
||||
Request.Builder().url(baseStatusUrl).get().build()
|
||||
} catch (e: Exception) {
|
||||
runOnMain {
|
||||
callback(NodeStatus(isOnline = false, statusText = "INVALID URL"))
|
||||
callback(NodeStatus(isOnline = false, statusText = "INVALID URL", isReachable = false))
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -205,7 +205,7 @@ class TriggerClient(
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
val nodeStatus = response.use {
|
||||
val body = it.body?.string().orEmpty().trim()
|
||||
parseNodeStatus(body, it.isSuccessful)
|
||||
parseNodeStatus(body, it.isSuccessful, device.node)
|
||||
}
|
||||
runOnMain { callback(nodeStatus) }
|
||||
}
|
||||
@@ -223,17 +223,37 @@ class TriggerClient(
|
||||
val response = client.newCall(request).execute()
|
||||
response.use {
|
||||
val body = it.body?.string().orEmpty().trim()
|
||||
parseNodeStatus(body, it.isSuccessful)
|
||||
parseNodeStatus(body, it.isSuccessful, device.node)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
NodeStatus(isOnline = false, statusText = "OFFLINE", isReachable = false)
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseNodeStatus(body: String, isHttpSuccess: Boolean): NodeStatus {
|
||||
private fun parseNodeStatus(body: String, isHttpSuccess: Boolean, targetNode: String = ""): NodeStatus {
|
||||
if (body.startsWith("{")) {
|
||||
return try {
|
||||
val json = com.google.gson.JsonParser.parseString(body).asJsonObject
|
||||
val rootJson = com.google.gson.JsonParser.parseString(body).asJsonObject
|
||||
|
||||
// Support both single-node JSON ({"node":"debian", "online":true, ...})
|
||||
// and multi-node dashboard JSON ({"service":"Tailscale Sentinel", "nodes":{"debian":{...}, ...}})
|
||||
val json = if (rootJson.has("nodes") && rootJson.get("nodes").isJsonObject) {
|
||||
val nodesObj = rootJson.getAsJsonObject("nodes")
|
||||
var matchedObj: com.google.gson.JsonObject? = null
|
||||
if (targetNode.isNotBlank()) {
|
||||
for (key in nodesObj.keySet()) {
|
||||
if (key.equals(targetNode, ignoreCase = true)) {
|
||||
matchedObj = nodesObj.getAsJsonObject(key)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// If targetNode not matched or not provided, fallback to root or first entry
|
||||
matchedObj ?: if (nodesObj.size() > 0) nodesObj.entrySet().first().value.asJsonObject else rootJson
|
||||
} else {
|
||||
rootJson
|
||||
}
|
||||
|
||||
val online = when {
|
||||
json.has("online") && json.get("online").isJsonPrimitive && json.get("online").asJsonPrimitive.isBoolean ->
|
||||
json.get("online").asBoolean
|
||||
@@ -264,16 +284,17 @@ class TriggerClient(
|
||||
uptime = uptime,
|
||||
onSinceIst = onSinceIst,
|
||||
lastActiveAgo = lastActiveAgo,
|
||||
lastActiveIst = lastActiveIst
|
||||
lastActiveIst = lastActiveIst,
|
||||
isReachable = true
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
NodeStatus(isOnline = false, statusText = "ERROR")
|
||||
NodeStatus(isOnline = false, statusText = "ERROR", isReachable = true)
|
||||
}
|
||||
} else if (isHttpSuccess) {
|
||||
val isOnline = body.equals("ONLINE", ignoreCase = true)
|
||||
return NodeStatus(isOnline = isOnline, statusText = if (isOnline) "ONLINE" else "OFFLINE")
|
||||
return NodeStatus(isOnline = isOnline, statusText = if (isOnline) "ONLINE" else "OFFLINE", isReachable = true)
|
||||
} else {
|
||||
return NodeStatus(isOnline = false, statusText = "OFFLINE")
|
||||
return NodeStatus(isOnline = false, statusText = "OFFLINE", isReachable = false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,8 @@ class UpdateManager(
|
||||
.connectTimeout(30, TimeUnit.SECONDS)
|
||||
.readTimeout(60, TimeUnit.SECONDS)
|
||||
.build(),
|
||||
val githubRepo: String = "vaishnosingh/server-control-app"
|
||||
val primaryReleasesUrl: String = "https://forge.linuxguy.space/api/v1/repos/vaishnosingh/server-control-app/releases",
|
||||
val fallbackReleasesUrl: String = "https://api.github.com/repos/vaishnosingh/server-control-app/releases"
|
||||
) {
|
||||
private val mainHandler = try {
|
||||
val looper = Looper.getMainLooper()
|
||||
@@ -44,11 +45,27 @@ class UpdateManager(
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously checks GitHub Releases API for the latest published release.
|
||||
* Synchronously checks for updates from the self-hosted Git server (primary),
|
||||
* and automatically falls back to GitHub if the primary server fails or is unreachable.
|
||||
* Suitable for use inside background workers (e.g. WorkManager CoroutineWorker).
|
||||
*/
|
||||
fun checkForUpdateSync(currentVersionName: String): UpdateCheckResult {
|
||||
val url = "https://api.github.com/repos/$githubRepo/releases"
|
||||
// 1. Try Primary: Self-hosted Git server (forge.linuxguy.space)
|
||||
val primaryResult = queryReleasesSync(primaryReleasesUrl, currentVersionName)
|
||||
if (primaryResult is UpdateCheckResult.UpdateAvailable || primaryResult is UpdateCheckResult.UpToDate) {
|
||||
return primaryResult
|
||||
}
|
||||
|
||||
// 2. Fallback: GitHub releases API
|
||||
val fallbackResult = queryReleasesSync(fallbackReleasesUrl, currentVersionName)
|
||||
if (fallbackResult is UpdateCheckResult.UpdateAvailable || fallbackResult is UpdateCheckResult.UpToDate) {
|
||||
return fallbackResult
|
||||
}
|
||||
|
||||
return primaryResult // Return original error if both failed
|
||||
}
|
||||
|
||||
private fun queryReleasesSync(url: String, currentVersionName: String): UpdateCheckResult {
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.addHeader("Accept", "application/vnd.github.v3+json")
|
||||
@@ -63,16 +80,76 @@ class UpdateManager(
|
||||
return UpdateCheckResult.UpToDate
|
||||
}
|
||||
if (!res.isSuccessful) {
|
||||
return UpdateCheckResult.Error("HTTP ${res.code}: Unable to check updates")
|
||||
return UpdateCheckResult.Error("HTTP ${res.code}: Unable to check updates from $url")
|
||||
}
|
||||
val body = res.body?.string().orEmpty()
|
||||
parseReleases(body, currentVersionName)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
UpdateCheckResult.Error("Network error: ${e.localizedMessage}")
|
||||
UpdateCheckResult.Error("Network error ($url): ${e.localizedMessage}")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously checks for updates with primary (self-hosted) -> fallback (GitHub) strategy.
|
||||
*/
|
||||
fun checkForUpdate(
|
||||
currentVersionName: String,
|
||||
callback: (UpdateCheckResult) -> Unit
|
||||
) {
|
||||
// Try Primary first
|
||||
queryReleasesAsync(primaryReleasesUrl, currentVersionName) { primaryResult ->
|
||||
if (primaryResult is UpdateCheckResult.UpdateAvailable || primaryResult is UpdateCheckResult.UpToDate) {
|
||||
runOnMain { callback(primaryResult) }
|
||||
} else {
|
||||
// Primary failed, query GitHub fallback
|
||||
queryReleasesAsync(fallbackReleasesUrl, currentVersionName) { fallbackResult ->
|
||||
if (fallbackResult is UpdateCheckResult.UpdateAvailable || fallbackResult is UpdateCheckResult.UpToDate) {
|
||||
runOnMain { callback(fallbackResult) }
|
||||
} else {
|
||||
runOnMain { callback(primaryResult) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun queryReleasesAsync(
|
||||
url: String,
|
||||
currentVersionName: String,
|
||||
onComplete: (UpdateCheckResult) -> Unit
|
||||
) {
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.addHeader("Accept", "application/vnd.github.v3+json")
|
||||
.addHeader("User-Agent", "ServerControlApp")
|
||||
.get()
|
||||
.build()
|
||||
|
||||
okHttpClient.newCall(request).enqueue(object : Callback {
|
||||
override fun onFailure(call: Call, e: IOException) {
|
||||
onComplete(UpdateCheckResult.Error("Network error: ${e.localizedMessage}"))
|
||||
}
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
response.use { res ->
|
||||
if (res.code == 404) {
|
||||
onComplete(UpdateCheckResult.UpToDate)
|
||||
return
|
||||
}
|
||||
if (!res.isSuccessful) {
|
||||
onComplete(UpdateCheckResult.Error("HTTP ${res.code}: Unable to check updates"))
|
||||
return
|
||||
}
|
||||
|
||||
val body = res.body?.string().orEmpty()
|
||||
val result = parseReleases(body, currentVersionName)
|
||||
onComplete(result)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun parseReleases(body: String, currentVersionName: String): UpdateCheckResult {
|
||||
return try {
|
||||
val jsonElement = JsonParser.parseString(body)
|
||||
@@ -150,50 +227,7 @@ class UpdateManager(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks GitHub Releases API for the latest published release.
|
||||
*/
|
||||
fun checkForUpdate(
|
||||
currentVersionName: String,
|
||||
callback: (UpdateCheckResult) -> Unit
|
||||
) {
|
||||
// Query releases list so we always inspect the highest semantic version
|
||||
val url = "https://api.github.com/repos/$githubRepo/releases"
|
||||
|
||||
val request = Request.Builder()
|
||||
.url(url)
|
||||
.addHeader("Accept", "application/vnd.github.v3+json")
|
||||
.addHeader("User-Agent", "ServerControlApp")
|
||||
.get()
|
||||
.build()
|
||||
|
||||
okHttpClient.newCall(request).enqueue(object : Callback {
|
||||
override fun onFailure(call: Call, e: IOException) {
|
||||
runOnMain {
|
||||
callback(UpdateCheckResult.Error("Network error: ${e.localizedMessage}"))
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResponse(call: Call, response: Response) {
|
||||
response.use { res ->
|
||||
if (res.code == 404) {
|
||||
runOnMain { callback(UpdateCheckResult.UpToDate) }
|
||||
return
|
||||
}
|
||||
if (!res.isSuccessful) {
|
||||
runOnMain {
|
||||
callback(UpdateCheckResult.Error("HTTP ${res.code}: Unable to check updates"))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
val body = res.body?.string().orEmpty()
|
||||
val result = parseReleases(body, currentVersionName)
|
||||
runOnMain { callback(result) }
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads the APK file to the app's cache directory and streams progress updates.
|
||||
|
||||
@@ -159,8 +159,8 @@ class ActionTriggerWorker(
|
||||
NotificationHelper.sendNotification(
|
||||
context = appContext,
|
||||
id = device.id.hashCode(),
|
||||
title = "✅ Server Online: ${device.name}",
|
||||
message = "${device.name} is now online and reachable!",
|
||||
title = "✅ Server Online",
|
||||
message = "${device.name} is now online and reachable.",
|
||||
isSuccess = true
|
||||
)
|
||||
ServerWidgetProvider.updateAllWidgets(appContext)
|
||||
@@ -173,8 +173,8 @@ class ActionTriggerWorker(
|
||||
NotificationHelper.sendNotification(
|
||||
context = appContext,
|
||||
id = device.id.hashCode(),
|
||||
title = "🛑 Server Offline: ${device.name}",
|
||||
message = "${device.name} has shut down and is now offline.",
|
||||
title = "⚠️ Server Offline",
|
||||
message = "${device.name} has powered off or is offline.",
|
||||
isSuccess = false
|
||||
)
|
||||
ServerWidgetProvider.updateAllWidgets(appContext)
|
||||
@@ -182,15 +182,9 @@ class ActionTriggerWorker(
|
||||
}
|
||||
}
|
||||
TriggerAction.REBOOT -> {
|
||||
// No notification for reboot per user instructions
|
||||
if (attempts >= 2 && status.isOnline) {
|
||||
appSettings.setLastStatus(device.id, true)
|
||||
NotificationHelper.sendNotification(
|
||||
context = appContext,
|
||||
id = device.id.hashCode(),
|
||||
title = "✅ Reboot Complete: ${device.name}",
|
||||
message = "${device.name} successfully rebooted and is back online!",
|
||||
isSuccess = true
|
||||
)
|
||||
ServerWidgetProvider.updateAllWidgets(appContext)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ class ServerMonitorWorker(
|
||||
NotificationHelper.sendNotification(
|
||||
context = appContext,
|
||||
id = device.id.hashCode(),
|
||||
title = if (status.isOnline) "✅ Server Back Online" else "⚠️ Server Offline",
|
||||
title = if (status.isOnline) "✅ Server Online" else "⚠️ Server Offline",
|
||||
message = if (status.isOnline) {
|
||||
"${device.name} is now online and reachable."
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user