Commit 5c2d548d authored by Mygod's avatar Mygod

Reduce the number of default methods

parent 2fa8bc7f
...@@ -91,10 +91,8 @@ object BaseService { ...@@ -91,10 +91,8 @@ object BaseService {
} }
var closeReceiverRegistered = false var closeReceiverRegistered = false
private fun state() = state
val binder = object : IShadowsocksService.Stub() { val binder = object : IShadowsocksService.Stub() {
override fun getState(): Int = state() override fun getState(): Int = this@Data.state
override fun getProfileName(): String = profile?.formattedName ?: "Idle" override fun getProfileName(): String = profile?.formattedName ?: "Idle"
override fun registerCallback(cb: IShadowsocksServiceCallback) { override fun registerCallback(cb: IShadowsocksServiceCallback) {
...@@ -182,6 +180,24 @@ object BaseService { ...@@ -182,6 +180,24 @@ object BaseService {
} }
File(app.filesDir, "shadowsocks.json").bufferedWriter().use { it.write(config.toString()) } File(app.filesDir, "shadowsocks.json").bufferedWriter().use { it.write(config.toString()) }
} }
val aclFile: File? get() {
val route = profile!!.route
return if (route == Acl.ALL) null else
Acl.getFile(if (route == Acl.CUSTOM_RULES) Acl.CUSTOM_RULES_FLATTENED else route)
}
fun changeState(s: Int, msg: String? = null) {
if (state == s && msg == null) return
if (callbacks.registeredCallbackCount > 0) app.handler.post {
val n = callbacks.beginBroadcast()
for (i in 0 until n) try {
callbacks.getBroadcastItem(i).stateChanged(s, binder.profileName, msg)
} catch (_: Exception) { } // ignore
callbacks.finishBroadcast()
}
state = s
}
} }
interface Interface { interface Interface {
val tag: String val tag: String
...@@ -221,7 +237,7 @@ object BaseService { ...@@ -221,7 +237,7 @@ object BaseService {
"-t", "600", "-t", "600",
"-c", "shadowsocks.json")) "-c", "shadowsocks.json"))
val acl = getAclFile() val acl = data.aclFile
if (acl != null) { if (acl != null) {
cmd += "--acl" cmd += "--acl"
cmd += acl.absolutePath cmd += acl.absolutePath
...@@ -232,12 +248,6 @@ object BaseService { ...@@ -232,12 +248,6 @@ object BaseService {
data.sslocalProcess = GuardedProcess(cmd).start() data.sslocalProcess = GuardedProcess(cmd).start()
} }
fun getAclFile(): File? {
val route = data.profile!!.route
return if (route == Acl.ALL) null else
Acl.getFile(if (route == Acl.CUSTOM_RULES) Acl.CUSTOM_RULES_FLATTENED else route)
}
fun createNotification(): ServiceNotification fun createNotification(): ServiceNotification
fun startRunner() { fun startRunner() {
...@@ -254,14 +264,14 @@ object BaseService { ...@@ -254,14 +264,14 @@ object BaseService {
fun stopRunner(stopService: Boolean, msg: String? = null) { fun stopRunner(stopService: Boolean, msg: String? = null) {
// channge the state // channge the state
changeState(STOPPING) val data = data
data.changeState(STOPPING)
app.track(tag, "stop") app.track(tag, "stop")
killProcesses() killProcesses()
// clean up recevier // clean up recevier
val data = data
this as Service this as Service
if (data.closeReceiverRegistered) { if (data.closeReceiverRegistered) {
unregisterReceiver(data.closeReceiver) unregisterReceiver(data.closeReceiver)
...@@ -279,7 +289,7 @@ object BaseService { ...@@ -279,7 +289,7 @@ object BaseService {
data.trafficMonitorThread = null data.trafficMonitorThread = null
// change the state // change the state
changeState(STOPPED, msg) data.changeState(STOPPED, msg)
// stop the service if nothing has bound to it // stop the service if nothing has bound to it
if (stopService) stopSelf() if (stopService) stopSelf()
...@@ -318,7 +328,7 @@ object BaseService { ...@@ -318,7 +328,7 @@ object BaseService {
data.notification = createNotification() data.notification = createNotification()
app.track(tag, "start") app.track(tag, "start")
changeState(CONNECTING) data.changeState(CONNECTING)
thread { thread {
this as Context this as Context
...@@ -369,7 +379,7 @@ object BaseService { ...@@ -369,7 +379,7 @@ object BaseService {
if (profile.route !in arrayOf(Acl.ALL, Acl.CUSTOM_RULES)) AclSyncJob.schedule(profile.route) if (profile.route !in arrayOf(Acl.ALL, Acl.CUSTOM_RULES)) AclSyncJob.schedule(profile.route)
changeState(CONNECTED) data.changeState(CONNECTED)
} catch (_: UnknownHostException) { } catch (_: UnknownHostException) {
stopRunner(true, getString(R.string.invalid_server)) stopRunner(true, getString(R.string.invalid_server))
} catch (_: VpnService.NullConnectionException) { } catch (_: VpnService.NullConnectionException) {
...@@ -381,19 +391,6 @@ object BaseService { ...@@ -381,19 +391,6 @@ object BaseService {
} }
return Service.START_NOT_STICKY return Service.START_NOT_STICKY
} }
fun changeState(s: Int, msg: String? = null) {
val data = instances[this]!!
if (data.state == s && msg == null) return
if (data.callbacks.registeredCallbackCount > 0) app.handler.post {
val n = data.callbacks.beginBroadcast()
for (i in 0 until n) try {
data.callbacks.getBroadcastItem(i).stateChanged(s, data.binder.profileName, msg)
} catch (_: Exception) { } // ignore
data.callbacks.finishBroadcast()
}
data.state = s
}
} }
private val instances = WeakHashMap<Interface, Data>() private val instances = WeakHashMap<Interface, Data>()
......
...@@ -44,6 +44,7 @@ object LocalDnsService { ...@@ -44,6 +44,7 @@ object LocalDnsService {
override fun startNativeProcesses() { override fun startNativeProcesses() {
super.startNativeProcesses() super.startNativeProcesses()
val data = data
val profile = data.profile!! val profile = data.profile!!
fun makeDns(name: String, address: String, edns: Boolean = true): JSONObject { fun makeDns(name: String, address: String, edns: Boolean = true): JSONObject {
...@@ -89,7 +90,7 @@ object LocalDnsService { ...@@ -89,7 +90,7 @@ object LocalDnsService {
.put("PrimaryDNS", localDns) .put("PrimaryDNS", localDns)
.put("AlternativeDNS", remoteDns) .put("AlternativeDNS", remoteDns)
.put("IPNetworkFile", "china_ip_list.txt") .put("IPNetworkFile", "china_ip_list.txt")
.put("DomainFile", getAclFile()!!.absolutePath) .put("DomainFile", data.aclFile!!.absolutePath)
Acl.CHINALIST -> config Acl.CHINALIST -> config
.put("PrimaryDNS", localDns) .put("PrimaryDNS", localDns)
.put("AlternativeDNS", remoteDns) .put("AlternativeDNS", remoteDns)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment