feat: Add app logo & badges to README, implement auto update check and UpdateDialog

This commit is contained in:
LZX committed 2026-10-19 22:28:00 +08:00
1 parent 463e69bc4b
commit 4e77cfa372
11 files changed
+438 -67

No files matched your search

+15 -1
View File
@@ -1,6 +1,20 @@
<div align="center">
<img src="assets/logo.png" width="128" height="128" alt="OpenLoc Logo" />
# OpenLoc
OpenLoc 是一款专为 Android 平台设计的现代地理位置模拟与隐私辅助工具。
专为 Android 平台设计的现代地理位置模拟与隐私辅助工具
<p align="center">
<img src="https://img.shields.io/badge/Platform-Android-3DDC84?style=flat-square&logo=android&logoColor=white" alt="Platform" />
<img src="https://img.shields.io/badge/Language-Kotlin-7F52FF?style=flat-square&logo=kotlin&logoColor=white" alt="Language" />
<img src="https://img.shields.io/badge/UI-Jetpack_Compose_/_Material_3-4285F4?style=flat-square&logo=jetpackcompose&logoColor=white" alt="UI" />
<img src="https://img.shields.io/badge/Privacy-Local_Only-00C853?style=flat-square" alt="Privacy" />
<img src="https://img.shields.io/badge/License-GPL--3.0-007AFF?style=flat-square" alt="License" />
</p>
</div>
---
@@ -0,0 +1,103 @@
package com.lzxnone.openloc.core.util
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL
/**
* 版本更新信息
*/
data class UpdateInfo(
val hasUpdate: Boolean,
val latestVersion: String,
val currentVersion: String,
val releaseTitle: String,
val changelog: String,
val releaseUrl: String
)
/**
* GitHub Releases 版本更新检查器
*/
object UpdateChecker {
private const val GITHUB_API_URL = "https://api.github.com/repos/lzxnone/openloc/releases/latest"
/**
* 检查 GitHub 是否有新版本
*/
suspend fun checkUpdate(currentVersion: String = "v1.0.0"): Result<UpdateInfo> = withContext(Dispatchers.IO) {
try {
val url = URL(GITHUB_API_URL)
val conn = (url.openConnection() as HttpURLConnection).apply {
requestMethod = "GET"
connectTimeout = 8000
readTimeout = 8000
setRequestProperty("Accept", "application/vnd.github.v3+json")
setRequestProperty("User-Agent", "OpenLoc-App")
}
val code = conn.responseCode
if (code == 200) {
val responseText = conn.inputStream.bufferedReader().use { it.readText() }
val json = JSONObject(responseText)
val tagName = json.optString("tag_name", "").trim()
val title = json.optString("name", tagName).trim()
val body = json.optString("body", "").trim()
val htmlUrl = json.optString("html_url", "https://github.com/lzxnone/openloc/releases").trim()
val hasUpdate = isNewerVersion(tagName, currentVersion)
Result.success(
UpdateInfo(
hasUpdate = hasUpdate,
latestVersion = tagName.ifEmpty { currentVersion },
currentVersion = currentVersion,
releaseTitle = title.ifEmpty { tagName },
changelog = body,
releaseUrl = htmlUrl
)
)
} else if (code == 404) {
// 仓库尚未发布任何 Release 时 GitHub 会返回 404
Result.success(
UpdateInfo(
hasUpdate = false,
latestVersion = currentVersion,
currentVersion = currentVersion,
releaseTitle = "",
changelog = "",
releaseUrl = "https://github.com/lzxnone/openloc/releases"
)
)
} else {
Result.failure(Exception("HTTP $code"))
}
} catch (e: Exception) {
Result.failure(e)
}
}
/**
* 比较版本号:remote > current 则返回 true
*/
fun isNewerVersion(remoteTag: String, currentVer: String): Boolean {
if (remoteTag.isBlank()) return false
val cleanRemote = remoteTag.trim().removePrefix("v").removePrefix("V")
val cleanCurrent = currentVer.trim().removePrefix("v").removePrefix("V")
val remoteParts = cleanRemote.split(".").mapNotNull { it.toIntOrNull() }
val currentParts = cleanCurrent.split(".").mapNotNull { it.toIntOrNull() }
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 true
if (r < c) return false
}
return false
}
}
@@ -114,10 +114,15 @@ object AppPreferences {
private const val KEY_ENABLE_SENSOR_SIMULATION = "enable_sensor_simulation"
private const val KEY_ENABLE_STEALTH = "enable_stealth"
private const val KEY_ENABLE_DEV_CAMOUFLAGE = "enable_dev_camouflage"
private const val KEY_AUTO_CHECK_UPDATE = "auto_check_update"
var appLanguage by mutableStateOf(AppLanguage.SYSTEM)
private set
var isAutoCheckUpdateEnabled by mutableStateOf(true)
private set
var searchProvider by mutableStateOf(SearchProvider.AMAP)
private set
@@ -221,8 +226,10 @@ object AppPreferences {
isSensorSimulationEnabled = prefs.getBoolean(KEY_ENABLE_SENSOR_SIMULATION, true)
isStealthEnabled = prefs.getBoolean(KEY_ENABLE_STEALTH, true)
isDevCamouflageEnabled = prefs.getBoolean(KEY_ENABLE_DEV_CAMOUFLAGE, true)
isAutoCheckUpdateEnabled = prefs.getBoolean(KEY_AUTO_CHECK_UPDATE, true)
}
fun updateMockLocation(latitude: Double, longitude: Double, name: String = "", isUserManual: Boolean = true) {
mockLatitude = latitude
mockLongitude = longitude
@@ -498,4 +505,10 @@ object AppPreferences {
isDevCamouflageEnabled = enabled
prefs.edit().putBoolean(KEY_ENABLE_DEV_CAMOUFLAGE, enabled).apply()
}
fun updateAutoCheckUpdateEnabled(enabled: Boolean) {
isAutoCheckUpdateEnabled = enabled
prefs.edit().putBoolean(KEY_AUTO_CHECK_UPDATE, enabled).apply()
}
}
@@ -0,0 +1,120 @@
package com.lzxnone.openloc.ui.components
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.NewReleases
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.lzxnone.openloc.R
import com.lzxnone.openloc.core.util.UpdateInfo
/**
* 新版本更新弹窗 (Material 3)
*/
@Composable
fun UpdateDialog(
updateInfo: UpdateInfo,
onDismiss: () -> Unit
) {
val context = LocalContext.current
val scrollState = rememberScrollState()
AlertDialog(
onDismissRequest = onDismiss,
icon = {
Icon(
imageVector = Icons.Outlined.NewReleases,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(32.dp)
)
},
title = {
Text(
text = stringResource(id = R.string.home_update_available_title, updateInfo.latestVersion),
style = MaterialTheme.typography.titleLarge.copy(
fontWeight = FontWeight.Bold
)
)
},
text = {
Column(
modifier = Modifier
.fillMaxWidth()
.heightIn(max = 280.dp)
.verticalScroll(scrollState)
) {
if (updateInfo.releaseTitle.isNotBlank() && updateInfo.releaseTitle != updateInfo.latestVersion) {
Text(
text = updateInfo.releaseTitle,
style = MaterialTheme.typography.titleMedium.copy(
fontWeight = FontWeight.SemiBold
),
color = MaterialTheme.colorScheme.onSurface
)
Spacer(modifier = Modifier.height(8.dp))
}
if (updateInfo.changelog.isNotBlank()) {
Text(
text = updateInfo.changelog,
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
} else {
Text(
text = stringResource(id = R.string.home_update_dialog_msg),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
},
confirmButton = {
Button(
onClick = {
openBrowser(context, updateInfo.releaseUrl)
onDismiss()
}
) {
Text(text = stringResource(id = R.string.home_update_btn_go))
}
},
dismissButton = {
TextButton(onClick = onDismiss) {
Text(text = stringResource(id = R.string.home_update_btn_later))
}
}
)
}
private fun openBrowser(context: Context, url: String) {
try {
val targetUri = Uri.parse(url.ifBlank { "https://github.com/lzxnone/openloc/releases" })
val intent = Intent(Intent.ACTION_VIEW, targetUri).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
} catch (_: Exception) {
}
}
@@ -24,12 +24,22 @@ import com.lzxnone.openloc.ui.screens.home.HomeScreen
import com.lzxnone.openloc.ui.screens.location.LocationScreen
import com.lzxnone.openloc.ui.screens.settings.SettingsScreen
import android.widget.Toast
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.lzxnone.openloc.R
import com.lzxnone.openloc.core.util.UpdateChecker
import com.lzxnone.openloc.core.util.UpdateInfo
import com.lzxnone.openloc.data.preference.AppMockMode
import com.lzxnone.openloc.ui.components.UpdateDialog
import com.lzxnone.openloc.ui.screens.apps.AppSelectionScreen
import kotlinx.coroutines.launch
/**
* 根容器页面:承载 TopAppBar、底部导航栏与各个 Tab 内容切换
@@ -38,13 +48,49 @@ import com.lzxnone.openloc.ui.screens.apps.AppSelectionScreen
fun MainScreen(
modifier: Modifier = Modifier
) {
val context = LocalContext.current
val coroutineScope = rememberCoroutineScope()
var currentTab by rememberSaveable { mutableStateOf(NavTab.HOME) }
var activeAppSelectionMode by rememberSaveable { mutableStateOf<AppMockMode?>(null) }
var updateInfoToDisplay by remember { mutableStateOf<UpdateInfo?>(null) }
// 响应式的总开关状态与激活状态
val isServiceEnabled = AppPreferences.isServiceEnabled
val isModuleActive = remember { HookStatus.isModuleActive() }
// 每次进入软件,若开启了自动检查更新,则在后台静默检测,发现新版才弹窗
LaunchedEffect(Unit) {
if (AppPreferences.isAutoCheckUpdateEnabled) {
val result = UpdateChecker.checkUpdate(currentVersion = "v1.0.0")
result.getOrNull()?.let { info ->
if (info.hasUpdate) {
updateInfoToDisplay = info
}
}
}
}
val checkingUpdateText = stringResource(id = R.string.home_checking_update)
val checkFailedText = stringResource(id = R.string.home_check_update_failed)
val alreadyLatestText = stringResource(id = R.string.home_already_latest, "v1.0.0")
val performManualCheck: () -> Unit = {
Toast.makeText(context, checkingUpdateText, Toast.LENGTH_SHORT).show()
coroutineScope.launch {
val result = UpdateChecker.checkUpdate(currentVersion = "v1.0.0")
result.onSuccess { info ->
if (info.hasUpdate) {
updateInfoToDisplay = info
} else {
Toast.makeText(context, alreadyLatestText, Toast.LENGTH_SHORT).show()
}
}.onFailure {
Toast.makeText(context, checkFailedText, Toast.LENGTH_SHORT).show()
}
}
}
Box(modifier = modifier.fillMaxSize()) {
Scaffold(
modifier = Modifier.fillMaxSize(),
@@ -77,7 +123,8 @@ fun MainScreen(
AppPreferences.updateServiceEnabled(enabled)
},
onOpenBlacklist = { activeAppSelectionMode = AppMockMode.BLACKLIST },
onOpenWhitelist = { activeAppSelectionMode = AppMockMode.WHITELIST }
onOpenWhitelist = { activeAppSelectionMode = AppMockMode.WHITELIST },
onCheckUpdate = performManualCheck
)
NavTab.LOCATION -> LocationScreen()
NavTab.SETTINGS -> SettingsScreen()
@@ -99,5 +146,14 @@ fun MainScreen(
)
}
}
// 新版本更新弹窗
updateInfoToDisplay?.let { info ->
UpdateDialog(
updateInfo = info,
onDismiss = { updateInfoToDisplay = null }
)
}
}
}
@@ -22,6 +22,7 @@ fun HomeScreen(
onServiceEnabledChange: (Boolean) -> Unit,
onOpenBlacklist: () -> Unit,
onOpenWhitelist: () -> Unit,
onCheckUpdate: () -> Unit,
modifier: Modifier = Modifier
) {
val scrollState = rememberScrollState()
@@ -57,8 +58,12 @@ fun HomeScreen(
// 4. 版本与更新模块卡片
VersionCard(
versionName = "v1.0.0"
versionName = "v1.0.0",
isAutoCheckEnabled = AppPreferences.isAutoCheckUpdateEnabled,
onAutoCheckChange = { AppPreferences.updateAutoCheckUpdateEnabled(it) },
onCheckUpdate = onCheckUpdate
)
}
}
@@ -1,6 +1,5 @@
package com.lzxnone.openloc.ui.screens.home
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
@@ -20,32 +19,33 @@ import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.lzxnone.openloc.R
import com.lzxnone.openloc.data.preference.AppPreferences
/**
* 主页底部版本模块卡片
* 显示应用当前版本(如 v1.0.0)与“检查更新”按钮
* 显示应用当前版本(如 v1.0.0)、“检查更新”按钮,以及“启动时自动检查更新”开关
*/
@Composable
fun VersionCard(
versionName: String = stringResource(id = R.string.home_version_name),
onCheckUpdate: (() -> Unit)? = null,
isAutoCheckEnabled: Boolean = AppPreferences.isAutoCheckUpdateEnabled,
onAutoCheckChange: (Boolean) -> Unit = { AppPreferences.updateAutoCheckUpdateEnabled(it) },
onCheckUpdate: () -> Unit,
modifier: Modifier = Modifier
) {
val context = LocalContext.current
val placeholderToast = stringResource(id = R.string.home_check_updates_placeholder)
Card(
modifier = modifier.fillMaxWidth(),
shape = RoundedCornerShape(16.dp),
@@ -56,78 +56,112 @@ fun VersionCard(
defaultElevation = 1.dp
)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp, vertical = 16.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Column(modifier = Modifier.fillMaxWidth()) {
// 上半部分:版本信息与手动检查更新按钮
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp, vertical = 16.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.weight(1f)
horizontalArrangement = Arrangement.SpaceBetween
) {
Surface(
shape = CircleShape,
color = MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.5f),
modifier = Modifier.size(40.dp)
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.weight(1f)
) {
Box(contentAlignment = Alignment.Center) {
Icon(
imageVector = Icons.Outlined.Info,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(22.dp)
Surface(
shape = CircleShape,
color = MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.5f),
modifier = Modifier.size(40.dp)
) {
Box(contentAlignment = Alignment.Center) {
Icon(
imageVector = Icons.Outlined.Info,
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(22.dp)
)
}
}
Spacer(modifier = Modifier.width(14.dp))
Column {
Text(
text = stringResource(id = R.string.home_version_card_title),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
Spacer(modifier = Modifier.height(2.dp))
Text(
text = versionName,
style = MaterialTheme.typography.titleMedium.copy(
fontWeight = FontWeight.Bold
),
color = MaterialTheme.colorScheme.onSurface
)
}
}
Spacer(modifier = Modifier.width(14.dp))
Spacer(modifier = Modifier.width(12.dp))
Column {
Text(
text = stringResource(id = R.string.home_version_card_title),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
FilledTonalButton(
onClick = onCheckUpdate,
shape = RoundedCornerShape(12.dp),
colors = ButtonDefaults.filledTonalButtonColors(
containerColor = MaterialTheme.colorScheme.secondaryContainer,
contentColor = MaterialTheme.colorScheme.onSecondaryContainer
)
Spacer(modifier = Modifier.height(2.dp))
) {
Icon(
imageVector = Icons.Outlined.Refresh,
contentDescription = null,
modifier = Modifier.size(16.dp)
)
Spacer(modifier = Modifier.width(6.dp))
Text(
text = versionName,
style = MaterialTheme.typography.titleMedium.copy(
fontWeight = FontWeight.Bold
),
color = MaterialTheme.colorScheme.onSurface
text = stringResource(id = R.string.home_check_updates_btn),
style = MaterialTheme.typography.labelLarge.copy(
fontWeight = FontWeight.Medium
)
)
}
}
Spacer(modifier = Modifier.width(12.dp))
HorizontalDivider(
modifier = Modifier.padding(horizontal = 20.dp),
color = MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.4f)
)
FilledTonalButton(
onClick = {
if (onCheckUpdate != null) {
onCheckUpdate()
} else {
Toast.makeText(context, placeholderToast, Toast.LENGTH_SHORT).show()
}
},
shape = RoundedCornerShape(12.dp),
colors = ButtonDefaults.filledTonalButtonColors(
containerColor = MaterialTheme.colorScheme.secondaryContainer,
contentColor = MaterialTheme.colorScheme.onSecondaryContainer
)
// 下半部分:自动检查更新选项(默认开启)
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp, vertical = 14.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Icon(
imageVector = Icons.Outlined.Refresh,
contentDescription = null,
modifier = Modifier.size(16.dp)
)
Spacer(modifier = Modifier.width(6.dp))
Text(
text = stringResource(id = R.string.home_check_updates_btn),
style = MaterialTheme.typography.labelLarge.copy(
fontWeight = FontWeight.Medium
Column(modifier = Modifier.weight(1f)) {
Text(
text = stringResource(id = R.string.home_auto_check_update_title),
style = MaterialTheme.typography.titleSmall.copy(
fontWeight = FontWeight.SemiBold
),
color = MaterialTheme.colorScheme.onSurface
)
Spacer(modifier = Modifier.height(2.dp))
Text(
text = stringResource(id = R.string.home_auto_check_update_desc),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
Spacer(modifier = Modifier.width(16.dp))
Switch(
checked = isAutoCheckEnabled,
onCheckedChange = onAutoCheckChange
)
}
}
+10 -1
View File
@@ -171,6 +171,15 @@
<string name="home_version_card_title">当前版本</string>
<string name="home_version_name">v1.0.0</string>
<string name="home_check_updates_btn">检查更新</string>
<string name="home_check_updates_placeholder">当前已是最新版本</string>
<string name="home_auto_check_update_title">启动时自动检查更新</string>
<string name="home_auto_check_update_desc">每次进入软件自动联网检测 GitHub 最新版本</string>
<string name="home_checking_update">正在检查更新…</string>
<string name="home_update_available_title">发现新版本: %1$s</string>
<string name="home_update_dialog_msg">OpenLoc 已在 GitHub 发布最新版本,欢迎前往下载更新。</string>
<string name="home_update_btn_go">前往更新</string>
<string name="home_update_btn_later">稍后</string>
<string name="home_check_update_failed">检查更新失败,请检查网络</string>
<string name="home_already_latest">当前已是最新版本 (%1$s)</string>
</resources>
+10 -1
View File
@@ -171,6 +171,15 @@
<string name="home_version_card_title">当前版本</string>
<string name="home_version_name">v1.0.0</string>
<string name="home_check_updates_btn">检查更新</string>
<string name="home_check_updates_placeholder">当前已是最新版本</string>
<string name="home_auto_check_update_title">启动时自动检查更新</string>
<string name="home_auto_check_update_desc">每次进入软件自动联网检测 GitHub 最新版本</string>
<string name="home_checking_update">正在检查更新…</string>
<string name="home_update_available_title">发现新版本: %1$s</string>
<string name="home_update_dialog_msg">OpenLoc 已在 GitHub 发布最新版本,欢迎前往下载更新。</string>
<string name="home_update_btn_go">前往更新</string>
<string name="home_update_btn_later">稍后</string>
<string name="home_check_update_failed">检查更新失败,请检查网络</string>
<string name="home_already_latest">当前已是最新版本 (%1$s)</string>
</resources>
+9 -1
View File
@@ -171,5 +171,13 @@
<string name="home_version_card_title">Current Version</string>
<string name="home_version_name">v1.0.0</string>
<string name="home_check_updates_btn">Check for Updates</string>
<string name="home_check_updates_placeholder">Already the latest version</string>
<string name="home_auto_check_update_title">Auto Check on Launch</string>
<string name="home_auto_check_update_desc">Automatically check for new releases when opening the app</string>
<string name="home_checking_update">Checking for updates…</string>
<string name="home_update_available_title">New Version Available: %1$s</string>
<string name="home_update_dialog_msg">A new version of OpenLoc has been released on GitHub.</string>
<string name="home_update_btn_go">Go to Update</string>
<string name="home_update_btn_later">Later</string>
<string name="home_check_update_failed">Failed to check for updates, please check network</string>
<string name="home_already_latest">Already the latest version (%1$s)</string>
</resources>
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB