Skip to content

Commit

Permalink
Merge pull request #502 from Mygod/master
Browse files Browse the repository at this point in the history
Fix dynamic traffic stat in profile selector
  • Loading branch information
madeye committed Dec 29, 2015
2 parents 5ddd8a1 + f9667d7 commit 04c86a5
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
11 changes: 5 additions & 6 deletions src/main/scala/com/github/shadowsocks/BaseService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)) {
Expand Down
9 changes: 5 additions & 4 deletions src/main/scala/com/github/shadowsocks/Shadowsocks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
24 changes: 0 additions & 24 deletions src/main/scala/com/github/shadowsocks/utils/TrafficMonitor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 04c86a5

Please sign in to comment.