Browse Source

Replace ifs with switches

pull/559/head
TacoTheDank 4 years ago
parent
commit
8c9789aa3d
  1. 34
      app/src/main/java/org/transdroid/core/service/ControlService.java
  2. 22
      app/src/main/java/org/transdroid/daemon/adapters/aria2c/Aria2Adapter.java
  3. 81
      app/src/main/java/org/transdroid/daemon/adapters/bitComet/BitCometAdapter.java
  4. 38
      app/src/main/java/org/transdroid/daemon/adapters/kTorrent/FileListParser.java
  5. 70
      app/src/main/java/org/transdroid/daemon/adapters/kTorrent/StatsParser.java
  6. 76
      app/src/main/java/org/transdroid/daemon/adapters/qBittorrent/QBittorrentAdapter.java
  7. 36
      app/src/main/java/org/transdroid/daemon/adapters/tTorrent/TTorrentAdapter.java
  8. 55
      app/src/main/java/org/transdroid/daemon/adapters/tfb4rt/StatsParser.java

34
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()) {
task = ResumeAllTask.create(adapter); case INTENT_RESUMEALL:
} else if (intent.getAction().equals(INTENT_PAUSEALL)) { task = ResumeAllTask.create(adapter);
task = PauseAllTask.create(adapter); break;
} else if (intent.getAction().equals(INTENT_STARTALL)) { case INTENT_PAUSEALL:
task = StartAllTask.create(adapter, false); task = PauseAllTask.create(adapter);
} else if (intent.getAction().equals(INTENT_STOPALL)) { break;
task = StopAllTask.create(adapter); case INTENT_STARTALL:
} else if (intent.getAction().equals(INTENT_SETTRANSFERRATES)) { task = StartAllTask.create(adapter, false);
// NOTE: If the upload or download rate was not specified, it will be reset on the server instead break;
int uploadRate = intent.getIntExtra(EXTRA_UPLOAD_RATE, -1); case INTENT_STOPALL:
int downloadRate = intent.getIntExtra(EXTRA_DOWNLOAD_RATE, -1); task = StopAllTask.create(adapter);
task = SetTransferRatesTask break;
.create(adapter, uploadRate == -1 ? null : uploadRate, downloadRate == -1 ? null : downloadRate); case INTENT_SETTRANSFERRATES:
// 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 downloadRate = intent.getIntExtra(EXTRA_DOWNLOAD_RATE, -1);
task = SetTransferRatesTask
.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

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

@ -448,16 +448,18 @@ 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) {
return isFinished ? TorrentStatus.Seeding : TorrentStatus.Downloading; case "active":
} else if (state.equals("waiting")) { return isFinished ? TorrentStatus.Seeding : TorrentStatus.Downloading;
return TorrentStatus.Queued; case "waiting":
} else if (state.equals("paused") || state.equals("complete")) { return TorrentStatus.Queued;
return TorrentStatus.Paused; case "paused":
} else if (state.equals("error")) { case "complete":
return TorrentStatus.Error; return TorrentStatus.Paused;
} else if (state.equals("removed")) { case "error":
return TorrentStatus.Checking; return TorrentStatus.Error;
case "removed":
return TorrentStatus.Checking;
} }
return TorrentStatus.Unknown; return TorrentStatus.Unknown;
} }

81
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) {
name = xpp.getText().trim(); case "name":
} else if (tagName.equals("id")) { name = xpp.getText().trim();
id = Integer.parseInt(xpp.getText().trim()); break;
} else if (tagName.equals("infohash")) { case "id":
hash = xpp.getText().trim(); id = Integer.parseInt(xpp.getText().trim());
} else if (tagName.equals("state")) { break;
status = convertStatus(xpp.getText()); case "infohash":
} else if (tagName.equals("bytes_downloaded")) { hash = xpp.getText().trim();
sizeDone = Integer.parseInt(xpp.getText()); break;
} else if (tagName.equals("bytes_uploaded")) { case "state":
sizeUp = Integer.parseInt(xpp.getText()); status = convertStatus(xpp.getText());
} else if (tagName.equals("size")) { break;
totalSize = Long.parseLong(xpp.getText()); case "bytes_downloaded":
} else if (tagName.equals("down_speed")) { sizeDone = Integer.parseInt(xpp.getText());
rateDown = Integer.parseInt(xpp.getText()); break;
} else if (tagName.equals("up_speed")) { case "bytes_uploaded":
rateUp = Integer.parseInt(xpp.getText()); sizeUp = Integer.parseInt(xpp.getText());
} else if (tagName.equals("seeders")) { break;
seeders = Integer.parseInt(xpp.getText()); case "size":
} else if (tagName.equals("total_seeders")) { totalSize = Long.parseLong(xpp.getText());
seedersTotal = Integer.parseInt(xpp.getText()); break;
} else if (tagName.equals("peers")) { case "down_speed":
leechers = Integer.parseInt(xpp.getText()); rateDown = Integer.parseInt(xpp.getText());
} else if (tagName.equals("total_peers")) { break;
leechersTotal = Integer.parseInt(xpp.getText()); case "up_speed":
} else if (tagName.equals("progress_permillage")) { rateUp = Integer.parseInt(xpp.getText());
progress = convertProgress(xpp.getText()); break;
} else if (tagName.equals("created_time")) { case "seeders":
dateAdded = new Date(Long.parseLong(xpp.getText())); seeders = Integer.parseInt(xpp.getText());
} else if (tagName.equals("comment")) { break;
label = xpp.getText().trim(); case "total_seeders":
seedersTotal = Integer.parseInt(xpp.getText());
break;
case "peers":
leechers = Integer.parseInt(xpp.getText());
break;
case "total_peers":
leechersTotal = Integer.parseInt(xpp.getText());
break;
case "progress_permillage":
progress = convertProgress(xpp.getText());
break;
case "created_time":
dateAdded = new Date(Long.parseLong(xpp.getText()));
break;
case "comment":
label = xpp.getText().trim();
break;
} }
} }
} }

