Skip to content

Commit b2ba3cd

Browse files
authored
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.
1 parent d56d56e commit b2ba3cd

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

app/src/main/java/com/github/kr328/clash/MainApplication.kt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import com.github.kr328.clash.service.util.sendServiceRecreated
1010
import com.github.kr328.clash.util.clashDir
1111
import java.io.File
1212
import java.io.FileOutputStream
13-
import java.io.IOException
14-
import java.io.InputStream
15-
import java.io.OutputStream
1613

1714

1815
@Suppress("unused")
@@ -39,31 +36,41 @@ class MainApplication : Application() {
3936
}
4037

4138
private fun extractGeoFiles() {
42-
clashDir.mkdirs();
39+
clashDir.mkdirs()
4340

41+
val updateDate = packageManager.getPackageInfo(packageName, 0).lastUpdateTime
4442
val geoipFile = File(clashDir, "geoip.metadb")
45-
if(!geoipFile.exists()) {
43+
if (geoipFile.exists() && geoipFile.lastModified() < updateDate) {
44+
geoipFile.delete()
45+
}
46+
if (!geoipFile.exists()) {
4647
FileOutputStream(geoipFile).use {
47-
assets.open("geoip.metadb").copyTo(it);
48+
assets.open("geoip.metadb").copyTo(it)
4849
}
4950
}
5051

5152
val geositeFile = File(clashDir, "geosite.dat")
52-
if(!geositeFile.exists()) {
53+
if (geositeFile.exists() && geositeFile.lastModified() < updateDate) {
54+
geositeFile.delete()
55+
}
56+
if (!geositeFile.exists()) {
5357
FileOutputStream(geositeFile).use {
54-
assets.open("geosite.dat").copyTo(it);
58+
assets.open("geosite.dat").copyTo(it)
5559
}
5660
}
57-
58-
val ASNFile = File(clashDir, "ASN.mmdb")
59-
if(!ASNFile.exists()) {
60-
FileOutputStream(ASNFile).use {
61-
assets.open("ASN.mmdb").copyTo(it);
61+
62+
val asnFile = File(clashDir, "ASN.mmdb")
63+
if (asnFile.exists() && asnFile.lastModified() < updateDate) {
64+
asnFile.delete()
65+
}
66+
if (!asnFile.exists()) {
67+
FileOutputStream(asnFile).use {
68+
assets.open("ASN.mmdb").copyTo(it)
6269
}
6370
}
6471
}
6572

6673
fun finalize() {
6774
Global.destroy()
6875
}
69-
}
76+
}

0 commit comments

Comments
 (0)