Commit b19f6662 authored by Max Lv's avatar Max Lv

update

parent 4d27c8c0
package com.github.shadowsocks;
interface IStateService {
int getState();
String getMessage();
}
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<string name="service_summary">Enable / Disable Proxy</string> <string name="service_summary">Enable / Disable Proxy</string>
<!-- proxy category --> <!-- proxy category -->
<string name="proxy_cat">Shadowsocks Settings</string> <string name="proxy_cat">Server Settings</string>
<string name="proxy">Server</string> <string name="proxy">Server</string>
<string name="proxy_summary">Hostname of your Shadowsocks server</string> <string name="proxy_summary">Hostname of your Shadowsocks server</string>
<string name="remote_port">Remote Port</string> <string name="remote_port">Remote Port</string>
......
...@@ -63,7 +63,6 @@ class ShadowVpnService extends VpnService { ...@@ -63,7 +63,6 @@ class ShadowVpnService extends VpnService {
val TAG = "ShadowVpnService" val TAG = "ShadowVpnService"
val BASE = "/data/data/com.github.shadowsocks/" val BASE = "/data/data/com.github.shadowsocks/"
val SHADOWSOCKS_CONF = "{\"server\": [%s], \"server_port\": %d, \"local_port\": %d, \"password\": %s, \"timeout\": %d}" val SHADOWSOCKS_CONF = "{\"server\": [%s], \"server_port\": %d, \"local_port\": %d, \"password\": %s, \"timeout\": %d}"
val MSG_CONNECT_START = 0
val MSG_CONNECT_FINISH = 1 val MSG_CONNECT_FINISH = 1
val MSG_CONNECT_SUCCESS = 2 val MSG_CONNECT_SUCCESS = 2
val MSG_CONNECT_FAIL = 3 val MSG_CONNECT_FAIL = 3
...@@ -103,30 +102,27 @@ class ShadowVpnService extends VpnService { ...@@ -103,30 +102,27 @@ class ShadowVpnService extends VpnService {
if (state != s) { if (state != s) {
state = s state = s
if (m != null) message = m if (m != null) message = m
sendBroadcast(new Intent(Utils.ACTION_UPDATE_STATE)) val intent = new Intent(Action.UPDATE_STATE)
intent.putExtra(Extra.STATE, state)
intent.putExtra(Extra.MESSAGE, message)
sendBroadcast(intent)
} }
} }
val handler: Handler = new Handler { val handler: Handler = new Handler {
override def handleMessage(msg: Message) { override def handleMessage(msg: Message) {
val ed: SharedPreferences.Editor = settings.edit
msg.what match { msg.what match {
case MSG_CONNECT_START =>
changeState(State.CONNECTING)
case MSG_CONNECT_SUCCESS => case MSG_CONNECT_SUCCESS =>
changeState(State.CONNECTED) changeState(State.CONNECTED)
ed.putBoolean("isRunning", true)
case MSG_CONNECT_FAIL => case MSG_CONNECT_FAIL =>
changeState(State.FAILED) changeState(State.STOPPED)
ed.putBoolean("isRunning", false)
case MSG_VPN_ERROR => case MSG_VPN_ERROR =>
if (msg.obj != null) changeState(State.FAILED, msg.obj.asInstanceOf[String]) if (msg.obj != null) changeState(State.STOPPED, msg.obj.asInstanceOf[String])
case MSG_STOP_SELF => case MSG_STOP_SELF =>
destroy() destroy()
stopSelf() stopSelf()
case _ => case _ =>
} }
ed.commit
super.handleMessage(msg) super.handleMessage(msg)
} }
} }
...@@ -185,6 +181,10 @@ class ShadowVpnService extends VpnService { ...@@ -185,6 +181,10 @@ class ShadowVpnService extends VpnService {
return return
} }
settings.edit().putBoolean("isRunning", true).commit()
changeState(State.CONNECTING)
appHost = settings.getString("proxy", "127.0.0.1") appHost = settings.getString("proxy", "127.0.0.1")
if (appHost == "198.199.101.152") { if (appHost == "198.199.101.152") {
appHost = "s.maxcdn.info" appHost = "s.maxcdn.info"
...@@ -213,9 +213,9 @@ class ShadowVpnService extends VpnService { ...@@ -213,9 +213,9 @@ class ShadowVpnService extends VpnService {
if (isHTTPProxy) { if (isHTTPProxy) {
localPort -= 1 localPort -= 1
} }
new Thread(new Runnable { new Thread(new Runnable {
def run() { def run() {
handler.sendEmptyMessage(MSG_CONNECT_START)
killProcesses() killProcesses()
...@@ -384,16 +384,7 @@ class ShadowVpnService extends VpnService { ...@@ -384,16 +384,7 @@ class ShadowVpnService extends VpnService {
if (VpnService.SERVICE_INTERFACE == action) { if (VpnService.SERVICE_INTERFACE == action) {
return super.onBind(intent) return super.onBind(intent)
} }
null
new IStateService.Stub {
def getMessage: String = {
val m = message
message = null
m
}
def getState: Int = state
}
} }
override def onCreate() { override def onCreate() {
...@@ -407,7 +398,7 @@ class ShadowVpnService extends VpnService { ...@@ -407,7 +398,7 @@ class ShadowVpnService extends VpnService {
// register close receiver // register close receiver
val filter = new IntentFilter() val filter = new IntentFilter()
filter.addAction(Intent.ACTION_SHUTDOWN) filter.addAction(Intent.ACTION_SHUTDOWN)
filter.addAction(Utils.ACTION_CLOSE) filter.addAction(Action.CLOSE)
receiver = new BroadcastReceiver { receiver = new BroadcastReceiver {
def onReceive(p1: Context, p2: Intent) { def onReceive(p1: Context, p2: Intent) {
destroy() destroy()
...@@ -419,6 +410,7 @@ class ShadowVpnService extends VpnService { ...@@ -419,6 +410,7 @@ class ShadowVpnService extends VpnService {
def destroy() { def destroy() {
changeState(State.STOPPED) changeState(State.STOPPED)
settings.edit.putBoolean("isRunning", false).commit
EasyTracker.getTracker.sendEvent(TAG, "stop", getVersionName, 0L) EasyTracker.getTracker.sendEvent(TAG, "stop", getVersionName, 0L)
if (receiver != null) { if (receiver != null) {
unregisterReceiver(receiver) unregisterReceiver(receiver)
...@@ -429,9 +421,6 @@ class ShadowVpnService extends VpnService { ...@@ -429,9 +421,6 @@ class ShadowVpnService extends VpnService {
killProcesses() killProcesses()
} }
}.start() }.start()
val ed: SharedPreferences.Editor = settings.edit
ed.putBoolean("isRunning", false)
ed.commit
if (conn != null) { if (conn != null) {
conn.close() conn.close()
conn = null conn = null
......
...@@ -94,10 +94,10 @@ object Shadowsocks { ...@@ -94,10 +94,10 @@ object Shadowsocks {
} }
} }
override def onStart() { override def onCreate(bundle: Bundle) {
super.onStart() super.onCreate(bundle)
val filter = new IntentFilter() val filter = new IntentFilter()
filter.addAction(Utils.ACTION_UPDATE_FRAGMENT) filter.addAction(Action.UPDATE_FRAGMENT)
receiver = new BroadcastReceiver { receiver = new BroadcastReceiver {
def onReceive(p1: Context, p2: Intent) { def onReceive(p1: Context, p2: Intent) {
setPreferenceEnabled() setPreferenceEnabled()
...@@ -106,8 +106,8 @@ object Shadowsocks { ...@@ -106,8 +106,8 @@ object Shadowsocks {
getActivity.getApplicationContext.registerReceiver(receiver, filter) getActivity.getApplicationContext.registerReceiver(receiver, filter)
} }
override def onStop() { override def onDestroy() {
super.onStop() super.onDestroy()
getActivity.getApplicationContext.unregisterReceiver(receiver) getActivity.getApplicationContext.unregisterReceiver(receiver)
} }
...@@ -144,10 +144,10 @@ object Shadowsocks { ...@@ -144,10 +144,10 @@ object Shadowsocks {
} }
} }
override def onStart() { override def onCreate(bundle: Bundle) {
super.onStart() super.onCreate(bundle)
val filter = new IntentFilter() val filter = new IntentFilter()
filter.addAction(Utils.ACTION_UPDATE_FRAGMENT) filter.addAction(Action.UPDATE_FRAGMENT)
receiver = new BroadcastReceiver { receiver = new BroadcastReceiver {
def onReceive(p1: Context, p2: Intent) { def onReceive(p1: Context, p2: Intent) {
setPreferenceEnabled() setPreferenceEnabled()
...@@ -156,8 +156,8 @@ object Shadowsocks { ...@@ -156,8 +156,8 @@ object Shadowsocks {
getActivity.getApplicationContext.registerReceiver(receiver, filter) getActivity.getApplicationContext.registerReceiver(receiver, filter)
} }
override def onStop() { override def onDestroy() {
super.onStop() super.onDestroy()
getActivity.getApplicationContext.unregisterReceiver(receiver) getActivity.getApplicationContext.unregisterReceiver(receiver)
} }
...@@ -207,21 +207,8 @@ class Shadowsocks ...@@ -207,21 +207,8 @@ class Shadowsocks
private var settings: SharedPreferences = null private var settings: SharedPreferences = null
private var prepared = false private var prepared = false
private var state = State.INIT private var state = State.INIT
private var binder: IStateService = null
private var receiver: StateBroadcastReceiver = null private var receiver: StateBroadcastReceiver = null
private val connection = new ServiceConnection {
def onServiceDisconnected(name: ComponentName) {
onStateChanged(binder.getState, binder.getMessage)
binder = null
}
def onServiceConnected(name: ComponentName, s: IBinder) {
binder = IStateService.Stub.asInterface(s)
onStateChanged(binder.getState, binder.getMessage)
}
}
private val handler: Handler = new Handler { private val handler: Handler = new Handler {
override def handleMessage(msg: Message) { override def handleMessage(msg: Message) {
val ed: SharedPreferences.Editor = settings.edit val ed: SharedPreferences.Editor = settings.edit
...@@ -356,7 +343,8 @@ class Shadowsocks ...@@ -356,7 +343,8 @@ class Shadowsocks
override def onCreate(savedInstanceState: Bundle) { override def onCreate(savedInstanceState: Bundle) {
setHeaderRes(R.xml.shadowsocks_headers) setHeaderRes(R.xml.shadowsocks_headers)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val switchLayout: RelativeLayout = getLayoutInflater
val switchLayout = getLayoutInflater
.inflate(R.layout.layout_switch, null) .inflate(R.layout.layout_switch, null)
.asInstanceOf[RelativeLayout] .asInstanceOf[RelativeLayout]
getSupportActionBar.setCustomView(switchLayout) getSupportActionBar.setCustomView(switchLayout)
...@@ -364,12 +352,15 @@ class Shadowsocks ...@@ -364,12 +352,15 @@ class Shadowsocks
getSupportActionBar.setDisplayShowCustomEnabled(true) getSupportActionBar.setDisplayShowCustomEnabled(true)
getSupportActionBar.setDisplayShowHomeEnabled(false) getSupportActionBar.setDisplayShowHomeEnabled(false)
switchButton = switchLayout.findViewById(R.id.switchButton).asInstanceOf[Switch]
val title: TextView = switchLayout.findViewById(R.id.title).asInstanceOf[TextView] val title: TextView = switchLayout.findViewById(R.id.title).asInstanceOf[TextView]
val tf: Typeface = Typefaces.get(this, "fonts/Iceland.ttf") val tf: Typeface = Typefaces.get(this, "fonts/Iceland.ttf")
if (tf != null) title.setTypeface(tf) if (tf != null) title.setTypeface(tf)
title.setText(R.string.app_name) title.setText(R.string.app_name)
switchButton = switchLayout.findViewById(R.id.switchButton).asInstanceOf[Switch]
settings = PreferenceManager.getDefaultSharedPreferences(this) settings = PreferenceManager.getDefaultSharedPreferences(this)
receiver = new StateBroadcastReceiver()
registerReceiver(receiver, new IntentFilter(Action.UPDATE_STATE))
val init: Boolean = !settings.getBoolean("isRunning", false) && val init: Boolean = !settings.getBoolean("isRunning", false) &&
!settings.getBoolean("isConnecting", false) !settings.getBoolean("isConnecting", false)
...@@ -464,11 +455,9 @@ class Shadowsocks ...@@ -464,11 +455,9 @@ class Shadowsocks
.build() .build()
switchButton.setEnabled(false) switchButton.setEnabled(false)
Crouton.makeText(Shadowsocks.this, R.string.vpn_status, style).show() Crouton.makeText(Shadowsocks.this, R.string.vpn_status, style).show()
if (binder == null) bindService(new Intent(this, classOf[ShadowVpnService]), connection, 0)
} else {
if (binder == null) bindService(new Intent(this, classOf[ShadowsocksService]), connection, 0)
} }
setPreferenceEnabled(false) setPreferenceEnabled(false)
onStateChanged(State.CONNECTED, null)
} else { } else {
switchButton.setEnabled(true) switchButton.setEnabled(true)
switchButton.setChecked(false) switchButton.setChecked(false)
...@@ -482,6 +471,7 @@ class Shadowsocks ...@@ -482,6 +471,7 @@ class Shadowsocks
} }
}.start() }.start()
} }
onStateChanged(State.STOPPED, null)
} }
} }
...@@ -512,14 +502,11 @@ class Shadowsocks ...@@ -512,14 +502,11 @@ class Shadowsocks
override def onStart() { override def onStart() {
super.onStart() super.onStart()
receiver = new StateBroadcastReceiver()
registerReceiver(receiver, new IntentFilter(Utils.ACTION_UPDATE_STATE))
EasyTracker.getInstance.activityStart(this) EasyTracker.getInstance.activityStart(this)
} }
override def onStop() { override def onStop() {
super.onStop() super.onStop()
unregisterReceiver(receiver)
EasyTracker.getInstance.activityStop(this) EasyTracker.getInstance.activityStop(this)
if (progressDialog != null) { if (progressDialog != null) {
progressDialog.dismiss() progressDialog.dismiss()
...@@ -530,7 +517,7 @@ class Shadowsocks ...@@ -530,7 +517,7 @@ class Shadowsocks
override def onDestroy() { override def onDestroy() {
super.onDestroy() super.onDestroy()
Crouton.cancelAllCroutons() Crouton.cancelAllCroutons()
if (binder != null) unbindService(connection) unregisterReceiver(receiver)
} }
def reset() { def reset() {
...@@ -589,7 +576,7 @@ class Shadowsocks ...@@ -589,7 +576,7 @@ class Shadowsocks
stopService(new Intent(this, classOf[ShadowVpnService])) stopService(new Intent(this, classOf[ShadowVpnService]))
} }
if (ShadowsocksService.isServiceStarted(this)) { if (ShadowsocksService.isServiceStarted(this)) {
sendBroadcast(new Intent(Utils.ACTION_CLOSE)) sendBroadcast(new Intent(Action.CLOSE))
} }
} }
...@@ -617,7 +604,6 @@ class Shadowsocks ...@@ -617,7 +604,6 @@ class Shadowsocks
if (ShadowVpnService.isServiceStarted(this)) return false if (ShadowVpnService.isServiceStarted(this)) return false
val it: Intent = new Intent(this, classOf[ShadowVpnService]) val it: Intent = new Intent(this, classOf[ShadowVpnService])
startService(it) startService(it)
bindService(it, connection, 0)
val style = new Style.Builder() val style = new Style.Builder()
.setBackgroundColorValue(Style.holoBlueLight) .setBackgroundColorValue(Style.holoBlueLight)
.setDuration(Style.DURATION_INFINITE) .setDuration(Style.DURATION_INFINITE)
...@@ -628,7 +614,6 @@ class Shadowsocks ...@@ -628,7 +614,6 @@ class Shadowsocks
if (ShadowsocksService.isServiceStarted(this)) return false if (ShadowsocksService.isServiceStarted(this)) return false
val it: Intent = new Intent(this, classOf[ShadowsocksService]) val it: Intent = new Intent(this, classOf[ShadowsocksService])
startService(it) startService(it)
bindService(it, connection, 0)
} }
true true
} }
...@@ -702,7 +687,7 @@ class Shadowsocks ...@@ -702,7 +687,7 @@ class Shadowsocks
if (!switchButton.isChecked) switchButton.setChecked(true) if (!switchButton.isChecked) switchButton.setChecked(true)
setPreferenceEnabled(false) setPreferenceEnabled(false)
} }
case State.FAILED => { case State.STOPPED => {
clearDialog() clearDialog()
if (switchButton.isChecked) { if (switchButton.isChecked) {
switchButton.setEnabled(true) switchButton.setEnabled(true)
...@@ -719,24 +704,16 @@ class Shadowsocks ...@@ -719,24 +704,16 @@ class Shadowsocks
} }
setPreferenceEnabled(true) setPreferenceEnabled(true)
} }
case State.STOPPED => {
clearDialog()
Crouton.cancelAllCroutons()
if (switchButton.isChecked) switchButton.setChecked(false)
setPreferenceEnabled(true)
}
} }
sendBroadcast(new Intent(Utils.ACTION_UPDATE_FRAGMENT)) sendBroadcast(new Intent(Action.UPDATE_FRAGMENT))
} }
} }
class StateBroadcastReceiver extends BroadcastReceiver { class StateBroadcastReceiver extends BroadcastReceiver {
override def onReceive(context: Context, intent: Intent) { override def onReceive(context: Context, intent: Intent) {
if (binder != null) { val state = intent.getIntExtra(Extra.STATE, State.INIT)
val s = binder.getState val message = intent.getStringExtra(Extra.MESSAGE)
val m = binder.getMessage onStateChanged(state, message)
onStateChanged(s, m)
}
} }
} }
} }
...@@ -90,7 +90,6 @@ class ShadowsocksService extends Service { ...@@ -90,7 +90,6 @@ class ShadowsocksService extends Service {
"-j DNAT --to-destination 127.0.0.1:8123\n" "-j DNAT --to-destination 127.0.0.1:8123\n"
val DNS_PORT = 8153 val DNS_PORT = 8153
val MSG_CONNECT_START = 0
val MSG_CONNECT_FINISH = 1 val MSG_CONNECT_FINISH = 1
val MSG_CONNECT_SUCCESS = 2 val MSG_CONNECT_SUCCESS = 2
val MSG_CONNECT_FAIL = 3 val MSG_CONNECT_FAIL = 3
...@@ -128,7 +127,9 @@ class ShadowsocksService extends Service { ...@@ -128,7 +127,9 @@ class ShadowsocksService extends Service {
private def changeState(s: Int) { private def changeState(s: Int) {
if (state != s) { if (state != s) {
state = s state = s
sendBroadcast(new Intent(Utils.ACTION_UPDATE_STATE)) val intent = new Intent(Action.UPDATE_STATE)
intent.putExtra(Extra.STATE, state)
sendBroadcast(intent)
} }
} }
...@@ -136,14 +137,11 @@ class ShadowsocksService extends Service { ...@@ -136,14 +137,11 @@ class ShadowsocksService extends Service {
override def handleMessage(msg: Message) { override def handleMessage(msg: Message) {
val ed: SharedPreferences.Editor = settings.edit val ed: SharedPreferences.Editor = settings.edit
msg.what match { msg.what match {
case MSG_CONNECT_START =>
changeState(State.CONNECTING)
case MSG_CONNECT_SUCCESS => case MSG_CONNECT_SUCCESS =>
changeState(State.CONNECTED) changeState(State.CONNECTED)
ed.putBoolean("isRunning", true) ed.putBoolean("isRunning", true)
case MSG_CONNECT_FAIL => case MSG_CONNECT_FAIL =>
changeState(State.FAILED) changeState(State.STOPPED)
ed.putBoolean("isRunning", false)
case MSG_STOP_SELF => case MSG_STOP_SELF =>
stopSelf() stopSelf()
case _ => case _ =>
...@@ -219,6 +217,8 @@ class ShadowsocksService extends Service { ...@@ -219,6 +217,8 @@ class ShadowsocksService extends Service {
return return
} }
changeState(State.CONNECTING)
appHost = settings.getString("proxy", "127.0.0.1") appHost = settings.getString("proxy", "127.0.0.1")
if (appHost == "198.199.101.152") { if (appHost == "198.199.101.152") {
appHost = "s.maxcdn.info" appHost = "s.maxcdn.info"
...@@ -249,7 +249,6 @@ class ShadowsocksService extends Service { ...@@ -249,7 +249,6 @@ class ShadowsocksService extends Service {
} }
new Thread(new Runnable { new Thread(new Runnable {
def run() { def run() {
handler.sendEmptyMessage(MSG_CONNECT_START)
killProcesses() killProcesses()
...@@ -341,7 +340,7 @@ class ShadowsocksService extends Service { ...@@ -341,7 +340,7 @@ class ShadowsocksService extends Service {
val openIntent: Intent = new Intent(this, classOf[Shadowsocks]) val openIntent: Intent = new Intent(this, classOf[Shadowsocks])
openIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) openIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val contentIntent: PendingIntent = PendingIntent.getActivity(this, 0, openIntent, 0) val contentIntent: PendingIntent = PendingIntent.getActivity(this, 0, openIntent, 0)
val closeIntent: Intent = new Intent(Utils.ACTION_CLOSE) val closeIntent: Intent = new Intent(Action.CLOSE)
val actionIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, closeIntent, 0) val actionIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, closeIntent, 0)
val builder: NotificationCompat.Builder = new NotificationCompat.Builder(this) val builder: NotificationCompat.Builder = new NotificationCompat.Builder(this)
builder builder
...@@ -373,10 +372,7 @@ class ShadowsocksService extends Service { ...@@ -373,10 +372,7 @@ class ShadowsocksService extends Service {
} }
def onBind(intent: Intent): IBinder = { def onBind(intent: Intent): IBinder = {
new IStateService.Stub { null
def getMessage: String = null
def getState: Int = state
}
} }
override def onCreate() { override def onCreate() {
...@@ -410,7 +406,7 @@ class ShadowsocksService extends Service { ...@@ -410,7 +406,7 @@ class ShadowsocksService extends Service {
// register close receiver // register close receiver
val filter = new IntentFilter() val filter = new IntentFilter()
filter.addAction(Intent.ACTION_SHUTDOWN) filter.addAction(Intent.ACTION_SHUTDOWN)
filter.addAction(Utils.ACTION_CLOSE) filter.addAction(Action.CLOSE)
receiver = new BroadcastReceiver() { receiver = new BroadcastReceiver() {
def onReceive(p1: Context, p2: Intent) { def onReceive(p1: Context, p2: Intent) {
stopSelf() stopSelf()
......
...@@ -54,17 +54,23 @@ import android.app.ActivityManager ...@@ -54,17 +54,23 @@ import android.app.ActivityManager
object State { object State {
val INIT = 0 val INIT = 0
val CONNECTED = 1 val CONNECTING = 1
val CONNECTING = 2 val CONNECTED = 2
val FAILED = 3 val STOPPED = 3
val STOPPED = 4
} }
object Utils { object Action {
val CLOSE = "com.github.shadowsocks.ACTION_SHUTDOWN"
val UPDATE_STATE = "com.github.shadowsocks.ACTION_UPDATE_STATE"
val UPDATE_FRAGMENT = "com.github.shadowsocks.ACTION_UPDATE_FRAGMENT"
}
val ACTION_CLOSE = "com.github.shadowsocks.ACTION_SHUTDOWN" object Extra {
val ACTION_UPDATE_STATE = "com.github.shadowsocks.ACTION_UPDATE_STATE" val STATE = "state"
val ACTION_UPDATE_FRAGMENT = "com.github.shadowsocks.ACTION_UPDATE_FRAGMENT" val MESSAGE = "message"
}
object Utils {
val TAG: String = "Shadowsocks" val TAG: String = "Shadowsocks"
val ABI_PROP: String = "ro.product.cpu.abi" val ABI_PROP: String = "ro.product.cpu.abi"
......
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