Browse Source

Replace ifs with switches

pull/559/head
TacoTheDank 4 years ago
parent
commit
8c9789aa3d
  1. 16
      app/src/main/java/org/transdroid/core/service/ControlService.java
  2. 12
      app/src/main/java/org/transdroid/daemon/adapters/aria2c/Aria2Adapter.java
  3. 49
      app/src/main/java/org/transdroid/daemon/adapters/bitComet/BitCometAdapter.java
  4. 22
      app/src/main/java/org/transdroid/daemon/adapters/kTorrent/FileListParser.java
  5. 40
      app/src/main/java/org/transdroid/daemon/adapters/kTorrent/StatsParser.java
  6. 40
      app/src/main/java/org/transdroid/daemon/adapters/qBittorrent/QBittorrentAdapter.java
  7. 26
      app/src/main/java/org/transdroid/daemon/adapters/tTorrent/TTorrentAdapter.java
  8. 35
      app/src/main/java/org/transdroid/daemon/adapters/tfb4rt/StatsParser.java

16
app/src/main/java/org/transdroid/core/service/ControlService.java

@ -102,20 +102,26 @@ public class ControlService extends IntentService {
// See which action should be performed on the server // See which action should be performed on the server
IDaemonAdapter adapter = server.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this); IDaemonAdapter adapter = server.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this);
DaemonTask task = null; DaemonTask task = null;
if (intent.getAction().equals(INTENT_RESUMEALL)) { switch (intent.getAction()) {
case INTENT_RESUMEALL:
task = ResumeAllTask.create(adapter); task = ResumeAllTask.create(adapter);
} else if (intent.getAction().equals(INTENT_PAUSEALL)) { break;
case INTENT_PAUSEALL:
task = PauseAllTask.create(adapter); task = PauseAllTask.create(adapter);
} else if (intent.getAction().equals(INTENT_STARTALL)) { break;
case INTENT_STARTALL:
task = StartAllTask.create(adapter, false); task = StartAllTask.create(adapter, false);
} else if (intent.getAction().equals(INTENT_STOPALL)) { break;
case INTENT_STOPALL:
task = StopAllTask.create(adapter); task = StopAllTask.create(adapter);
} else if (intent.getAction().equals(INTENT_SETTRANSFERRATES)) { break;
case INTENT_SETTRANSFERRATES:
// NOTE: If the upload or download rate was not specified, it will be reset on the server instead // NOTE: If the upload or download rate was not specified, it will be reset on the server instead
int uploadRate = intent.getIntExtra(EXTRA_UPLOAD_RATE, -1); int uploadRate = intent.getIntExtra(EXTRA_UPLOAD_RATE, -1);
int downloadRate = intent.getIntExtra(EXTRA_DOWNLOAD_RATE, -1); int downloadRate = intent.getIntExtra(EXTRA_DOWNLOAD_RATE, -1);
task = SetTransferRatesTask task = SetTransferRatesTask
.create(adapter, uploadRate == -1 ? null : uploadRate, downloadRate == -1 ? null : downloadRate); .create(adapter, uploadRate == -1 ? null : uploadRate, downloadRate == -1 ? null : downloadRate);
break;
} }
// Execute the task, if we have one now // Execute the task, if we have one now

12
app/src/main/java/org/transdroid/daemon/adapters/aria2c/Aria2Adapter.java

