From 86f32ea67c3280c61e57f2b9d7ad88178fe370ed Mon Sep 17 00:00:00 2001 From: Mygod Date: Tue, 24 May 2016 11:52:06 +0800 Subject: [PATCH] Add app.isNatEnabled for performance and readability Warning: This is a boring commit. --- .../shadowsocks/ServiceBoundContext.scala | 3 +- .../com/github/shadowsocks/Shadowsocks.scala | 37 +++++++------------ .../shadowsocks/ShadowsocksApplication.scala | 3 +- .../ShadowsocksRunnerActivity.scala | 8 ++-- .../ShadowsocksRunnerService.scala | 4 +- .../shadowsocks/ShadowsocksSettings.scala | 2 +- 6 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala b/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala index d43d3f89..c2df8d68 100644 --- a/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala +++ b/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala @@ -55,8 +55,7 @@ trait ServiceBoundContext extends Context with IBinder.DeathRecipient { def attachService(callback: IShadowsocksServiceCallback.Stub = null) { this.callback = callback if (bgService == null) { - val s = - if (app.isVpnEnabled) classOf[ShadowsocksVpnService] else classOf[ShadowsocksNatService] + val s = if (app.isNatEnabled) classOf[ShadowsocksNatService] else classOf[ShadowsocksVpnService] val intent = new Intent(this, s) intent.setAction(Action.SERVICE) diff --git a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala index abf21ecb..166974d6 100644 --- a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala +++ b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala @@ -130,10 +130,10 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { changeSwitch(checked = true) preferences.setEnabled(false) stat.setVisibility(View.VISIBLE) - if (app.isVpnEnabled) { + if (app.isNatEnabled) connectionTestText.setVisibility(View.GONE) else { connectionTestText.setVisibility(View.VISIBLE) connectionTestText.setText(getString(R.string.connection_test_pending)) - } else connectionTestText.setVisibility(View.GONE) + } case State.STOPPED => fab.setBackgroundTintList(greyTint) fabProgressCircle.postDelayed(hideCircle, 1000) @@ -178,7 +178,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { // Update the UI if (fab != null) fab.setEnabled(true) 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) snackbar.setAction(R.string.switch_to_vpn, (_ => preferences.natSwitch.setChecked(false)): View.OnClickListener) snackbar.show @@ -307,11 +307,7 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { cmd.append("chmod 666 %s/%s-vpn.pid".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task)) } - if (app.isVpnEnabled) { - Console.runCommand(cmd.toArray) - } else { - Console.runRootCommand(cmd.toArray) - } + if (app.isNatEnabled) Console.runRootCommand(cmd.toArray) else Console.runCommand(cmd.toArray) cmd.clear() @@ -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.conf".formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task)) } - if (app.isVpnEnabled) Console.runCommand(cmd.toArray) else { + if (app.isNatEnabled) { Console.runRootCommand(cmd.toArray) Console.runRootCommand(Utils.iptables + " -t nat -F OUTPUT") - } + } else Console.runCommand(cmd.toArray) } def cancelStart() { @@ -344,15 +340,13 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { def prepareStartService() { Utils.ThrowableFuture { - if (app.isVpnEnabled) { + if (app.isNatEnabled) serviceLoad() else { val intent = VpnService.prepare(this) if (intent != null) { startActivityForResult(intent, REQUEST_CONNECT) } else { handler.post(() => onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null)) } - } else { - serviceLoad() } } } @@ -412,14 +406,11 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { success = false result = getString(R.string.connection_test_error, e.getMessage) } - synchronized(if (testCount == id) { - if (app.isVpnEnabled) handler.post(() => { - if (success) connectionTestText.setText(result) else { - connectionTestText.setText(R.string.connection_test_fail) - Snackbar.make(findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show - } - }) - }) + synchronized(if (testCount == id && app.isVpnEnabled) handler.post(() => + if (success) connectionTestText.setText(result) else { + connectionTestText.setText(R.string.connection_test_fail) + Snackbar.make(findViewById(android.R.id.content), result, Snackbar.LENGTH_LONG).show + })) } } } @@ -470,10 +461,10 @@ class Shadowsocks extends AppCompatActivity with ServiceBoundContext { preferences.setEnabled(false) fabProgressCircle.postDelayed(hideCircle, 100) stat.setVisibility(View.VISIBLE) - if (app.isVpnEnabled) { + if (app.isNatEnabled) connectionTestText.setVisibility(View.GONE) else { connectionTestText.setVisibility(View.VISIBLE) connectionTestText.setText(getString(R.string.connection_test_pending)) - } else connectionTestText.setVisibility(View.GONE) + } case State.STOPPING => fab.setBackgroundTintList(greyTint) serviceStarted = false diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala index e1f10b07..91bdb4c8 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala @@ -67,7 +67,8 @@ class ShadowsocksApplication extends Application { lazy val editor = settings.edit 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 { getPackageManager.getPackageInfo(getPackageName, 0).versionName diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerActivity.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerActivity.scala index 58c164ba..bc71eb50 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerActivity.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerActivity.scala @@ -65,16 +65,16 @@ class ShadowsocksRunnerActivity extends Activity with ServiceBoundContext { } def startBackgroundService() { - if (app.isVpnEnabled) { + if (app.isNatEnabled) { + bgService.use(ConfigUtils.loadFromSharedPreferences) + finish() + } else { val intent = VpnService.prepare(ShadowsocksRunnerActivity.this) if (intent != null) { startActivityForResult(intent, REQUEST_CONNECT) } else { onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null) } - } else { - bgService.use(ConfigUtils.loadFromSharedPreferences) - finish() } } diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerService.scala index daa8a7cc..929b6161 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksRunnerService.scala @@ -58,15 +58,13 @@ class ShadowsocksRunnerService extends Service with ServiceBoundContext { } def startBackgroundService() { - if (app.isVpnEnabled) { + if (app.isNatEnabled) bgService.use(ConfigUtils.loadFromSharedPreferences) else { val intent = VpnService.prepare(ShadowsocksRunnerService.this) if (intent == null) { if (bgService != null) { bgService.use(ConfigUtils.loadFromSharedPreferences) } } - } else { - bgService.use(ConfigUtils.loadFromSharedPreferences) } stopSelf() } diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala index b57dded7..61ef14c3 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala @@ -156,7 +156,7 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan for (name <- Key.isNAT #:: PROXY_PREFS.toStream #::: FEATURE_PREFS.toStream) { val pref = findPreference(name) if (pref != null) pref.setEnabled(enabled && - (name != Key.isProxyApps || Utils.isLollipopOrAbove || !app.isVpnEnabled)) + (name != Key.isProxyApps || Utils.isLollipopOrAbove || app.isNatEnabled)) } }