Commit a029f276 authored by Max Lv's avatar Max Lv

fix a crash

parent 2bf44c7e
...@@ -106,8 +106,9 @@ trait BaseService { ...@@ -106,8 +106,9 @@ trait BaseService {
override def run() { override def run() {
if (state != s) { if (state != s) {
val n = callbacks.beginBroadcast() val n = callbacks.beginBroadcast()
val m = if (msg != null) msg.substring(0, 64) else null
for (i <- 0 to n - 1) { for (i <- 0 to n - 1) {
callbacks.getBroadcastItem(i).stateChanged(s, msg.substring(0, 64)) callbacks.getBroadcastItem(i).stateChanged(s, m)
} }
callbacks.finishBroadcast() callbacks.finishBroadcast()
state = s state = s
......
...@@ -99,18 +99,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -99,18 +99,7 @@ class ShadowsocksNatService extends Service with BaseService {
private var timer: Timer = null private var timer: Timer = null
private val TIMER_INTERVAL = 2 private val TIMER_INTERVAL = 2
val handler: Handler = new Handler { val handler: Handler = new Handler()
override def handleMessage(msg: Message) {
msg.what match {
case Msg.CONNECT_SUCCESS =>
changeState(State.CONNECTED)
case Msg.CONNECT_FAIL =>
changeState(State.STOPPED)
case _ =>
}
super.handleMessage(msg)
}
}
def startShadowsocksDaemon() { def startShadowsocksDaemon() {
val cmd: String = (Path.BASE + val cmd: String = (Path.BASE +
...@@ -463,7 +452,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -463,7 +452,7 @@ class ShadowsocksNatService extends Service with BaseService {
case ex: Exception => case ex: Exception =>
notifyAlert(getString(R.string.forward_fail), getString(R.string.service_failed)) notifyAlert(getString(R.string.forward_fail), getString(R.string.service_failed))
stopRunner() stopRunner()
handler.sendEmptyMessageDelayed(Msg.CONNECT_FAIL, 500) changeState(State.STOPPED)
return return
} }
} }
...@@ -488,13 +477,12 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -488,13 +477,12 @@ class ShadowsocksNatService extends Service with BaseService {
if (resolved && handleConnection) { if (resolved && handleConnection) {
notifyForegroundAlert(getString(R.string.forward_success), notifyForegroundAlert(getString(R.string.forward_success),
getString(R.string.service_running).format(config.profileName)) getString(R.string.service_running).format(config.profileName))
handler.sendEmptyMessageDelayed(Msg.CONNECT_SUCCESS, 500) changeState(State.CONNECTED)
} else { } else {
notifyAlert(getString(R.string.forward_fail), getString(R.string.service_failed)) notifyAlert(getString(R.string.forward_fail), getString(R.string.service_failed))
stopRunner() stopRunner()
handler.sendEmptyMessageDelayed(Msg.CONNECT_FAIL, 500) changeState(State.STOPPED)
} }
handler.sendEmptyMessageDelayed(Msg.CONNECT_FINISH, 500)
} }
} }
......
...@@ -72,20 +72,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -72,20 +72,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
var apps: Array[ProxiedApp] = null var apps: Array[ProxiedApp] = null
var config: Config = null var config: Config = null
val handler: Handler = new Handler { val handler: Handler = new Handler()
override def handleMessage(msg: Message) {
msg.what match {
case Msg.CONNECT_SUCCESS =>
changeState(State.CONNECTED)
case Msg.CONNECT_FAIL =>
changeState(State.STOPPED)
case Msg.VPN_ERROR =>
if (msg.obj != null) changeState(State.STOPPED, msg.obj.asInstanceOf[String])
case _ =>
}
super.handleMessage(msg)
}
}
def startShadowsocksDaemon() { def startShadowsocksDaemon() {
val cmd: String = (Path.BASE + val cmd: String = (Path.BASE +
...@@ -178,10 +165,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -178,10 +165,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
conn = builder.establish() conn = builder.establish()
} catch { } catch {
case ex: IllegalStateException => case ex: IllegalStateException =>
val msg = new Message() changeState(State.STOPPED, ex.getMessage)
msg.what = Msg.VPN_ERROR
msg.obj = ex.getMessage
handler.sendMessage(msg)
conn = null conn = null
case ex: Exception => conn = null case ex: Exception => conn = null
} }
...@@ -314,7 +298,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -314,7 +298,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
case ex: Exception => case ex: Exception =>
notifyAlert(getString(R.string.forward_fail), getString(R.string.service_failed)) notifyAlert(getString(R.string.forward_fail), getString(R.string.service_failed))
stopRunner() stopRunner()
handler.sendEmptyMessageDelayed(Msg.CONNECT_FAIL, 500) changeState(State.STOPPED)
return return
} }
} }
...@@ -337,13 +321,12 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -337,13 +321,12 @@ class ShadowsocksVpnService extends VpnService with BaseService {
} }
if (resolved && handleConnection) { if (resolved && handleConnection) {
handler.sendEmptyMessageDelayed(Msg.CONNECT_SUCCESS, 300) changeState(State.CONNECTED)
} else { } else {
notifyAlert(getString(R.string.forward_fail), getString(R.string.service_failed)) notifyAlert(getString(R.string.forward_fail), getString(R.string.service_failed))
handler.sendEmptyMessageDelayed(Msg.CONNECT_FAIL, 300) changeState(State.STOPPED)
stopRunner() stopRunner()
} }
handler.sendEmptyMessageDelayed(Msg.CONNECT_FINISH, 300)
} }
} }
......
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