forked from xuhaoyang/ClashForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathMainApplication.kt
More file actions
95 lines (79 loc) · 2.67 KB
/
Copy pathMainApplication.kt
File metadata and controls
95 lines (79 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.github.kr328.clash
import android.app.Application
import android.content.Context
import com.github.kr328.clash.common.Global
import com.github.kr328.clash.common.compat.currentProcessName
import com.github.kr328.clash.common.log.Log
import com.github.kr328.clash.remote.Remote
import com.github.kr328.clash.service.util.sendServiceRecreated
import com.github.kr328.clash.util.clashDir
import java.io.File
import java.io.FileOutputStream
@Suppress("unused")
class MainApplication : Application() {
private var wifiAutomator: WifiAutomator? = null
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
Global.init(this)
}
override fun onCreate() {
super.onCreate()
val processName = currentProcessName
extractGeoFiles()
Log.d("Process $processName started")
if (processName == packageName) {
Remote.launch()
wifiAutomator = WifiAutomator(this)
wifiAutomator?.start()
} else {
sendServiceRecreated()
}
}
override fun onTerminate() {
super.onTerminate()
wifiAutomator?.stop()
}
private fun extractGeoFiles() {
clashDir.mkdirs()
val updateDate = packageManager.getPackageInfo(packageName, 0).lastUpdateTime
val geoipFile = File(clashDir, "geoip.metadb")
if (geoipFile.exists() && geoipFile.lastModified() < updateDate) {
geoipFile.delete()
}
if (!geoipFile.exists()) {
FileOutputStream(geoipFile).use {
assets.open("geoip.metadb").copyTo(it)
}
}
val geositeFile = File(clashDir, "geosite.dat")
if (geositeFile.exists() && geositeFile.lastModified() < updateDate) {
geositeFile.delete()
}
if (!geositeFile.exists()) {
FileOutputStream(geositeFile).use {
assets.open("geosite.dat").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)
}
}
val bundleMRSFile = File(clashDir, "BundleMRS.7z")
if (bundleMRSFile.exists() && bundleMRSFile.lastModified() < updateDate) {
bundleMRSFile.delete()
}
if (!bundleMRSFile.exists()) {
FileOutputStream(bundleMRSFile).use {
assets.open("BundleMRS.7z").copyTo(it)
}
}
}
fun finalize() {
Global.destroy()
}
}