From b05adbdd434ce7c8cb3cc95c1bf96fed418b30df Mon Sep 17 00:00:00 2001 From: Mygod Date: Mon, 9 Nov 2015 20:32:22 +0800 Subject: [PATCH] Remove redundant Global Proxy --- src/main/AndroidManifest.xml | 12 +--- .../com/github/shadowsocks/aidl/Config.java | 20 ++---- src/main/res/layout/layout_apps.xml | 70 ++++++++++++------- src/main/res/layout/layout_apps_item.xml | 2 +- src/main/res/values-zh/strings.xml | 4 +- src/main/res/values/strings.xml | 4 +- src/main/res/xml/pref_all.xml | 14 +--- .../com/github/shadowsocks/AppManager.scala | 8 ++- .../com/github/shadowsocks/Shadowsocks.scala | 17 +++-- .../shadowsocks/ShadowsocksNatService.scala | 4 +- .../shadowsocks/ShadowsocksSettings.scala | 15 +++- .../shadowsocks/ShadowsocksVpnService.scala | 2 +- .../shadowsocks/database/DBHelper.scala | 28 +++++--- .../github/shadowsocks/database/Profile.scala | 20 +----- .../shadowsocks/database/ProfileManager.scala | 8 +-- .../shadowsocks/utils/ConfigUtils.scala | 13 ++-- .../github/shadowsocks/utils/Constants.scala | 5 +- 17 files changed, 122 insertions(+), 124 deletions(-) diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index c5ac5989..14c0bb78 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -64,7 +64,7 @@ @@ -75,14 +75,8 @@ - - - - - + android:label="@string/proxied_apps" + android:parentActivityName=".Shadowsocks"/> - - + + + + + + + - + + + android:layout_marginRight="8dp"/> diff --git a/src/main/res/values-zh/strings.xml b/src/main/res/values-zh/strings.xml index 02bd0674..da320a87 100755 --- a/src/main/res/values-zh/strings.xml +++ b/src/main/res/values-zh/strings.xml @@ -25,11 +25,9 @@ 国内路由 绕过所有位于中国的网站 路由 - 全局代理 - 设置系统代理 分应用代理 为应用程序分别设置代理 - 分应用代理 + 开 绕行模式 启用该选项,以使所选应用程序的流量不经过代理 自动连接 diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index cd88ddf2..597957e6 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -40,11 +40,9 @@ Bypass all sites located in China Route - Global Proxy - Set up system-wide proxy Per-App Proxy Set proxy for selected apps - Per-App Proxy + On Bypass Mode Enable this option to bypass selected apps Auto Connect diff --git a/src/main/res/xml/pref_all.xml b/src/main/res/xml/pref_all.xml index aa33012e..8382cabc 100644 --- a/src/main/res/xml/pref_all.xml +++ b/src/main/res/xml/pref_all.xml @@ -77,18 +77,10 @@ android:summary="@string/ipv6_summary" android:title="@string/ipv6"/> - - - + android:title="@string/proxied_apps"/> { val intent = getParentActivityIntent @@ -268,6 +268,12 @@ class AppManager extends AppCompatActivity with OnCheckedChangeListener with OnC WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT)) + findViewById(R.id.onSwitch).asInstanceOf[Switch] + .setOnCheckedChangeListener((button: CompoundButton, checked: Boolean) => { + ShadowsocksApplication.settings.edit().putBoolean(Key.isProxyApps, checked).apply() + finish() + }) + val bypassSwitch = findViewById(R.id.bypassSwitch).asInstanceOf[Switch] bypassSwitch.setOnCheckedChangeListener((button: CompoundButton, checked: Boolean) => ShadowsocksApplication.settings.edit().putBoolean(Key.isBypassApps, checked).apply()) diff --git a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala index 7e9abe41..3989c9cf 100644 --- a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala +++ b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala @@ -60,7 +60,7 @@ import android.widget._ import com.github.jorgecastilloprz.FABProgressCircle import com.github.shadowsocks.aidl.{IShadowsocksService, IShadowsocksServiceCallback} import com.github.shadowsocks.database._ -import com.github.shadowsocks.preferences.{DropDownPreference, PasswordEditTextPreference, SummaryEditTextPreference} +import com.github.shadowsocks.preferences.{NumberPickerPreference, DropDownPreference, PasswordEditTextPreference, SummaryEditTextPreference} import com.github.shadowsocks.utils._ import com.google.android.gms.ads.{AdRequest, AdSize, AdView} @@ -98,7 +98,7 @@ object Shadowsocks { 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.isGlobalProxy, Key.proxyedApps, Key.isUdpDns, Key.isIpv6) + 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, Executable.TUN2SOCKS) @@ -112,6 +112,10 @@ object Shadowsocks { pref.asInstanceOf[PasswordEditTextPreference].setText(value) } + def updateNumberPickerPreference(pref: Preference, value: Int): Unit = { + pref.asInstanceOf[NumberPickerPreference].setValue(value) + } + def updateSummaryEditTextPreference(pref: Preference, value: String) { pref.setSummary(value) pref.asInstanceOf[SummaryEditTextPreference].setText(value) @@ -125,12 +129,12 @@ object Shadowsocks { name match { case Key.profileName => updateSummaryEditTextPreference(pref, profile.name) case Key.proxy => updateSummaryEditTextPreference(pref, profile.host) - case Key.remotePort => updateSummaryEditTextPreference(pref, profile.remotePort.toString) - case Key.localPort => updateSummaryEditTextPreference(pref, profile.localPort.toString) + 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.isGlobalProxy => updateSwitchPreference(pref, profile.global) + 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) @@ -501,8 +505,7 @@ class Shadowsocks for (name <- Shadowsocks.FEATURE_PREFS) { val pref = preferences.findPreference(name) if (pref != null) { - if (Seq(Key.isGlobalProxy, Key.proxyedApps) - .contains(name)) { + if (name == Key.isProxyApps) { pref.setEnabled(enabled && (Utils.isLollipopOrAbove || !ShadowsocksApplication.isVpnEnabled)) } else { pref.setEnabled(enabled) diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala index 1ea60a5f..54fdfa3d 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala @@ -390,10 +390,10 @@ class ShadowsocksNatService extends Service with BaseService { init_sb.append(Utils.getIptables + " -t nat -A OUTPUT -p udp --dport 53 -j DNAT --to-destination 127.0.0.1:" + 8153) - if (config.isGlobalProxy || config.isBypassApps) { + if (!config.isProxyApps || config.isBypassApps) { http_sb.append(Utils.getIptables + CMD_IPTABLES_DNAT_ADD_SOCKS) } - if (!config.isGlobalProxy) { + if (config.isProxyApps) { if (apps == null || apps.length <= 0) { apps = AppManager.getProxiedApps(this, config.proxiedAppString) } diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala index 79de4e4b..82a3f0d4 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala @@ -6,7 +6,7 @@ import android.app.AlertDialog import android.content.{DialogInterface, Intent} import android.net.Uri import android.os.{Build, Bundle} -import android.preference.{Preference, PreferenceFragment} +import android.preference.{SwitchPreference, Preference, PreferenceFragment} import android.webkit.{WebViewClient, WebView} import com.github.shadowsocks.utils.Key @@ -14,10 +14,18 @@ import com.github.shadowsocks.utils.Key class ShadowsocksSettings extends PreferenceFragment { private lazy val activity = getActivity.asInstanceOf[Shadowsocks] + private var isProxyApps: SwitchPreference = _ + override def onCreate(savedInstanceState: Bundle) { super.onCreate(savedInstanceState) addPreferencesFromResource(R.xml.pref_all) + isProxyApps = findPreference(Key.isProxyApps).asInstanceOf[SwitchPreference] + isProxyApps.setOnPreferenceChangeListener((preference: Preference, newValue: Any) => { + startActivity(new Intent(activity, classOf[AppManager])) + newValue.asInstanceOf[Boolean] // keep it ON + }) + findPreference(Key.isNAT).setOnPreferenceChangeListener((preference: Preference, newValue: Any) => if (ShadowsocksApplication.isRoot) activity.handler.post(() => { activity.deattachService() @@ -62,4 +70,9 @@ class ShadowsocksSettings extends PreferenceFragment { true }) } + + override def onResume = { + super.onResume + isProxyApps.setChecked(ShadowsocksApplication.settings.getBoolean(Key.isProxyApps, false)) // update + } } diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala index ba14253f..0b454a62 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala @@ -394,7 +394,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { if (Utils.isLollipopOrAbove) { - if (!config.isGlobalProxy) { + if (config.isProxyApps) { val apps = AppManager.getProxiedApps(this, config.proxiedAppString) val pkgSet: mutable.HashSet[String] = new mutable.HashSet[String] for (app <- apps) { diff --git a/src/main/scala/com/github/shadowsocks/database/DBHelper.scala b/src/main/scala/com/github/shadowsocks/database/DBHelper.scala index b3e39670..d2ed5a4c 100644 --- a/src/main/scala/com/github/shadowsocks/database/DBHelper.scala +++ b/src/main/scala/com/github/shadowsocks/database/DBHelper.scala @@ -39,19 +39,19 @@ package com.github.shadowsocks.database -import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper import android.content.Context import android.database.sqlite.SQLiteDatabase +import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper +import com.j256.ormlite.dao.Dao import com.j256.ormlite.support.ConnectionSource import com.j256.ormlite.table.TableUtils -import com.j256.ormlite.dao.Dao object DBHelper { val PROFILE = "profile.db" } class DBHelper(val context: Context) - extends OrmLiteSqliteOpenHelper(context, DBHelper.PROFILE, null, 11) { + extends OrmLiteSqliteOpenHelper(context, DBHelper.PROFILE, null, 12) { lazy val profileDao: Dao[Profile, Int] = getDao(classOf[Profile]) @@ -63,21 +63,31 @@ class DBHelper(val context: Context) newVersion: Int) { if (oldVersion != newVersion) { if (oldVersion < 7) { - profileDao.executeRaw("DROP TABLE IF EXISTS 'profile';") + profileDao.executeRawNoArgs("DROP TABLE IF EXISTS 'profile';") onCreate(database, connectionSource) } else { if (oldVersion < 8) { - profileDao.executeRaw("ALTER TABLE `profile` ADD COLUMN udpdns SMALLINT;") - profileDao.executeRaw("ALTER TABLE `profile` ADD COLUMN route VARCHAR;") + profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN udpdns SMALLINT;") + profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN route VARCHAR;") } if (oldVersion < 9) { - profileDao.executeRaw("ALTER TABLE `profile` ADD COLUMN route VARCHAR;") + profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN route VARCHAR;") } if (oldVersion < 10) { - profileDao.executeRaw("ALTER TABLE `profile` ADD COLUMN auth SMALLINT;") + profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN auth SMALLINT;") } if (oldVersion < 11) { - profileDao.executeRaw("ALTER TABLE `profile` ADD COLUMN ipv6 SMALLINT;") + profileDao.executeRawNoArgs("ALTER TABLE `profile` ADD COLUMN ipv6 SMALLINT;") + } + if (oldVersion < 12) { + profileDao.executeRawNoArgs("ALTER TABLE `profile` RENAME TO `tmp`;") + onCreate(database, connectionSource) + profileDao.executeRawNoArgs( + "INSERT INTO `profile`(id, name, host, localPort, remotePort, password, method, route, proxyApps, bypass," + + " udpdns, auth, ipv6, individual) " + + "SELECT id, name, host, localPort, remotePort, password, method, route, 1 - global, bypass, udpdns, auth," + + " ipv6, individual FROM `tmp`;") + profileDao.executeRawNoArgs("DROP TABLE `tmp`;") } } } diff --git a/src/main/scala/com/github/shadowsocks/database/Profile.scala b/src/main/scala/com/github/shadowsocks/database/Profile.scala index 19d48932..8be48264 100644 --- a/src/main/scala/com/github/shadowsocks/database/Profile.scala +++ b/src/main/scala/com/github/shadowsocks/database/Profile.scala @@ -63,30 +63,15 @@ class Profile { @DatabaseField var method: String = "rc4" - @DatabaseField - var date: String = "" - @DatabaseField var route: String = "all" @DatabaseField - var upload: Int = 0 - - @DatabaseField - var download: Int = 0 - - @DatabaseField - var chnroute: Boolean = true - - @DatabaseField - var global: Boolean = true + var proxyApps: Boolean = false @DatabaseField var bypass: Boolean = false - @DatabaseField - var traffic: Boolean = false - @DatabaseField var udpdns: Boolean = false @@ -98,7 +83,4 @@ class Profile { @DatabaseField(dataType = DataType.LONG_STRING) var individual: String = "" - - @DatabaseField(dataType = DataType.LONG_STRING) - var description: String = "" } diff --git a/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala b/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala index c9e9d099..24da9121 100644 --- a/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala +++ b/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala @@ -117,10 +117,8 @@ class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) { } val edit = settings.edit() - edit.putBoolean(Key.isGlobalProxy, profile.global) - edit.putBoolean(Key.isGFWList, profile.chnroute) + edit.putBoolean(Key.isProxyApps, profile.proxyApps) edit.putBoolean(Key.isBypassApps, profile.bypass) - edit.putBoolean(Key.isTrafficStat, profile.traffic) edit.putBoolean(Key.isUdpDns, profile.udpdns) edit.putBoolean(Key.isAuth, profile.auth) edit.putBoolean(Key.isIpv6, profile.ipv6) @@ -143,10 +141,8 @@ class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) { profile.id = settings.getInt(Key.profileId, -1) - profile.global = settings.getBoolean(Key.isGlobalProxy, false) - profile.chnroute = settings.getBoolean(Key.isGFWList, false) + profile.proxyApps = settings.getBoolean(Key.isProxyApps, false) profile.bypass = settings.getBoolean(Key.isBypassApps, false) - profile.traffic = settings.getBoolean(Key.isTrafficStat, false) profile.udpdns = settings.getBoolean(Key.isUdpDns, false) profile.auth = settings.getBoolean(Key.isAuth, false) profile.ipv6 = settings.getBoolean(Key.isIpv6, false) diff --git a/src/main/scala/com/github/shadowsocks/utils/ConfigUtils.scala b/src/main/scala/com/github/shadowsocks/utils/ConfigUtils.scala index cf2bcf65..73dc5db0 100644 --- a/src/main/scala/com/github/shadowsocks/utils/ConfigUtils.scala +++ b/src/main/scala/com/github/shadowsocks/utils/ConfigUtils.scala @@ -241,16 +241,13 @@ object ConfigUtils { val password = proxy(2).trim val method = proxy(3).trim - new Config(config.isGlobalProxy, config.isGFWList, config.isBypassApps, config.isTrafficStat, - config.isUdpDns, config.isAuth, config.isIpv6, config.profileName, host, password, method, config.proxiedAppString, config.route, port, - config.localPort) + new Config(config.isProxyApps, config.isBypassApps, config.isUdpDns, config.isAuth, config.isIpv6, + config.profileName, host, password, method, config.proxiedAppString, config.route, port, config.localPort) } def load(settings: SharedPreferences): Config = { - val isGlobalProxy = settings.getBoolean(Key.isGlobalProxy, false) - val isGFWList = settings.getBoolean(Key.isGFWList, false) + val isProxyApps = settings.getBoolean(Key.isProxyApps, false) val isBypassApps = settings.getBoolean(Key.isBypassApps, false) - val isTrafficStat = settings.getBoolean(Key.isTrafficStat, false) val isUdpDns = settings.getBoolean(Key.isUdpDns, false) val isAuth = settings.getBoolean(Key.isAuth, false) val isIpv6 = settings.getBoolean(Key.isIpv6, false) @@ -265,7 +262,7 @@ object ConfigUtils { val localPort = settings.getInt(Key.localPort, 1984) val proxiedAppString = settings.getString(Key.proxied, "") - new Config(isGlobalProxy, isGFWList, isBypassApps, isTrafficStat, isUdpDns, isAuth, isIpv6, profileName, proxy, - sitekey, encMethod, proxiedAppString, route, remotePort, localPort) + new Config(isProxyApps, isBypassApps, isUdpDns, isAuth, isIpv6, profileName, proxy, sitekey, encMethod, + proxiedAppString, route, remotePort, localPort) } } diff --git a/src/main/scala/com/github/shadowsocks/utils/Constants.scala b/src/main/scala/com/github/shadowsocks/utils/Constants.scala index a2c6a860..07b645fd 100644 --- a/src/main/scala/com/github/shadowsocks/utils/Constants.scala +++ b/src/main/scala/com/github/shadowsocks/utils/Constants.scala @@ -67,16 +67,13 @@ object Key { val profiles = "profiles" val isNAT = "isNAT" - val proxyedApps = "proxyedApps" val route = "route" val isRunning = "isRunning" val isAutoConnect = "isAutoConnect" - val isGlobalProxy = "isGlobalProxy" - val isGFWList = "isGFWList" + val isProxyApps = "isProxyApps" val isBypassApps = "isBypassApps" - val isTrafficStat = "isTrafficStat" val isUdpDns = "isUdpDns" val isAuth= "isAuth" val isIpv6= "isIpv6"