From e63caf8efd153634994323e665c57bb084f08807 Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Tue, 21 Feb 2012 17:53:11 +0100 Subject: [PATCH] Adding proper altenative (turtle)mode support. Adding basic server stats (not displayed yet; issue 131). --- .../drawable-hdpi/icon_turtle_title_off.png | Bin 0 -> 799 bytes .../org/transdroid/gui/TorrentsFragment.java | 33 ++++++++++++- .../daemon/Bitflu/BitfluAdapter.java | 4 ++ lib/src/org/transdroid/daemon/Daemon.java | 4 ++ .../org/transdroid/daemon/DaemonMethod.java | 3 +- .../Transmission/TransmissionAdapter.java | 11 ++++- .../transdroid/daemon/task/GetStatsTask.java | 30 ++++++++++++ .../task/GetStatsTaskSuccessResult.java | 46 ++++++++++++++++++ 8 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 android/res/drawable-hdpi/icon_turtle_title_off.png create mode 100644 lib/src/org/transdroid/daemon/task/GetStatsTask.java create mode 100644 lib/src/org/transdroid/daemon/task/GetStatsTaskSuccessResult.java diff --git a/android/res/drawable-hdpi/icon_turtle_title_off.png b/android/res/drawable-hdpi/icon_turtle_title_off.png new file mode 100644 index 0000000000000000000000000000000000000000..32fc109ecec61500bda07998c23c8339944abac5 GIT binary patch literal 799 zcmV+)1K|9LP)Px#24YJ`L;(K){{a7>y{D4^000SaNLh0L01FcU01FcV0GgZ_0008ANkl z&ui0Q7{{NyOGEOu&5xy9X$hNcpk%f^^)PmrU}c0scMvuZ=1B#?n}33c-o=9;>Mq{I zo3NXQLJy*K3|4dpH$k!qrLZKhET+xt!4>wyA5B|jU>^7$Lc)^|@AJIh=b;E8aFzqX z8IKS!1PlQ~zz{G594zQT6Qok9hwXN|fDl3q!!$CP%-w3Wx;rdDs^4WHo6SC=Y5LO6 z&dxc}3kwVN z<>lp6(EKMA=wm?;q{YR>4N(+Nft{2niezbN>4UE8ry!HbVmIqgX7ylq9L$J?ToNV$9CYUKki~W@ctJ2wYbz%aTkcFMd$zxr=aw^&ux=dD&tH%&8ej~^Ty!1newix4^w0O0%naT}p& zU>L^k+h!C+S+OkZif!A}AeZqZSUeslnx@%2&({pYxE9!E^7;G&+qN&7rkS@aYhXi< z#bUmqD2D60&Aq)nD;kY{9$`dgpdD z8okMJoB{xG$8n1L`}=)O9*pruv)Q~302Ba30Khwh?|+ii>-9*fRPwsdnx?fjH#bMS zojx5W!IF9o?&&!#nx%yAHnQ&nZ0YktLFa!(% dL%{z7@H?`{2zt?(O?UtR002ovPDHLkV1k|4a#;WX literal 0 HcmV?d00001 diff --git a/android/src/org/transdroid/gui/TorrentsFragment.java b/android/src/org/transdroid/gui/TorrentsFragment.java index 4afa19c0..d8d42887 100644 --- a/android/src/org/transdroid/gui/TorrentsFragment.java +++ b/android/src/org/transdroid/gui/TorrentsFragment.java @@ -39,6 +39,8 @@ import org.transdroid.daemon.task.AddByUrlTask; import org.transdroid.daemon.task.DaemonTask; import org.transdroid.daemon.task.DaemonTaskFailureResult; import org.transdroid.daemon.task.DaemonTaskSuccessResult; +import org.transdroid.daemon.task.GetStatsTask; +import org.transdroid.daemon.task.GetStatsTaskSuccessResult; import org.transdroid.daemon.task.PauseAllTask; import org.transdroid.daemon.task.PauseTask; import org.transdroid.daemon.task.RemoveTask; @@ -554,7 +556,7 @@ public class TorrentsFragment extends Fragment implements IDaemonCallback, OnTou miSearch.setIcon(R.drawable.icon_search_title); miSearch.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT); MenuItem miAltMode = menu.add(0, MENU_ALTMODE_ID, 0, R.string.menu_altmode); - miAltMode.setIcon(R.drawable.icon_turtle_title); + miAltMode.setIcon(inAlternativeMode? R.drawable.icon_turtle_title: R.drawable.icon_turtle_title_off); miAltMode.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); miAltMode.setVisible(daemon != null && Daemon.supportsSetAlternativeMode(daemon.getType())); @@ -1381,6 +1383,9 @@ public class TorrentsFragment extends Fragment implements IDaemonCallback, OnTou public void updateTorrentList() { if (daemon != null) { queue.enqueue(RetrieveTask.create(daemon)); + if (Daemon.supportsStats(daemon.getType())) { + queue.enqueue(GetStatsTask.create(daemon)); + } } } @@ -1519,6 +1524,7 @@ public class TorrentsFragment extends Fragment implements IDaemonCallback, OnTou } inAlternativeMode = !inAlternativeMode; + updateAlternativeModeIcon(); queue.enqueue(SetAlternativeModeTask.create(daemon, inAlternativeMode)); } @@ -1665,6 +1671,21 @@ public class TorrentsFragment extends Fragment implements IDaemonCallback, OnTou } break; + case GetStats: + + // Only bother if we still are still looking at the same daemon since the task was queued + if (result.getTask().getAdapterType() == daemon.getType()) { + + GetStatsTaskSuccessResult stats = (GetStatsTaskSuccessResult) result; + if (Daemon.supportsSetAlternativeMode(daemon.getType())) { + // Update the alternative/tutle mode indicator + inAlternativeMode = stats.isAlternativeModeEnabled(); + updateAlternativeModeIcon(); + } + + } + break; + case Remove: // Show 'removed' message @@ -1730,10 +1751,20 @@ public class TorrentsFragment extends Fragment implements IDaemonCallback, OnTou Toast.makeText(getActivity(), getString(R.string.torrent_locationset, ((SetDownloadLocationTask)result.getTask()).getNewLocation()), Toast.LENGTH_SHORT).show(); break; + case SetAlternativeMode: + + // Updated the mode: now update the server stats to reflect the real new status (since the action might have been unsuccessful for some reason) + queue.enqueue(GetStatsTask.create(daemon)); + break; } } + private void updateAlternativeModeIcon() { + // By invalidation the options menu it gets redrawn and the turtle icon gets updated + getSupportActivity().invalidateOptionsMenu(); + } + /** * Shows the proper text when the torrent list is empty */ diff --git a/lib/src/org/transdroid/daemon/Bitflu/BitfluAdapter.java b/lib/src/org/transdroid/daemon/Bitflu/BitfluAdapter.java index be7d4e18..97c43f54 100644 --- a/lib/src/org/transdroid/daemon/Bitflu/BitfluAdapter.java +++ b/lib/src/org/transdroid/daemon/Bitflu/BitfluAdapter.java @@ -45,6 +45,8 @@ import org.transdroid.daemon.task.DaemonTaskResult; import org.transdroid.daemon.task.DaemonTaskSuccessResult; import org.transdroid.daemon.task.GetFileListTask; import org.transdroid.daemon.task.GetFileListTaskSuccessResult; +import org.transdroid.daemon.task.GetStatsTask; +import org.transdroid.daemon.task.GetStatsTaskSuccessResult; import org.transdroid.daemon.task.RemoveTask; import org.transdroid.daemon.task.RetrieveTask; import org.transdroid.daemon.task.RetrieveTaskSuccessResult; @@ -94,6 +96,8 @@ public class BitfluAdapter implements IDaemonAdapter { // Request all torrents from server JSONObject result = makeBitfluRequest(RPC_TORRENT_LIST); return new RetrieveTaskSuccessResult((RetrieveTask) task, parseJsonRetrieveTorrents(result.getJSONArray(JSON_ROOT))); + case GetStats: + return new GetStatsTaskSuccessResult((GetStatsTask) task, false, -1); case Pause: makeBitfluRequest(RPC_PAUSE_TORRENT + task.getTargetTorrent().getUniqueID()); return new DaemonTaskSuccessResult(task); diff --git a/lib/src/org/transdroid/daemon/Daemon.java b/lib/src/org/transdroid/daemon/Daemon.java index b5d6a732..35db8770 100644 --- a/lib/src/org/transdroid/daemon/Daemon.java +++ b/lib/src/org/transdroid/daemon/Daemon.java @@ -179,6 +179,10 @@ public enum Daemon { return 8080; } + public static boolean supportsStats(Daemon type) { + return type == Transmission || type == Bitflu; + } + public static boolean supportsAvailability(Daemon type) { return type == uTorrent || type == BitTorrent || type == DLinkRouterBT || type == Transmission || type == Vuze || type == BuffaloNas; } diff --git a/lib/src/org/transdroid/daemon/DaemonMethod.java b/lib/src/org/transdroid/daemon/DaemonMethod.java index 9d3e6d47..12375d56 100644 --- a/lib/src/org/transdroid/daemon/DaemonMethod.java +++ b/lib/src/org/transdroid/daemon/DaemonMethod.java @@ -42,7 +42,8 @@ public enum DaemonMethod { SetDownloadLocation (17), GetTorrentDetails (18), SetTrackers (19), - SetAlternativeMode (20); + SetAlternativeMode (20), + GetStats (21); private int code; private static final Map lookup = new HashMap(); diff --git a/lib/src/org/transdroid/daemon/Transmission/TransmissionAdapter.java b/lib/src/org/transdroid/daemon/Transmission/TransmissionAdapter.java index 9f43c136..174487a5 100644 --- a/lib/src/org/transdroid/daemon/Transmission/TransmissionAdapter.java +++ b/lib/src/org/transdroid/daemon/Transmission/TransmissionAdapter.java @@ -56,6 +56,8 @@ import org.transdroid.daemon.task.DaemonTaskResult; import org.transdroid.daemon.task.DaemonTaskSuccessResult; import org.transdroid.daemon.task.GetFileListTask; import org.transdroid.daemon.task.GetFileListTaskSuccessResult; +import org.transdroid.daemon.task.GetStatsTask; +import org.transdroid.daemon.task.GetStatsTaskSuccessResult; import org.transdroid.daemon.task.GetTorrentDetailsTask; import org.transdroid.daemon.task.GetTorrentDetailsTaskSuccessResult; import org.transdroid.daemon.task.PauseTask; @@ -145,10 +147,17 @@ public class TransmissionAdapter implements IDaemonAdapter { fields.put(field); } request.put("fields", fields); - + JSONObject result = makeRequest(buildRequestObject("torrent-get", request)); return new RetrieveTaskSuccessResult((RetrieveTask) task, parseJsonRetrieveTorrents(result.getJSONObject("arguments"))); + case GetStats: + + // Request the current server statistics + JSONObject stats = makeRequest(buildRequestObject("session-get", new JSONObject())).getJSONObject("arguments"); + return new GetStatsTaskSuccessResult((GetStatsTask) task, stats.getBoolean("alt-speed-enabled"), + stats.getLong("download-dir-free-space")); + case GetTorrentDetails: // Request fine details of a specific torrent diff --git a/lib/src/org/transdroid/daemon/task/GetStatsTask.java b/lib/src/org/transdroid/daemon/task/GetStatsTask.java new file mode 100644 index 00000000..02beb751 --- /dev/null +++ b/lib/src/org/transdroid/daemon/task/GetStatsTask.java @@ -0,0 +1,30 @@ +/* + * This file is part of Transdroid + * + * Transdroid is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Transdroid is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Transdroid. If not, see . + * + */ + package org.transdroid.daemon.task; + +import org.transdroid.daemon.DaemonMethod; +import org.transdroid.daemon.IDaemonAdapter; + +public class GetStatsTask extends DaemonTask { + protected GetStatsTask(IDaemonAdapter adapter) { + super(adapter, DaemonMethod.GetStats, null, null); + } + public static GetStatsTask create(IDaemonAdapter adapter) { + return new GetStatsTask(adapter); + } +} diff --git a/lib/src/org/transdroid/daemon/task/GetStatsTaskSuccessResult.java b/lib/src/org/transdroid/daemon/task/GetStatsTaskSuccessResult.java new file mode 100644 index 00000000..db8de58c --- /dev/null +++ b/lib/src/org/transdroid/daemon/task/GetStatsTaskSuccessResult.java @@ -0,0 +1,46 @@ +/* + * This file is part of Transdroid + * + * Transdroid is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Transdroid is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Transdroid. If not, see . + * + */ + package org.transdroid.daemon.task; + + +/** + * The result of a successfully executed RetrieveTask on the daemon. + * + * @author erickok + * + */ +public class GetStatsTaskSuccessResult extends DaemonTaskSuccessResult { + + private final boolean alternativeModeEnabled; + private final long downloadDirFreeSpaceBytes; + + public GetStatsTaskSuccessResult(GetStatsTask executedTask, boolean alternativeModeEnabled, long downloadDirFreeSpaceBytes) { + super(executedTask); + this.alternativeModeEnabled = alternativeModeEnabled; + this.downloadDirFreeSpaceBytes = downloadDirFreeSpaceBytes; + } + + public boolean isAlternativeModeEnabled() { + return alternativeModeEnabled; + } + + public long getDownloadDirFreeSpaceBytes() { + return downloadDirFreeSpaceBytes; + } + +}