From 24bdb287646f94b134d0073e2a2c4e90f09a5eb0 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Mon, 11 Feb 2013 15:53:43 +0100 Subject: Fix display of speed in the notification. --- src/de/blinkt/openvpn/OpenVPNMangement.java | 1 + src/de/blinkt/openvpn/OpenVpnManagementThread.java | 4 +--- src/de/blinkt/openvpn/OpenVpnService.java | 21 ++++++++++++++------- 3 files changed, 16 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/de/blinkt/openvpn/OpenVPNMangement.java b/src/de/blinkt/openvpn/OpenVPNMangement.java index 0c6d7163..85a3f784 100644 --- a/src/de/blinkt/openvpn/OpenVPNMangement.java +++ b/src/de/blinkt/openvpn/OpenVPNMangement.java @@ -1,6 +1,7 @@ package de.blinkt.openvpn; public interface OpenVPNMangement { + int mBytecountinterval=2; void reconnect(); diff --git a/src/de/blinkt/openvpn/OpenVpnManagementThread.java b/src/de/blinkt/openvpn/OpenVpnManagementThread.java index 5cd70987..d17ed6c5 100644 --- a/src/de/blinkt/openvpn/OpenVpnManagementThread.java +++ b/src/de/blinkt/openvpn/OpenVpnManagementThread.java @@ -25,7 +25,6 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNMangement { private VpnProfile mProfile; private OpenVpnService mOpenVPNService; private LinkedList mFDList=new LinkedList(); - private int mBytecountinterval=2; private LocalServerSocket mServerSocket; private boolean mReleaseHold=true; private boolean mWaitingForRelease=false; @@ -273,8 +272,7 @@ public class OpenVpnManagementThread implements Runnable, OpenVPNMangement { long out = Long.parseLong(argument.substring(comma+1)); OpenVPN.updateByteCount(in,out); - - + } diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java index cacbb85e..66ab9e30 100644 --- a/src/de/blinkt/openvpn/OpenVpnService.java +++ b/src/de/blinkt/openvpn/OpenVpnService.java @@ -565,9 +565,9 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac if(mDisplayBytecount) { String netstat = String.format(getString(R.string.statusline_bytecount), humanReadableByteCount(in, false), - humanReadableByteCount(diffin, false), + humanReadableByteCount(diffin/OpenVPNMangement.mBytecountinterval, true), humanReadableByteCount(out, false), - humanReadableByteCount(diffout, false)); + humanReadableByteCount(diffout/OpenVPNMangement.mBytecountinterval, true)); boolean lowpriority = !mNotificationalwaysVisible; showNotification(netstat,null,lowpriority,mConnecttime, OpenVPN.LEVEL_CONNECTED); @@ -576,12 +576,19 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac } // From: http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java - public static String humanReadableByteCount(long bytes, boolean si) { - int unit = si ? 1000 : 1024; - if (bytes < unit) return bytes + " B"; + public static String humanReadableByteCount(long bytes, boolean mbit) { + if(mbit) + bytes = bytes *8; + int unit = mbit ? 1000 : 1024; + if (bytes < unit) + return bytes + (mbit ? " bit" : " B"); + int exp = (int) (Math.log(bytes) / Math.log(unit)); - String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i"); - return String.format(Locale.getDefault(),"%.1f %sB", bytes / Math.pow(unit, exp), pre); + String pre = (mbit ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (mbit ? "" : ""); + if(mbit) + return String.format(Locale.getDefault(),"%.1f %sbit", bytes / Math.pow(unit, exp), pre); + else + return String.format(Locale.getDefault(),"%.1f %sB", bytes / Math.pow(unit, exp), pre); } @Override -- cgit v1.2.3