38
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) {
path = xpp.getText().trim(); case "path":
} else if (name.equals("size")) { path = xpp.getText().trim();
size = StatsParser.convertSize(xpp.getText()); break;
} else if (name.equals("priority")) { case "size":
priority = convertPriority(xpp.getText()); size = StatsParser.convertSize(xpp.getText());
} else if (name.equals("percentage")) { break;
partDone = StatsParser.convertProgress(xpp.getText()); case "priority":
priority = convertPriority(xpp.getText());
break;
case "percentage":
partDone = StatsParser.convertProgress(xpp.getText());
break;
} }
} }
} }
@ -111,14 +116,15 @@ 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) {
return Priority.Off; case "20":
} else if (priority.equals("30")) { return Priority.Off;
return Priority.Low; case "30":
} else if (priority.equals("50")) { return Priority.Low;
return Priority.High; case "50":
} else { return Priority.High;
return Priority.Normal; default:
return Priority.Normal;
} }
} }

70
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) {
tname = xpp.getText().trim(); case "name":
//} else if (name.equals("info_hash")) { tname = xpp.getText().trim();
//hash = xpp.getText().trim(); //} else if (name.equals("info_hash")) {
} else if (name.equals("status")) { //hash = xpp.getText().trim();
status = convertStatus(xpp.getText()); break;
} else if (name.equals("bytes_downloaded")) { case "status":
down = convertSize(xpp.getText()); status = convertStatus(xpp.getText());
} else if (name.equals("bytes_uploaded")) { break;
up = convertSize(xpp.getText()); case "bytes_downloaded":
} else if (name.equals("total_bytes_to_download")) { down = convertSize(xpp.getText());
total = convertSize(xpp.getText()); break;
} else if (name.equals("download_rate")) { case "bytes_uploaded":
downRate = convertRate(xpp.getText()); up = convertSize(xpp.getText());
} else if (name.equals("upload_rate")) { break;
upRate = convertRate(xpp.getText()); case "total_bytes_to_download":
} else if (name.equals("seeders")) { total = convertSize(xpp.getText());
seeders = Integer.parseInt(xpp.getText()); break;
} else if (name.equals("seeders_total")) { case "download_rate":
seedersTotal = Integer.parseInt(xpp.getText()); downRate = convertRate(xpp.getText());
} else if (name.equals("leechers")) { break;
leechers = Integer.parseInt(xpp.getText()); case "upload_rate":
} else if (name.equals("leechers_total")) { upRate = convertRate(xpp.getText());
leechersTotal = Integer.parseInt(xpp.getText()); break;
} else if (name.equals("percentage")) { case "seeders":
progress = convertProgress(xpp.getText()); seeders = Integer.parseInt(xpp.getText());
} else if (name.equals("num_files")) { break;
numFiles = Integer.parseInt(xpp.getText()); case "seeders_total":
seedersTotal = Integer.parseInt(xpp.getText());
break;
case "leechers":
leechers = Integer.parseInt(xpp.getText());
break;
case "leechers_total":
leechersTotal = Integer.parseInt(xpp.getText());
break;
case "percentage":
progress = convertProgress(xpp.getText());
break;
case "num_files":
numFiles = Integer.parseInt(xpp.getText());
break;
} }
} }
} }

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

