From f9667d767e0a8103fa407f455752317cd97c14b2 Mon Sep 17 00:00:00 2001 From: Mygod Date: Tue, 29 Dec 2015 20:27:30 +0800 Subject: [PATCH] Fix dynamic traffic stat in profile selector Fix #495 --- .../aidl/IShadowsocksServiceCallback.aidl | 3 +-- .../com/github/shadowsocks/BaseService.scala | 11 ++++----- .../shadowsocks/ProfileManagerActivity.scala | 16 ++++++------- .../com/github/shadowsocks/Shadowsocks.scala | 9 +++---- .../shadowsocks/ShadowsocksNotification.scala | 14 ++++++----- .../shadowsocks/utils/TrafficMonitor.scala | 24 ------------------- 6 files changed, 26 insertions(+), 51 deletions(-) diff --git a/src/main/aidl/com/github/shadowsocks/aidl/IShadowsocksServiceCallback.aidl b/src/main/aidl/com/github/shadowsocks/aidl/IShadowsocksServiceCallback.aidl index ff9aa551b2..8718118d47 100644 --- a/src/main/aidl/com/github/shadowsocks/aidl/IShadowsocksServiceCallback.aidl +++ b/src/main/aidl/com/github/shadowsocks/aidl/IShadowsocksServiceCallback.aidl @@ -2,6 +2,5 @@ package com.github.shadowsocks.aidl; interface IShadowsocksServiceCallback { oneway void stateChanged(int state, String msg); - oneway void trafficUpdated(String txRate, String rxRate, - String txTotal, String rxTotal); + oneway void trafficUpdated(long txRate, long rxRate, long txTotal, long rxTotal); } diff --git a/src/main/scala/com/github/shadowsocks/BaseService.scala b/src/main/scala/com/github/shadowsocks/BaseService.scala index 701ca73fe9..3709ddf741 100644 --- a/src/main/scala/com/github/shadowsocks/BaseService.scala +++ b/src/main/scala/com/github/shadowsocks/BaseService.scala @@ -90,8 +90,7 @@ trait BaseService extends Service { timer.schedule(task, 1000, 1000) } TrafficMonitor.updateRate() - cb.trafficUpdated(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate, - TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal) + cb.trafficUpdated(TrafficMonitor.txRate, TrafficMonitor.rxRate, TrafficMonitor.txTotal, TrafficMonitor.rxTotal) } } @@ -162,10 +161,10 @@ trait BaseService extends Service { val handler = new Handler(getContext.getMainLooper) handler.post(() => { if (callbacksCount > 0) { - val txRate = TrafficMonitor.getTxRate - val rxRate = TrafficMonitor.getRxRate - val txTotal = TrafficMonitor.getTxTotal - val rxTotal = TrafficMonitor.getRxTotal + val txRate = TrafficMonitor.txRate + val rxRate = TrafficMonitor.rxRate + val txTotal = TrafficMonitor.txTotal + val rxTotal = TrafficMonitor.rxTotal val n = callbacks.beginBroadcast() for (i <- 0 until n) { try { diff --git a/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala b/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala index 401cbd6d78..20e5e882ed 100644 --- a/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala +++ b/src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala @@ -61,17 +61,15 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe }) } - def updateText(refetch: Boolean = false) { + def updateText(txTotal: Long = 0, rxTotal: Long = 0) { val builder = new SpannableStringBuilder - val item = if (refetch) ShadowsocksApplication.profileManager.getProfile(this.item.id) match { - case Some(profile) => profile - case None => return - } else this.item + val tx = item.tx + txTotal + val rx = item.rx + rxTotal builder.append(item.name) - if (item.tx != 0 || item.rx != 0) { + if (tx != 0 || rx != 0) { val start = builder.length builder.append(getString(R.string.stat_profiles, - TrafficMonitor.formatTraffic(item.tx), TrafficMonitor.formatTraffic(item.rx))) + TrafficMonitor.formatTraffic(tx), TrafficMonitor.formatTraffic(rx))) builder.setSpan(new TextAppearanceSpan(ProfileManagerActivity.this, android.R.style.TextAppearance_Small), start + 1, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) } @@ -207,8 +205,8 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe attachService(new IShadowsocksServiceCallback.Stub { def stateChanged(state: Int, msg: String) = () // ignore - def trafficUpdated(txRate: String, rxRate: String, txTotal: String, rxTotal: String) = - if (selectedItem != null) selectedItem.updateText(true) + def trafficUpdated(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long) = + if (selectedItem != null) selectedItem.updateText(txTotal, rxTotal) }) if (ShadowsocksApplication.settings.getBoolean(profileTip, true)) { diff --git a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala index 82d1b04cb5..410120a775 100644 --- a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala +++ b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala @@ -154,14 +154,15 @@ class Shadowsocks state = s }) } - def trafficUpdated(txRate: String, rxRate: String, txTotal: String, rxTotal: String) { - trafficCache = getString(R.string.stat_summary).formatLocal(Locale.ENGLISH, txRate, rxRate, txTotal, rxTotal) + def trafficUpdated(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long) { + trafficCache = getString(R.string.stat_summary).formatLocal(Locale.ENGLISH, + TrafficMonitor.formatTraffic(txRate), TrafficMonitor.formatTraffic(rxRate), + TrafficMonitor.formatTraffic(txTotal), TrafficMonitor.formatTraffic(rxTotal)) handler.post(updateTraffic) } } - def updateTraffic(): Unit = if (trafficCache == null) callback.trafficUpdated(TrafficMonitor.getTxRate, - TrafficMonitor.getRxRate, TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal) else { + def updateTraffic(): Unit = if (trafficCache == null) callback.trafficUpdated(0, 0, 0, 0) else { if (connectionTestResult == null) connectionTestResult = getString(R.string.connection_test_pending) if (preferences.natSwitch.isChecked) { preferences.stat.setSummary(trafficCache) diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksNotification.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksNotification.scala index 4c609f53a5..02296281e2 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksNotification.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksNotification.scala @@ -9,20 +9,22 @@ import android.support.v4.app.NotificationCompat import android.support.v4.app.NotificationCompat.BigTextStyle import android.support.v4.content.ContextCompat import com.github.shadowsocks.aidl.IShadowsocksServiceCallback.Stub -import com.github.shadowsocks.utils.{Action, State, Utils} +import com.github.shadowsocks.utils.{TrafficMonitor, Action, State, Utils} /** * @author Mygod */ class ShadowsocksNotification(private val service: BaseService, profileName: String, visible: Boolean = false) { - private lazy val keyGuard = service.getSystemService(Context.KEYGUARD_SERVICE).asInstanceOf[KeyguardManager] + private val keyGuard = service.getSystemService(Context.KEYGUARD_SERVICE).asInstanceOf[KeyguardManager] private lazy val nm = service.getSystemService(Context.NOTIFICATION_SERVICE).asInstanceOf[NotificationManager] private lazy val callback = new Stub { override def stateChanged(state: Int, msg: String) = () // ignore - override def trafficUpdated(txRate: String, rxRate: String, txTotal: String, rxTotal: String) { - builder.setContentText(service.getString(R.string.traffic_summary).formatLocal(Locale.ENGLISH, txRate, rxRate)) - style.bigText(service.getString(R.string.stat_summary) - .formatLocal(Locale.ENGLISH, txRate, rxRate, txTotal, rxTotal)) + override def trafficUpdated(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long) { + val txr = TrafficMonitor.formatTraffic(txRate) + val rxr = TrafficMonitor.formatTraffic(rxRate) + builder.setContentText(service.getString(R.string.traffic_summary).formatLocal(Locale.ENGLISH, txr, rxr)) + style.bigText(service.getString(R.string.stat_summary).formatLocal(Locale.ENGLISH, txr, rxr, + TrafficMonitor.formatTraffic(txTotal), TrafficMonitor.formatTraffic(rxTotal))) show() } } diff --git a/src/main/scala/com/github/shadowsocks/utils/TrafficMonitor.scala b/src/main/scala/com/github/shadowsocks/utils/TrafficMonitor.scala index 168fd6ed16..8ffee8dfc8 100644 --- a/src/main/scala/com/github/shadowsocks/utils/TrafficMonitor.scala +++ b/src/main/scala/com/github/shadowsocks/utils/TrafficMonitor.scala @@ -80,29 +80,5 @@ object TrafficMonitor { rxLast = 0 dirty = true } - - def getTxTotal(): String = { - formatTraffic(txTotal) - } - - def getRxTotal(): String = { - formatTraffic(rxTotal) - } - - def getTotal(): String = { - formatTraffic(txTotal + rxTotal) - } - - def getTxRate(): String = { - formatTraffic(txRate) - } - - def getRxRate(): String = { - formatTraffic(rxRate) - } - - def getRate(): String = { - formatTraffic(txRate + rxRate) - } }