From c1730e4fa667d59f9b15876fcbd089ae70bcac19 Mon Sep 17 00:00:00 2001 From: Mygod Date: Tue, 24 Nov 2015 21:16:08 +0800 Subject: [PATCH] Add a toggle for showing traffic in notification. TODO: Disabling it causes weird behavior. :-( --- src/main/res/values-zh/strings.xml | 3 +- src/main/res/values/strings.xml | 3 +- src/main/res/xml/pref_all.xml | 2 ++ .../com/github/shadowsocks/Shadowsocks.scala | 5 +-- .../shadowsocks/ShadowsocksApplication.scala | 3 +- .../shadowsocks/ShadowsocksNotification.scala | 35 +++++++++++-------- .../shadowsocks/ShadowsocksSettings.scala | 20 ++++++----- .../github/shadowsocks/utils/Constants.scala | 1 + 8 files changed, 43 insertions(+), 29 deletions(-) diff --git a/src/main/res/values-zh/strings.xml b/src/main/res/values-zh/strings.xml index bb8dbe89..da978fdb 100755 --- a/src/main/res/values-zh/strings.xml +++ b/src/main/res/values-zh/strings.xml @@ -32,8 +32,7 @@ 启用该选项,以使所选应用程序的流量不经过代理 自动连接 随系统启动后台服务 - 流量统计 - 在通知栏上显示当前上下行速率 + 在通知内显示当前上下行速率 后台服务已开始运行。 影梭 → %s 无法连接远程服务器 diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 564fb4eb..0524eb80 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -13,6 +13,7 @@ Use NAT mode instead of VPN mode NAT mode is disabled without root Network Traffic + Show traffic in notification Sent: \t\t\t\t\t%3$s\t↑\t%1$s/s\nReceived: \t%4$s\t↓\t%2$s/s \n%1$s↑\t%2$s↓ %1$s/s↑\t%2$s/s↓ @@ -55,8 +56,6 @@ Enable this option to bypass selected apps Auto Connect Enable Shadowsocks on startup - Traffic Stats - Show traffic stats in notification bar UDP Forwarding Forward UDP packets to remote diff --git a/src/main/res/xml/pref_all.xml b/src/main/res/xml/pref_all.xml index 2390e8e3..8ab4e23f 100644 --- a/src/main/res/xml/pref_all.xml +++ b/src/main/res/xml/pref_all.xml @@ -82,6 +82,8 @@ android:key="isAutoConnect" android:summary="@string/auto_connect_summary" android:title="@string/auto_connect"/> + update(intent.getAction) val screenFilter = new IntentFilter() screenFilter.addAction(Intent.ACTION_SCREEN_ON) @@ -49,32 +49,39 @@ class ShadowsocksNotification(private val service: BaseService, profileName: Str service.registerReceiver(lockReceiver, screenFilter) } else setVisible(visible).show() - private def update(action: String) = if (service.getState == State.CONNECTED) { + def update(action: String = Intent.ACTION_SCREEN_ON) = if (service.getState == State.CONNECTED) { if (action == Intent.ACTION_SCREEN_OFF) { if (showOnUnlock) setVisible(false) - if (showTraffic) service.binder.unregisterCallback(callback) - } else if (action == Intent.ACTION_USER_PRESENT || - action == Intent.ACTION_SCREEN_ON && !keyGuard.inKeyguardRestrictedInputMode) { - setVisible(showOnUnlock) - if (showTraffic) service.binder.registerCallback(callback) - } + unregisterCallback // unregister callback to save battery + } else if (action == Intent.ACTION_SCREEN_ON) { + if (ShadowsocksApplication.notificationTraffic) { + service.binder.registerCallback(callback) + callbackRegistered = true + } else unregisterCallback + if (!keyGuard.inKeyguardRestrictedInputMode) setVisible(showOnUnlock) + } else if (action == Intent.ACTION_USER_PRESENT) setVisible(showOnUnlock) show() } + private def unregisterCallback = if (callbackRegistered) { + service.binder.unregisterCallback(callback) + callbackRegistered = false + } + def setVisible(visible: Boolean) = { builder.setPriority(if (visible) NotificationCompat.PRIORITY_DEFAULT else NotificationCompat.PRIORITY_MIN) this } - def notification = if (showTraffic) style.build() else builder.build() + def notification = if (callbackRegistered) style.build() else builder.build() def show() = service.startForeground(1, notification) - def destroy() = { + def destroy() { if (lockReceiver != null) { service.unregisterReceiver(lockReceiver) lockReceiver = null } - if (showTraffic) service.binder.unregisterCallback(callback) + unregisterCallback service.stopForeground(true) } } diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala index f6e25db2..0831eee7 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksSettings.scala @@ -2,7 +2,8 @@ package com.github.shadowsocks import java.util.Locale -import android.content.{DialogInterface, Intent} +import android.content.SharedPreferences.OnSharedPreferenceChangeListener +import android.content.{DialogInterface, Intent, SharedPreferences} import android.net.Uri import android.os.{Build, Bundle} import android.preference.{Preference, PreferenceFragment, SwitchPreference} @@ -11,7 +12,7 @@ import android.webkit.{WebView, WebViewClient} import com.github.shadowsocks.utils.Key // TODO: Move related logic here -class ShadowsocksSettings extends PreferenceFragment { +class ShadowsocksSettings extends PreferenceFragment with OnSharedPreferenceChangeListener { private lazy val activity = getActivity.asInstanceOf[Shadowsocks] private var isProxyApps: SwitchPreference = _ @@ -19,6 +20,7 @@ class ShadowsocksSettings extends PreferenceFragment { override def onCreate(savedInstanceState: Bundle) { super.onCreate(savedInstanceState) addPreferencesFromResource(R.xml.pref_all) + getPreferenceManager.getSharedPreferences.registerOnSharedPreferenceChangeListener(this) isProxyApps = findPreference(Key.isProxyApps).asInstanceOf[SwitchPreference] isProxyApps.setOnPreferenceChangeListener((preference: Preference, newValue: Any) => { @@ -26,12 +28,6 @@ class ShadowsocksSettings extends PreferenceFragment { newValue.asInstanceOf[Boolean] // keep it ON }) - findPreference(Key.isNAT).setOnPreferenceChangeListener((preference: Preference, newValue: Any) => - if (ShadowsocksApplication.isRoot) activity.handler.post(() => { - activity.deattachService() - activity.attachService() - }) else false) - findPreference("recovery").setOnPreferenceClickListener((preference: Preference) => { ShadowsocksApplication.track(Shadowsocks.TAG, "reset") activity.recovery() @@ -75,4 +71,12 @@ class ShadowsocksSettings extends PreferenceFragment { super.onResume isProxyApps.setChecked(ShadowsocksApplication.settings.getBoolean(Key.isProxyApps, false)) // update } + + def onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) = key match { + case Key.isNAT => if (ShadowsocksApplication.isRoot) activity.handler.post(() => { + activity.deattachService() + activity.attachService() + }) + case _ => + } } diff --git a/src/main/scala/com/github/shadowsocks/utils/Constants.scala b/src/main/scala/com/github/shadowsocks/utils/Constants.scala index cfc405c9..97bf71b0 100644 --- a/src/main/scala/com/github/shadowsocks/utils/Constants.scala +++ b/src/main/scala/com/github/shadowsocks/utils/Constants.scala @@ -68,6 +68,7 @@ object Key { val profiles = "profiles" val isNAT = "isNAT" val route = "route" + val notificationTraffic = "notificationTraffic" val stat = "stat" val isRunning = "isRunning"