From 048836193763696d6aa29cd8e36b16eb5354a43d Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 9 Dec 2015 17:57:10 +0100 Subject: [PATCH] Network speed improvements --- .../eu/faircode/netguard/ActivitySettings.java | 5 ++++- .../eu/faircode/netguard/SinkholeService.java | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/ActivitySettings.java b/app/src/main/java/eu/faircode/netguard/ActivitySettings.java index aecd3c22..8c8cc65f 100644 --- a/app/src/main/java/eu/faircode/netguard/ActivitySettings.java +++ b/app/src/main/java/eu/faircode/netguard/ActivitySettings.java @@ -285,7 +285,10 @@ public class ActivitySettings extends AppCompatActivity implements SharedPrefere } else SinkholeService.reload("other", "setting changed", this); - } else if ("stats_base".equals(name)) + } else if ("show_stats".equals(name)) + SinkholeService.reload(null, "setting changed", this); + + else if ("stats_base".equals(name)) getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_stats_base, prefs.getString(name, "5"))); else if ("auto_enable".equals(name)) diff --git a/app/src/main/java/eu/faircode/netguard/SinkholeService.java b/app/src/main/java/eu/faircode/netguard/SinkholeService.java index b7fd5f86..ce24f07b 100644 --- a/app/src/main/java/eu/faircode/netguard/SinkholeService.java +++ b/app/src/main/java/eu/faircode/netguard/SinkholeService.java @@ -210,6 +210,10 @@ public class SinkholeService extends VpnService { startDebug(vpn); if (prev != null) stopVPN(prev); + + // Restart stats + stopStats(); + startStats(); break; case stop: @@ -272,6 +276,7 @@ public class SinkholeService extends VpnService { private void stopStats() { Log.i(TAG, "Stats stop"); stats = false; + mServiceHandler.removeMessages(MSG_STATS_UPDATE); NotificationManagerCompat.from(SinkholeService.this).cancel(NOTIFY_TRAFFIC); } @@ -297,14 +302,14 @@ public class SinkholeService extends VpnService { long ct = SystemClock.elapsedRealtime(); long ctx = TrafficStats.getTotalTxBytes(); long rtx = TrafficStats.getTotalRxBytes(); - if (ct > 0 && (rx > 0 || tx > 0)) { + if (t > 0 && tx > 0 && rx > 0) { float dt = (ct - t) / 1000f; txsec = (ctx - tx) / dt; rxsec = (rtx - rx) / dt; - Log.i(TAG, "tx=" + txsec + " rx=" + rxsec); gt.add(ct); gtx.add(txsec); grx.add(rxsec); + Log.i(TAG, "tx=" + txsec + " rx=" + rxsec); } t = ct; tx = ctx; @@ -373,7 +378,12 @@ public class SinkholeService extends VpnService { // Update remote view RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.traffic); remoteViews.setImageViewBitmap(R.id.ivTraffic, bitmap); - remoteViews.setTextViewText(R.id.tvTraffic, String.format("%.0f/%.0f B/sec", txsec, rxsec)); + if (txsec < 1024 && rxsec < 1024) + remoteViews.setTextViewText(R.id.tvTraffic, String.format("%.0f / %.0f B/sec", txsec, rxsec)); + else if (txsec < 1024 * 1024 && rxsec < 1024 * 1024) + remoteViews.setTextViewText(R.id.tvTraffic, String.format("%.1f / %.1f KiB/sec", txsec / 1024, rxsec / 1024)); + else + remoteViews.setTextViewText(R.id.tvTraffic, String.format("%.1f / %.1f MiB/sec", txsec / 1024 / 1024, rxsec / 1024 / 1024)); // Show notification Intent main = new Intent(SinkholeService.this, ActivityMain.class);