@ -448,15 +448,17 @@ public class Aria2Adapter implements IDaemonAdapter {
private TorrentStatus convertAriaState(String state, boolean isFinished) { private TorrentStatus convertAriaState(String state, boolean isFinished) {
// Aria2 sends a string as status code // Aria2 sends a string as status code
// (http://aria2.sourceforge.net/manual/en/html/aria2c.html#aria2.tellStatus) // (http://aria2.sourceforge.net/manual/en/html/aria2c.html#aria2.tellStatus)
if (state.equals("active")) { switch (state) {
case "active":
return isFinished ? TorrentStatus.Seeding : TorrentStatus.Downloading; return isFinished ? TorrentStatus.Seeding : TorrentStatus.Downloading;
} else if (state.equals("waiting")) { case "waiting":
return TorrentStatus.Queued; return TorrentStatus.Queued;
} else if (state.equals("paused") || state.equals("complete")) { case "paused":
case "complete":
return TorrentStatus.Paused; return TorrentStatus.Paused;
} else if (state.equals("error")) { case "error":
return TorrentStatus.Error; return TorrentStatus.Error;
} else if (state.equals("removed")) { case "removed":
return TorrentStatus.Checking; return TorrentStatus.Checking;
} }
return TorrentStatus.Unknown; return TorrentStatus.Unknown;

49
app/src/main/java/org/transdroid/daemon/adapters/bitComet/BitCometAdapter.java

@ -643,38 +643,55 @@ public class BitCometAdapter implements IDaemonAdapter {
// Probably encountered a torrent property, i.e. '<type>BT</type>' // Probably encountered a torrent property, i.e. '<type>BT</type>'
next = xpp.next(); next = xpp.next();
if (next == XmlPullParser.TEXT) { if (next == XmlPullParser.TEXT) {
if (tagName.equals("name")) { switch (tagName) {
case "name":
name = xpp.getText().trim(); name = xpp.getText().trim();
} else if (tagName.equals("id")) { break;
case "id":
id = Integer.parseInt(xpp.getText().trim()); id = Integer.parseInt(xpp.getText().trim());
} else if (tagName.equals("infohash")) { break;
case "infohash":
hash = xpp.getText().trim(); hash = xpp.getText().trim();
} else if (tagName.equals("state")) { break;
case "state":
status = convertStatus(xpp.getText()); status = convertStatus(xpp.getText());
} else if (tagName.equals("bytes_downloaded")) { break;
case "bytes_downloaded":
sizeDone = Integer.parseInt(xpp.getText()); sizeDone = Integer.parseInt(xpp.getText());
} else if (tagName.equals("bytes_uploaded")) { break;
case "bytes_uploaded":
sizeUp = Integer.parseInt(xpp.getText()); sizeUp = Integer.parseInt(xpp.getText());
} else if (tagName.equals("size")) { break;
case "size":
totalSize = Long.parseLong(xpp.getText()); totalSize = Long.parseLong(xpp.getText());
} else if (tagName.equals("down_speed")) { break;
case "down_speed":
rateDown = Integer.parseInt(xpp.getText()); rateDown = Integer.parseInt(xpp.getText());
} else if (tagName.equals("up_speed")) { break;
case "up_speed":
rateUp = Integer.parseInt(xpp.getText()); rateUp = Integer.parseInt(xpp.getText());
} else if (tagName.equals("seeders")) { break;
case "seeders":
seeders = Integer.parseInt(xpp.getText()); seeders = Integer.parseInt(xpp.getText());
} else if (tagName.equals("total_seeders")) { break;
case "total_seeders":
seedersTotal = Integer.parseInt(xpp.getText()); seedersTotal = Integer.parseInt(xpp.getText());
} else if (tagName.equals("peers")) { break;
case "peers":
leechers = Integer.parseInt(xpp.getText()); leechers = Integer.parseInt(xpp.getText());
} else if (tagName.equals("total_peers")) { break;
case "total_peers":
leechersTotal = Integer.parseInt(xpp.getText()); leechersTotal = Integer.parseInt(xpp.getText());
} else if (tagName.equals("progress_permillage")) { break;
case "progress_permillage":
progress = convertProgress(xpp.getText()); progress = convertProgress(xpp.getText());
} else if (tagName.equals("created_time")) { break;
case "created_time":
dateAdded = new Date(Long.parseLong(xpp.getText())); dateAdded = new Date(Long.parseLong(xpp.getText()));
} else if (tagName.equals("comment")) { break;
case "comment":
label = xpp.getText().trim(); label = xpp.getText().trim();
break;
} }
} }
} }

22
app/src/main/java/org/transdroid/daemon/adapters/kTorrent/FileListParser.java

@ -75,14 +75,19 @@ public class FileListParser {
// Probably encountered a file property, i.e. '<percentage>73.09</percentage>' // Probably encountered a file property, i.e. '<percentage>73.09</percentage>'
next = xpp.next(); next = xpp.next();
if (next == XmlPullParser.TEXT) { if (next == XmlPullParser.TEXT) {
if (name.equals("path")) { switch (name) {
case "path":
path = xpp.getText().trim(); path = xpp.getText().trim();
} else if (name.equals("size")) { break;
case "size":
size = StatsParser.convertSize(xpp.getText()); size = StatsParser.convertSize(xpp.getText());
} else if (name.equals("priority")) { break;
case "priority":
priority = convertPriority(xpp.getText()); priority = convertPriority(xpp.getText());
} else if (name.equals("percentage")) { break;
case "percentage":
partDone = StatsParser.convertProgress(xpp.getText()); partDone = StatsParser.convertProgress(xpp.getText());
break;
} }
} }
} }
@ -111,13 +116,14 @@ public class FileListParser {
* @return The priority as enum type, i.e. Priority.Off * @return The priority as enum type, i.e. Priority.Off
*/ */
private static Priority convertPriority(String priority) { private static Priority convertPriority(String priority) {
if (priority.equals("20")) { switch (priority) {
case "20":
return Priority.Off; return Priority.Off;
} else if (priority.equals("30")) { case "30":
return Priority.Low; return Priority.Low;
} else if (priority.equals("50")) { case "50":
return Priority.High; return Priority.High;
} else { default:
return Priority.Normal; return Priority.Normal;
} }
} }

40
app/src/main/java/org/transdroid/daemon/adapters/kTorrent/StatsParser.java

@ -112,34 +112,48 @@ public class StatsParser {
// Probably encountered a torrent property, i.e. '<status>Stopped</status>' // Probably encountered a torrent property, i.e. '<status>Stopped</status>'
next = xpp.next(); next = xpp.next();
if (next == XmlPullParser.TEXT) { if (next == XmlPullParser.TEXT) {
if (name.equals("name")) { switch (name) {
case "name":
tname = xpp.getText().trim(); tname = xpp.getText().trim();
//} else if (name.equals("info_hash")) { //} else if (name.equals("info_hash")) {
//hash = xpp.getText().trim(); //hash = xpp.getText().trim();
} else if (name.equals("status")) { break;
case "status":
status = convertStatus(xpp.getText()); status = convertStatus(xpp.getText());
} else if (name.equals("bytes_downloaded")) { break;
case "bytes_downloaded":
down = convertSize(xpp.getText()); down = convertSize(xpp.getText());
} else if (name.equals("bytes_uploaded")) { break;
case "bytes_uploaded":
up = convertSize(xpp.getText()); up = convertSize(xpp.getText());
} else if (name.equals("total_bytes_to_download")) { break;
case "total_bytes_to_download":
total = convertSize(xpp.getText()); total = convertSize(xpp.getText());
} else if (name.equals("download_rate")) { break;
case "download_rate":
downRate = convertRate(xpp.getText()); downRate = convertRate(xpp.getText());
} else if (name.equals("upload_rate")) { break;
case "upload_rate":
upRate = convertRate(xpp.getText()); upRate = convertRate(xpp.getText());
} else if (name.equals("seeders")) { break;
case "seeders":
seeders = Integer.parseInt(xpp.getText()); seeders = Integer.parseInt(xpp.getText());
} else if (name.equals("seeders_total")) { break;
case "seeders_total":
seedersTotal = Integer.parseInt(xpp.getText()); seedersTotal = Integer.parseInt(xpp.getText());
} else if (name.equals("leechers")) { break;
case "leechers":
leechers = Integer.parseInt(xpp.getText()); leechers = Integer.parseInt(xpp.getText());
} else if (name.equals("leechers_total")) { break;
case "leechers_total":
leechersTotal = Integer.parseInt(xpp.getText()); leechersTotal = Integer.parseInt(xpp.getText());
} else if (name.equals("percentage")) { break;
case "percentage":
progress = convertProgress(xpp.getText()); progress = convertProgress(xpp.getText());
} else if (name.equals("num_files")) { break;
case "num_files":
numFiles = Integer.parseInt(xpp.getText()); numFiles = Integer.parseInt(xpp.getText());
break;
} }
} }
} }

40
app/src/main/java/org/transdroid/daemon/adapters/qBittorrent/QBittorrentAdapter.java

@ -776,13 +776,14 @@ public class QBittorrentAdapter implements IDaemonAdapter {
return (long) number; return (long) number;
} }
// Returns size in B-based long // Returns size in B-based long
if (parts[1].equals("TiB")) { switch (parts[1]) {
case "TiB":
return (long) (number * 1024L * 1024L * 1024L * 1024L); return (long) (number * 1024L * 1024L * 1024L * 1024L);
} else if (parts[1].equals("GiB")) { case "GiB":
return (long) (number * 1024L * 1024L * 1024L); return (long) (number * 1024L * 1024L * 1024L);
} else if (parts[1].equals("MiB")) { case "MiB":
return (long) (number * 1024L * 1024L); return (long) (number * 1024L * 1024L);
} else if (parts[1].equals("KiB")) { case "KiB":
return (long) (number * 1024L); return (long) (number * 1024L);
} }
return (long) number; return (long) number;
@ -811,11 +812,12 @@ public class QBittorrentAdapter implements IDaemonAdapter {
return -1; return -1;
} }
// Returns size in B-based int // Returns size in B-based int
if (parts[1].equals("GiB/s")) { switch (parts[1]) {
case "GiB/s":
return (int) (number * 1024 * 1024 * 1024); return (int) (number * 1024 * 1024 * 1024);
} else if (parts[1].equals("MiB/s")) { case "MiB/s":
return (int) (number * 1024 * 1024); return (int) (number * 1024 * 1024);
} else if (parts[1].equals("KiB/s")) { case "KiB/s":
return (int) (number * 1024); return (int) (number * 1024);
} }
return (int) (Double.parseDouble(normalizeNumber(parts[0]))); return (int) (Double.parseDouble(normalizeNumber(parts[0])));
@ -833,27 +835,29 @@ public class QBittorrentAdapter implements IDaemonAdapter {
private TorrentStatus parseStatus(String state) { private TorrentStatus parseStatus(String state) {
// Status is given as a descriptive string // Status is given as a descriptive string
if (state.equals("error")) { switch (state) {
case "error":
return TorrentStatus.Error; return TorrentStatus.Error;
} else if (state.equals("downloading") || state.equals("metaDL")) { case "downloading":
case "metaDL":
return TorrentStatus.Downloading; return TorrentStatus.Downloading;
} else if (state.equals("uploading")) { case "uploading":
return TorrentStatus.Seeding; return TorrentStatus.Seeding;
} else if (state.equals("pausedDL")) { case "pausedDL":
return TorrentStatus.Paused; return TorrentStatus.Paused;
} else if (state.equals("pausedUP")) { case "pausedUP":
return TorrentStatus.Paused; return TorrentStatus.Paused;
} else if (state.equals("stalledUP")) { case "stalledUP":
return TorrentStatus.Seeding; return TorrentStatus.Seeding;
} else if (state.equals("stalledDL")) { case "stalledDL":
return TorrentStatus.Downloading; return TorrentStatus.Downloading;
} else if (state.equals("checkingUP")) { case "checkingUP":
return TorrentStatus.Checking; return TorrentStatus.Checking;
} else if (state.equals("checkingDL")) { case "checkingDL":
return TorrentStatus.Checking; return TorrentStatus.Checking;
} else if (state.equals("queuedDL")) { case "queuedDL":
return TorrentStatus.Queued; return TorrentStatus.Queued;
} else if (state.equals("queuedUP")) { case "queuedUP":
return TorrentStatus.Queued; return TorrentStatus.Queued;
} }
return TorrentStatus.Unknown; return TorrentStatus.Unknown;

26
app/src/main/java/org/transdroid/daemon/adapters/tTorrent/TTorrentAdapter.java

@ -383,25 +383,21 @@ public class TTorrentAdapter implements IDaemonAdapter {
private TorrentStatus parseStatus(String state) { private TorrentStatus parseStatus(String state) {
// Status is given as a descriptive string // Status is given as a descriptive string
if (state.equals("downloading")) { switch (state) {
case "downloading":
case "stalledDL":
return TorrentStatus.Downloading; return TorrentStatus.Downloading;
} else if (state.equals("uploading")) { case "uploading":
case "stalledUP":
return TorrentStatus.Seeding; return TorrentStatus.Seeding;
} else if (state.equals("pausedDL")) { case "pausedDL":
case "pausedUL":
return TorrentStatus.Paused; return TorrentStatus.Paused;
} else if (state.equals("pausedUL")) { case "checkingUP":
return TorrentStatus.Paused; case "checkingDL":
} else if (state.equals("stalledUP")) {
return TorrentStatus.Seeding;
} else if (state.equals("stalledDL")) {
return TorrentStatus.Downloading;
} else if (state.equals("checkingUP")) {
return TorrentStatus.Checking; return TorrentStatus.Checking;
} else if (state.equals("checkingDL")) { case "queuedDL":
return TorrentStatus.Checking; case "queuedUL":
} else if (state.equals("queuedDL")) {
return TorrentStatus.Queued;
} else if (state.equals("queuedUL")) {
return TorrentStatus.Queued; return TorrentStatus.Queued;
} }
return TorrentStatus.Unknown; return TorrentStatus.Unknown;

35
app/src/main/java/org/transdroid/daemon/adapters/tfb4rt/StatsParser.java

@ -105,20 +105,28 @@ public class StatsParser {
next = xpp.next(); next = xpp.next();
if (next == XmlPullParser.TEXT) { if (next == XmlPullParser.TEXT) {
try { try {
if (type.equals("Size")) { switch (type) {
case "Size":
size = convertSize(xpp.getText()); size = convertSize(xpp.getText());
} else if (type.equals("Status")) { break;
case "Status":
status = convertStatus(xpp.getText()); status = convertStatus(xpp.getText());
} else if (type.equals("Progress")) { break;
case "Progress":
progress = convertProgress(xpp.getText()); progress = convertProgress(xpp.getText());
} else if (type.equals("Down")) { break;
case "Down":
down = convertRate(xpp.getText()); down = convertRate(xpp.getText());
} else if (type.equals("Up")) { break;
case "Up":
up = convertRate(xpp.getText()); up = convertRate(xpp.getText());
} else if (type.equals("Estimated Time")) { break;
case "Estimated Time":
time = convertEta(xpp.getText()); time = convertEta(xpp.getText());
} else if (type.equals("T. Up")) { break;
case "T. Up":
upSize = convertSize(xpp.getText()); upSize = convertSize(xpp.getText());
break;
} }
} catch (Exception e) { } catch (Exception e) {
throw new DaemonException(ExceptionType.ConnectionError, e.toString()); throw new DaemonException(ExceptionType.ConnectionError, e.toString());
@ -235,15 +243,14 @@ public class StatsParser {
* @return The status as TorrentStatus or Unknown if it could not been parsed * @return The status as TorrentStatus or Unknown if it could not been parsed
*/ */
private static TorrentStatus convertStatus(String status) { private static TorrentStatus convertStatus(String status) {
if (status.equals("Leeching")) { switch (status) {
case "Leeching":
return TorrentStatus.Downloading; return TorrentStatus.Downloading;
} else if (status.equals("Seeding")) { case "Seeding":
return TorrentStatus.Seeding; return TorrentStatus.Seeding;
} else if (status.equals("Stopped")) { case "Stopped":
return TorrentStatus.Paused; case "New":
} else if (status.equals("New")) { case "Done":
return TorrentStatus.Paused;
} else if (status.equals("Done")) {
return TorrentStatus.Paused; return TorrentStatus.Paused;
} }
return TorrentStatus.Unknown; return TorrentStatus.Unknown;

Loading…
Cancel
Save