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