@ -776,14 +776,15 @@ 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]) {
return (long) (number * 1024L * 1024L * 1024L * 1024L); case "TiB":
} else if (parts[1].equals("GiB")) { return (long) (number * 1024L * 1024L * 1024L * 1024L);
return (long) (number * 1024L * 1024L * 1024L); case "GiB":
} else if (parts[1].equals("MiB")) { return (long) (number * 1024L * 1024L * 1024L);
return (long) (number * 1024L * 1024L); case "MiB":
} else if (parts[1].equals("KiB")) { return (long) (number * 1024L * 1024L);
return (long) (number * 1024L); case "KiB":
return (long) (number * 1024L);
} }
return (long) number; return (long) number;
} }
@ -811,12 +812,13 @@ 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]) {
return (int) (number * 1024 * 1024 * 1024); case "GiB/s":
} else if (parts[1].equals("MiB/s")) { return (int) (number * 1024 * 1024 * 1024);
return (int) (number * 1024 * 1024); case "MiB/s":
} else if (parts[1].equals("KiB/s")) { return (int) (number * 1024 * 1024);
return (int) (number * 1024); case "KiB/s":
return (int) (number * 1024);
} }
return (int) (Double.parseDouble(normalizeNumber(parts[0]))); return (int) (Double.parseDouble(normalizeNumber(parts[0])));
} }
@ -833,28 +835,30 @@ 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) {
return TorrentStatus.Error; case "error":
} else if (state.equals("downloading") || state.equals("metaDL")) { return TorrentStatus.Error;
return TorrentStatus.Downloading; case "downloading":
} else if (state.equals("uploading")) { case "metaDL":
return TorrentStatus.Seeding; return TorrentStatus.Downloading;
} else if (state.equals("pausedDL")) { case "uploading":
return TorrentStatus.Paused; return TorrentStatus.Seeding;
} else if (state.equals("pausedUP")) { case "pausedDL":
return TorrentStatus.Paused; return TorrentStatus.Paused;
} else if (state.equals("stalledUP")) { case "pausedUP":
return TorrentStatus.Seeding; return TorrentStatus.Paused;
} else if (state.equals("stalledDL")) { case "stalledUP":
return TorrentStatus.Downloading; return TorrentStatus.Seeding;
} else if (state.equals("checkingUP")) { case "stalledDL":
return TorrentStatus.Checking; return TorrentStatus.Downloading;
} else if (state.equals("checkingDL")) { case "checkingUP":
return TorrentStatus.Checking; return TorrentStatus.Checking;
} else if (state.equals("queuedDL")) { case "checkingDL":
return TorrentStatus.Queued; return TorrentStatus.Checking;
} else if (state.equals("queuedUP")) { case "queuedDL":
return TorrentStatus.Queued; return TorrentStatus.Queued;
case "queuedUP":
return TorrentStatus.Queued;
} }
return TorrentStatus.Unknown; return TorrentStatus.Unknown;
} }

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

@ -383,26 +383,22 @@ 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) {
return TorrentStatus.Downloading; case "downloading":
} else if (state.equals("uploading")) { case "stalledDL":
return TorrentStatus.Seeding; return TorrentStatus.Downloading;
} else if (state.equals("pausedDL")) { case "uploading":
return TorrentStatus.Paused; case "stalledUP":
} else if (state.equals("pausedUL")) { return TorrentStatus.Seeding;
return TorrentStatus.Paused; case "pausedDL":
} else if (state.equals("stalledUP")) { case "pausedUL":
return TorrentStatus.Seeding; return TorrentStatus.Paused;
} else if (state.equals("stalledDL")) { case "checkingUP":
return TorrentStatus.Downloading; case "checkingDL":
} else if (state.equals("checkingUP")) { return TorrentStatus.Checking;
return TorrentStatus.Checking; case "queuedDL":
} else if (state.equals("checkingDL")) { case "queuedUL":
return TorrentStatus.Checking; return TorrentStatus.Queued;
} else if (state.equals("queuedDL")) {
return TorrentStatus.Queued;
} else if (state.equals("queuedUL")) {
return TorrentStatus.Queued;
} }
return TorrentStatus.Unknown; return TorrentStatus.Unknown;
} }

55
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) {
size = convertSize(xpp.getText()); case "Size":
} else if (type.equals("Status")) { size = convertSize(xpp.getText());
status = convertStatus(xpp.getText()); break;
} else if (type.equals("Progress")) { case "Status":
progress = convertProgress(xpp.getText()); status = convertStatus(xpp.getText());
} else if (type.equals("Down")) { break;
down = convertRate(xpp.getText()); case "Progress":
} else if (type.equals("Up")) { progress = convertProgress(xpp.getText());
up = convertRate(xpp.getText()); break;
} else if (type.equals("Estimated Time")) { case "Down":
time = convertEta(xpp.getText()); down = convertRate(xpp.getText());
} else if (type.equals("T. Up")) { break;
upSize = convertSize(xpp.getText()); case "Up":
up = convertRate(xpp.getText());
break;
case "Estimated Time":
time = convertEta(xpp.getText());
break;
case "T. Up":
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,16 +243,15 @@ 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) {
return TorrentStatus.Downloading; case "Leeching":
} else if (status.equals("Seeding")) { return TorrentStatus.Downloading;
return TorrentStatus.Seeding; case "Seeding":
} else if (status.equals("Stopped")) { return TorrentStatus.Seeding;
return TorrentStatus.Paused; case "Stopped":
} else if (status.equals("New")) { case "New":
return TorrentStatus.Paused; case "Done":
} else if (status.equals("Done")) { return TorrentStatus.Paused;
return TorrentStatus.Paused;
} }
return TorrentStatus.Unknown; return TorrentStatus.Unknown;
} }

Loading…
Cancel
Save