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 00000000..32fc109e Binary files /dev/null and b/android/res/drawable-hdpi/icon_turtle_title_off.png differ 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; + } + +}