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..631bac8 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' @@ -36,7 +36,7 @@ group='com.github.se_bastiaan' android { compileSdkVersion 23 - buildToolsVersion "23.0.2" + buildToolsVersion "23.0.3" defaultConfig { minSdkVersion 15 @@ -58,7 +58,7 @@ android { } ext { - libtorrentVersion = '1.1.0.24' + libtorrentVersion = '1.1.0.31' } 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..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,26 +178,26 @@ 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 pieceLength = torrentHandle.getTorrentInfo().getPieceLength(); + int pieceCount = lastPieceIndexLocal - firstPieceIndexLocal + 1; + int pieceLength = torrentHandle.getTorrentInfo().pieceLength(); int activePieceCount; if (pieceLength > 0) { activePieceCount = (int) (prepareSize / pieceLength); @@ -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; } @@ -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; @@ -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 97a2254..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 @@ -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; @@ -49,14 +49,14 @@ 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; 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; @@ -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).handle().getInfoHash()); + currentTorrent = new Torrent(th, listener, torrentOptions.prepareSize); + torrentSession.addListener(currentTorrent); + } + }; + private TorrentStream(TorrentOptions options) { torrentOptions = options; initialise(); @@ -90,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; @@ -111,7 +126,7 @@ public void run() { torrentSession.addListener(dhtStatsAlertListener); - dht = new DHT(torrentSession); + dht = new Dht(torrentSession); dht.start(); initialising = false; @@ -277,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); @@ -452,23 +465,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/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(); } } 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; } } 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. * 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 aa38c3e..658f691 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 @@ -45,12 +45,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 streamUrl = "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); @@ -90,41 +105,27 @@ protected void onResume() { } } - View.OnClickListener onClickListener = new View.OnClickListener() { - @Override - public void onClick(View v) { - progressBar.setProgress(0); - if(torrentStream.isStreaming()) { - torrentStream.stopStream(); - button.setText("Start stream"); - return; - } - torrentStream.startStream(streamUrl); - 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"); @@ -134,13 +135,27 @@ 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"); } + + View.OnClickListener onClickListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + progressBar.setProgress(0); + if(torrentStream.isStreaming()) { + torrentStream.stopStream(); + button.setText("Start stream"); + return; + } + torrentStream.startStream(streamUrl); + button.setText("Stop stream"); + } + }; }