Commit 4e22d26e authored by Max Lv's avatar Max Lv

refine #100

parent 028c5a64
...@@ -49,8 +49,8 @@ import android.content.Context ...@@ -49,8 +49,8 @@ import android.content.Context
trait BaseService { trait BaseService {
var state = State.INIT @volatile var state = State.INIT
var callbackCount = 0 @volatile var callbackCount = 0
final val callbacks = new RemoteCallbackList[IShadowsocksServiceCallback] final val callbacks = new RemoteCallbackList[IShadowsocksServiceCallback]
...@@ -105,6 +105,7 @@ trait BaseService { ...@@ -105,6 +105,7 @@ trait BaseService {
handler.post(new Runnable { handler.post(new Runnable {
override def run() { override def run() {
if (state != s) { if (state != s) {
if (callbackCount > 0) {
val n = callbacks.beginBroadcast() val n = callbacks.beginBroadcast()
for (i <- 0 to n - 1) { for (i <- 0 to n - 1) {
try { try {
...@@ -114,6 +115,7 @@ trait BaseService { ...@@ -114,6 +115,7 @@ trait BaseService {
} }
} }
callbacks.finishBroadcast() callbacks.finishBroadcast()
}
state = s state = s
} }
} }
......
...@@ -507,7 +507,7 @@ class Shadowsocks ...@@ -507,7 +507,7 @@ class Shadowsocks
val intent = new Intent(this, s) val intent = new Intent(this, s)
intent.setAction(Action.SERVICE) intent.setAction(Action.SERVICE)
bindService(intent, connection, Context.BIND_AUTO_CREATE) bindService(intent, connection, Context.BIND_AUTO_CREATE)
if (!isVpnEnabled) startService(new Intent(this, s)) startService(new Intent(this, s))
} }
} }
......
...@@ -309,6 +309,11 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -309,6 +309,11 @@ class ShadowsocksNatService extends Service with BaseService {
} }
} }
override def onStartCommand(intent: Intent, flags: Int, startId: Int): Int = {
Service.START_NOT_STICKY
}
def killProcesses() { def killProcesses() {
Console.runRootCommand(Utils.getIptables + " -t nat -F OUTPUT") Console.runRootCommand(Utils.getIptables + " -t nat -F OUTPUT")
...@@ -328,10 +333,6 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -328,10 +333,6 @@ class ShadowsocksNatService extends Service with BaseService {
Console.runCommand(ab.toArray) Console.runCommand(ab.toArray)
} }
override def onStartCommand(intent: Intent, flags: Int, startId: Int): Int = {
Service.START_STICKY
}
def flushDNS() { def flushDNS() {
Console.runRootCommand(Array("ndc resolver flushdefaultif", "ndc resolver flushif wlan0")) Console.runRootCommand(Array("ndc resolver flushdefaultif", "ndc resolver flushif wlan0"))
} }
...@@ -453,11 +454,9 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -453,11 +454,9 @@ class ShadowsocksNatService extends Service with BaseService {
filter.addAction(Action.CLOSE) filter.addAction(Action.CLOSE)
receiver = new BroadcastReceiver() { receiver = new BroadcastReceiver() {
def onReceive(p1: Context, p2: Intent) { def onReceive(p1: Context, p2: Intent) {
spawn {
stopRunner() stopRunner()
} }
} }
}
registerReceiver(receiver, filter) registerReceiver(receiver, filter)
// start tracker // start tracker
...@@ -580,7 +579,6 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -580,7 +579,6 @@ class ShadowsocksNatService extends Service with BaseService {
override def stopBackgroundService() { override def stopBackgroundService() {
stopRunner() stopRunner()
stopSelf()
} }
override def getTag = TAG override def getTag = TAG
......
...@@ -253,7 +253,10 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -253,7 +253,10 @@ class ShadowsocksVpnService extends VpnService with BaseService {
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) notificationManager = getSystemService(Context.NOTIFICATION_SERVICE)
.asInstanceOf[NotificationManager] .asInstanceOf[NotificationManager]
}
override def onStartCommand(intent: Intent, flags: Int, startId: Int): Int = {
Service.START_NOT_STICKY
} }
override def onRevoke() { override def onRevoke() {
...@@ -295,11 +298,9 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -295,11 +298,9 @@ class ShadowsocksVpnService extends VpnService with BaseService {
filter.addAction(Intent.ACTION_SHUTDOWN) filter.addAction(Intent.ACTION_SHUTDOWN)
receiver = new BroadcastReceiver { receiver = new BroadcastReceiver {
def onReceive(p1: Context, p2: Intent) { def onReceive(p1: Context, p2: Intent) {
spawn {
stopRunner() stopRunner()
} }
} }
}
registerReceiver(receiver, filter) registerReceiver(receiver, filter)
changeState(State.CONNECTING) changeState(State.CONNECTING)
......
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