From 1fb25929bfed5586a0d8fe490bcf6535a49935d8 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Wed, 31 May 2017 16:06:38 +0200 Subject: Implement localisation of bit/s and B. (cloes #685) This should the mega octet (French) user and whatever characters Cyrillic uses happy --- .../blinkt/openvpn/core/DeviceStateReceiver.java | 2 +- .../de/blinkt/openvpn/core/OpenVPNService.java | 54 ++++++++++++++++------ .../de/blinkt/openvpn/fragments/GraphFragment.java | 20 ++++---- .../de/blinkt/openvpn/fragments/LogFragment.java | 6 ++- main/src/main/res/values/strings.xml | 13 +++++- 5 files changed, 63 insertions(+), 32 deletions(-) diff --git a/main/src/main/java/de/blinkt/openvpn/core/DeviceStateReceiver.java b/main/src/main/java/de/blinkt/openvpn/core/DeviceStateReceiver.java index c7760a0e..7b5946a3 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/DeviceStateReceiver.java +++ b/main/src/main/java/de/blinkt/openvpn/core/DeviceStateReceiver.java @@ -103,7 +103,7 @@ public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountL if (windowtraffic < TRAFFIC_LIMIT) { screen = connectState.DISCONNECTED; VpnStatus.logInfo(R.string.screenoff_pause, - OpenVPNService.humanReadableByteCount(TRAFFIC_LIMIT, false), TRAFFIC_WINDOW); + "64 kB", TRAFFIC_WINDOW); mManagement.pause(getPauseReason()); } diff --git a/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java b/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java index a4c66e9d..753ef0ab 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java +++ b/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java @@ -19,6 +19,7 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.pm.ShortcutManager; import android.content.res.Configuration; +import android.content.res.Resources; import android.net.ConnectivityManager; import android.net.VpnService; import android.os.Binder; @@ -117,19 +118,42 @@ 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 mbit) { - if (mbit) + public static String humanReadableByteCount(long bytes, boolean speed, Resources res) { + if (speed) bytes = bytes * 8; - int unit = mbit ? 1000 : 1024; - if (bytes < unit) - return bytes + (mbit ? "\u2009bit" : "\u2009B"); - - int exp = (int) (Math.log(bytes) / Math.log(unit)); - String pre = (mbit ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (mbit ? "" : ""); - if (mbit) - return String.format(Locale.getDefault(), "%.1f %sbit", bytes / Math.pow(unit, exp), pre); + int unit = speed ? 1000 : 1024; + + + int exp = Math.min((int) (Math.log(bytes) / Math.log(unit)), 3); + + float bytesUnit = Math.round(bytes / Math.pow(unit, exp)); + + if (speed) + switch (exp) { + case 0: + return res.getString(R.string.bits_per_second, bytesUnit); + case 1: + return res.getString(R.string.kbits_per_second, bytesUnit); + case 2: + return res.getString(R.string.mbits_per_second, bytesUnit); + default: + return res.getString(R.string.gbits_per_second, bytesUnit); + } else - return String.format(Locale.getDefault(), "%.1f %sB", bytes / Math.pow(unit, exp), pre); + switch (exp) { + case 0: + return res.getString(R.string.volume_byte, bytesUnit); + case 1: + return res.getString(R.string.volume_kbyte, bytesUnit); + case 2: + return res.getString(R.string.volume_mbyte, bytesUnit); + default: + return res.getString(R.string.volume_gbyte, bytesUnit); + + } + + + } @Override @@ -1051,10 +1075,10 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac public void updateByteCount(long in, long out, long diffIn, long diffOut) { if (mDisplayBytecount) { String netstat = String.format(getString(R.string.statusline_bytecount), - humanReadableByteCount(in, false), - humanReadableByteCount(diffIn / OpenVPNManagement.mBytecountInterval, true), - humanReadableByteCount(out, false), - humanReadableByteCount(diffOut / OpenVPNManagement.mBytecountInterval, true)); + humanReadableByteCount(in, false, getResources()), + humanReadableByteCount(diffIn / OpenVPNManagement.mBytecountInterval, true, getResources()), + humanReadableByteCount(out, false, getResources()), + humanReadableByteCount(diffOut / OpenVPNManagement.mBytecountInterval, true, getResources())); int priority = mNotificationAlwaysVisible ? PRIORITY_DEFAULT : PRIORITY_MIN; showNotification(netstat, null, priority, mConnecttime, LEVEL_CONNECTED); diff --git a/main/src/main/java/de/blinkt/openvpn/fragments/GraphFragment.java b/main/src/main/java/de/blinkt/openvpn/fragments/GraphFragment.java index e9e6fa47..8273ca9b 100644 --- a/main/src/main/java/de/blinkt/openvpn/fragments/GraphFragment.java +++ b/main/src/main/java/de/blinkt/openvpn/fragments/GraphFragment.java @@ -7,6 +7,7 @@ package de.blinkt.openvpn.fragments; import android.app.Fragment; import android.content.Context; +import android.content.res.Resources; import android.os.Bundle; import android.os.Handler; import android.support.annotation.NonNull; @@ -135,12 +136,13 @@ public class GraphFragment extends Fragment implements VpnStatus.ByteCountListen long now = (System.currentTimeMillis() / 100) - firstTs; int interval = OpenVPNManagement.mBytecountInterval * 10; + Resources res = getActivity().getResources(); final String netstat = String.format(getString(R.string.statusline_bytecount), - humanReadableByteCount(in, false), - humanReadableByteCount(diffIn / OpenVPNManagement.mBytecountInterval, true), - humanReadableByteCount(out, false), - humanReadableByteCount(diffOut / OpenVPNManagement.mBytecountInterval, true)); + humanReadableByteCount(in, false, res), + humanReadableByteCount(diffIn / OpenVPNManagement.mBytecountInterval, true, res), + humanReadableByteCount(out, false, res), + humanReadableByteCount(diffOut / OpenVPNManagement.mBytecountInterval, true, res)); getActivity().runOnUiThread(new Runnable() { @Override @@ -225,6 +227,7 @@ public class GraphFragment extends Fragment implements VpnStatus.ByteCountListen YAxis yAxis = holder.chart.getAxisLeft(); yAxis.setLabelCount(5, false); + final Resources res = getActivity().getResources(); yAxis.setValueFormatter(new IAxisValueFormatter() { @Override public String getFormattedValue(float value, AxisBase axis) { @@ -233,7 +236,7 @@ public class GraphFragment extends Fragment implements VpnStatus.ByteCountListen if (mLogScale) value = (float) Math.pow(10, value)/8; - return humanReadableByteCount((long) value, true) + "/s"; + return humanReadableByteCount((long) value, true, res); } }); @@ -265,13 +268,6 @@ public class GraphFragment extends Fragment implements VpnStatus.ByteCountListen return convertView; } - - public void getAverageForGraphList(boolean in, int timeperiod) { - - - - } - private LineData getDataSet(int timeperiod) { LinkedList dataIn = new LinkedList<>(); diff --git a/main/src/main/java/de/blinkt/openvpn/fragments/LogFragment.java b/main/src/main/java/de/blinkt/openvpn/fragments/LogFragment.java index 7962aac5..30f922ae 100644 --- a/main/src/main/java/de/blinkt/openvpn/fragments/LogFragment.java +++ b/main/src/main/java/de/blinkt/openvpn/fragments/LogFragment.java @@ -16,6 +16,7 @@ import android.content.ClipboardManager; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.res.Resources; import android.database.DataSetObserver; import android.graphics.drawable.Drawable; import android.os.Bundle; @@ -117,8 +118,9 @@ public class LogFragment extends ListFragment implements StateListener, SeekBar. @Override public void updateByteCount(long in, long out, long diffIn, long diffOut) { //%2$s/s %1$s - ↑%4$s/s %3$s - final String down = String.format("%2$s/s %1$s", humanReadableByteCount(in, false), humanReadableByteCount(diffIn / OpenVPNManagement.mBytecountInterval, true)); - final String up = String.format("%2$s/s %1$s", humanReadableByteCount(out, false), humanReadableByteCount(diffOut / OpenVPNManagement.mBytecountInterval, true)); + Resources res = getActivity().getResources(); + final String down = String.format("%2$s %1$s", humanReadableByteCount(in, false, res), humanReadableByteCount(diffIn / OpenVPNManagement.mBytecountInterval, true, res)); + final String up = String.format("%2$s %1$s", humanReadableByteCount(out, false, res), humanReadableByteCount(diffOut / OpenVPNManagement.mBytecountInterval, true, res)); if (mUpStatus != null && mDownStatus != null) { if (getActivity() != null) { diff --git a/main/src/main/res/values/strings.xml b/main/src/main/res/values/strings.xml index c450a00a..c20d1fe4 100755 --- a/main/src/main/res/values/strings.xml +++ b/main/src/main/res/values/strings.xml @@ -250,7 +250,7 @@ Connecting (TCP) Authentication failed Waiting for usable network - ↓%2$s/s %1$s - ↑%4$s/s %3$s + ↓%2$s %1$s - ↑%4$s %3$s Not connected Connecting to VPN %s Connecting to VPN %s @@ -408,7 +408,7 @@ No VPN selected. Default VPN VPN used in places where a default VPN needed. These are currently on boot, for Always-On and the Quick Settings Tile. - Currently slected VPN: \'%s\' + Currently selected VPN: \'%s\' Reconnect Toggle VPN Connect to %s @@ -432,5 +432,14 @@ Last 5 minutes In Out + %.0f bit/s + %.1f kbit/s + %.1f Mbit/s + %.1f Gbit/s + + %.0f B + %.1f kB + %.1f MB + %.1f GB -- cgit v1.2.3