Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
68faad9a62 | ||
|
|
d7a393e9c5 |
No files matched your search
@@ -11,8 +11,8 @@ android {
|
|||||||
applicationId = "com.lzxnone.tailscalemanager"
|
applicationId = "com.lzxnone.tailscalemanager"
|
||||||
minSdk = 28
|
minSdk = 28
|
||||||
targetSdk = 36
|
targetSdk = 36
|
||||||
versionCode = 101
|
versionCode = 103
|
||||||
versionName = "1.0.1"
|
versionName = "1.0.3"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,21 @@
|
|||||||
package com.lzxnone.tailscalemanager.core.update
|
package com.lzxnone.tailscalemanager.core.update
|
||||||
|
|
||||||
|
import com.lzxnone.tailscalemanager.BuildConfig
|
||||||
import com.lzxnone.tailscalemanager.model.AppUpdateInfo
|
import com.lzxnone.tailscalemanager.model.AppUpdateInfo
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import org.json.JSONArray
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
import java.io.InputStreamReader
|
import java.io.InputStreamReader
|
||||||
import java.net.HttpURLConnection
|
import java.net.HttpURLConnection
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
import java.util.Locale
|
||||||
|
|
||||||
object AppUpdateManager {
|
object AppUpdateManager {
|
||||||
|
|
||||||
private const val UPDATE_URL = "https://update.lzxnone.cloud/tailscale_manager/version.json"
|
private const val UPDATE_URL =
|
||||||
|
"https://gitea.lzxnone.cloud/api/v1/repos/lzxnone/TailscaleManager/releases/latest"
|
||||||
|
|
||||||
suspend fun checkAppUpdate(): Result<AppUpdateInfo> = withContext(Dispatchers.IO) {
|
suspend fun checkAppUpdate(): Result<AppUpdateInfo> = withContext(Dispatchers.IO) {
|
||||||
try {
|
try {
|
||||||
@@ -30,22 +34,7 @@ object AppUpdateManager {
|
|||||||
val response = reader.use { it.readText() }
|
val response = reader.use { it.readText() }
|
||||||
connection.disconnect()
|
connection.disconnect()
|
||||||
|
|
||||||
val json = JSONObject(response)
|
Result.success(parseReleaseJson(response))
|
||||||
val versionCode = json.optLong("versionCode", 0L)
|
|
||||||
val versionName = json.optString("versionName", "")
|
|
||||||
val changelog = json.optString("changelog", "")
|
|
||||||
val downloadUrl = json.optString("downloadUrl", "")
|
|
||||||
val apkSize = json.optString("apkSize", "")
|
|
||||||
|
|
||||||
Result.success(
|
|
||||||
AppUpdateInfo(
|
|
||||||
versionCode = versionCode,
|
|
||||||
versionName = versionName,
|
|
||||||
changelog = changelog,
|
|
||||||
downloadUrl = downloadUrl,
|
|
||||||
apkSize = apkSize
|
|
||||||
)
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
connection.disconnect()
|
connection.disconnect()
|
||||||
Result.failure(Exception("HTTP error $responseCode"))
|
Result.failure(Exception("HTTP error $responseCode"))
|
||||||
@@ -54,4 +43,80 @@ object AppUpdateManager {
|
|||||||
Result.failure(e)
|
Result.failure(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal fun parseReleaseJson(response: String): AppUpdateInfo {
|
||||||
|
val json = JSONObject(response)
|
||||||
|
val rawTag = json.optString("tag_name", "").trim()
|
||||||
|
val versionName = rawTag.removePrefix("v").removePrefix("V")
|
||||||
|
val versionCode = parseVersionCode(versionName)
|
||||||
|
val changelog = json.optString("body", "").ifBlank {
|
||||||
|
json.optString("name", "")
|
||||||
|
}
|
||||||
|
|
||||||
|
val (downloadUrl, apkSize) = selectApkAsset(
|
||||||
|
assets = json.optJSONArray("assets"),
|
||||||
|
fallbackUrl = json.optString("html_url", "")
|
||||||
|
)
|
||||||
|
|
||||||
|
return AppUpdateInfo(
|
||||||
|
versionCode = versionCode,
|
||||||
|
versionName = versionName,
|
||||||
|
changelog = changelog,
|
||||||
|
downloadUrl = downloadUrl,
|
||||||
|
apkSize = apkSize,
|
||||||
|
hasUpdate = isNewerVersion(versionName, BuildConfig.VERSION_NAME)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun selectApkAsset(assets: JSONArray?, fallbackUrl: String): Pair<String, String> {
|
||||||
|
if (assets == null || assets.length() == 0) {
|
||||||
|
return fallbackUrl to ""
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i in 0 until assets.length()) {
|
||||||
|
val asset = assets.optJSONObject(i) ?: continue
|
||||||
|
val name = asset.optString("name", "")
|
||||||
|
val downloadUrl = asset.optString("browser_download_url", "")
|
||||||
|
val size = asset.optLong("size", 0L)
|
||||||
|
if (name.endsWith(".apk", ignoreCase = true) && downloadUrl.isNotBlank()) {
|
||||||
|
val sizeText = if (size > 0L) {
|
||||||
|
String.format(Locale.US, "%.2f MB", size / (1024.0 * 1024.0))
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
return downloadUrl to sizeText
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fallbackUrl to ""
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun isNewerVersion(remoteVersion: String, currentVersion: String): Boolean {
|
||||||
|
val remoteParts = parseVersionParts(remoteVersion)
|
||||||
|
val currentParts = parseVersionParts(currentVersion)
|
||||||
|
if (remoteParts.isEmpty()) return false
|
||||||
|
|
||||||
|
val maxLen = maxOf(remoteParts.size, currentParts.size)
|
||||||
|
for (i in 0 until maxLen) {
|
||||||
|
val r = remoteParts.getOrElse(i) { 0 }
|
||||||
|
val c = currentParts.getOrElse(i) { 0 }
|
||||||
|
if (r != c) return r > c
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun parseVersionCode(versionName: String): Long {
|
||||||
|
val parts = parseVersionParts(versionName)
|
||||||
|
if (parts.isEmpty()) return 0L
|
||||||
|
val major = parts.getOrElse(0) { 0 }
|
||||||
|
val minor = parts.getOrElse(1) { 0 }
|
||||||
|
val patch = parts.getOrElse(2) { 0 }
|
||||||
|
return major * 10000L + minor * 100L + patch
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun parseVersionParts(version: String): List<Int> {
|
||||||
|
val cleaned = version.trim().removePrefix("v").removePrefix("V")
|
||||||
|
if (cleaned.isBlank()) return emptyList()
|
||||||
|
return cleaned.split(".")
|
||||||
|
.map { segment -> segment.takeWhile { it.isDigit() }.toIntOrNull() ?: 0 }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.lzxnone.tailscalemanager.core.util
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
|
import android.widget.Toast
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safely opens an external URL in the browser even when called from a wrapped
|
||||||
|
* configuration Context rather than a direct Activity Context.
|
||||||
|
*/
|
||||||
|
fun openExternalUrl(context: Context, url: String): Boolean {
|
||||||
|
if (url.isBlank()) return false
|
||||||
|
return runCatching {
|
||||||
|
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
|
||||||
|
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
}
|
||||||
|
context.startActivity(intent)
|
||||||
|
true
|
||||||
|
}.onFailure { error ->
|
||||||
|
val msg = error.localizedMessage ?: "Unable to open URL"
|
||||||
|
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
|
||||||
|
}.getOrDefault(false)
|
||||||
|
}
|
||||||
@@ -5,5 +5,6 @@ data class AppUpdateInfo(
|
|||||||
val versionName: String,
|
val versionName: String,
|
||||||
val changelog: String,
|
val changelog: String,
|
||||||
val downloadUrl: String,
|
val downloadUrl: String,
|
||||||
val apkSize: String
|
val apkSize: String,
|
||||||
|
val hasUpdate: Boolean = false
|
||||||
)
|
)
|
||||||
@@ -3,9 +3,8 @@ package com.lzxnone.tailscalemanager.ui.components
|
|||||||
import android.content.ClipData
|
import android.content.ClipData
|
||||||
import android.content.ClipboardManager
|
import android.content.ClipboardManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
|
||||||
import android.net.Uri
|
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
import com.lzxnone.tailscalemanager.core.util.openExternalUrl
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
@@ -119,8 +118,7 @@ fun LoginDialog(
|
|||||||
|
|
||||||
Button(
|
Button(
|
||||||
onClick = {
|
onClick = {
|
||||||
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(loginUrl))
|
openExternalUrl(context, loginUrl)
|
||||||
context.startActivity(browserIntent)
|
|
||||||
},
|
},
|
||||||
modifier = Modifier.weight(1f)
|
modifier = Modifier.weight(1f)
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
package com.lzxnone.tailscalemanager.ui.screens
|
package com.lzxnone.tailscalemanager.ui.screens
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import android.net.Uri
|
|
||||||
import androidx.compose.foundation.BorderStroke
|
import androidx.compose.foundation.BorderStroke
|
||||||
|
import com.lzxnone.tailscalemanager.core.util.openExternalUrl
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
@@ -262,8 +261,7 @@ fun AccountScreen(
|
|||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.clip(RoundedCornerShape(12.dp))
|
.clip(RoundedCornerShape(12.dp))
|
||||||
.clickable {
|
.clickable {
|
||||||
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://login.tailscale.com/admin"))
|
openExternalUrl(context, "https://login.tailscale.com/admin")
|
||||||
context.startActivity(browserIntent)
|
|
||||||
}
|
}
|
||||||
.padding(vertical = 12.dp),
|
.padding(vertical = 12.dp),
|
||||||
verticalAlignment = Alignment.CenterVertically
|
verticalAlignment = Alignment.CenterVertically
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.lzxnone.tailscalemanager.ui.screens
|
package com.lzxnone.tailscalemanager.ui.screens
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import android.net.Uri
|
|
||||||
import androidx.activity.compose.BackHandler
|
import androidx.activity.compose.BackHandler
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
@@ -37,6 +35,7 @@ import com.lzxnone.tailscalemanager.ui.components.DeleteConfirmDialog
|
|||||||
import com.lzxnone.tailscalemanager.ui.components.EditHostnameDialog
|
import com.lzxnone.tailscalemanager.ui.components.EditHostnameDialog
|
||||||
import com.lzxnone.tailscalemanager.ui.components.ExitNodeBottomSheet
|
import com.lzxnone.tailscalemanager.ui.components.ExitNodeBottomSheet
|
||||||
import com.lzxnone.tailscalemanager.ui.components.LoginDialog
|
import com.lzxnone.tailscalemanager.ui.components.LoginDialog
|
||||||
|
import com.lzxnone.tailscalemanager.core.util.openExternalUrl
|
||||||
import com.lzxnone.tailscalemanager.ui.components.LogoutConfirmDialog
|
import com.lzxnone.tailscalemanager.ui.components.LogoutConfirmDialog
|
||||||
import com.lzxnone.tailscalemanager.ui.components.ModuleSyncDialog
|
import com.lzxnone.tailscalemanager.ui.components.ModuleSyncDialog
|
||||||
import com.lzxnone.tailscalemanager.ui.components.RootMissingDialog
|
import com.lzxnone.tailscalemanager.ui.components.RootMissingDialog
|
||||||
@@ -241,10 +240,7 @@ fun MainScreen(
|
|||||||
AppUpdateDialog(
|
AppUpdateDialog(
|
||||||
updateInfo = appUpdateInfo,
|
updateInfo = appUpdateInfo,
|
||||||
onDownload = { url ->
|
onDownload = { url ->
|
||||||
try {
|
openExternalUrl(context, url)
|
||||||
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
|
|
||||||
context.startActivity(intent)
|
|
||||||
} catch (_: Exception) {}
|
|
||||||
viewModel.dismissAppUpdate()
|
viewModel.dismissAppUpdate()
|
||||||
},
|
},
|
||||||
onDismiss = { viewModel.dismissAppUpdate() },
|
onDismiss = { viewModel.dismissAppUpdate() },
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ class MainViewModel : ViewModel() {
|
|||||||
if (appUpdateResult.isSuccess) {
|
if (appUpdateResult.isSuccess) {
|
||||||
val info = appUpdateResult.getOrNull()
|
val info = appUpdateResult.getOrNull()
|
||||||
val suppressedVersion = AppPreferences.getSuppressedAppUpdateVersion()
|
val suppressedVersion = AppPreferences.getSuppressedAppUpdateVersion()
|
||||||
if (info != null && info.versionCode > BuildConfig.VERSION_CODE && info.versionCode != suppressedVersion) {
|
if (info != null && info.hasUpdate && info.versionCode != suppressedVersion) {
|
||||||
_appUpdateInfo.value = info
|
_appUpdateInfo.value = info
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -417,7 +417,7 @@ class MainViewModel : ViewModel() {
|
|||||||
val result = AppUpdateManager.checkAppUpdate()
|
val result = AppUpdateManager.checkAppUpdate()
|
||||||
if (result.isSuccess) {
|
if (result.isSuccess) {
|
||||||
val info = result.getOrNull()
|
val info = result.getOrNull()
|
||||||
if (info != null && info.versionCode > BuildConfig.VERSION_CODE) {
|
if (info != null && info.hasUpdate) {
|
||||||
_appUpdateInfo.value = info
|
_appUpdateInfo.value = info
|
||||||
} else {
|
} else {
|
||||||
android.widget.Toast.makeText(
|
android.widget.Toast.makeText(
|
||||||
|
|||||||
Reference in new issue
Block a user