Commit 4906ce8a authored by Mygod's avatar Mygod

Support switching to NAT/VPN without restarting the app

parent 1f0f9211
...@@ -247,8 +247,8 @@ class AppManager extends Activity with OnCheckedChangeListener with OnClickListe ...@@ -247,8 +247,8 @@ class AppManager extends Activity with OnCheckedChangeListener with OnClickListe
val array = proxiedAppString.split(" ") val array = proxiedAppString.split(" ")
val bypass = array(0).toBoolean val bypass = array(0).toBoolean
val apps = if (array.size > 1) array(1) else "" val apps = if (array.size > 1) array(1) else ""
prefs.edit.putBoolean(Key.isBypassApps, bypass).commit() prefs.edit.putBoolean(Key.isBypassApps, bypass).apply()
prefs.edit.putString(Key.proxied, apps).commit() prefs.edit.putString(Key.proxied, apps).apply()
Toast.makeText(this, R.string.action_import_msg, Toast.LENGTH_SHORT).show() Toast.makeText(this, R.string.action_import_msg, Toast.LENGTH_SHORT).show()
// Restart activity // Restart activity
val intent = getIntent val intent = getIntent
...@@ -299,7 +299,7 @@ class AppManager extends Activity with OnCheckedChangeListener with OnClickListe ...@@ -299,7 +299,7 @@ class AppManager extends Activity with OnCheckedChangeListener with OnClickListe
val bypassSwitch = findViewById(R.id.bypassSwitch).asInstanceOf[Switch] val bypassSwitch = findViewById(R.id.bypassSwitch).asInstanceOf[Switch]
val prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext) val prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext)
bypassSwitch.setOnCheckedChangeListener((button: CompoundButton, checked: Boolean) => bypassSwitch.setOnCheckedChangeListener((button: CompoundButton, checked: Boolean) =>
prefs.edit().putBoolean(Key.isBypassApps, checked).commit()) prefs.edit().putBoolean(Key.isBypassApps, checked).apply())
bypassSwitch.setChecked(prefs.getBoolean(Key.isBypassApps, false)) bypassSwitch.setChecked(prefs.getBoolean(Key.isBypassApps, false))
appListView = findViewById(R.id.applistview).asInstanceOf[ListView] appListView = findViewById(R.id.applistview).asInstanceOf[ListView]
...@@ -325,7 +325,7 @@ class AppManager extends Activity with OnCheckedChangeListener with OnClickListe ...@@ -325,7 +325,7 @@ class AppManager extends Activity with OnCheckedChangeListener with OnClickListe
}) })
val edit: SharedPreferences.Editor = prefs.edit val edit: SharedPreferences.Editor = prefs.edit
edit.putString(Key.proxied, proxiedApps.toString()) edit.putString(Key.proxied, proxiedApps.toString())
edit.commit edit.apply
} }
val loadStartRunnable = new Runnable { val loadStartRunnable = new Runnable {
......
...@@ -223,7 +223,7 @@ class Shadowsocks ...@@ -223,7 +223,7 @@ class Shadowsocks
state = bgService.getState state = bgService.getState
if (!status.getBoolean(getVersionName, false)) { if (!status.getBoolean(getVersionName, false)) {
status.edit.putBoolean(getVersionName, true).commit() status.edit.putBoolean(getVersionName, true).apply()
recovery() recovery()
} }
} }
...@@ -515,13 +515,13 @@ class Shadowsocks ...@@ -515,13 +515,13 @@ class Shadowsocks
title.setOnLongClickListener((v: View) => { title.setOnLongClickListener((v: View) => {
if (Utils.isLollipopOrAbove && bgService != null if (Utils.isLollipopOrAbove && bgService != null
&& (bgService.getState == State.INIT || bgService.getState == State.STOPPED)) { && (bgService.getState == State.INIT || bgService.getState == State.STOPPED)) {
val natEnabled = status.getBoolean(Key.isNAT, false) val natEnabled = !status.getBoolean(Key.isNAT, !Utils.isLollipopOrAbove)
status.edit().putBoolean(Key.isNAT, !natEnabled).commit() status.edit().putBoolean(Key.isNAT, natEnabled).apply()
if (!natEnabled) { deattachService()
Toast.makeText(getBaseContext, R.string.enable_nat, Toast.LENGTH_LONG).show() attachService()
} else { if (!Utils.isLollipopOrAbove) setPreferenceEnabled(State.isAvailable(bgService.getState))
Toast.makeText(getBaseContext, R.string.disable_nat, Toast.LENGTH_LONG).show() Toast.makeText(getBaseContext, if (natEnabled) R.string.enable_nat else R.string.disable_nat, Toast.LENGTH_LONG)
} .show()
true true
} else { } else {
false false
...@@ -532,13 +532,9 @@ class Shadowsocks ...@@ -532,13 +532,9 @@ class Shadowsocks
registerReceiver(preferenceReceiver, new IntentFilter(Action.UPDATE_PREFS)) registerReceiver(preferenceReceiver, new IntentFilter(Action.UPDATE_PREFS))
// Bind to the service // Bind to the service
Future { handler.post(() => {
val isRoot = (!Utils.isLollipopOrAbove || status.getBoolean(Key.isNAT, false)) && Console.isRoot attachService()
handler.post(() => { })
status.edit.putBoolean(Key.isRoot, isRoot).commit()
attachService()
})
}
} }
def attachService() { def attachService() {
...@@ -942,15 +938,10 @@ class Shadowsocks ...@@ -942,15 +938,10 @@ class Shadowsocks
} }
} }
var isRoot: Option[Boolean] = None
def isVpnEnabled: Boolean = { def isVpnEnabled: Boolean = {
if (vpnEnabled < 0) { if (isRoot.isEmpty) isRoot = Some(Console.isRoot)
vpnEnabled = if (!status.getBoolean(Key.isRoot, false)) { !(isRoot.get && status.getBoolean(Key.isNAT, !Utils.isLollipopOrAbove))
1
} else {
0
}
}
if (vpnEnabled == 1) true else false
} }
def serviceStop() { def serviceStop() {
...@@ -1051,6 +1042,7 @@ class Shadowsocks ...@@ -1051,6 +1042,7 @@ class Shadowsocks
fab.setImageResource(R.drawable.ic_cloud_queue) fab.setImageResource(R.drawable.ic_cloud_queue)
fab.setEnabled(false) fab.setEnabled(false)
fabProgressCircle.show() fabProgressCircle.show()
setPreferenceEnabled(enabled = false)
} }
state = s state = s
}) })
......
...@@ -65,19 +65,13 @@ class ShadowsocksRunnerActivity extends Activity { ...@@ -65,19 +65,13 @@ class ShadowsocksRunnerActivity extends Activity {
} }
// Variables // Variables
var vpnEnabled = -1 var isRoot: Option[Boolean] = None
var bgService: IShadowsocksService = null var bgService: IShadowsocksService = _
var receiver:BroadcastReceiver = null var receiver:BroadcastReceiver = _
def isVpnEnabled: Boolean = { def isVpnEnabled: Boolean = {
if (vpnEnabled < 0) { if (isRoot.isEmpty) isRoot = Some(Console.isRoot)
vpnEnabled = if ((!Utils.isLollipopOrAbove || status.getBoolean(Key.isNAT, false)) && Console.isRoot) { !(isRoot.get && status.getBoolean(Key.isNAT, !Utils.isLollipopOrAbove))
0
} else {
1
}
}
if (vpnEnabled == 1) true else false
} }
...@@ -127,6 +121,7 @@ class ShadowsocksRunnerActivity extends Activity { ...@@ -127,6 +121,7 @@ class ShadowsocksRunnerActivity extends Activity {
} else { } else {
attachService() attachService()
} }
finish
} }
override def onDestroy() { override def onDestroy() {
......
...@@ -64,18 +64,12 @@ class ShadowsocksRunnerService extends Service { ...@@ -64,18 +64,12 @@ class ShadowsocksRunnerService extends Service {
} }
// Variables // Variables
var vpnEnabled = -1 var isRoot: Option[Boolean] = None
var bgService: IShadowsocksService = null var bgService: IShadowsocksService = _
def isVpnEnabled: Boolean = { def isVpnEnabled: Boolean = {
if (vpnEnabled < 0) { if (isRoot.isEmpty) isRoot = Some(Console.isRoot)
vpnEnabled = if ((!Utils.isLollipopOrAbove || status.getBoolean(Key.isNAT, false)) && Console.isRoot) { !(isRoot.get && status.getBoolean(Key.isNAT, !Utils.isLollipopOrAbove))
0
} else {
1
}
}
if (vpnEnabled == 1) true else false
} }
override def onBind(intent: Intent): IBinder = { override def onBind(intent: Intent): IBinder = {
......
...@@ -133,7 +133,7 @@ class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) { ...@@ -133,7 +133,7 @@ class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) {
edit.putString(Key.proxied, profile.individual) edit.putString(Key.proxied, profile.individual)
edit.putInt(Key.profileId, profile.id) edit.putInt(Key.profileId, profile.id)
edit.putString(Key.route, profile.route) edit.putString(Key.route, profile.route)
edit.commit() edit.apply()
profile profile
} }
......
...@@ -66,7 +66,6 @@ object Key { ...@@ -66,7 +66,6 @@ object Key {
val proxied = "Proxyed" val proxied = "Proxyed"
val isNAT = "isNAT" val isNAT = "isNAT"
val isRoot = "isRoot"
val status = "status" val status = "status"
val proxyedApps = "proxyedApps" val proxyedApps = "proxyedApps"
val route = "route" val route = "route"
......
...@@ -75,20 +75,13 @@ object Utils { ...@@ -75,20 +75,13 @@ object Utils {
val TIME_OUT: Int = -99 val TIME_OUT: Int = -99
var initialized: Boolean = false var initialized: Boolean = false
var hasRedirectSupport: Int = -1 var hasRedirectSupport: Int = -1
var isRoot: Int = -1
var shell: String = null var shell: String = null
var root_shell: String = null var root_shell: String = null
var iptables: String = null var iptables: String = null
var data_path: String = null var data_path: String = null
var rootTries = 0 var rootTries = 0
def isLollipopOrAbove: Boolean = { def isLollipopOrAbove: Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
true
} else {
false
}
}
def getSignature(context: Context): String = { def getSignature(context: Context): String = {
val info = context val info = context
......
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