Commit 86f32ea6 authored by Mygod's avatar Mygod

Add app.isNatEnabled for performance and readability

Warning: This is a boring commit.
parent 461a0bda
...@@ -55,8 +55,7 @@ trait ServiceBoundContext extends Context with IBinder.DeathRecipient { ...@@ -55,8 +55,7 @@ trait ServiceBoundContext extends Context with IBinder.DeathRecipient {
def attachService(callback: IShadowsocksServiceCallback.Stub = null) { def attachService(callback: IShadowsocksServiceCallback.Stub = null) {
this.callback = callback this.callback = callback
if (bgService == null) { if (bgService == null) {
val s = val s = if (app.isNatEnabled) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService]
if (app.isVpnEnabled) classOf[ShadowsocksVpnService] else classOf[ShadowsocksNatService]
val intent = new Intent(this, s) val intent = new Intent(this, s)
intent.setAction(Action.SERVICE) intent.setAction(Action.SERVICE)
......
...@@ -130,10 +130,10 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -130,10 +130,10 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
changeSwitch(checked = true) changeSwitch(checked = true)
preferences.setEnabled(false) preferences.setEnabled(false)
stat.setVisibility(View.VISIBLE) stat.setVisibility(View.VISIBLE)
if (app.isVpnEnabled) { if (app.isNatEnabled) connectionTestText.setVisibility(View.GONE) else {
connectionTestText.setVisibility(View.VISIBLE) connectionTestText.setVisibility(View.VISIBLE)
connectionTestText.setText(getString(R.string.connection_test_pending)) connectionTestText.setText(getString(R.string.connection_test_pending))
} else connectionTestText.setVisibility(View.GONE) }
case State.STOPPED => case State.STOPPED =>
fab.setBackgroundTintList(greyTint) fab.setBackgroundTintList(greyTint)
fabProgressCircle.postDelayed(hideCircle, 1000) fabProgressCircle.postDelayed(hideCircle, 1000)
...@@ -178,7 +178,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -178,7 +178,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
// Update the UI // Update the UI
if (fab != null) fab.setEnabled(true) if (fab != null) fab.setEnabled(true)
updateState() updateState()
if (Build.VERSION.SDK_INT >= 21 && !app.isVpnEnabled) { if (Build.VERSION.SDK_INT >= 21 && app.isNatEnabled) {
val snackbar = Snackbar.make(findViewById(android.R.id.content), R.string.nat_deprecated, Snackbar.LENGTH_LONG) val snackbar = Snackbar.make(findViewById(android.R.id.content), R.string.nat_deprecated, Snackbar.LENGTH_LONG)
snackbar.setAction(R.string.switch_to_vpn, (_ => preferences.natSwitch.setChecked(false)): View.OnClickListener) snackbar.setAction(R.string.switch_to_vpn, (_ => preferences.natSwitch.setChecked(false)): View.OnClickListener)
snackbar.show snackbar.show
...@@ -307,11 +307,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -307,11 +307,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
cmd.append("chmod 666 %s/%s-vpn.pid".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task)) cmd.append("chmod 666 %s/%s-vpn.pid".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task))
} }
if (app.isVpnEnabled) { if (app.isNatEnabled) Console.runRootCommand(cmd.toArray) else Console.runCommand(cmd.toArray)
Console.runCommand(cmd.toArray)
} else {
Console.runRootCommand(cmd.toArray)
}
cmd.clear() cmd.clear()
...@@ -331,10 +327,10 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -331,10 +327,10 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
cmd.append("rm -f %s/%s-vpn.pid".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task)) cmd.append("rm -f %s/%s-vpn.pid".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task))
cmd.append("rm -f %s/%s-vpn.conf".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task)) cmd.append("rm -f %s/%s-vpn.conf".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task))
} }
if (app.isVpnEnabled) Console.runCommand(cmd.toArray) else { if (app.isNatEnabled) {
Console.runRootCommand(cmd.toArray) Console.runRootCommand(cmd.toArray)
Console.runRootCommand(Utils.iptables + " -t nat -F OUTPUT") Console.runRootCommand(Utils.iptables + " -t nat -F OUTPUT")
} } else Console.runCommand(cmd.toArray)
} }
def cancelStart() { def cancelStart() {
...@@ -344,15 +340,13 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -344,15 +340,13 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
def prepareStartService() { def prepareStartService() {
Utils.ThrowableFuture { Utils.ThrowableFuture {
if (app.isVpnEnabled) { if (app.isNatEnabled) serviceLoad() else {
val intent = VpnService.prepare(this) val intent = VpnService.prepare(this)
if (intent != null) { if (intent != null) {
startActivityForResult(intent, REQUEST_CONNECT) startActivityForResult(intent, REQUEST_CONNECT)
} else { } else {
handler.post(() => onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null)) handler.post(() => onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null))
} }
} else {
serviceLoad()
} }
} }
} }
...@@ -412,14 +406,11 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -412,14 +406,11 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
success = false success = false
result = getString(R.string.connection_test_error, e.getMessage) result = getString(R.string.connection_test_error, e.getMessage)
} }
synchronized(if (testCount == id) { synchronized(if (testCount == id && app.isVpnEnabled) handler.post(() =>
if (app.isVpnEnabled) handler.post(() => { if (success) connectionTestText.setText(result) else {
if (success) connectionTestText.setText(result) else { connectionTestText.setText(R.string.connection_test_fail)
connectionTestText.setText(R.string.connection_test_fail) Snackbar.make(findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show
Snackbar.make(findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show }))
}
})
})
} }
} }
} }
...@@ -470,10 +461,10 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { ...@@ -470,10 +461,10 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext {
preferences.setEnabled(false) preferences.setEnabled(false)
fabProgressCircle.postDelayed(hideCircle, 100) fabProgressCircle.postDelayed(hideCircle, 100)
stat.setVisibility(View.VISIBLE) stat.setVisibility(View.VISIBLE)
if (app.isVpnEnabled) { if (app.isNatEnabled) connectionTestText.setVisibility(View.GONE) else {
connectionTestText.setVisibility(View.VISIBLE) connectionTestText.setVisibility(View.VISIBLE)
connectionTestText.setText(getString(R.string.connection_test_pending)) connectionTestText.setText(getString(R.string.connection_test_pending))
} else connectionTestText.setVisibility(View.GONE) }
case State.STOPPING => case State.STOPPING =>
fab.setBackgroundTintList(greyTint) fab.setBackgroundTintList(greyTint)
serviceStarted = false serviceStarted = false
......
...@@ -67,7 +67,8 @@ class ShadowsocksApplication extends Application { ...@@ -67,7 +67,8 @@ class ShadowsocksApplication extends Application {
lazy val editor = settings.edit lazy val editor = settings.edit
lazy val profileManager = new ProfileManager(settings, dbHelper) lazy val profileManager = new ProfileManager(settings, dbHelper)
def isVpnEnabled = !settings.getBoolean(Key.isNAT, false) def isNatEnabled = settings.getBoolean(Key.isNAT, false)
def isVpnEnabled = !isNatEnabled
def getVersionName = try { def getVersionName = try {
getPackageManager.getPackageInfo(getPackageName, 0).versionName getPackageManager.getPackageInfo(getPackageName, 0).versionName
......
...@@ -65,16 +65,16 @@ class ShadowsocksRunnerActivity extends Activity with ServiceBoundContext { ...@@ -65,16 +65,16 @@ class ShadowsocksRunnerActivity extends Activity with ServiceBoundContext {
} }
def startBackgroundService() { def startBackgroundService() {
if (app.isVpnEnabled) { if (app.isNatEnabled) {
bgService.use(ConfigUtils.loadFromSharedPreferences)
finish()
} else {
val intent = VpnService.prepare(ShadowsocksRunnerActivity.this) val intent = VpnService.prepare(ShadowsocksRunnerActivity.this)
if (intent != null) { if (intent != null) {
startActivityForResult(intent, REQUEST_CONNECT) startActivityForResult(intent, REQUEST_CONNECT)
} else { } else {
onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null) onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null)
} }
} else {
bgService.use(ConfigUtils.loadFromSharedPreferences)
finish()
} }
} }
......
...@@ -58,15 +58,13 @@ class ShadowsocksRunnerService extends Service with ServiceBoundContext { ...@@ -58,15 +58,13 @@ class ShadowsocksRunnerService extends Service with ServiceBoundContext {
} }
def startBackgroundService() { def startBackgroundService() {
if (app.isVpnEnabled) { if (app.isNatEnabled) bgService.use(ConfigUtils.loadFromSharedPreferences) else {
val intent = VpnService.prepare(ShadowsocksRunnerService.this) val intent = VpnService.prepare(ShadowsocksRunnerService.this)
if (intent == null) { if (intent == null) {
if (bgService != null) { if (bgService != null) {
bgService.use(ConfigUtils.loadFromSharedPreferences) bgService.use(ConfigUtils.loadFromSharedPreferences)
} }
} }
} else {
bgService.use(ConfigUtils.loadFromSharedPreferences)
} }
stopSelf() stopSelf()
} }
......
...@@ -156,7 +156,7 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan ...@@ -156,7 +156,7 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
for (name <- Key.isNAT #:: PROXY_PREFS.toStream #::: FEATURE_PREFS.toStream) { for (name <- Key.isNAT #:: PROXY_PREFS.toStream #::: FEATURE_PREFS.toStream) {
val pref = findPreference(name) val pref = findPreference(name)
if (pref != null) pref.setEnabled(enabled && if (pref != null) pref.setEnabled(enabled &&
(name != Key.isProxyApps || Utils.isLollipopOrAbove || !app.isVpnEnabled)) (name != Key.isProxyApps || Utils.isLollipopOrAbove || app.isNatEnabled))
} }
} }
......
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