Commit 509a4e79 authored by Mygod's avatar Mygod

Add network traffic display in profile selector

parent f4401f8b
......@@ -74,6 +74,10 @@
<string name="stat">网络流量</string>
<string name="stat_summary">发送:\t%1$s\t|\t接收:\t%2$s\n上传:\t%3$s\t|\t下载:\t%4$s</string>
<string name="stat_profiles">\n发送:\t%1$s\t|\t接收:\t%2$s</string>
<plurals name="bytes">
<item quantity="other">字节</item>
</plurals>
<!-- profile -->
<string name="add_profile_dialog">为影梭添加此配置文件?</string>
......
......@@ -14,6 +14,11 @@
<string name="nat_summary_no_root">NAT mode is disabled without root</string>
<string name="stat">Network Traffic</string>
<string name="stat_summary">Send: \t%1$s\t|\tReceive: \t%2$s\nUpload: \t%3$s\t|\tDownload: \t%4$s</string>
<string name="stat_profiles">\nSend: \t%1$s\t|\tReceive: \t%2$s</string>
<plurals name="bytes">
<item quantity="one">Byte</item>
<item quantity="other">Bytes</item>
</plurals>
<!-- proxy category -->
<string name="proxy_cat">Server Settings</string>
......
......@@ -10,11 +10,13 @@ import android.support.v7.widget.Toolbar.OnMenuItemClickListener
import android.support.v7.widget.helper.ItemTouchHelper
import android.support.v7.widget.helper.ItemTouchHelper.SimpleCallback
import android.support.v7.widget.{DefaultItemAnimator, LinearLayoutManager, RecyclerView, Toolbar}
import android.text.style.TextAppearanceSpan
import android.text.{Spanned, SpannableStringBuilder}
import android.view.View.{OnAttachStateChangeListener, OnClickListener}
import android.view.{LayoutInflater, MenuItem, View, ViewGroup}
import android.widget.{LinearLayout, ImageView, CheckedTextView, Toast}
import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.utils.{Parser, Utils}
import com.github.shadowsocks.utils.{TrafficMonitor, Parser, Utils}
import com.google.zxing.integration.android.IntentIntegrator
import net.glxn.qrgen.android.QRCode
......@@ -54,10 +56,21 @@ class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClickListe
})
}
def updateText() {
val builder = new SpannableStringBuilder
builder.append(item.name)
val start = builder.length
builder.append(getString(R.string.stat_profiles,
TrafficMonitor.formatTraffic(item.tx), TrafficMonitor.formatTraffic(item.rx)))
builder.setSpan(new TextAppearanceSpan(ProfileManagerActivity.this, android.R.style.TextAppearance_Small),
start + 1, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
text.setText(builder)
}
def bind(item: Profile) {
text.setText(item.name)
text.setChecked(item.id == ShadowsocksApplication.profileId)
this.item = item
updateText()
text.setChecked(item.id == ShadowsocksApplication.profileId)
}
def onClick(v: View) = {
......
package com.github.shadowsocks.utils
import java.lang.{Math, System}
import java.util.Locale
import java.text.DecimalFormat
import android.net.TrafficStats
import android.os.Process
import com.github.shadowsocks.{R, ShadowsocksApplication}
case class Traffic(tx: Long, rx: Long, timestamp: Long)
......@@ -25,18 +26,17 @@ object TrafficMonitor {
Math.max(TrafficStats.getTotalRxBytes - TrafficStats.getUidRxBytes(uid), 0), System.currentTimeMillis())
}
def formatTraffic(n: Long): String = {
if (n <= 1024) {
"%d KB".formatLocal(Locale.ENGLISH, n)
} else if (n > 1024) {
"%d MB".formatLocal(Locale.ENGLISH, n / 1024)
} else if (n > 1024 * 1024) {
"%d GB".formatLocal(Locale.ENGLISH, n / 1024 / 1024)
} else if (n > 1024 * 1024 * 1024) {
"%d TB".formatLocal(Locale.ENGLISH, n / 1024 / 1024 / 1024)
} else {
">1024 TB"
private val units = Array("KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", "BB", "NB", "DB", "CB")
private val numberFormat = new DecimalFormat("0.00")
def formatTraffic(size: Long): String = {
var n: Double = size
var i = -1
while (n >= 1024) {
n /= 1024
i = i + 1
}
if (i < 0) size + " " + ShadowsocksApplication.instance.getResources.getQuantityString(R.plurals.bytes, size.toInt)
else numberFormat.format(n) + ' ' + units(i)
}
def update() {
......@@ -46,8 +46,8 @@ object TrafficMonitor {
txRate = (now.tx - last.tx) / delta
rxRate = (now.rx - last.rx) / delta
}
txTotal += (now.tx - last.tx) / 1024
rxTotal += (now.rx - last.rx) / 1024
txTotal += now.tx - last.tx
rxTotal += now.rx - last.rx
last = now
}
......
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