Browse Source

Add sequential download and first+last piece prioratization download

modes, implemented for qbittorrent
pull/515/head
Phillip Dykman 5 years ago
parent
commit
cd99cbb159
  1. 26
      app/src/main/java/org/transdroid/core/gui/DetailsActivity.java
  2. 27
      app/src/main/java/org/transdroid/core/gui/DetailsFragment.java
  3. 4
      app/src/main/java/org/transdroid/core/gui/TorrentTasksExecutor.java
  4. 26
      app/src/main/java/org/transdroid/core/gui/TorrentsActivity.java
  5. 8
      app/src/main/java/org/transdroid/daemon/Daemon.java
  6. 4
      app/src/main/java/org/transdroid/daemon/DaemonMethod.java
  7. 23
      app/src/main/java/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java
  8. 23
      app/src/main/java/org/transdroid/daemon/Torrent.java
  9. 31
      app/src/main/java/org/transdroid/daemon/task/ToggleFirstLastPieceDownloadTask.java
  10. 31
      app/src/main/java/org/transdroid/daemon/task/ToggleSequentialDownloadTask.java
  11. 31
      app/src/main/res/menu/fragment_details.xml
  12. 4
      app/src/main/res/values/strings.xml

26
app/src/main/java/org/transdroid/core/gui/DetailsActivity.java

