2828
2929public class TorrentStreamer extends ReactContextBaseJavaModule implements TorrentListener {
3030
31- private TorrentStream mTorrentStream ;
31+ private TorrentStream mTorrentStream = null ;
3232 private ReactApplicationContext context ;
3333
3434 public TorrentStreamer (ReactApplicationContext reactContext ) {
3535 super (reactContext );
3636 this .context = reactContext ;
37+ }
38+
39+ @ Override
40+ public String getName () {
41+ return "TorrentStreamerAndroid" ;
42+ }
3743
44+ @ ReactMethod
45+ public void stop () {
46+ if (mTorrentStream != null && mTorrentStream .isStreaming ()) {
47+ mTorrentStream .stopStream ();
48+ }
49+ }
50+
51+ @ ReactMethod
52+ public void setup (String location , Boolean removeAfterStop ) {
3853 TorrentOptions torrentOptions = new TorrentOptions .Builder ()
39- .saveLocation (Environment . getExternalStoragePublicDirectory ( Environment . DIRECTORY_DOWNLOADS ) )
54+ .saveLocation (location )
4055 .maxConnections (200 )
4156 .autoDownload (true )
42- .removeFilesAfterStop (true )
57+ .removeFilesAfterStop (removeAfterStop )
4358 .build ();
4459
4560 mTorrentStream = TorrentStream .init (torrentOptions );
4661 mTorrentStream .addListener (this );
4762 }
4863
49- @ Override
50- public String getName ( ) {
51- return "TorrentStreamerAndroid" ;
64+ @ ReactMethod
65+ public void setup ( String location ) {
66+ this . setup ( location , true ) ;
5267 }
5368
54- @ ReactMethod
55- public void stop () {
56- if (mTorrentStream .isStreaming ()){
57- mTorrentStream .stopStream ();
69+ private void setup () {
70+ if (mTorrentStream == null ) {
71+ this .setup ("" + Environment .getExternalStoragePublicDirectory (Environment .DIRECTORY_DOWNLOADS ), true );
5872 }
5973 }
6074
6175 @ ReactMethod
6276 public void start (String magnetUrl ) {
63-
64-
77+ this .setup ();
6578
6679 mTorrentStream .startStream (magnetUrl );
6780 }
6881
69- private void sendEvent (String eventName ,
70- @ Nullable WritableMap params ) {
82+ private void sendEvent (String eventName , @ Nullable WritableMap params ) {
7183 this .context
7284 .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class )
7385 .emit (eventName , params );
7486 }
7587
7688 @ Override
7789 public void onStreamPrepared (Torrent torrent ) {
78-
7990// Log.d("data", "OnStreamPrepared");
8091
81-
8292 WritableMap params = Arguments .createMap ();
8393 params .putString ("data" , "OnStreamPrepared" );
8494 sendEvent ("progress" , params );
@@ -89,11 +99,10 @@ public void onStreamPrepared(Torrent torrent) {
8999 public void onStreamStarted (Torrent torrent ) {
90100// Log.d("data", "onStreamStarted");
91101
92-
93102 WritableMap params = Arguments .createMap ();
94103 params .putString ("data" , "onStreamStarted" );
95104 sendEvent ("progress" , params );
96- }
105+ }
97106
98107 @ Override
99108 public void onStreamError (Torrent torrent , Exception e ) {
@@ -106,9 +115,9 @@ public void onStreamError(Torrent torrent, Exception e) {
106115 public void onStreamReady (Torrent torrent ) {
107116// Log.d("url", torrent.getVideoFile().toString());
108117
109-
110118 WritableMap params = Arguments .createMap ();
111119 params .putString ("url" , torrent .getVideoFile ().toString ());
120+ params .putString ("filename" , torrent .getTorrentHandle ().name ());
112121 sendEvent ("ready" , params );
113122 }
114123
@@ -120,10 +129,10 @@ public void onStreamProgress(Torrent torrent, StreamStatus status) {
120129// Log.d("seeds", "" + status.seeds);
121130
122131 WritableMap params = Arguments .createMap ();
123- params .putString ("buffer" , "" + status .bufferProgress );
124- params .putString ("downloadSpeed" , "" + status .downloadSpeed );
125- params .putString ("progress" , "" + status .progress );
126- params .putString ("seeds" , "" + status .seeds );
132+ params .putString ("buffer" , "" + status .bufferProgress );
133+ params .putString ("downloadSpeed" , "" + status .downloadSpeed );
134+ params .putString ("progress" , "" + status .progress );
135+ params .putString ("seeds" , "" + status .seeds );
127136 sendEvent ("status" , params );
128137 }
129138
@@ -139,6 +148,7 @@ public void open(String url, String type) {
139148 Intent intent = new Intent (Intent .ACTION_VIEW );
140149 intent .setDataAndType (Uri .parse (url ), type );
141150 intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
151+
142152 //Check that an app exists to receive the intent
143153 if (intent .resolveActivity (this .context .getPackageManager ()) != null ) {
144154 this .context .startActivity (intent );
0 commit comments