Commit 9fb0639f authored by Mygod's avatar Mygod

Move preference related logic to ShadowsocksSettings

parent 164becfe
...@@ -49,7 +49,6 @@ import android.content.res.AssetManager ...@@ -49,7 +49,6 @@ import android.content.res.AssetManager
import android.graphics.Typeface import android.graphics.Typeface
import android.net.VpnService import android.net.VpnService
import android.os._ import android.os._
import android.preference.{Preference, SwitchPreference}
import android.support.design.widget.{FloatingActionButton, Snackbar} import android.support.design.widget.{FloatingActionButton, Snackbar}
import android.support.v4.content.ContextCompat import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
...@@ -60,7 +59,6 @@ import android.widget._ ...@@ -60,7 +59,6 @@ import android.widget._
import com.github.jorgecastilloprz.FABProgressCircle import com.github.jorgecastilloprz.FABProgressCircle
import com.github.shadowsocks.aidl.IShadowsocksServiceCallback import com.github.shadowsocks.aidl.IShadowsocksServiceCallback
import com.github.shadowsocks.database._ import com.github.shadowsocks.database._
import com.github.shadowsocks.preferences.{DropDownPreference, NumberPickerPreference, PasswordEditTextPreference, SummaryEditTextPreference}
import com.github.shadowsocks.utils._ import com.github.shadowsocks.utils._
import com.google.android.gms.ads.{AdRequest, AdSize, AdView} import com.google.android.gms.ads.{AdRequest, AdSize, AdView}
...@@ -91,52 +89,9 @@ object Shadowsocks { ...@@ -91,52 +89,9 @@ object Shadowsocks {
// Constants // Constants
val TAG = "Shadowsocks" val TAG = "Shadowsocks"
val REQUEST_CONNECT = 1 val REQUEST_CONNECT = 1
val PREFS_NAME = "Shadowsocks"
val PROXY_PREFS = Array(Key.profileName, Key.proxy, Key.remotePort, Key.localPort, Key.sitekey, Key.encMethod,
Key.isAuth)
val FEATURE_PREFS = Array(Key.route, Key.isProxyApps, Key.isUdpDns, Key.isIpv6)
val EXECUTABLES = Array(Executable.PDNSD, Executable.REDSOCKS, Executable.SS_TUNNEL, Executable.SS_LOCAL, val EXECUTABLES = Array(Executable.PDNSD, Executable.REDSOCKS, Executable.SS_TUNNEL, Executable.SS_LOCAL,
Executable.TUN2SOCKS) Executable.TUN2SOCKS)
// Helper functions
def updateDropDownPreference(pref: Preference, value: String) {
pref.asInstanceOf[DropDownPreference].setValue(value)
}
def updatePasswordEditTextPreference(pref: Preference, value: String) {
pref.setSummary(value)
pref.asInstanceOf[PasswordEditTextPreference].setText(value)
}
def updateNumberPickerPreference(pref: Preference, value: Int) {
pref.asInstanceOf[NumberPickerPreference].setValue(value)
}
def updateSummaryEditTextPreference(pref: Preference, value: String) {
pref.setSummary(value)
pref.asInstanceOf[SummaryEditTextPreference].setText(value)
}
def updateSwitchPreference(pref: Preference, value: Boolean) {
pref.asInstanceOf[SwitchPreference].setChecked(value)
}
def updatePreference(pref: Preference, name: String, profile: Profile) {
name match {
case Key.profileName => updateSummaryEditTextPreference(pref, profile.name)
case Key.proxy => updateSummaryEditTextPreference(pref, profile.host)
case Key.remotePort => updateNumberPickerPreference(pref, profile.remotePort)
case Key.localPort => updateNumberPickerPreference(pref, profile.localPort)
case Key.sitekey => updatePasswordEditTextPreference(pref, profile.password)
case Key.encMethod => updateDropDownPreference(pref, profile.method)
case Key.route => updateDropDownPreference(pref, profile.route)
case Key.isProxyApps => updateSwitchPreference(pref, profile.proxyApps)
case Key.isUdpDns => updateSwitchPreference(pref, profile.udpdns)
case Key.isAuth => updateSwitchPreference(pref, profile.auth)
case Key.isIpv6 => updateSwitchPreference(pref, profile.ipv6)
}
}
} }
class Shadowsocks class Shadowsocks
...@@ -166,7 +121,7 @@ class Shadowsocks ...@@ -166,7 +121,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) preferences.setEnabled(false)
case State.CONNECTED => case State.CONNECTED =>
fab.setBackgroundTintList(greenTint) fab.setBackgroundTintList(greenTint)
if (state == State.CONNECTING) { if (state == State.CONNECTING) {
...@@ -176,7 +131,7 @@ class Shadowsocks ...@@ -176,7 +131,7 @@ class Shadowsocks
} }
fab.setEnabled(true) fab.setEnabled(true)
changeSwitch(checked = true) changeSwitch(checked = true)
setPreferenceEnabled(enabled = false) preferences.setEnabled(false)
case State.STOPPED => case State.STOPPED =>
fab.setBackgroundTintList(greyTint) fab.setBackgroundTintList(greyTint)
handler.postDelayed(() => fabProgressCircle.hide(), 1000) handler.postDelayed(() => fabProgressCircle.hide(), 1000)
...@@ -189,13 +144,13 @@ class Shadowsocks ...@@ -189,13 +144,13 @@ class Shadowsocks
(_ => preferences.natSwitch.setChecked(false)): View.OnClickListener) (_ => preferences.natSwitch.setChecked(false)): View.OnClickListener)
snackbar.show snackbar.show
} }
setPreferenceEnabled(enabled = true) preferences.setEnabled(true)
case State.STOPPING => case State.STOPPING =>
fab.setBackgroundTintList(greyTint) fab.setBackgroundTintList(greyTint)
fab.setImageResource(R.drawable.ic_cloud_queue) fab.setImageResource(R.drawable.ic_cloud_queue)
fab.setEnabled(false) fab.setEnabled(false)
if (state == State.CONNECTED) fabProgressCircle.show() // ignore for stopped if (state == State.CONNECTED) fabProgressCircle.show() // ignore for stopped
setPreferenceEnabled(enabled = false) preferences.setEnabled(false)
} }
state = s state = s
}) })
...@@ -367,12 +322,7 @@ class Shadowsocks ...@@ -367,12 +322,7 @@ class Shadowsocks
changeSwitch(checked = false) changeSwitch(checked = false)
} }
def isReady: Boolean = { def isReady: Boolean = checkText(Key.proxy) && checkText(Key.sitekey) && bgService != null
if (!checkText(Key.proxy)) return false
if (!checkText(Key.sitekey)) return false
if (bgService == null) return false
true
}
def prepareStartService() { def prepareStartService() {
ThrowableFuture { ThrowableFuture {
...@@ -462,25 +412,25 @@ class Shadowsocks ...@@ -462,25 +412,25 @@ class Shadowsocks
fab.setBackgroundTintList(greyTint) fab.setBackgroundTintList(greyTint)
serviceStarted = false serviceStarted = false
fab.setImageResource(R.drawable.ic_cloud_queue) fab.setImageResource(R.drawable.ic_cloud_queue)
setPreferenceEnabled(false) preferences.setEnabled(false)
fabProgressCircle.show() fabProgressCircle.show()
case State.CONNECTED => case State.CONNECTED =>
fab.setBackgroundTintList(greenTint) fab.setBackgroundTintList(greenTint)
serviceStarted = true serviceStarted = true
fab.setImageResource(R.drawable.ic_cloud) fab.setImageResource(R.drawable.ic_cloud)
setPreferenceEnabled(false) preferences.setEnabled(false)
fabProgressCircle.postDelayed(fabProgressCircle.hide, 100) fabProgressCircle.postDelayed(fabProgressCircle.hide, 100)
case State.STOPPING => case State.STOPPING =>
fab.setBackgroundTintList(greyTint) fab.setBackgroundTintList(greyTint)
serviceStarted = false serviceStarted = false
fab.setImageResource(R.drawable.ic_cloud_queue) fab.setImageResource(R.drawable.ic_cloud_queue)
setPreferenceEnabled(false) preferences.setEnabled(false)
fabProgressCircle.show() fabProgressCircle.show()
case _ => case _ =>
fab.setBackgroundTintList(greyTint) fab.setBackgroundTintList(greyTint)
serviceStarted = false serviceStarted = false
fab.setImageResource(R.drawable.ic_cloud_off) fab.setImageResource(R.drawable.ic_cloud_off)
setPreferenceEnabled(true) preferences.setEnabled(true)
fabProgressCircle.postDelayed(fabProgressCircle.hide, 100) fabProgressCircle.postDelayed(fabProgressCircle.hide, 100)
} }
state = bgService.getState state = bgService.getState
...@@ -498,26 +448,6 @@ class Shadowsocks ...@@ -498,26 +448,6 @@ class Shadowsocks
updateTraffic() updateTraffic()
} }
private def setPreferenceEnabled(enabled: Boolean) {
preferences.findPreference(Key.isNAT).setEnabled(enabled)
for (name <- Shadowsocks.PROXY_PREFS) {
val pref = preferences.findPreference(name)
if (pref != null) {
pref.setEnabled(enabled)
}
}
for (name <- Shadowsocks.FEATURE_PREFS) {
val pref = preferences.findPreference(name)
if (pref != null) {
if (name == Key.isProxyApps) {
pref.setEnabled(enabled && (Utils.isLollipopOrAbove || !ShadowsocksApplication.isVpnEnabled))
} else {
pref.setEnabled(enabled)
}
}
}
}
private def updatePreferenceScreen() { private def updatePreferenceScreen() {
val profile = currentProfile val profile = currentProfile
if (profile.host == "198.199.101.152" && adView == null) { if (profile.host == "198.199.101.152" && adView == null) {
...@@ -528,14 +458,7 @@ class Shadowsocks ...@@ -528,14 +458,7 @@ class Shadowsocks
adView.loadAd(new AdRequest.Builder().build()) adView.loadAd(new AdRequest.Builder().build())
} }
for (name <- Shadowsocks.PROXY_PREFS) { preferences.update(profile)
val pref = preferences.findPreference(name)
Shadowsocks.updatePreference(pref, name, profile)
}
for (name <- Shadowsocks.FEATURE_PREFS) {
val pref = preferences.findPreference(name)
Shadowsocks.updatePreference(pref, name, profile)
}
} }
override def onStop() { override def onStop() {
......
...@@ -11,11 +11,61 @@ import android.os.{Build, Bundle} ...@@ -11,11 +11,61 @@ import android.os.{Build, Bundle}
import android.preference.{Preference, PreferenceFragment, SwitchPreference} import android.preference.{Preference, PreferenceFragment, SwitchPreference}
import android.support.v7.app.AlertDialog import android.support.v7.app.AlertDialog
import android.webkit.{WebView, WebViewClient} import android.webkit.{WebView, WebViewClient}
import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.preferences.{DropDownPreference, NumberPickerPreference, PasswordEditTextPreference, SummaryEditTextPreference}
import com.github.shadowsocks.utils.CloseUtils._ import com.github.shadowsocks.utils.CloseUtils._
import com.github.shadowsocks.utils.Key import com.github.shadowsocks.utils.{Key, Utils}
object ShadowsocksSettings {
// Constants
val PREFS_NAME = "Shadowsocks"
val PROXY_PREFS = Array(Key.profileName, Key.proxy, Key.remotePort, Key.localPort, Key.sitekey, Key.encMethod,
Key.isAuth)
val FEATURE_PREFS = Array(Key.route, Key.isProxyApps, Key.isUdpDns, Key.isIpv6)
// Helper functions
def updateDropDownPreference(pref: Preference, value: String) {
pref.asInstanceOf[DropDownPreference].setValue(value)
}
def updatePasswordEditTextPreference(pref: Preference, value: String) {
pref.setSummary(value)
pref.asInstanceOf[PasswordEditTextPreference].setText(value)
}
def updateNumberPickerPreference(pref: Preference, value: Int) {
pref.asInstanceOf[NumberPickerPreference].setValue(value)
}
def updateSummaryEditTextPreference(pref: Preference, value: String) {
pref.setSummary(value)
pref.asInstanceOf[SummaryEditTextPreference].setText(value)
}
def updateSwitchPreference(pref: Preference, value: Boolean) {
pref.asInstanceOf[SwitchPreference].setChecked(value)
}
def updatePreference(pref: Preference, name: String, profile: Profile) {
name match {
case Key.profileName => updateSummaryEditTextPreference(pref, profile.name)
case Key.proxy => updateSummaryEditTextPreference(pref, profile.host)
case Key.remotePort => updateNumberPickerPreference(pref, profile.remotePort)
case Key.localPort => updateNumberPickerPreference(pref, profile.localPort)
case Key.sitekey => updatePasswordEditTextPreference(pref, profile.password)
case Key.encMethod => updateDropDownPreference(pref, profile.method)
case Key.route => updateDropDownPreference(pref, profile.route)
case Key.isProxyApps => updateSwitchPreference(pref, profile.proxyApps)
case Key.isUdpDns => updateSwitchPreference(pref, profile.udpdns)
case Key.isAuth => updateSwitchPreference(pref, profile.auth)
case Key.isIpv6 => updateSwitchPreference(pref, profile.ipv6)
}
}
}
// TODO: Move related logic here
class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChangeListener { class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChangeListener {
import ShadowsocksSettings._
private def activity = getActivity.asInstanceOf[Shadowsocks] private def activity = getActivity.asInstanceOf[Shadowsocks]
lazy val natSwitch = findPreference(Key.isNAT).asInstanceOf[SwitchPreference] lazy val natSwitch = findPreference(Key.isNAT).asInstanceOf[SwitchPreference]
var stat: Preference = _ var stat: Preference = _
...@@ -128,4 +178,28 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan ...@@ -128,4 +178,28 @@ class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChan
}) })
case _ => case _ =>
} }
def setEnabled(enabled: Boolean) {
findPreference(Key.isNAT).setEnabled(enabled)
for (name <- PROXY_PREFS) {
val pref = findPreference(name)
if (pref != null) pref.setEnabled(enabled)
}
for (name <- FEATURE_PREFS) {
val pref = findPreference(name)
if (pref != null) pref.setEnabled(enabled &&
(name != Key.isProxyApps || Utils.isLollipopOrAbove || !ShadowsocksApplication.isVpnEnabled))
}
}
def update(profile: Profile) {
for (name <- PROXY_PREFS) {
val pref = findPreference(name)
updatePreference(pref, name, profile)
}
for (name <- FEATURE_PREFS) {
val pref = findPreference(name)
updatePreference(pref, name, profile)
}
}
} }
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