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 { @@ -102,20 +102,26 @@ public class ControlService extends IntentService {
// See which action should be performed on the server
IDaemonAdapter adapter = server.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this);
DaemonTask task = null;
if (intent.getAction().equals(INTENT_RESUMEALL)) {
task = ResumeAllTask.create(adapter);
} else if (intent.getAction().equals(INTENT_PAUSEALL)) {
task = PauseAllTask.create(adapter);
} else if (intent.getAction().equals(INTENT_STARTALL)) {
task = StartAllTask.create(adapter, false);
} else if (intent.getAction().equals(INTENT_STOPALL)) {
task = StopAllTask.create(adapter);
} else if (intent.getAction().equals(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);
switch (intent.getAction()) {
case INTENT_RESUMEALL:
task = ResumeAllTask.create(adapter);
break;
case INTENT_PAUSEALL:
task = PauseAllTask.create(adapter);
break;
case INTENT_STARTALL:
task = StartAllTask.create(adapter, false);
break;
case INTENT_STOPALL:
task = StopAllTask.create(adapter);
break;
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

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

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

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

@ -643,38 +643,55 @@ public class BitCometAdapter implements IDaemonAdapter { @@ -643,38 +643,55 @@ public class BitCometAdapter implements IDaemonAdapter {
// Probably encountered a torrent property, i.e. '<type>BT</type>'
next = xpp.next();
if (next == XmlPullParser.TEXT) {
if (tagName.equals("name")) {
name = xpp.getText().trim();
} else if (tagName.equals("id")) {
id = Integer.parseInt(xpp.getText().trim());
} else if (tagName.equals("infohash")) {
hash = xpp.getText().trim();
} else if (tagName.equals("state")) {
status = convertStatus(xpp.getText());
} else if (tagName.equals("bytes_downloaded")) {
sizeDone = Integer.parseInt(xpp.getText());
} else if (tagName.equals("bytes_uploaded")) {
sizeUp = Integer.parseInt(xpp.getText());
} else if (tagName.equals("size")) {
totalSize = Long.parseLong(xpp.getText());
} else if (tagName.equals("down_speed")) {
rateDown = Integer.parseInt(xpp.getText());
} else if (tagName.equals("up_speed")) {
rateUp = Integer.parseInt(xpp.getText());
} else if (tagName.equals("seeders")) {
seeders = Integer.parseInt(xpp.getText());
} else if (tagName.equals("total_seeders")) {
seedersTotal = Integer.parseInt(xpp.getText());
} else if (tagName.equals("peers")) {
leechers = Integer.parseInt(xpp.getText());
} else if (tagName.equals("total_peers")) {
leechersTotal = Integer.parseInt(xpp.getText());
} else if (tagName.equals("progress_permillage")) {
progress = convertProgress(xpp.getText());
} else if (tagName.equals("created_time")) {
dateAdded = new Date(Long.parseLong(xpp.getText()));
} else if (tagName.equals("comment")) {
label = xpp.getText().trim();
switch (tagName) {
case "name":
name = xpp.getText().trim();
break;
case "id":
id = Integer.parseInt(xpp.getText().trim());
break;
case "infohash":
hash = xpp.getText().trim();
break;
case "state":
status = convertStatus(xpp.getText());
break;
case "bytes_downloaded":
sizeDone = Integer.parseInt(xpp.getText());
break;
case "bytes_uploaded":
sizeUp = Integer.parseInt(xpp.getText());
break;
case "size":
totalSize = Long.parseLong(xpp.getText());
break;
case "down_speed":
rateDown = Integer.parseInt(xpp.getText());
break;
case "up_speed":
rateUp = Integer.parseInt(xpp.getText());
break;
case "seeders":
seeders = Integer.parseInt(xpp.getText());
break;
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 { @@ -75,14 +75,19 @@ public class FileListParser {
// Probably encountered a file property, i.e. '<percentage>73.09</percentage>'
next = xpp.next();
if (next == XmlPullParser.TEXT) {
if (name.equals("path")) {
path = xpp.getText().trim();
} else if (name.equals("size")) {
size = StatsParser.convertSize(xpp.getText());
} else if (name.equals("priority")) {
priority = convertPriority(xpp.getText());
} else if (name.equals("percentage")) {
partDone = StatsParser.convertProgress(xpp.getText());
switch (name) {
case "path":
path = xpp.getText().trim();
break;
case "size":
size = StatsParser.convertSize(xpp.getText());
break;
case "priority":
priority = convertPriority(xpp.getText());
break;
case "percentage":
partDone = StatsParser.convertProgress(xpp.getText());
break;
}
}
}
@ -111,14 +116,15 @@ public class FileListParser { @@ -111,14 +116,15 @@ public class FileListParser {
* @return The priority as enum type, i.e. Priority.Off
*/
private static Priority convertPriority(String priority) {
if (priority.equals("20")) {
return Priority.Off;
} else if (priority.equals("30")) {
return Priority.Low;
} else if (priority.equals("50")) {
return Priority.High;
} else {
return Priority.Normal;
switch (priority) {
case "20":
return Priority.Off;
case "30":
return Priority.Low;
case "50":
return Priority.High;
default:
return Priority.Normal;
}
}

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

@ -112,34 +112,48 @@ public class StatsParser { @@ -112,34 +112,48 @@ public class StatsParser {
// Probably encountered a torrent property, i.e. '<status>Stopped</status>'
next = xpp.next();
if (next == XmlPullParser.TEXT) {
if (name.equals("name")) {
tname = xpp.getText().trim();
//} else if (name.equals("info_hash")) {
//hash = xpp.getText().trim();
} else if (name.equals("status")) {
status = convertStatus(xpp.getText());
} else if (name.equals("bytes_downloaded")) {
down = convertSize(xpp.getText());
} else if (name.equals("bytes_uploaded")) {
up = convertSize(xpp.getText());
} else if (name.equals("total_bytes_to_download")) {
total = convertSize(xpp.getText());
} else if (name.equals("download_rate")) {
downRate = convertRate(xpp.getText());
} else if (name.equals("upload_rate")) {
upRate = convertRate(xpp.getText());
} else if (name.equals("seeders")) {
seeders = Integer.parseInt(xpp.getText());
} else if (name.equals("seeders_total")) {
seedersTotal = Integer.parseInt(xpp.getText());
} else if (name.equals("leechers")) {
leechers = Integer.parseInt(xpp.getText());
} else if (name.equals("leechers_total")) {
leechersTotal = Integer.parseInt(xpp.getText());
} else if (name.equals("percentage")) {
progress = convertProgress(xpp.getText());
} else if (name.equals("num_files")) {
numFiles = Integer.parseInt(xpp.getText());
switch (name) {
case "name":
tname = xpp.getText().trim();
//} else if (name.equals("info_hash")) {
//hash = xpp.getText().trim();
break;
case "status":
status = convertStatus(xpp.getText());
break;
case "bytes_downloaded":
down = convertSize(xpp.getText());
break;
case "bytes_uploaded":
up = convertSize(xpp.getText());
break;
case "total_bytes_to_download":
total = convertSize(xpp.getText());
break;
case "download_rate":
downRate = convertRate(xpp.getText());
break;
case "upload_rate":
upRate = convertRate(xpp.getText());
break;
case "seeders":
seeders = Integer.parseInt(xpp.getText());
break;
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 { @@ -776,14 +776,15 @@ public class QBittorrentAdapter implements IDaemonAdapter {
return (long) number;
}
// Returns size in B-based long
if (parts[1].equals("TiB")) {
return (long) (number * 1024L * 1024L * 1024L * 1024L);
} else if (parts[1].equals("GiB")) {
return (long) (number * 1024L * 1024L * 1024L);
} else if (parts[1].equals("MiB")) {
return (long) (number * 1024L * 1024L);
} else if (parts[1].equals("KiB")) {
return (long) (number * 1024L);
switch (parts[1]) {
case "TiB":
return (long) (number * 1024L * 1024L * 1024L * 1024L);
case "GiB":
return (long) (number * 1024L * 1024L * 1024L);
case "MiB":
return (long) (number * 1024L * 1024L);
case "KiB":
return (long) (number * 1024L);
}
return (long) number;
}
@ -811,12 +812,13 @@ public class QBittorrentAdapter implements IDaemonAdapter { @@ -811,12 +812,13 @@ public class QBittorrentAdapter implements IDaemonAdapter {
return -1;
}
// Returns size in B-based int
if (parts[1].equals("GiB/s")) {
return (int) (number * 1024 * 1024 * 1024);
} else if (parts[1].equals("MiB/s")) {
return (int) (number * 1024 * 1024);
} else if (parts[1].equals("KiB/s")) {
return (int) (number * 1024);
switch (parts[1]) {
case "GiB/s":
return (int) (number * 1024 * 1024 * 1024);
case "MiB/s":
return (int) (number * 1024 * 1024);
case "KiB/s":
return (int) (number * 1024);
}
return (int) (Double.parseDouble(normalizeNumber(parts[0])));
}
@ -833,28 +835,30 @@ public class QBittorrentAdapter implements IDaemonAdapter { @@ -833,28 +835,30 @@ public class QBittorrentAdapter implements IDaemonAdapter {
private TorrentStatus parseStatus(String state) {
// Status is given as a descriptive string
if (state.equals("error")) {
return TorrentStatus.Error;
} else if (state.equals("downloading") || state.equals("metaDL")) {
return TorrentStatus.Downloading;
} else if (state.equals("uploading")) {
return TorrentStatus.Seeding;
} else if (state.equals("pausedDL")) {
return TorrentStatus.Paused;
} else if (state.equals("pausedUP")) {
return TorrentStatus.Paused;
} else if (state.equals("stalledUP")) {
return TorrentStatus.Seeding;
} else if (state.equals("stalledDL")) {
return TorrentStatus.Downloading;
} else if (state.equals("checkingUP")) {
return TorrentStatus.Checking;
} else if (state.equals("checkingDL")) {
return TorrentStatus.Checking;
} else if (state.equals("queuedDL")) {
return TorrentStatus.Queued;
} else if (state.equals("queuedUP")) {
return TorrentStatus.Queued;
switch (state) {
case "error":
return TorrentStatus.Error;
case "downloading":
case "metaDL":
return TorrentStatus.Downloading;
case "uploading":
return TorrentStatus.Seeding;
case "pausedDL":
return TorrentStatus.Paused;
case "pausedUP":
return TorrentStatus.Paused;
case "stalledUP":
return TorrentStatus.Seeding;
case "stalledDL":
return TorrentStatus.Downloading;
case "checkingUP":
return TorrentStatus.Checking;
case "checkingDL":
return TorrentStatus.Checking;
case "queuedDL":
return TorrentStatus.Queued;
case "queuedUP":
return TorrentStatus.Queued;
}
return TorrentStatus.Unknown;
}

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

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

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

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

Loading…
Cancel
Save