Commit 394860a8 authored by Max Lv's avatar Max Lv

fix traffic stats

parent a7b2bd57
......@@ -5,8 +5,8 @@ import sbtandroid._
import sbtandroid.AndroidKeys._
object App {
val version = "1.7.0"
val versionCode = 37
val version = "1.7.1"
val versionCode = 38
}
object General {
......
......@@ -40,6 +40,8 @@
<string name="bypass_apps_summary">Enable this option to bypass selected apps</string>
<string name="auto_connect">Auto Connect</string>
<string name="auto_connect_summary">Enable Shadowsocks on startup</string>
<string name="traffic_stat">Traffic Stats</string>
<string name="traffic_stat_summary">Show traffic stats on notification bar</string>
<!-- notification category -->
<string name="forward_success">Shadowsocks started.</string>
......
......@@ -28,6 +28,12 @@
android:title="@string/proxied_apps">
<intent android:action="com.github.shadowsocks.AppManager"/>
</Preference>
<CheckBoxPreference
android:key="isTrafficStat"
android:defaultValue="true"
android:summary="@string/traffic_stat_summary"
android:title="@string/traffic_stat">
</CheckBoxPreference>
<CheckBoxPreference
android:key="isAutoConnect"
android:summary="@string/auto_connect_summary"
......
......@@ -136,7 +136,7 @@ object Shadowsocks {
if (pref != null) {
val status = getActivity.getSharedPreferences(Key.status, Context.MODE_PRIVATE)
val isRoot = status.getBoolean(Key.isRoot, false)
if (Seq(Key.isAutoConnect, Key.isGlobalProxy,
if (Seq(Key.isAutoConnect, Key.isGlobalProxy, Key.isTrafficStat,
Key.isBypassApps, Key.proxyedApps).contains(name)) {
pref.setEnabled(enabled && isRoot)
} else {
......@@ -506,7 +506,7 @@ class Shadowsocks
for (name <- Shadowsocks.FEATRUE_PREFS) {
val pref = findPreference(name)
if (pref != null) {
if (Seq(Key.isAutoConnect, Key.isGlobalProxy,
if (Seq(Key.isAutoConnect, Key.isGlobalProxy, Key.isTrafficStat,
Key.isBypassApps, Key.proxyedApps).contains(name)) {
pref.setEnabled(enabled && isRoot)
} else {
......
......@@ -105,7 +105,7 @@ class ShadowsocksService extends Service {
private var last: TrafficStat = null
private var lastTxRate = 0
private var lastRxRate = 0
private val timer = new Timer(true)
private var timer: Timer = null
private val TIMER_INTERVAL = 1
private def changeState(s: Int) {
......@@ -193,6 +193,32 @@ class ShadowsocksService extends Service {
config = Extra.get(intent)
if (config.isTrafficStat) {
// initialize timer
val task = new TimerTask {
def run() {
val now = new TrafficStat(TrafficStats.getTotalTxBytes,
TrafficStats.getTotalRxBytes, java.lang.System.currentTimeMillis())
val txRate = ((now.tx - last.tx) / 1024 / TIMER_INTERVAL).toInt
val rxRate = ((now.rx - last.rx) / 1024 / TIMER_INTERVAL).toInt
last = now
if (lastTxRate == txRate && lastRxRate == rxRate) {
return
} else {
lastTxRate = txRate
lastRxRate = rxRate
}
if (state == State.CONNECTED) {
notifyForegroundAlert(getString(R.string.forward_success),
getString(R.string.service_status).format(txRate, rxRate), txRate + rxRate)
}
}
}
last = new TrafficStat(TrafficStats.getTotalTxBytes,
TrafficStats.getTotalRxBytes, java.lang.System.currentTimeMillis())
timer = new Timer(true)
timer.schedule(task, TIMER_INTERVAL * 1000, TIMER_INTERVAL * 1000)
}
spawn {
killProcesses()
......@@ -308,7 +334,9 @@ class ShadowsocksService extends Service {
bitmap.getHeight - (bitmap.getHeight - bounds.height()) / 2, paint)
builder.setLargeIcon(bitmap)
if (rate < 1000) {
if (rate == 0) {
builder.setSmallIcon(R.drawable.ic_stat_shadowsocks)
} else if (rate < 1000) {
builder.setSmallIcon(R.drawable.ic_stat_speed, rate)
} else if (rate <= 10000) {
val mb = rate / 100 - 10 + 1000
......@@ -390,38 +418,19 @@ class ShadowsocksService extends Service {
}
}
registerReceiver(receiver, filter)
// initialize timer
val task = new TimerTask {
def run() {
val now = new TrafficStat(TrafficStats.getUidTxBytes(getApplicationInfo.uid),
TrafficStats.getUidRxBytes(getApplicationInfo.uid), java.lang.System.currentTimeMillis())
val txRate = ((now.tx - last.tx) / 1024 / TIMER_INTERVAL).toInt
val rxRate = ((now.rx - last.rx) / 1024 / TIMER_INTERVAL).toInt
last = now
if (lastTxRate == txRate && lastRxRate == rxRate) {
return
} else {
lastTxRate = txRate
lastRxRate = rxRate
}
if (state == State.CONNECTED) {
notifyForegroundAlert(getString(R.string.forward_success),
getString(R.string.service_status).format(txRate, rxRate), txRate + rxRate)
}
}
}
last = new TrafficStat(TrafficStats.getUidTxBytes(getApplicationInfo.uid),
TrafficStats.getUidRxBytes(getApplicationInfo.uid), java.lang.System.currentTimeMillis())
timer.schedule(task, TIMER_INTERVAL * 1000, TIMER_INTERVAL * 1000)
}
/** Called when the activity is closed. */
override def onDestroy() {
// reset timer
if (timer != null) {
timer.cancel()
timer = null
}
// clean up context
changeState(State.STOPPED)
timer.cancel()
EasyTracker.getTracker.setStartSession(false)
EasyTracker.getTracker.sendEvent(TAG, "stop", getVersionName, 0L)
stopForegroundCompat(1)
......
......@@ -104,7 +104,7 @@ object Config {
}
}
case class Config(isGlobalProxy: Boolean, isGFWList: Boolean, isBypassApps: Boolean,
case class Config(isGlobalProxy: Boolean, isGFWList: Boolean, isBypassApps: Boolean, isTrafficStat: Boolean,
var proxy: String, sitekey: String, encMethod: String, remotePort: Int,
localPort: Int, proxiedAppString: String)
......@@ -121,6 +121,7 @@ object Key {
val isGlobalProxy = "isGlobalProxy"
val isGFWList = "isGFWList"
val isBypassApps = "isBypassApps"
val isTrafficStat = "isTrafficStat"
val proxy = "proxy"
val sitekey = "sitekey"
......@@ -152,6 +153,7 @@ object Extra {
edit.putBoolean(Key.isGlobalProxy, config.isGlobalProxy)
edit.putBoolean(Key.isGFWList, config.isGFWList)
edit.putBoolean(Key.isBypassApps, config.isBypassApps)
edit.putBoolean(Key.isTrafficStat, config.isTrafficStat)
edit.putString(Key.proxy, config.proxy)
edit.putString(Key.sitekey, config.sitekey)
......@@ -166,6 +168,8 @@ object Extra {
val isGlobalProxy = intent.getBooleanExtra(Key.isGlobalProxy, false)
val isGFWList = intent.getBooleanExtra(Key.isGFWList, false)
val isBypassApps = intent.getBooleanExtra(Key.isBypassApps, false)
val isTrafficStat = intent.getBooleanExtra(Key.isTrafficStat, false)
val proxy = intent.getStringExtra(Key.proxy)
val sitekey = intent.getStringExtra(Key.sitekey)
val encMethod = intent.getStringExtra(Key.encMethod)
......@@ -173,7 +177,7 @@ object Extra {
val localPort = intent.getIntExtra(Key.localPort, 1984)
val proxiedString = intent.getStringExtra(Key.proxied)
new Config(isGlobalProxy, isGFWList, isBypassApps, proxy, sitekey, encMethod, remotePort,
new Config(isGlobalProxy, isGFWList, isBypassApps, isTrafficStat, proxy, sitekey, encMethod, remotePort,
localPort, proxiedString)
}
......@@ -181,6 +185,8 @@ object Extra {
val isGlobalProxy = settings.getBoolean(Key.isGlobalProxy, false)
val isGFWList = settings.getBoolean(Key.isGFWList, false)
val isBypassApps = settings.getBoolean(Key.isBypassApps, false)
val isTrafficStat = settings.getBoolean(Key.isTrafficStat, false)
val proxy = settings.getString(Key.proxy, "127.0.0.1") match {
case "198.199.101.152" => if (!BuildConfig.DEBUG) "ss.maxcdn.info" else "198.199.101.152"
case s: String => s
......@@ -207,6 +213,7 @@ object Extra {
intent.putExtra(Key.isGlobalProxy, isGlobalProxy)
intent.putExtra(Key.isGFWList, isGFWList)
intent.putExtra(Key.isBypassApps, isBypassApps)
intent.putExtra(Key.isTrafficStat, isTrafficStat)
intent.putExtra(Key.proxy, proxy)
intent.putExtra(Key.sitekey, sitekey)
......
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