Add app.isNatEnabled for performance and readability
Warning: This is a boring commit.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user