From 09f33eeef4b5c9c3c54b9f99c1a8a4cf24890362 Mon Sep 17 00:00:00 2001 From: Jack Wakefield Date: Sat, 16 Apr 2016 12:17:57 +0100 Subject: [PATCH 1/9] Fixes for 1.1.0.24 --- .../se_bastiaan/torrentstream/Torrent.java | 50 +++++++++---------- .../torrentstream/TorrentStream.java | 2 +- .../listeners/DHTStatsAlertListener.java | 2 +- .../listeners/TorrentAddedAlertListener.java | 4 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java index 5130695..0839964 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java @@ -74,7 +74,7 @@ public Torrent(TorrentHandle torrentHandle, TorrentListener listener, Long prepa this.prepareSize = prepareSize; - torrentHandle.setPriority(Priority.NORMAL.getSwig()); + torrentHandle.setPriority(Priority.NORMAL.swig()); if (selectedFileIndex == -1) setLargestFile(); @@ -91,9 +91,9 @@ private void resetPriorities() { Priority[] priorities = torrentHandle.getPiecePriorities(); for (int i = 0; i < priorities.length; i++) { if (i >= firstPieceIndex && i <= lastPieceIndex) { - torrentHandle.setPiecePriority(i, Priority.NORMAL); + torrentHandle.piecePriority(i, Priority.NORMAL); } else { - torrentHandle.setPiecePriority(i, Priority.IGNORE); + torrentHandle.piecePriority(i, Priority.IGNORE); } } } @@ -108,7 +108,7 @@ public TorrentHandle getTorrentHandle() { } public File getVideoFile() { - return new File(torrentHandle.getSavePath() + "/" + torrentHandle.getTorrentInfo().getFiles().getFilePath(selectedFileIndex)); + return new File(torrentHandle.getSavePath() + "/" + torrentHandle.getTorrentInfo().files().filePath(selectedFileIndex)); } /** @@ -149,13 +149,13 @@ public void setLargestFile() { */ public void setSelectedFileIndex(Integer selectedFileIndex) { TorrentInfo torrentInfo = torrentHandle.getTorrentInfo(); - FileStorage fileStorage = torrentInfo.getFiles(); + FileStorage fileStorage = torrentInfo.files(); if (selectedFileIndex == -1) { long highestFileSize = 0; int selectedFile = -1; - for (int i = 0; i < fileStorage.getNumFiles(); i++) { - long fileSize = fileStorage.getFileSize(i); + for (int i = 0; i < fileStorage.numFiles(); i++) { + long fileSize = fileStorage.fileSize(i); if (highestFileSize < fileSize) { highestFileSize = fileSize; torrentHandle.setFilePriority(selectedFile, Priority.IGNORE); @@ -167,7 +167,7 @@ public void setSelectedFileIndex(Integer selectedFileIndex) { } selectedFileIndex = selectedFile; } else { - for (int i = 0; i < fileStorage.getNumFiles(); i++) { + for (int i = 0; i < fileStorage.numFiles(); i++) { if (i == selectedFileIndex) { torrentHandle.setFilePriority(i, Priority.NORMAL); } else { @@ -225,10 +225,10 @@ public void setSelectedFileIndex(Integer selectedFileIndex) { * @return {@link String[]} */ public String[] getFileNames() { - FileStorage fileStorage = torrentHandle.getTorrentInfo().getFiles(); - String[] fileNames = new String[fileStorage.getNumFiles()]; - for (int i = 0; i < fileStorage.getNumFiles(); i++) { - fileNames[i] = fileStorage.getFileName(i); + FileStorage fileStorage = torrentHandle.getTorrentInfo().files(); + String[] fileNames = new String[fileStorage.numFiles()]; + for (int i = 0; i < fileStorage.numFiles(); i++) { + fileNames[i] = fileStorage.fileName(i); } return fileNames; } @@ -240,26 +240,26 @@ public String[] getFileNames() { public void startDownload() { if (state == State.STREAMING) return; state = State.STARTING; - torrentHandle.setPriority(Priority.NORMAL.getSwig()); + torrentHandle.setPriority(Priority.NORMAL.swig()); List indices = new ArrayList<>(); Priority[] priorities = torrentHandle.getPiecePriorities(); for (int i = 0; i < priorities.length; i++) { if (priorities[i] != Priority.IGNORE) { - torrentHandle.setPiecePriority(i, Priority.NORMAL); + torrentHandle.piecePriority(i, Priority.NORMAL); } } for (int i = 0; i < piecesToPrepare; i++) { indices.add(lastPieceIndex - i); - torrentHandle.setPiecePriority(lastPieceIndex - i, Priority.SEVEN); + torrentHandle.piecePriority(lastPieceIndex - i, Priority.SEVEN); torrentHandle.setPieceDeadline(lastPieceIndex - i, 1000); } for (int i = 0; i < piecesToPrepare; i++) { indices.add(firstPieceIndex + i); - torrentHandle.setPiecePriority(firstPieceIndex + i, Priority.SEVEN); + torrentHandle.piecePriority(firstPieceIndex + i, Priority.SEVEN); torrentHandle.setPieceDeadline(firstPieceIndex + i, 1000); } @@ -290,7 +290,7 @@ private void startSequentialMode() { torrentHandle.setSequentialDownload(true); } else { for (int i = firstPieceIndex + piecesToPrepare; i < firstPieceIndex + piecesToPrepare + 5; i++) { - torrentHandle.setPiecePriority(i, Priority.SEVEN); + torrentHandle.piecePriority(i, Priority.SEVEN); torrentHandle.setPieceDeadline(i, 1000); } } @@ -312,11 +312,11 @@ public State getState() { */ private void pieceFinished(PieceFinishedAlert alert) { if (state == State.STREAMING && hasPieces != null) { - hasPieces[alert.getPieceIndex() - firstPieceIndex] = true; + hasPieces[alert.pieceIndex() - firstPieceIndex] = true; - for (int i = alert.getPieceIndex() - firstPieceIndex; i < hasPieces.length; i++) { + for (int i = alert.pieceIndex() - firstPieceIndex; i < hasPieces.length; i++) { if (!hasPieces[i]) { - torrentHandle.setPiecePriority(i + firstPieceIndex, Priority.SEVEN); + torrentHandle.piecePriority(i + firstPieceIndex, Priority.SEVEN); torrentHandle.setPieceDeadline(i + firstPieceIndex, 1000); break; } @@ -325,13 +325,13 @@ private void pieceFinished(PieceFinishedAlert alert) { Iterator piecesIterator = preparePieces.iterator(); while (piecesIterator.hasNext()) { int index = piecesIterator.next(); - if (index == alert.getPieceIndex()) { + if (index == alert.pieceIndex()) { piecesIterator.remove(); } } if (hasPieces != null) { - hasPieces[alert.getPieceIndex() - firstPieceIndex] = true; + hasPieces[alert.pieceIndex() - firstPieceIndex] = true; } if (preparePieces.size() == 0) { @@ -373,14 +373,14 @@ private void sendStreamProgress() { @Override public int[] types() { return new int[]{ - AlertType.PIECE_FINISHED.getSwig(), - AlertType.BLOCK_FINISHED.getSwig() + AlertType.PIECE_FINISHED.swig(), + AlertType.BLOCK_FINISHED.swig() }; } @Override public void alert(Alert alert) { - switch (alert.getType()) { + switch (alert.type()) { case PIECE_FINISHED: pieceFinished((PieceFinishedAlert) alert); break; diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java index 2ea535f..97a2254 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java @@ -321,7 +321,7 @@ public void run() { return; } - Priority[] priorities = new Priority[torrentInfo.getNumPieces()]; + Priority[] priorities = new Priority[torrentInfo.numPieces()]; for (int i = 0; i < priorities.length; i++) { priorities[i] = Priority.IGNORE; } diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/DHTStatsAlertListener.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/DHTStatsAlertListener.java index e67b6ba..41ecfff 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/DHTStatsAlertListener.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/DHTStatsAlertListener.java @@ -28,7 +28,7 @@ public abstract class DHTStatsAlertListener implements AlertListener { @Override public int[] types() { - return new int[]{AlertType.DHT_STATS.getSwig()}; + return new int[]{AlertType.DHT_STATS.swig()}; } public void alert(Alert alert) { diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/TorrentAddedAlertListener.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/TorrentAddedAlertListener.java index 27c7d65..200751c 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/TorrentAddedAlertListener.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/TorrentAddedAlertListener.java @@ -24,12 +24,12 @@ public abstract class TorrentAddedAlertListener implements AlertListener { @Override public int[] types() { - return new int[]{AlertType.TORRENT_ADDED.getSwig()}; + return new int[]{AlertType.TORRENT_ADDED.swig()}; } @Override public void alert(Alert alert) { - switch (alert.getType()) { + switch (alert.type()) { case TORRENT_ADDED: torrentAdded((TorrentAddedAlert) alert); break; From 6acab47bdb53d94ed9ffade9bfb1aa23960d8828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastiaan?= Date: Sat, 16 Apr 2016 13:29:48 +0200 Subject: [PATCH 2/9] Fixes for 1.1.0.24 (#11) --- .../se_bastiaan/torrentstream/Torrent.java | 50 +++++++++---------- .../torrentstream/TorrentStream.java | 2 +- .../listeners/DHTStatsAlertListener.java | 2 +- .../listeners/TorrentAddedAlertListener.java | 4 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java index 5130695..0839964 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java @@ -74,7 +74,7 @@ public Torrent(TorrentHandle torrentHandle, TorrentListener listener, Long prepa this.prepareSize = prepareSize; - torrentHandle.setPriority(Priority.NORMAL.getSwig()); + torrentHandle.setPriority(Priority.NORMAL.swig()); if (selectedFileIndex == -1) setLargestFile(); @@ -91,9 +91,9 @@ private void resetPriorities() { Priority[] priorities = torrentHandle.getPiecePriorities(); for (int i = 0; i < priorities.length; i++) { if (i >= firstPieceIndex && i <= lastPieceIndex) { - torrentHandle.setPiecePriority(i, Priority.NORMAL); + torrentHandle.piecePriority(i, Priority.NORMAL); } else { - torrentHandle.setPiecePriority(i, Priority.IGNORE); + torrentHandle.piecePriority(i, Priority.IGNORE); } } } @@ -108,7 +108,7 @@ public TorrentHandle getTorrentHandle() { } public File getVideoFile() { - return new File(torrentHandle.getSavePath() + "/" + torrentHandle.getTorrentInfo().getFiles().getFilePath(selectedFileIndex)); + return new File(torrentHandle.getSavePath() + "/" + torrentHandle.getTorrentInfo().files().filePath(selectedFileIndex)); } /** @@ -149,13 +149,13 @@ public void setLargestFile() { */ public void setSelectedFileIndex(Integer selectedFileIndex) { TorrentInfo torrentInfo = torrentHandle.getTorrentInfo(); - FileStorage fileStorage = torrentInfo.getFiles(); + FileStorage fileStorage = torrentInfo.files(); if (selectedFileIndex == -1) { long highestFileSize = 0; int selectedFile = -1; - for (int i = 0; i < fileStorage.getNumFiles(); i++) { - long fileSize = fileStorage.getFileSize(i); + for (int i = 0; i < fileStorage.numFiles(); i++) { + long fileSize = fileStorage.fileSize(i); if (highestFileSize < fileSize) { highestFileSize = fileSize; torrentHandle.setFilePriority(selectedFile, Priority.IGNORE); @@ -167,7 +167,7 @@ public void setSelectedFileIndex(Integer selectedFileIndex) { } selectedFileIndex = selectedFile; } else { - for (int i = 0; i < fileStorage.getNumFiles(); i++) { + for (int i = 0; i < fileStorage.numFiles(); i++) { if (i == selectedFileIndex) { torrentHandle.setFilePriority(i, Priority.NORMAL); } else { @@ -225,10 +225,10 @@ public void setSelectedFileIndex(Integer selectedFileIndex) { * @return {@link String[]} */ public String[] getFileNames() { - FileStorage fileStorage = torrentHandle.getTorrentInfo().getFiles(); - String[] fileNames = new String[fileStorage.getNumFiles()]; - for (int i = 0; i < fileStorage.getNumFiles(); i++) { - fileNames[i] = fileStorage.getFileName(i); + FileStorage fileStorage = torrentHandle.getTorrentInfo().files(); + String[] fileNames = new String[fileStorage.numFiles()]; + for (int i = 0; i < fileStorage.numFiles(); i++) { + fileNames[i] = fileStorage.fileName(i); } return fileNames; } @@ -240,26 +240,26 @@ public String[] getFileNames() { public void startDownload() { if (state == State.STREAMING) return; state = State.STARTING; - torrentHandle.setPriority(Priority.NORMAL.getSwig()); + torrentHandle.setPriority(Priority.NORMAL.swig()); List indices = new ArrayList<>(); Priority[] priorities = torrentHandle.getPiecePriorities(); for (int i = 0; i < priorities.length; i++) { if (priorities[i] != Priority.IGNORE) { - torrentHandle.setPiecePriority(i, Priority.NORMAL); + torrentHandle.piecePriority(i, Priority.NORMAL); } } for (int i = 0; i < piecesToPrepare; i++) { indices.add(lastPieceIndex - i); - torrentHandle.setPiecePriority(lastPieceIndex - i, Priority.SEVEN); + torrentHandle.piecePriority(lastPieceIndex - i, Priority.SEVEN); torrentHandle.setPieceDeadline(lastPieceIndex - i, 1000); } for (int i = 0; i < piecesToPrepare; i++) { indices.add(firstPieceIndex + i); - torrentHandle.setPiecePriority(firstPieceIndex + i, Priority.SEVEN); + torrentHandle.piecePriority(firstPieceIndex + i, Priority.SEVEN); torrentHandle.setPieceDeadline(firstPieceIndex + i, 1000); } @@ -290,7 +290,7 @@ private void startSequentialMode() { torrentHandle.setSequentialDownload(true); } else { for (int i = firstPieceIndex + piecesToPrepare; i < firstPieceIndex + piecesToPrepare + 5; i++) { - torrentHandle.setPiecePriority(i, Priority.SEVEN); + torrentHandle.piecePriority(i, Priority.SEVEN); torrentHandle.setPieceDeadline(i, 1000); } } @@ -312,11 +312,11 @@ public State getState() { */ private void pieceFinished(PieceFinishedAlert alert) { if (state == State.STREAMING && hasPieces != null) { - hasPieces[alert.getPieceIndex() - firstPieceIndex] = true; + hasPieces[alert.pieceIndex() - firstPieceIndex] = true; - for (int i = alert.getPieceIndex() - firstPieceIndex; i < hasPieces.length; i++) { + for (int i = alert.pieceIndex() - firstPieceIndex; i < hasPieces.length; i++) { if (!hasPieces[i]) { - torrentHandle.setPiecePriority(i + firstPieceIndex, Priority.SEVEN); + torrentHandle.piecePriority(i + firstPieceIndex, Priority.SEVEN); torrentHandle.setPieceDeadline(i + firstPieceIndex, 1000); break; } @@ -325,13 +325,13 @@ private void pieceFinished(PieceFinishedAlert alert) { Iterator piecesIterator = preparePieces.iterator(); while (piecesIterator.hasNext()) { int index = piecesIterator.next(); - if (index == alert.getPieceIndex()) { + if (index == alert.pieceIndex()) { piecesIterator.remove(); } } if (hasPieces != null) { - hasPieces[alert.getPieceIndex() - firstPieceIndex] = true; + hasPieces[alert.pieceIndex() - firstPieceIndex] = true; } if (preparePieces.size() == 0) { @@ -373,14 +373,14 @@ private void sendStreamProgress() { @Override public int[] types() { return new int[]{ - AlertType.PIECE_FINISHED.getSwig(), - AlertType.BLOCK_FINISHED.getSwig() + AlertType.PIECE_FINISHED.swig(), + AlertType.BLOCK_FINISHED.swig() }; } @Override public void alert(Alert alert) { - switch (alert.getType()) { + switch (alert.type()) { case PIECE_FINISHED: pieceFinished((PieceFinishedAlert) alert); break; diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java index 2ea535f..97a2254 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java @@ -321,7 +321,7 @@ public void run() { return; } - Priority[] priorities = new Priority[torrentInfo.getNumPieces()]; + Priority[] priorities = new Priority[torrentInfo.numPieces()]; for (int i = 0; i < priorities.length; i++) { priorities[i] = Priority.IGNORE; } diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/DHTStatsAlertListener.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/DHTStatsAlertListener.java index e67b6ba..41ecfff 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/DHTStatsAlertListener.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/DHTStatsAlertListener.java @@ -28,7 +28,7 @@ public abstract class DHTStatsAlertListener implements AlertListener { @Override public int[] types() { - return new int[]{AlertType.DHT_STATS.getSwig()}; + return new int[]{AlertType.DHT_STATS.swig()}; } public void alert(Alert alert) { diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/TorrentAddedAlertListener.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/TorrentAddedAlertListener.java index 27c7d65..200751c 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/TorrentAddedAlertListener.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/TorrentAddedAlertListener.java @@ -24,12 +24,12 @@ public abstract class TorrentAddedAlertListener implements AlertListener { @Override public int[] types() { - return new int[]{AlertType.TORRENT_ADDED.getSwig()}; + return new int[]{AlertType.TORRENT_ADDED.swig()}; } @Override public void alert(Alert alert) { - switch (alert.getType()) { + switch (alert.type()) { case TORRENT_ADDED: torrentAdded((TorrentAddedAlert) alert); break; From 8e28adfed7785bde76cd0793d5fca0a5aa5358ba Mon Sep 17 00:00:00 2001 From: Mohamed Ezzat Date: Thu, 21 Apr 2016 13:49:40 +0200 Subject: [PATCH 3/9] Multiple quality improvements (#12) --- .../torrentstream/TorrentStream.java | 36 ++++++++-------- .../torrentstreamer/sample/MainActivity.java | 41 ++++++++++--------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java index 97a2254..df7e7ac 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java @@ -64,11 +64,28 @@ public class TorrentStream { private String currentTorrentUrl; private Integer dhtNodes = 0; - private List listeners = new ArrayList<>(); + private final List listeners = new ArrayList<>(); private HandlerThread libTorrentThread, streamingThread; private Handler libTorrentHandler, streamingHandler; + private final DHTStatsAlertListener dhtStatsAlertListener = new DHTStatsAlertListener() { + @Override + public void stats(int totalDhtNodes) { + dhtNodes = totalDhtNodes; + } + }; + + private final TorrentAddedAlertListener torrentAddedAlertListener = new TorrentAddedAlertListener() { + @Override + public void torrentAdded(TorrentAddedAlert alert) { + InternalTorrentListener listener = new InternalTorrentListener(); + TorrentHandle th = torrentSession.findTorrent((alert).getHandle().getInfoHash()); + currentTorrent = new Torrent(th, listener, torrentOptions.prepareSize); + torrentSession.addListener(currentTorrent); + } + }; + private TorrentStream(TorrentOptions options) { torrentOptions = options; initialise(); @@ -452,23 +469,6 @@ public void removeListener(TorrentListener listener) { listeners.remove(listener); } - private DHTStatsAlertListener dhtStatsAlertListener = new DHTStatsAlertListener() { - @Override - public void stats(int totalDhtNodes) { - dhtNodes = totalDhtNodes; - } - }; - - private TorrentAddedAlertListener torrentAddedAlertListener = new TorrentAddedAlertListener() { - @Override - public void torrentAdded(TorrentAddedAlert alert) { - InternalTorrentListener listener = new InternalTorrentListener(); - TorrentHandle th = torrentSession.findTorrent((alert).getHandle().getInfoHash()); - currentTorrent = new Torrent(th, listener, torrentOptions.prepareSize); - torrentSession.addListener(currentTorrent); - } - }; - protected class InternalTorrentListener implements TorrentListener { public void onStreamStarted(final Torrent torrent) { diff --git a/sample/src/main/java/com/github/se_bastiaan/torrentstreamer/sample/MainActivity.java b/sample/src/main/java/com/github/se_bastiaan/torrentstreamer/sample/MainActivity.java index 6cf9d3a..78649e9 100644 --- a/sample/src/main/java/com/github/se_bastiaan/torrentstreamer/sample/MainActivity.java +++ b/sample/src/main/java/com/github/se_bastiaan/torrentstreamer/sample/MainActivity.java @@ -46,12 +46,27 @@ @SuppressLint("SetTextI18n") public class MainActivity extends AppCompatActivity implements TorrentListener { + private static final String TORRENT = "Torrent"; private Button button; private ProgressBar progressBar; private TorrentStream torrentStream; private String mStreamUrl = "magnet:?xt=urn:btih:88594aaacbde40ef3e2510c47374ec0aa396c08e&dn=bbb%5Fsunflower%5F1080p%5F30fps%5Fnormal.mp4&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80%2Fannounce&ws=http%3A%2F%2Fdistribution.bbb3d.renderfarming.net%2Fvideo%2Fmp4%2Fbbb%5Fsunflower%5F1080p%5F30fps%5Fnormal.mp4"; + View.OnClickListener mOnClickListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + progressBar.setProgress(0); + if(torrentStream.isStreaming()) { + torrentStream.stopStream(); + button.setText("Start stream"); + return; + } + torrentStream.startStream(mStreamUrl); + button.setText("Stop stream"); + } + }; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -91,41 +106,27 @@ protected void onResume() { } } - View.OnClickListener mOnClickListener = new View.OnClickListener() { - @Override - public void onClick(View v) { - progressBar.setProgress(0); - if(torrentStream.isStreaming()) { - torrentStream.stopStream(); - button.setText("Start stream"); - return; - } - torrentStream.startStream(mStreamUrl); - button.setText("Stop stream"); - } - }; - @Override public void onStreamPrepared(Torrent torrent) { - Log.d("Torrent", "OnStreamPrepared"); + Log.d(TORRENT, "OnStreamPrepared"); torrent.startDownload(); } @Override public void onStreamStarted(Torrent torrent) { - Log.d("Torrent", "onStreamStarted"); + Log.d(TORRENT, "onStreamStarted"); } @Override public void onStreamError(Torrent torrent, Exception e) { - Log.e("Torrent", "onStreamError", e); + Log.e(TORRENT, "onStreamError", e); button.setText("Start stream"); } @Override public void onStreamReady(Torrent torrent) { progressBar.setProgress(100); - Log.d("Torrent", "onStreamReady: " + torrent.getVideoFile()); + Log.d(TORRENT, "onStreamReady: " + torrent.getVideoFile()); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(torrent.getVideoFile().toString())); intent.setDataAndType(Uri.parse(torrent.getVideoFile().toString()), "video/mp4"); @@ -135,13 +136,13 @@ public void onStreamReady(Torrent torrent) { @Override public void onStreamProgress(Torrent torrent, StreamStatus status) { if(status.bufferProgress <= 100 && progressBar.getProgress() < 100 && progressBar.getProgress() != status.bufferProgress) { - Log.d("Torrent", "Progress: " + status.bufferProgress); + Log.d(TORRENT, "Progress: " + status.bufferProgress); progressBar.setProgress(status.bufferProgress); } } @Override public void onStreamStopped() { - Log.d("Torrent", "onStreamStopped"); + Log.d(TORRENT, "onStreamStopped"); } } From 6949bc661b3effd6a89124e3947fbc6d4e63433b Mon Sep 17 00:00:00 2001 From: Mohamed Ezzat Date: Sun, 1 May 2016 01:19:17 +0200 Subject: [PATCH 4/9] squid:S1118 - Utility classes should not have public constructors --- .../github/se_bastiaan/torrentstream/utils/FileUtils.java | 6 +++++- .../github/se_bastiaan/torrentstream/utils/ThreadUtils.java | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/utils/FileUtils.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/utils/FileUtils.java index f8809a6..7158239 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/utils/FileUtils.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/utils/FileUtils.java @@ -21,7 +21,11 @@ import java.io.File; -public class FileUtils { +public final class FileUtils { + + private FileUtils() throws InstantiationException { + throw new InstantiationException("This class is not created for instantiation"); + } /** * Delete every item below the File location diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/utils/ThreadUtils.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/utils/ThreadUtils.java index cb8940b..e4d6547 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/utils/ThreadUtils.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/utils/ThreadUtils.java @@ -22,8 +22,11 @@ import android.os.Handler; import android.os.Looper; -public class ThreadUtils { +public final class ThreadUtils { + private ThreadUtils() throws InstantiationException { + throw new InstantiationException("This class is not created for instantiation"); + } /** * Execute the given {@link Runnable} on the ui thread. * From a94f0321b79a5ca9c01f0b3cf2eb9452338a2dd7 Mon Sep 17 00:00:00 2001 From: Jack Wakefield Date: Mon, 16 May 2016 22:02:25 +0100 Subject: [PATCH 5/9] Update gradle --- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- library/build.gradle | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index a547cb5..169c8d2 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' + classpath 'com.android.tools.build:gradle:2.1.0' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 54a4bca..8940b06 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip diff --git a/library/build.gradle b/library/build.gradle index 97dbe3f..8abc3e7 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -20,7 +20,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' + classpath 'com.android.tools.build:gradle:2.1.0' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' From 250ced7cefa6d6cebf2d147c90e723ecc6492fef Mon Sep 17 00:00:00 2001 From: Jack Wakefield Date: Mon, 16 May 2016 22:02:36 +0100 Subject: [PATCH 6/9] Update the build tools --- library/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/build.gradle b/library/build.gradle index 8abc3e7..e27ca6f 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -36,7 +36,7 @@ group='com.github.se_bastiaan' android { compileSdkVersion 23 - buildToolsVersion "23.0.2" + buildToolsVersion "23.0.3" defaultConfig { minSdkVersion 15 From 001790dcbc1af27c5a9d2ec0004e635382c752c5 Mon Sep 17 00:00:00 2001 From: Jack Wakefield Date: Mon, 16 May 2016 22:03:23 +0100 Subject: [PATCH 7/9] Update to jlibtorrent 1.1.0.30 --- library/build.gradle | 2 +- .../com/github/se_bastiaan/torrentstream/Torrent.java | 4 ++-- .../se_bastiaan/torrentstream/TorrentStream.java | 8 ++++---- .../torrentstream/listeners/DHTStatsAlertListener.java | 10 +++++----- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/library/build.gradle b/library/build.gradle index e27ca6f..02231c6 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -58,7 +58,7 @@ android { } ext { - libtorrentVersion = '1.1.0.24' + libtorrentVersion = '1.1.0.30' } dependencies { diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java index 0839964..cada85e 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java @@ -197,7 +197,7 @@ public void setSelectedFileIndex(Integer selectedFileIndex) { lastPieceIndex = piecePriorities.length - 1; } int pieceCount = lastPieceIndex - firstPieceIndex + 1; - int pieceLength = torrentHandle.getTorrentInfo().getPieceLength(); + int pieceLength = torrentHandle.getTorrentInfo().pieceLength(); int activePieceCount; if (pieceLength > 0) { activePieceCount = (int) (prepareSize / pieceLength); @@ -271,7 +271,7 @@ public void startDownload() { TorrentInfo torrentInfo = torrentHandle.getTorrentInfo(); TorrentStatus status = torrentHandle.getStatus(); - double blockCount = indices.size() * torrentInfo.getPieceLength() / status.getBlockSize(); + double blockCount = indices.size() * torrentInfo.pieceLength() / status.getBlockSize(); progressStep = 100 / blockCount; diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java index 97a2254..da8cfe5 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java @@ -20,7 +20,7 @@ import android.os.Handler; import android.os.HandlerThread; -import com.frostwire.jlibtorrent.DHT; +import com.frostwire.jlibtorrent.Dht; import com.frostwire.jlibtorrent.Downloader; import com.frostwire.jlibtorrent.Priority; import com.frostwire.jlibtorrent.Session; @@ -56,7 +56,7 @@ public class TorrentStream { private CountDownLatch initialisingLatch; private Session torrentSession; - private DHT dht; + private Dht dht; private Boolean initialising = false, initialised = false, isStreaming = false, isCanceled = false; private TorrentOptions torrentOptions; @@ -111,7 +111,7 @@ public void run() { torrentSession.addListener(dhtStatsAlertListener); - dht = new DHT(torrentSession); + dht = new Dht(torrentSession); dht.start(); initialising = false; @@ -463,7 +463,7 @@ public void stats(int totalDhtNodes) { @Override public void torrentAdded(TorrentAddedAlert alert) { InternalTorrentListener listener = new InternalTorrentListener(); - TorrentHandle th = torrentSession.findTorrent((alert).getHandle().getInfoHash()); + TorrentHandle th = torrentSession.findTorrent((alert).handle().getInfoHash()); currentTorrent = new Torrent(th, listener, torrentOptions.prepareSize); torrentSession.addListener(currentTorrent); } diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/DHTStatsAlertListener.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/DHTStatsAlertListener.java index 41ecfff..35fceb9 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/DHTStatsAlertListener.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/DHTStatsAlertListener.java @@ -20,10 +20,11 @@ package com.github.se_bastiaan.torrentstream.listeners; import com.frostwire.jlibtorrent.AlertListener; -import com.frostwire.jlibtorrent.DHTRoutingBucket; +import com.frostwire.jlibtorrent.DhtRoutingBucket; import com.frostwire.jlibtorrent.alerts.Alert; import com.frostwire.jlibtorrent.alerts.AlertType; import com.frostwire.jlibtorrent.alerts.DhtStatsAlert; +import java.util.ArrayList; public abstract class DHTStatsAlertListener implements AlertListener { @Override @@ -41,12 +42,11 @@ public void alert(Alert alert) { public abstract void stats(int totalDhtNodes); private int countTotalDHTNodes(DhtStatsAlert alert) { - final DHTRoutingBucket[] routingTable = alert.routingTable(); + final ArrayList routingTable = alert.routingTable(); int totalNodes = 0; - if (routingTable != null && routingTable.length > 0) { - for (int i = 0; i < routingTable.length; i++) { - DHTRoutingBucket bucket = routingTable[i]; + if (routingTable != null) { + for (DhtRoutingBucket bucket : routingTable) { totalNodes += bucket.numNodes(); } } From 0ef8897fa19e3d460edeb12eb2201acec8e518d3 Mon Sep 17 00:00:00 2001 From: Mohamed Ezzat Date: Tue, 17 May 2016 07:05:20 +0200 Subject: [PATCH 8/9] Multiple quality improvements - squid:HiddenFieldCheck, squid:SwitchLastCaseIsDefaultCheck, squid:S2974 --- .../se_bastiaan/torrentstream/Torrent.java | 24 ++++++++------- .../torrentstream/TorrentOptions.java | 2 +- .../torrentstream/TorrentStream.java | 30 ++++++++----------- .../listeners/TorrentAddedAlertListener.java | 2 ++ 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java index cada85e..f0824a6 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/Torrent.java @@ -178,25 +178,25 @@ public void setSelectedFileIndex(Integer selectedFileIndex) { this.selectedFileIndex = selectedFileIndex; Priority[] piecePriorities = torrentHandle.getPiecePriorities(); - int firstPieceIndex = -1; - int lastPieceIndex = -1; + int firstPieceIndexLocal = -1; + int lastPieceIndexLocal = -1; for (int i = 0; i < piecePriorities.length; i++) { if (piecePriorities[i] != Priority.IGNORE) { - if (firstPieceIndex == -1) { - firstPieceIndex = i; + if (firstPieceIndexLocal == -1) { + firstPieceIndexLocal = i; } piecePriorities[i] = Priority.IGNORE; } else { - if (firstPieceIndex != -1 && lastPieceIndex == -1) { - lastPieceIndex = i - 1; + if (firstPieceIndexLocal != -1 && lastPieceIndexLocal == -1) { + lastPieceIndexLocal = i - 1; } } } - if (lastPieceIndex == -1) { - lastPieceIndex = piecePriorities.length - 1; + if (lastPieceIndexLocal == -1) { + lastPieceIndexLocal = piecePriorities.length - 1; } - int pieceCount = lastPieceIndex - firstPieceIndex + 1; + int pieceCount = lastPieceIndexLocal - firstPieceIndexLocal + 1; int pieceLength = torrentHandle.getTorrentInfo().pieceLength(); int activePieceCount; if (pieceLength > 0) { @@ -214,8 +214,8 @@ public void setSelectedFileIndex(Integer selectedFileIndex) { activePieceCount = pieceCount / 2; } - this.firstPieceIndex = firstPieceIndex; - this.lastPieceIndex = lastPieceIndex; + this.firstPieceIndex = firstPieceIndexLocal; + this.lastPieceIndex = lastPieceIndexLocal; piecesToPrepare = activePieceCount; } @@ -387,6 +387,8 @@ public void alert(Alert alert) { case BLOCK_FINISHED: blockFinished((BlockFinishedAlert) alert); break; + default: + break; } } diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentOptions.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentOptions.java index 337b093..2636871 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentOptions.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentOptions.java @@ -18,7 +18,7 @@ import java.io.File; -public class TorrentOptions { +public final class TorrentOptions { protected String saveLocation = "/"; protected String proxyHost; diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java index a8893c0..c95e56d 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/TorrentStream.java @@ -49,7 +49,7 @@ import java.util.List; import java.util.concurrent.CountDownLatch; -public class TorrentStream { +public final class TorrentStream { private static final String LIBTORRENT_THREAD_NAME = "TORRENTSTREAM_LIBTORRENT", STREAMING_THREAD_NAME = "TORRENTSTREAMER_STREAMING"; private static TorrentStream sThis; @@ -107,10 +107,8 @@ private void initialise() { if (libTorrentThread != null && torrentSession != null) { resumeSession(); } else { - if (initialising || initialised) { - if (libTorrentThread != null) { - libTorrentThread.interrupt(); - } + if ((initialising || initialised) && libTorrentThread != null) { + libTorrentThread.interrupt(); } initialising = true; @@ -294,19 +292,17 @@ public void run() { currentTorrentUrl = torrentUrl; File saveDirectory = new File(torrentOptions.saveLocation); - if (!saveDirectory.isDirectory()) { - if (!saveDirectory.mkdirs()) { - for (final TorrentListener listener : listeners) { - ThreadUtils.runOnUiThread(new Runnable() { - @Override - public void run() { - listener.onStreamError(null, new DirectoryModifyException()); - } - }); - } - isStreaming = false; - return; + if (!saveDirectory.isDirectory() && !saveDirectory.mkdirs()) { + for (final TorrentListener listener : listeners) { + ThreadUtils.runOnUiThread(new Runnable() { + @Override + public void run() { + listener.onStreamError(null, new DirectoryModifyException()); + } + }); } + isStreaming = false; + return; } torrentSession.removeListener(torrentAddedAlertListener); diff --git a/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/TorrentAddedAlertListener.java b/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/TorrentAddedAlertListener.java index 200751c..156bc3e 100644 --- a/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/TorrentAddedAlertListener.java +++ b/library/src/main/java/com/github/se_bastiaan/torrentstream/listeners/TorrentAddedAlertListener.java @@ -33,6 +33,8 @@ public void alert(Alert alert) { case TORRENT_ADDED: torrentAdded((TorrentAddedAlert) alert); break; + default: + break; } } From 399da02c55a3a59326ba8e7c6830aacd6d6c4d17 Mon Sep 17 00:00:00 2001 From: Jack Wakefield Date: Mon, 30 May 2016 16:00:30 +0100 Subject: [PATCH 9/9] Update to jlibtorrent 1.1.0.31 --- library/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/build.gradle b/library/build.gradle index 02231c6..631bac8 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -58,7 +58,7 @@ android { } ext { - libtorrentVersion = '1.1.0.30' + libtorrentVersion = '1.1.0.31' } dependencies {