fix traffic stats
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user