@ -56,6 +56,8 @@ import org.transdroid.daemon.TorrentFile; @@ -56,6 +56,8 @@ import org.transdroid.daemon.TorrentFile;
import org.transdroid.daemon.task.DaemonTaskFailureResult;
import org.transdroid.daemon.task.DaemonTaskResult;
import org.transdroid.daemon.task.DaemonTaskSuccessResult;
import org.transdroid.daemon.task.ToggleSequentialDownloadTask;
import org.transdroid.daemon.task.ToggleFirstLastPieceDownloadTask;
import org.transdroid.daemon.task.ForceRecheckTask;
import org.transdroid.daemon.task.GetFileListTask;
import org.transdroid.daemon.task.GetFileListTaskSuccessResult;
@ -278,6 +280,30 @@ public class DetailsActivity extends AppCompatActivity implements TorrentTasksEx @@ -278,6 +280,30 @@ public class DetailsActivity extends AppCompatActivity implements TorrentTasksEx
}
}
@Background
@Override
public void toggleSequentialDownload(Torrent torrent, boolean sequentialState) {
torrent.mimicSequentialDownload(sequentialState);
DaemonTaskResult result = ToggleSequentialDownloadTask.create(currentConnection, torrent).execute(log);
if (result instanceof DaemonTaskSuccessResult) {
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.result_togglesequential));
} else {
onCommunicationError((DaemonTaskFailureResult) result, false);
}
}
@Background
@Override
public void toggleFirstLastPieceDownload(Torrent torrent, boolean firstLastPieceState) {
torrent.mimicFirstLastPieceDownload(firstLastPieceState);
DaemonTaskResult result = ToggleFirstLastPieceDownloadTask.create(currentConnection, torrent).execute(log);
if (result instanceof DaemonTaskSuccessResult) {
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.action_toggle_firstlastpiece));
} else {
onCommunicationError((DaemonTaskFailureResult) result, false);
}
}
@Background
@Override
public void forceRecheckTorrent(Torrent torrent) {

27
app/src/main/java/org/transdroid/core/gui/DetailsFragment.java

@ -301,6 +301,12 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen @@ -301,6 +301,12 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen
case R.id.action_stop:
stopTorrent();
return true;
case R.id.action_toggle_sequential:
toggleSequentialDownload(menuItem);
return true;
case R.id.action_toggle_firstlastpiece:
toggleFirstLastPieceDownload(menuItem);
return true;
case R.id.action_forcerecheck:
setForceRecheck();
return true;
@ -359,6 +365,15 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen @@ -359,6 +365,15 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen
detailsMenu.getMenu().findItem(R.id.action_setlabel).setVisible(setLabel);
boolean forceRecheck = Daemon.supportsForceRecheck(torrent.getDaemon());
detailsMenu.getMenu().findItem(R.id.action_forcerecheck).setVisible(forceRecheck);
boolean sequentialdl = Daemon.supportsSequentialDownload(torrent.getDaemon());
MenuItem seqMenuItem = detailsMenu.getMenu().findItem(R.id.action_toggle_sequential);
seqMenuItem.setVisible(sequentialdl);
seqMenuItem.setChecked(torrent.isSequentiallyDownloading());
boolean firstlastpiecedl = Daemon.supportsFirstLastPiece(torrent.getDaemon());
MenuItem flpMenuItem = detailsMenu.getMenu().findItem(R.id.action_toggle_firstlastpiece);
flpMenuItem.setVisible(firstlastpiecedl);
flpMenuItem.setChecked(torrent.isDownloadingFirstLastPieceFirst());
detailsMenu.getMenu().findItem(R.id.action_download_mode).setVisible(firstlastpiecedl || sequentialdl);
boolean setTrackers = Daemon.supportsSetTrackers(torrent.getDaemon());
detailsMenu.getMenu().findItem(R.id.action_updatetrackers).setVisible(setTrackers);
boolean setLocation = Daemon.supportsSetDownloadLocation(torrent.getDaemon());
@ -421,6 +436,18 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen @@ -421,6 +436,18 @@ public class DetailsFragment extends Fragment implements OnTrackersUpdatedListen
}
}
@OptionsItem(R.id.action_toggle_sequential)
protected void toggleSequentialDownload(MenuItem menuItem) {
if (getTasksExecutor() != null)
getTasksExecutor().toggleSequentialDownload(torrent, !menuItem.isChecked());
}
@OptionsItem(R.id.action_toggle_firstlastpiece)
protected void toggleFirstLastPieceDownload(MenuItem menuItem) {
if (getTasksExecutor() != null)
getTasksExecutor().toggleFirstLastPieceDownload(torrent, !menuItem.isChecked());
}
@OptionsItem(R.id.action_forcerecheck)
protected void setForceRecheck() {
if (getTasksExecutor() != null)

4
app/src/main/java/org/transdroid/core/gui/TorrentTasksExecutor.java

@ -40,6 +40,10 @@ public interface TorrentTasksExecutor { @@ -40,6 +40,10 @@ public interface TorrentTasksExecutor {
void removeTorrent(Torrent torrent, boolean withData);
void toggleSequentialDownload(Torrent torrent, boolean sequentialState);
void toggleFirstLastPieceDownload(Torrent torrent, boolean sequentialState);
void forceRecheckTorrent(Torrent torrent);
void updateLabel(Torrent torrent, String newLabel);

26
app/src/main/java/org/transdroid/core/gui/TorrentsActivity.java

@ -103,6 +103,8 @@ import org.transdroid.daemon.task.AddByUrlTask; @@ -103,6 +103,8 @@ import org.transdroid.daemon.task.AddByUrlTask;
import org.transdroid.daemon.task.DaemonTaskFailureResult;
import org.transdroid.daemon.task.DaemonTaskResult;
import org.transdroid.daemon.task.DaemonTaskSuccessResult;
import org.transdroid.daemon.task.ToggleSequentialDownloadTask;
import org.transdroid.daemon.task.ToggleFirstLastPieceDownloadTask;
import org.transdroid.daemon.task.ForceRecheckTask;
import org.transdroid.daemon.task.GetFileListTask;
import org.transdroid.daemon.task.GetFileListTaskSuccessResult;
@ -1225,6 +1227,30 @@ public class TorrentsActivity extends AppCompatActivity implements TorrentTasksE @@ -1225,6 +1227,30 @@ public class TorrentsActivity extends AppCompatActivity implements TorrentTasksE
}
}
@Background
@Override
public void toggleSequentialDownload(Torrent torrent, boolean sequentialState) {
torrent.mimicSequentialDownload(sequentialState);
DaemonTaskResult result = ToggleSequentialDownloadTask.create(currentConnection, torrent).execute(log);
if (result instanceof DaemonTaskSuccessResult) {
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.result_togglesequential));
} else {
onCommunicationError((DaemonTaskFailureResult) result, false);
}
}
@Background
@Override
public void toggleFirstLastPieceDownload(Torrent torrent, boolean firstLastPieceState) {
torrent.mimicFirstLastPieceDownload(firstLastPieceState);
DaemonTaskResult result = ToggleFirstLastPieceDownloadTask.create(currentConnection, torrent).execute(log);
if (result instanceof DaemonTaskSuccessResult) {
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.action_toggle_firstlastpiece));
} else {
onCommunicationError((DaemonTaskFailureResult) result, false);
}
}
@Background
@Override
public void forceRecheckTorrent(Torrent torrent) {

8
app/src/main/java/org/transdroid/daemon/Daemon.java

@ -412,6 +412,14 @@ public enum Daemon { @@ -412,6 +412,14 @@ public enum Daemon {
|| type == Transmission || type == Dummy || type == qBittorrent;
}
public static boolean supportsSequentialDownload(Daemon type) {
return type == qBittorrent;
}
public static boolean supportsFirstLastPiece(Daemon type) {
return type == qBittorrent;
}
public static boolean supportsExtraPassword(Daemon type) {
return type == Deluge || type == Aria2;
}

4
app/src/main/java/org/transdroid/daemon/DaemonMethod.java

@ -44,7 +44,9 @@ public enum DaemonMethod { @@ -44,7 +44,9 @@ public enum DaemonMethod {
SetTrackers (19),
SetAlternativeMode (20),
GetStats (21),
ForceRecheck (22);
ForceRecheck (22),
ToggleSequentialDownload(23),
ToggleFirstLastPieceDownload(24);
private int code;
private static final Map<Integer,DaemonMethod> lookup = new HashMap<>();

23
app/src/main/java/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java

@ -292,6 +292,18 @@ public class QbittorrentAdapter implements IDaemonAdapter { @@ -292,6 +292,18 @@ public class QbittorrentAdapter implements IDaemonAdapter {
makeRequest(log, "/command/recheck", new BasicNameValuePair("hash", task.getTargetTorrent().getUniqueID()));
return new DaemonTaskSuccessResult(task);
case ToggleSequentialDownload:
// Toggle sequential download mode on a torrent
makeRequest(log, "/command/toggleSequentialDownload", new BasicNameValuePair("hashes", task.getTargetTorrent().getUniqueID()));
return new DaemonTaskSuccessResult(task);
case ToggleFirstLastPieceDownload:
// Set policy for downloading first and last piece first on a torrent
makeRequest(log, "/command/toggleFirstLastPiecePrio", new BasicNameValuePair("hashes", task.getTargetTorrent().getUniqueID()));
return new DaemonTaskSuccessResult(task);
case SetLabel:
SetLabelTask labelTask = (SetLabelTask) task;
@ -492,6 +504,8 @@ public class QbittorrentAdapter implements IDaemonAdapter { @@ -492,6 +504,8 @@ public class QbittorrentAdapter implements IDaemonAdapter {
long uploaded;
int dlspeed;
int upspeed;
boolean dlseq = false;
boolean dlflp = false;
Date addedOn = null;
Date completionOn = null;
String label = null;
@ -507,6 +521,8 @@ public class QbittorrentAdapter implements IDaemonAdapter { @@ -507,6 +521,8 @@ public class QbittorrentAdapter implements IDaemonAdapter {
ratio = tor.getDouble("ratio");
dlspeed = tor.getInt("dlspeed");
upspeed = tor.getInt("upspeed");
dlseq = tor.getBoolean("seq_dl");
dlflp = tor.getBoolean("f_l_piece_prio");
if (tor.has("uploaded")) {
uploaded = tor.getLong("uploaded");
} else {
@ -535,7 +551,7 @@ public class QbittorrentAdapter implements IDaemonAdapter { @@ -535,7 +551,7 @@ public class QbittorrentAdapter implements IDaemonAdapter {
eta = (long) (size - (size * progress)) / dlspeed;
// Add the parsed torrent to the list
// @formatter:off
torrents.add(new Torrent(
Torrent torrent = new Torrent(
(long) i,
tor.getString("hash"),
tor.getString("name"),
@ -557,7 +573,10 @@ public class QbittorrentAdapter implements IDaemonAdapter { @@ -557,7 +573,10 @@ public class QbittorrentAdapter implements IDaemonAdapter {
addedOn,
completionOn,
null,
settings.getType()));
settings.getType());
torrent.mimicSequentialDownload(dlseq);
torrent.mimicFirstLastPieceDownload(dlflp);
torrents.add(torrent);
// @formatter:on
}

23
app/src/main/java/org/transdroid/daemon/Torrent.java

@ -49,6 +49,8 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl @@ -49,6 +49,8 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl
final private float partDone;
final private float available;
private String label;
private boolean sequentialDownload;
private boolean firstLastPieceDownload;
final private Date dateAdded;
final private Date dateDone;
@ -76,6 +78,8 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl @@ -76,6 +78,8 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl
this.partDone = in.readFloat();
this.available = in.readFloat();
this.label = in.readString();
this.sequentialDownload = in.readByte() != 0;
this.firstLastPieceDownload = in.readByte() != 0;
long lDateAdded = in.readLong();
this.dateAdded = (lDateAdded == -1) ? null : new Date(lDateAdded);
@ -109,6 +113,8 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl @@ -109,6 +113,8 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl
this.partDone = partDone;
this.available = available;
this.label = label;
this.sequentialDownload = false;
this.firstLastPieceDownload = false;
this.dateAdded = dateAdded;
if (realDateDone != null) {
@ -197,6 +203,13 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl @@ -197,6 +203,13 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl
return label;
}
public boolean isSequentiallyDownloading() {
return sequentialDownload;
}
public boolean isDownloadingFirstLastPieceFirst() {
return firstLastPieceDownload;
}
public Date getDateAdded() {
return dateAdded;
}
@ -342,6 +355,14 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl @@ -342,6 +355,14 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl
label = newLabel;
}
public void mimicSequentialDownload(boolean sequentialDownload) {
this.sequentialDownload = sequentialDownload;
}
public void mimicFirstLastPieceDownload(boolean firstLastPieceDownload) {
this.firstLastPieceDownload = firstLastPieceDownload;
}
public void mimicCheckingStatus() {
statusCode = TorrentStatus.Checking;
}
@ -399,6 +420,8 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl @@ -399,6 +420,8 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl
dest.writeFloat(partDone);
dest.writeFloat(available);
dest.writeString(label);
dest.writeByte((byte) (sequentialDownload ? 1 : 0));
dest.writeByte((byte) (firstLastPieceDownload ? 1 : 0));
dest.writeLong((dateAdded == null) ? -1 : dateAdded.getTime());
dest.writeLong((dateDone == null) ? -1 : dateDone.getTime());

31
app/src/main/java/org/transdroid/daemon/task/ToggleFirstLastPieceDownloadTask.java

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
/*
* This file is part of Transdroid <http://www.transdroid.org>
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
package org.transdroid.daemon.task;
import org.transdroid.daemon.DaemonMethod;
import org.transdroid.daemon.IDaemonAdapter;
import org.transdroid.daemon.Torrent;
public class ToggleFirstLastPieceDownloadTask extends DaemonTask {
protected ToggleFirstLastPieceDownloadTask(IDaemonAdapter adapter, Torrent targetTorrent) {
super(adapter, DaemonMethod.ToggleFirstLastPieceDownload, targetTorrent, null);
}
public static ToggleFirstLastPieceDownloadTask create(IDaemonAdapter adapter, Torrent targetTorrent) {
return new ToggleFirstLastPieceDownloadTask(adapter, targetTorrent);
}
}

31
app/src/main/java/org/transdroid/daemon/task/ToggleSequentialDownloadTask.java

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
/*
* This file is part of Transdroid <http://www.transdroid.org>
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
package org.transdroid.daemon.task;
import org.transdroid.daemon.DaemonMethod;
import org.transdroid.daemon.IDaemonAdapter;
import org.transdroid.daemon.Torrent;
public class ToggleSequentialDownloadTask extends DaemonTask {
protected ToggleSequentialDownloadTask(IDaemonAdapter adapter, Torrent targetTorrent) {
super(adapter, DaemonMethod.ToggleSequentialDownload, targetTorrent, null);
}
public static ToggleSequentialDownloadTask create(IDaemonAdapter adapter, Torrent targetTorrent) {
return new ToggleSequentialDownloadTask(adapter, targetTorrent);
}
}

31
app/src/main/res/menu/fragment_details.xml

@ -47,19 +47,36 @@ @@ -47,19 +47,36 @@
<item
android:id="@+id/action_start_direct"
android:icon="@drawable/ic_action_start"
android:orderInCategory="202"
android:orderInCategory="203"
android:title="@string/action_start"
app:showAsAction="always" />
<item
android:id="@+id/action_stop"
android:icon="@drawable/ic_action_stop"
android:orderInCategory="203"
android:orderInCategory="204"
android:title="@string/action_stop"
app:showAsAction="always" />
<item
android:id="@+id/action_download_mode"
android:icon="@drawable/ic_action_download"
android:orderInCategory="205"
android:title="@string/action_download_mode"
app:showAsAction="always" >
<menu>
<item
android:id="@+id/action_toggle_sequential"
android:title="@string/action_toggle_sequential"
android:checkable="true" />
<item
android:id="@+id/action_toggle_firstlastpiece"
android:title="@string/action_toggle_firstlastpiece"
android:checkable="true" />
</menu>
</item>
<item
android:id="@+id/action_remove"
android:icon="@drawable/ic_action_remove"
android:orderInCategory="204"
android:orderInCategory="206"
android:title="@string/action_remove"
app:showAsAction="always">
<menu>
@ -74,25 +91,25 @@ @@ -74,25 +91,25 @@
<item
android:id="@+id/action_setlabel"
android:icon="@drawable/ic_action_labels"
android:orderInCategory="205"
android:orderInCategory="207"
android:title="@string/action_setlabel"
app:showAsAction="always" />
<item
android:id="@+id/action_forcerecheck"
android:icon="@drawable/ic_action_force_recheck"
android:orderInCategory="206"
android:orderInCategory="208"
android:title="@string/action_forcerecheck"
app:showAsAction="always" />
<item
android:id="@+id/action_updatetrackers"
android:icon="@drawable/ic_action_trackers"
android:orderInCategory="207"
android:orderInCategory="209"
android:title="@string/action_updatetrackers"
app:showAsAction="always" />
<item
android:id="@+id/action_changelocation"
android:icon="@drawable/ic_action_save"
android:orderInCategory="208"
android:orderInCategory="210"
android:title="@string/action_changelocation"
app:showAsAction="always" />

4
app/src/main/res/values/strings.xml

@ -56,6 +56,9 @@ @@ -56,6 +56,9 @@
<string name="action_remove_default">Remove torrent</string>
<string name="action_remove_withdata">Remove and delete data</string>
<string name="action_setlabel">Set label</string>
<string name="action_download_mode">Set download mode</string>
<string name="action_toggle_sequential">Download sequentially</string>
<string name="action_toggle_firstlastpiece">Download first and last pieces first</string>
<string name="action_updatetrackers">Update trackers</string>
<string name="action_changelocation">Change storage location</string>
<string name="action_forcerecheck">Force data recheck</string>
@ -175,6 +178,7 @@ @@ -175,6 +178,7 @@
<string name="result_trackersupdated">Trackers updated</string>
<string name="result_labelset">Label set to \'%1$s\'</string>
<string name="result_labelremoved">Label removed</string>
<string name="result_togglesequential">Toggled sequential download mode</string>
<string name="result_recheckedstarted">Checking %1$s data</string>
<string name="result_locationset">Torrent moved to \'%1$s\'</string>
<string name="result_priotitiesset">File priorities updated</string>

Loading…
Cancel
Save