Commit 017dd425 authored by Max Lv's avatar Max Lv

Refine #602

parent 04ec29c9
...@@ -63,7 +63,7 @@ trait BaseService extends Service { ...@@ -63,7 +63,7 @@ trait BaseService extends Service {
private val closeReceiver: BroadcastReceiver = (context: Context, intent: Intent) => { private val closeReceiver: BroadcastReceiver = (context: Context, intent: Intent) => {
Toast.makeText(context, R.string.stopping, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.stopping, Toast.LENGTH_SHORT).show()
stopRunner() stopRunner(true)
} }
var closeReceiverRegistered: Boolean = _ var closeReceiverRegistered: Boolean = _
...@@ -106,8 +106,8 @@ trait BaseService extends Service { ...@@ -106,8 +106,8 @@ trait BaseService extends Service {
override def use(config: Config) = synchronized(state match { override def use(config: Config) = synchronized(state match {
case State.STOPPED => if (config != null) startRunner(config) case State.STOPPED => if (config != null) startRunner(config)
case State.CONNECTED => case State.CONNECTED =>
if (config == null) stopRunner() else if (config.profileId != BaseService.this.config.profileId) { if (config == null) stopRunner(true) else if (config.profileId != BaseService.this.config.profileId) {
stopRunner() stopRunner(false)
startRunner(config) startRunner(config)
} }
case _ => Log.w(BaseService.this.getClass.getSimpleName, "Illegal state when invoking use: " + state) case _ => Log.w(BaseService.this.getClass.getSimpleName, "Illegal state when invoking use: " + state)
...@@ -132,7 +132,7 @@ trait BaseService extends Service { ...@@ -132,7 +132,7 @@ trait BaseService extends Service {
} }
} }
def stopRunner() { def stopRunner(stopService: Boolean) {
// clean up recevier // clean up recevier
if (closeReceiverRegistered) { if (closeReceiverRegistered) {
unregisterReceiver(closeReceiver) unregisterReceiver(closeReceiver)
...@@ -152,7 +152,7 @@ trait BaseService extends Service { ...@@ -152,7 +152,7 @@ trait BaseService extends Service {
changeState(State.STOPPED) changeState(State.STOPPED)
// stop the service if nothing has bound to it // stop the service if nothing has bound to it
stopSelf() if (stopService) stopSelf()
} }
def updateTrafficTotal(tx: Long, rx: Long) { def updateTrafficTotal(tx: Long, rx: Long) {
......
...@@ -396,7 +396,7 @@ class ShadowsocksNatService extends BaseService { ...@@ -396,7 +396,7 @@ class ShadowsocksNatService extends BaseService {
} catch { } catch {
case ex: Exception => case ex: Exception =>
changeState(State.STOPPED, getString(R.string.service_failed)) changeState(State.STOPPED, getString(R.string.service_failed))
stopRunner() stopRunner(false)
this.config = null this.config = null
} }
} }
...@@ -427,13 +427,13 @@ class ShadowsocksNatService extends BaseService { ...@@ -427,13 +427,13 @@ class ShadowsocksNatService extends BaseService {
notification = new ShadowsocksNotification(this, config.profileName, true) notification = new ShadowsocksNotification(this, config.profileName, true)
} else { } else {
changeState(State.STOPPED, getString(R.string.service_failed)) changeState(State.STOPPED, getString(R.string.service_failed))
stopRunner() stopRunner(false)
} }
} }
} }
} }
override def stopRunner() { override def stopRunner(stopService: Boolean) {
// channge the state // channge the state
changeState(State.STOPPING) changeState(State.STOPPING)
...@@ -445,7 +445,7 @@ class ShadowsocksNatService extends BaseService { ...@@ -445,7 +445,7 @@ class ShadowsocksNatService extends BaseService {
// reset NAT // reset NAT
killProcesses() killProcesses()
super.stopRunner() super.stopRunner(stopService)
} }
override def getTag = TAG override def getTag = TAG
......
...@@ -106,10 +106,10 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -106,10 +106,10 @@ class ShadowsocksVpnService extends VpnService with BaseService {
} }
override def onRevoke() { override def onRevoke() {
stopRunner() stopRunner(false)
} }
override def stopRunner() { override def stopRunner(stopService: Boolean) {
if (vpnThread != null) { if (vpnThread != null) {
vpnThread.stopThread() vpnThread.stopThread()
...@@ -132,7 +132,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -132,7 +132,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
conn = null conn = null
} }
super.stopRunner() super.stopRunner(stopService)
} }
def getVersionName: String = { def getVersionName: String = {
...@@ -193,7 +193,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -193,7 +193,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
} catch { } catch {
case ex: Exception => case ex: Exception =>
changeState(State.STOPPED, getString(R.string.service_failed)) changeState(State.STOPPED, getString(R.string.service_failed))
stopRunner() stopRunner(false)
this.config = null this.config = null
} }
} }
...@@ -221,7 +221,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -221,7 +221,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
notification = new ShadowsocksNotification(this, config.profileName) notification = new ShadowsocksNotification(this, config.profileName)
} else { } else {
changeState(State.STOPPED, getString(R.string.service_failed)) changeState(State.STOPPED, getString(R.string.service_failed))
stopRunner() stopRunner(false)
} }
} }
} }
...@@ -386,7 +386,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -386,7 +386,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
} }
if (conn == null) { if (conn == null) {
stopRunner() stopRunner(false)
return -1 return -1
} }
......
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