From e4b5bc4dc19ec65f9a7657c32efc85d81beb8241 Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Tue, 17 Jun 2014 16:06:46 +0200 Subject: [PATCH] Another attempt to fix the localized qBittorrent issues as referenced in #115 and hopefully fixes #142. Who would think of localizing JSON? --- .../gui/settings/RssfeedSettingsActivity.java | 2 +- .../gui/settings/ServerSettingsActivity.java | 2 +- .../settings/WebsearchSettingsActivity.java | 2 +- .../Qbittorrent/QbittorrentAdapter.java | 40 +++++++++++-------- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/core/src/org/transdroid/core/gui/settings/RssfeedSettingsActivity.java b/core/src/org/transdroid/core/gui/settings/RssfeedSettingsActivity.java index 3c066da9..5dbd6446 100644 --- a/core/src/org/transdroid/core/gui/settings/RssfeedSettingsActivity.java +++ b/core/src/org/transdroid/core/gui/settings/RssfeedSettingsActivity.java @@ -20,7 +20,7 @@ import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.OptionsItem; import org.androidannotations.annotations.OptionsMenu; import org.transdroid.core.R; -import org.transdroid.core.app.settings.ApplicationSettings_; +import org.transdroid.core.app.settings.*; import android.annotation.TargetApi; import android.app.AlertDialog; diff --git a/core/src/org/transdroid/core/gui/settings/ServerSettingsActivity.java b/core/src/org/transdroid/core/gui/settings/ServerSettingsActivity.java index 4a5a5700..1be3d9cb 100644 --- a/core/src/org/transdroid/core/gui/settings/ServerSettingsActivity.java +++ b/core/src/org/transdroid/core/gui/settings/ServerSettingsActivity.java @@ -20,7 +20,7 @@ import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.OptionsItem; import org.androidannotations.annotations.OptionsMenu; import org.transdroid.core.R; -import org.transdroid.core.app.settings.ApplicationSettings_; +import org.transdroid.core.app.settings.*; import org.transdroid.daemon.Daemon; import android.annotation.TargetApi; diff --git a/core/src/org/transdroid/core/gui/settings/WebsearchSettingsActivity.java b/core/src/org/transdroid/core/gui/settings/WebsearchSettingsActivity.java index 6e874451..fc432f72 100644 --- a/core/src/org/transdroid/core/gui/settings/WebsearchSettingsActivity.java +++ b/core/src/org/transdroid/core/gui/settings/WebsearchSettingsActivity.java @@ -20,7 +20,7 @@ import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.OptionsItem; import org.androidannotations.annotations.OptionsMenu; import org.transdroid.core.R; -import org.transdroid.core.app.settings.ApplicationSettings_; +import org.transdroid.core.app.settings.*; import android.annotation.TargetApi; import android.app.AlertDialog; diff --git a/lib/src/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java b/lib/src/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java index a4e3bc3f..0b1dca96 100644 --- a/lib/src/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java +++ b/lib/src/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java @@ -399,9 +399,7 @@ public class QbittorrentAdapter implements IDaemonAdapter { private double parseRatio(String string) { // Ratio is given in "1.5" string format try { - // FIXME Hack for issue #115: Strip the possible . and , separators in a hopefully reliable fashion, for now - string = string.replace(",", "."); - return Double.parseDouble(string); + return Double.parseDouble(normalizeNumber(string)); } catch (Exception e) { return 0D; } @@ -412,20 +410,14 @@ public class QbittorrentAdapter implements IDaemonAdapter { if (string.equals("Unknown")) return -1; // Sizes are given in "1,023.3 MiB"-like string format - // FIXME Hack for issue #115: Strip the possible . and , separators in a hopefully reliable fashion, for now String[] parts = string.split(" "); - String part1 = ""; - if (parts[0].length() >= 3) - part1 = parts[0].substring(0, parts[0].length() - 3); - String part2 = parts[0].substring(parts[0].length() - 3); - parts[0] = part1.replace("Ê", "").replace(" ", "").replace(",", "").replace(".", "") + part2.replace(",", "."); - // Returns size in B-based long double number; try { - number = Double.parseDouble(parts[0]); + number = Double.parseDouble(normalizeNumber(parts[0])); } catch (Exception e) { return -1L; } + // Returns size in B-based long if (parts[1].equals("TiB")) { return (long) (number * 1024L * 1024L * 1024L * 1024L); } else if (parts[1].equals("GiB")) { @@ -453,19 +445,35 @@ public class QbittorrentAdapter implements IDaemonAdapter { // See https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-Documentation if (speed.equals("Unknown")) return -1; - // Speeds are in "21.9 KiB/s"-like string format - // Returns speed in B/s-based integer + // Sizes are given in "1,023.3 KiB/s"-like string format String[] parts = speed.split(" "); + double number; + try { + number = Double.parseDouble(normalizeNumber(parts[0])); + } catch (Exception e) { + return -1; + } + // Returns size in B-based int if (parts[1].equals("GiB/s")) { - return (int) (Double.parseDouble(parts[0]) * 1024 * 1024 * 1024); + return (int) (number * 1024 * 1024 * 1024); } else if (parts[1].equals("MiB/s")) { - return (int) (Double.parseDouble(parts[0]) * 1024 * 1024); + return (int) (number * 1024 * 1024); } else if (parts[1].equals("KiB/s")) { - return (int) (Double.parseDouble(parts[0]) * 1024); + return (int) (number * 1024); } return (int) (Double.parseDouble(parts[0])); } + private String normalizeNumber(String in) { + // FIXME Hack for issue #115: Strip the possible . and , separators in a hopefully reliable fashion, for now + if (in.length() >= 3) { + String part1 = in.substring(0, in.length() - 3); + String part2 = in.substring(in.length() - 3); + return part1.replace("Ê", "").replace(" ", "").replace(",", "").replace(".", "") + part2.replace(",", "."); + } + return in.replace(",", "."); + } + private TorrentStatus parseStatus(String state) { // Status is given as a descriptive string if (state.equals("downloading")) {