From 7ca40444679295bd34caf43e2f72c15d77171af8 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 1 Apr 2025 17:24:59 +0800 Subject: [PATCH] Added check for updates --- .../src/main/java/com/v2ray/ang/AppConfig.kt | 2 + .../com/v2ray/ang/dto/CheckUpdateResult.kt | 10 ++ .../java/com/v2ray/ang/dto/GitHubRelease.kt | 23 ++++ .../v2ray/ang/handler/UpdateCheckerManager.kt | 112 ++++++++++++++++++ .../java/com/v2ray/ang/ui/AboutActivity.kt | 39 ++++++ .../drawable-night/ic_check_update_24dp.xml | 11 ++ .../res/drawable/ic_check_update_24dp.xml | 11 ++ .../src/main/res/layout/activity_about.xml | 44 +++++++ .../app/src/main/res/values-ar/strings.xml | 6 + .../app/src/main/res/values-bn/strings.xml | 6 + .../src/main/res/values-bqi-rIR/strings.xml | 6 + .../app/src/main/res/values-fa/strings.xml | 6 + .../app/src/main/res/values-ru/strings.xml | 6 + .../app/src/main/res/values-vi/strings.xml | 18 ++- .../src/main/res/values-zh-rCN/strings.xml | 6 + .../src/main/res/values-zh-rTW/strings.xml | 6 + V2rayNG/app/src/main/res/values/strings.xml | 6 + 17 files changed, 312 insertions(+), 6 deletions(-) create mode 100644 V2rayNG/app/src/main/java/com/v2ray/ang/dto/CheckUpdateResult.kt create mode 100644 V2rayNG/app/src/main/java/com/v2ray/ang/dto/GitHubRelease.kt create mode 100644 V2rayNG/app/src/main/java/com/v2ray/ang/handler/UpdateCheckerManager.kt create mode 100644 V2rayNG/app/src/main/res/drawable-night/ic_check_update_24dp.xml create mode 100644 V2rayNG/app/src/main/res/drawable/ic_check_update_24dp.xml diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt index 7613c318..6d27cea7 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/AppConfig.kt @@ -57,6 +57,7 @@ object AppConfig { const val PREF_LOGLEVEL = "pref_core_loglevel" const val PREF_MODE = "pref_mode" const val PREF_IS_BOOTED = "pref_is_booted" + const val PREF_CHECK_UPDATE_PRE_RELEASE = "pref_check_update_pre_release" /** Cache keys. */ const val CACHE_SUBSCRIPTION_ID = "cache_subscription_id" @@ -93,6 +94,7 @@ object AppConfig { const val v2rayCustomRoutingListUrl = "https://raw.githubusercontent.com/2dust/v2rayCustomRoutingList/master/" const val v2rayNGUrl = "https://github.com/2dust/v2rayNG" + const val v2rayNGAPIUrl = "https://api.github.com/repos/2dust/v2rayNG/releases" const val v2rayNGIssues = "$v2rayNGUrl/issues" const val v2rayNGWikiMode = "$v2rayNGUrl/wiki/Mode" const val v2rayNGPrivacyPolicy = "https://raw.githubusercontent.com/2dust/v2rayNG/master/CR.md" diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/dto/CheckUpdateResult.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/dto/CheckUpdateResult.kt new file mode 100644 index 00000000..be4f62e5 --- /dev/null +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/dto/CheckUpdateResult.kt @@ -0,0 +1,10 @@ +package com.v2ray.ang.dto + +data class CheckUpdateResult( + val hasUpdate: Boolean, + val latestVersion: String? = null, + val releaseNotes: String? = null, + val downloadUrl: String? = null, + val error: String? = null, + val isPreRelease: Boolean = false +) \ No newline at end of file diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/dto/GitHubRelease.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/dto/GitHubRelease.kt new file mode 100644 index 00000000..0a7dce56 --- /dev/null +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/dto/GitHubRelease.kt @@ -0,0 +1,23 @@ +package com.v2ray.ang.dto + +import com.google.gson.annotations.SerializedName + +data class GitHubRelease( + @SerializedName("tag_name") + val tagName: String, + @SerializedName("body") + val body: String, + @SerializedName("assets") + val assets: List, + @SerializedName("prerelease") + val prerelease: Boolean = false, + @SerializedName("published_at") + val publishedAt: String = "" +) { + data class Asset( + @SerializedName("name") + val name: String, + @SerializedName("browser_download_url") + val browserDownloadUrl: String + ) +} \ No newline at end of file diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/handler/UpdateCheckerManager.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/handler/UpdateCheckerManager.kt new file mode 100644 index 00000000..fa5d0bcc --- /dev/null +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/handler/UpdateCheckerManager.kt @@ -0,0 +1,112 @@ +package com.v2ray.ang.handler + +import android.content.Context +import android.os.Build +import android.util.Log +import com.v2ray.ang.AppConfig +import com.v2ray.ang.BuildConfig +import com.v2ray.ang.dto.CheckUpdateResult +import com.v2ray.ang.dto.GitHubRelease +import com.v2ray.ang.util.HttpUtil +import com.v2ray.ang.util.JsonUtil +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import java.io.File +import java.io.FileOutputStream + +object UpdateCheckerManager { + suspend fun checkForUpdate(includePreRelease: Boolean = false): CheckUpdateResult = withContext(Dispatchers.IO) { + try { + val url = if (includePreRelease) { + AppConfig.v2rayNGAPIUrl + } else { + "${AppConfig.v2rayNGAPIUrl}/latest" + } + + var response = HttpUtil.getUrlContent(url, 5000) + if (response.isNullOrEmpty()) { + val httpPort = SettingsManager.getHttpPort() + response = HttpUtil.getUrlContent(url, 5000, httpPort) ?: throw IllegalStateException("Failed to get response") + } + + val latestRelease = if (includePreRelease) { + JsonUtil.fromJson(response, Array::class.java) + .filter { it.prerelease } + .maxByOrNull { it.publishedAt } + ?: throw IllegalStateException("No pre-release found") + } else { + JsonUtil.fromJson(response, GitHubRelease::class.java) + } + + val latestVersion = latestRelease.tagName.removePrefix("v") + Log.i(AppConfig.TAG, "Found new version: $latestVersion (current: ${BuildConfig.VERSION_NAME})") + + return@withContext if (compareVersions(latestVersion, BuildConfig.VERSION_NAME) > 0) { + val downloadUrl = getDownloadUrl(latestRelease, Build.SUPPORTED_ABIS[0]) + CheckUpdateResult( + hasUpdate = true, + latestVersion = latestVersion, + releaseNotes = latestRelease.body, + downloadUrl = downloadUrl, + isPreRelease = latestRelease.prerelease + ) + } else { + CheckUpdateResult(hasUpdate = false) + } + } catch (e: Exception) { + Log.e(AppConfig.TAG, "Failed to check for updates: ${e.message}") + return@withContext CheckUpdateResult(hasUpdate = false, error = e.message) + } + } + + suspend fun downloadApk(context: Context, downloadUrl: String): File? = withContext(Dispatchers.IO) { + try { + val httpPort = SettingsManager.getHttpPort() + val connection = HttpUtil.createProxyConnection(downloadUrl, httpPort, 10000, 10000, true) + ?: throw IllegalStateException("Failed to create connection") + + try { + val apkFile = File(context.cacheDir, "update.apk") + Log.i(AppConfig.TAG, "Downloading APK to: ${apkFile.absolutePath}") + + FileOutputStream(apkFile).use { outputStream -> + connection.inputStream.use { inputStream -> + inputStream.copyTo(outputStream) + } + } + Log.i(AppConfig.TAG, "APK download completed") + return@withContext apkFile + } catch (e: Exception) { + Log.e(AppConfig.TAG, "Failed to download APK: ${e.message}") + return@withContext null + } finally { + try { + connection.disconnect() + } catch (e: Exception) { + Log.e(AppConfig.TAG, "Error closing connection: ${e.message}") + } + } + } catch (e: Exception) { + Log.e(AppConfig.TAG, "Failed to initiate download: ${e.message}") + return@withContext null + } + } + + private fun compareVersions(version1: String, version2: String): Int { + val v1 = version1.split(".") + val v2 = version2.split(".") + + for (i in 0 until maxOf(v1.size, v2.size)) { + val num1 = if (i < v1.size) v1[i].toInt() else 0 + val num2 = if (i < v2.size) v2[i].toInt() else 0 + if (num1 != num2) return num1 - num2 + } + return 0 + } + + private fun getDownloadUrl(release: GitHubRelease, abi: String): String { + return release.assets.find { it.name.contains(abi) }?.browserDownloadUrl + ?: release.assets.firstOrNull()?.browserDownloadUrl + ?: throw IllegalStateException("No compatible APK found") + } +} diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/AboutActivity.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/AboutActivity.kt index 648996bc..280dd945 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/AboutActivity.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/AboutActivity.kt @@ -6,19 +6,25 @@ import android.os.Build import android.os.Bundle import android.util.Log import androidx.activity.result.contract.ActivityResultContracts +import androidx.appcompat.app.AlertDialog import androidx.core.content.ContextCompat import androidx.core.content.FileProvider +import androidx.lifecycle.lifecycleScope import com.tencent.mmkv.MMKV import com.v2ray.ang.AppConfig import com.v2ray.ang.BuildConfig import com.v2ray.ang.R import com.v2ray.ang.databinding.ActivityAboutBinding +import com.v2ray.ang.dto.CheckUpdateResult import com.v2ray.ang.extension.toast import com.v2ray.ang.extension.toastError import com.v2ray.ang.extension.toastSuccess +import com.v2ray.ang.handler.MmkvManager import com.v2ray.ang.handler.SpeedtestManager +import com.v2ray.ang.handler.UpdateCheckerManager import com.v2ray.ang.util.Utils import com.v2ray.ang.util.ZipUtil +import kotlinx.coroutines.launch import java.io.File import java.text.SimpleDateFormat import java.util.Locale @@ -97,6 +103,15 @@ class AboutActivity : BaseActivity() { } } + binding.layoutCheckUpdate.setOnClickListener { + checkForUpdates(binding.checkPreRelease.isChecked) + } + + binding.checkPreRelease.setOnCheckedChangeListener { _, isChecked -> + MmkvManager.encodeSettings(AppConfig.PREF_CHECK_UPDATE_PRE_RELEASE, isChecked) + } + binding.checkPreRelease.isChecked = MmkvManager.decodeSettingsBool(AppConfig.PREF_CHECK_UPDATE_PRE_RELEASE, false) + binding.layoutSoureCcode.setOnClickListener { Utils.openUri(this, AppConfig.v2rayNGUrl) } @@ -197,4 +212,28 @@ class AboutActivity : BaseActivity() { } } } + + private fun checkForUpdates(includePreRelease: Boolean) { + lifecycleScope.launch { + val result = UpdateCheckerManager.checkForUpdate(includePreRelease) + if (result.hasUpdate) { + showUpdateDialog(result) + } else { + toast(R.string.update_already_latest_version) + } + } + } + + private fun showUpdateDialog(result: CheckUpdateResult) { + AlertDialog.Builder(this) + .setTitle(getString(R.string.update_new_version_found, result.latestVersion)) + .setMessage(result.releaseNotes) + .setPositiveButton(R.string.update_now) { _, _ -> + result.downloadUrl?.let { + Utils.openUri(this, it) + } + } + .setNegativeButton(android.R.string.cancel, null) + .show() + } } \ No newline at end of file diff --git a/V2rayNG/app/src/main/res/drawable-night/ic_check_update_24dp.xml b/V2rayNG/app/src/main/res/drawable-night/ic_check_update_24dp.xml new file mode 100644 index 00000000..5a7c2fd0 --- /dev/null +++ b/V2rayNG/app/src/main/res/drawable-night/ic_check_update_24dp.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/V2rayNG/app/src/main/res/drawable/ic_check_update_24dp.xml b/V2rayNG/app/src/main/res/drawable/ic_check_update_24dp.xml new file mode 100644 index 00000000..b0f163c6 --- /dev/null +++ b/V2rayNG/app/src/main/res/drawable/ic_check_update_24dp.xml @@ -0,0 +1,11 @@ + + + + + diff --git a/V2rayNG/app/src/main/res/layout/activity_about.xml b/V2rayNG/app/src/main/res/layout/activity_about.xml index 62053559..0bbeae74 100644 --- a/V2rayNG/app/src/main/res/layout/activity_about.xml +++ b/V2rayNG/app/src/main/res/layout/activity_about.xml @@ -111,6 +111,50 @@ android:orientation="vertical" android:paddingTop="@dimen/padding_spacing_dp16"> + + + + + + + + + + + + + + فاصل الجزء (الحد الأدنى - الحد الأقصى) تفعيل الجزء + Check for update + Already on the latest version + New version found: %s + Update now + Check Pre-release + رمز استجابة سريعة (QRcode) تصدير إلى الحافظة diff --git a/V2rayNG/app/src/main/res/values-bn/strings.xml b/V2rayNG/app/src/main/res/values-bn/strings.xml index 5f37e6de..7de4de1d 100644 --- a/V2rayNG/app/src/main/res/values-bn/strings.xml +++ b/V2rayNG/app/src/main/res/values-bn/strings.xml @@ -313,6 +313,12 @@ ফ্র্যাগমেন্ট ইন্টারভ্যাল (ন্যূনতম-সর্বাধিক) ফ্র্যাগমেন্ট সক্রিয় করুন + Check for update + Already on the latest version + New version found: %s + Update now + Check Pre-release + QR কোড ক্লিপবোর্ডে রপ্তানি করুন diff --git a/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml b/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml index 65d501b1..13ab5ec7 100644 --- a/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml +++ b/V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml @@ -322,6 +322,12 @@ Fragment Interval (min-max) ر وندن Fragment + Check for update + Already on the latest version + New version found: %s + Update now + Check Pre-release + QRcode و در کشیڌن من کلیپ بورد diff --git a/V2rayNG/app/src/main/res/values-fa/strings.xml b/V2rayNG/app/src/main/res/values-fa/strings.xml index 790ed3de..aa0cf5f9 100644 --- a/V2rayNG/app/src/main/res/values-fa/strings.xml +++ b/V2rayNG/app/src/main/res/values-fa/strings.xml @@ -319,6 +319,12 @@ فاصله بین بسته های فرگمنت (حداقل-حداکثر) فعال کردن فرگمنت + Check for update + Already on the latest version + New version found: %s + Update now + Check Pre-release + QRcode خروجی گرفتن در کلیپ‌ بورد diff --git a/V2rayNG/app/src/main/res/values-ru/strings.xml b/V2rayNG/app/src/main/res/values-ru/strings.xml index 46a0a5f3..15607341 100644 --- a/V2rayNG/app/src/main/res/values-ru/strings.xml +++ b/V2rayNG/app/src/main/res/values-ru/strings.xml @@ -321,6 +321,12 @@ Интервал фрагментов (от - до) Использовать фрагментирование + Check for update + Already on the latest version + New version found: %s + Update now + Check Pre-release + QR-код Экспорт в буфер обмена diff --git a/V2rayNG/app/src/main/res/values-vi/strings.xml b/V2rayNG/app/src/main/res/values-vi/strings.xml index 0be072c2..c71d0393 100644 --- a/V2rayNG/app/src/main/res/values-vi/strings.xml +++ b/V2rayNG/app/src/main/res/values-vi/strings.xml @@ -309,6 +309,18 @@ Nhập gói đăng ký thành công! Nhập gói đăng ký không thành công! + Fragment Settings + Fragment Packets + Fragment Length (min-max) + Fragment Interval (min-max) + Enable Fragment + + Check for update + Already on the latest version + New version found: %s + Update now + Check Pre-release + Xuất ra mã QR (Chụp màn hình để lưu) Sao chép vào Clipboard @@ -340,12 +352,6 @@ Sáng Tối - Fragment Settings - Fragment Packets - Fragment Length (min-max) - Fragment Interval (min-max) - Enable Fragment - Follow config Bypass diff --git a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml index dedfdb86..7da94eef 100644 --- a/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rCN/strings.xml @@ -313,6 +313,12 @@ 分片间隔(最小 - 最大) 启用分片(Fragment) + 检查更新 + 目前已是最新版本 + 发现新版本: %s + 立即更新 + 检查 Pre-release + 二维码 导出至剪贴板 diff --git a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml index 8861d19c..013117b9 100644 --- a/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml +++ b/V2rayNG/app/src/main/res/values-zh-rTW/strings.xml @@ -313,6 +313,12 @@ 分片間隔(最小-最大) 啟用分片(Fragment) + 檢查更新 + 当前已是最新版本 + 發現新版本: %s + 立即更新 + 檢查 Pre-release + QR Code 匯出至剪貼簿 diff --git a/V2rayNG/app/src/main/res/values/strings.xml b/V2rayNG/app/src/main/res/values/strings.xml index b085a3df..cd2cfdf4 100644 --- a/V2rayNG/app/src/main/res/values/strings.xml +++ b/V2rayNG/app/src/main/res/values/strings.xml @@ -323,6 +323,12 @@ Fragment Interval (min-max) Enable Fragment + Check for update + Already on the latest version + New version found: %s + Update now + Check Pre-release + QRcode Export to clipboard