From b2ba3cd20bacd94386e296492454ef72d6c417da Mon Sep 17 00:00:00 2001 From: Howard Wu Date: Sun, 29 Jun 2025 12:30:01 +0800 Subject: [PATCH] Update geo file after app updates (#557) Delete and replace geo files if the app has been updated since the files were last modified. This ensures bundled geo files are refreshed after app updates. --- .../com/github/kr328/clash/MainApplication.kt | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/github/kr328/clash/MainApplication.kt b/app/src/main/java/com/github/kr328/clash/MainApplication.kt index 7f4ddf74..6bef37f9 100644 --- a/app/src/main/java/com/github/kr328/clash/MainApplication.kt +++ b/app/src/main/java/com/github/kr328/clash/MainApplication.kt @@ -10,9 +10,6 @@ import com.github.kr328.clash.service.util.sendServiceRecreated import com.github.kr328.clash.util.clashDir import java.io.File import java.io.FileOutputStream -import java.io.IOException -import java.io.InputStream -import java.io.OutputStream @Suppress("unused") @@ -39,26 +36,36 @@ class MainApplication : Application() { } private fun extractGeoFiles() { - clashDir.mkdirs(); + clashDir.mkdirs() + val updateDate = packageManager.getPackageInfo(packageName, 0).lastUpdateTime val geoipFile = File(clashDir, "geoip.metadb") - if(!geoipFile.exists()) { + if (geoipFile.exists() && geoipFile.lastModified() < updateDate) { + geoipFile.delete() + } + if (!geoipFile.exists()) { FileOutputStream(geoipFile).use { - assets.open("geoip.metadb").copyTo(it); + assets.open("geoip.metadb").copyTo(it) } } val geositeFile = File(clashDir, "geosite.dat") - if(!geositeFile.exists()) { + if (geositeFile.exists() && geositeFile.lastModified() < updateDate) { + geositeFile.delete() + } + if (!geositeFile.exists()) { FileOutputStream(geositeFile).use { - assets.open("geosite.dat").copyTo(it); + assets.open("geosite.dat").copyTo(it) } } - - val ASNFile = File(clashDir, "ASN.mmdb") - if(!ASNFile.exists()) { - FileOutputStream(ASNFile).use { - assets.open("ASN.mmdb").copyTo(it); + + val asnFile = File(clashDir, "ASN.mmdb") + if (asnFile.exists() && asnFile.lastModified() < updateDate) { + asnFile.delete() + } + if (!asnFile.exists()) { + FileOutputStream(asnFile).use { + assets.open("ASN.mmdb").copyTo(it) } } } @@ -66,4 +73,4 @@ class MainApplication : Application() { fun finalize() { Global.destroy() } -} \ No newline at end of file +}