This commit is contained in:
Max Lv
2015-11-21 11:59:05 +08:00
parent e636b9f78e
commit ff23bae671
6 changed files with 46 additions and 8 deletions
@@ -44,6 +44,9 @@ import java.util.{Timer, TimerTask}
import android.app.Notification
import android.content.Context
import android.os.{Handler, RemoteCallbackList}
import android.util.Log
import com.github.shadowsocks.database.{Profile, ProfileManager}
import com.github.shadowsocks.aidl.{Config, IShadowsocksService, IShadowsocksServiceCallback}
import com.github.shadowsocks.utils.{State, TrafficMonitor, TrafficMonitorThread}
@@ -127,6 +130,23 @@ trait BaseService {
}
}
def updateTrafficTotal(tx: Long, rx: Long) {
val handler = new Handler(getContext.getMainLooper)
handler.post(() => {
if (config != null) {
ShadowsocksApplication.profileManager.getProfile(config.profileId) match {
case Some(profile) => {
profile.tx += tx;
profile.rx += rx;
Log.d("test", profile.tx + " " + profile.rx)
ShadowsocksApplication.profileManager.updateProfile(profile)
}
case None => // Ignore
}
}
})
}
def clearChildProcessStream()
def stopBackgroundService()
def getServiceMode: Int
@@ -143,7 +163,7 @@ trait BaseService {
changeState(s, null)
}
def updateTraffic(txRate: String, rxRate: String, txTotal: String, rxTotal: String) {
def updateTrafficRate(txRate: String, rxRate: String, txTotal: String, rxTotal: String) {
val handler = new Handler(getContext.getMainLooper)
handler.post(() => {
if (callbackCount > 0) {
@@ -79,7 +79,7 @@ class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) {
true
} catch {
case ex: Exception =>
Log.e(Shadowsocks.TAG, "addProfile", ex)
Log.e(Shadowsocks.TAG, "updateProfile", ex)
false
}
}
@@ -149,9 +149,13 @@ class ProfileManager(settings: SharedPreferences, dbHelper: DBHelper) {
}
private def loadFromPreferences: Profile = {
val profile = new Profile()
profile.id = settings.getInt(Key.profileId, -1)
val id = settings.getInt(Key.profileId, -1)
val profile: Profile = getProfile(id) match {
case Some(p) => p
case _ => new Profile()
}
profile.proxyApps = settings.getBoolean(Key.isProxyApps, false)
profile.bypass = settings.getBoolean(Key.isBypassApps, false)
@@ -239,7 +239,7 @@ object ConfigUtils {
val method = proxy(3).trim
new Config(config.isProxyApps, config.isBypassApps, config.isUdpDns, config.isAuth, config.isIpv6,
config.profileName, host, password, method, config.proxiedAppString, config.route, port, config.localPort)
config.profileName, host, password, method, config.proxiedAppString, config.route, port, config.localPort, 0)
}
def load(settings: SharedPreferences): Config = {
@@ -259,7 +259,9 @@ object ConfigUtils {
val localPort = settings.getInt(Key.localPort, 1984)
val proxiedAppString = settings.getString(Key.proxied, "")
val profileId = settings.getInt(Key.profileId, -1)
new Config(isProxyApps, isBypassApps, isUdpDns, isAuth, isIpv6, profileName, proxy, sitekey, encMethod,
proxiedAppString, route, remotePort, localPort)
proxiedAppString, route, remotePort, localPort, profileId)
}
}
@@ -18,6 +18,10 @@ object TrafficMonitor {
var txTotal: Long = 0
var rxTotal: Long = 0
// Kilo bytes for the last query
var txLast: Long = 0
var rxLast: Long = 0
def getTraffic(tx: Long, rx: Long): Traffic = {
new Traffic(tx, rx, System.currentTimeMillis())
}
@@ -42,6 +46,8 @@ object TrafficMonitor {
txRate = (now.tx - last.tx) * 1000 / delta
rxRate = (now.rx - last.rx) * 1000 / delta
}
txLast = now.tx - last.tx
rxLast = now.rx - last.rx
txTotal += now.tx - last.tx
rxTotal += now.rx - last.rx
last = now
@@ -108,8 +108,9 @@ class TrafficMonitorThread(service: BaseService) extends Thread {
val stat = new String(array, "UTF-8").split("\\|")
if (stat.length == 2) {
TrafficMonitor.update(stat(0).toLong, stat(1).toLong)
service.updateTraffic(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate,
service.updateTrafficRate(TrafficMonitor.getTxRate, TrafficMonitor.getRxRate,
TrafficMonitor.getTxTotal, TrafficMonitor.getRxTotal)
service.updateTrafficTotal(TrafficMonitor.txLast, TrafficMonitor.rxLast)
}
output.write(0)