44
55import android .os .Bundle ;
66import android .os .Handler ;
7+ import android .os .Looper ;
78import android .text .TextUtils ;
89
910import androidx .annotation .NonNull ;
1011
12+ import com .tencent .liteav .base .util .LiteavLog ;
1113import com .tencent .rtmp .TXPlayInfoParams ;
1214import com .tencent .rtmp .downloader .ITXVodDownloadListener ;
1315import com .tencent .rtmp .downloader .ITXVodFilePreloadListener ;
2628import com .tencent .vod .flutter .messages .FtxMessages .TXVodDownloadMediaMsg ;
2729import com .tencent .vod .flutter .tools .TXCommonUtil ;
2830
29- import io .flutter .embedding .engine .plugins .FlutterPlugin ;
30- import io .flutter .plugin .common .EventChannel ;
31-
3231import java .math .BigDecimal ;
3332import java .util .ArrayList ;
3433import java .util .List ;
3534import java .util .Map ;
3635import java .util .concurrent .ExecutorService ;
3736import java .util .concurrent .Executors ;
3837
38+ import io .flutter .embedding .engine .plugins .FlutterPlugin ;
39+
3940/**
4041 * Download management, pre-download, and offline download.
4142 *
4243 * 下载管理,预下载、离线下载
4344 */
44- public class FTXDownloadManager implements ITXVodDownloadListener , TXFlutterDownloadApi {
45+ public class FTXDownloadManager implements ITXVodDownloadListener , TXFlutterDownloadApi , FtxMessages . VoidResult {
4546
46- private FlutterPlugin .FlutterPluginBinding mFlutterPluginBinding ;
47- private final EventChannel mEventChannel ;
48- private final FTXPlayerEventSink mEventSink = new FTXPlayerEventSink ();
49- private final Handler mMainHandler ;
47+ private static final String TAG = "FTXDownloadManager" ;
5048
51- private ExecutorService mPreloadPool = Executors .newCachedThreadPool ();
49+ private final FlutterPlugin .FlutterPluginBinding mFlutterPluginBinding ;
50+ private boolean isInitDownloadListener = false ;
51+ private final ExecutorService mPreloadPool = Executors .newCachedThreadPool ();
52+ private final FtxMessages .TXDownloadFlutterAPI mDownloadFlutterApi ;
53+ private final Handler mMainHandler = new Handler (Looper .getMainLooper ());
5254
5355 /**
5456 * Video download management.
@@ -57,45 +59,36 @@ public class FTXDownloadManager implements ITXVodDownloadListener, TXFlutterDown
5759 */
5860 public FTXDownloadManager (FlutterPlugin .FlutterPluginBinding flutterPluginBinding ) {
5961 mFlutterPluginBinding = flutterPluginBinding ;
60- mMainHandler = new Handler (mFlutterPluginBinding .getApplicationContext ().getMainLooper ());
61-
62- TXFlutterDownloadApi .setup (mFlutterPluginBinding .getBinaryMessenger (), this );
63-
64- mEventChannel = new EventChannel (flutterPluginBinding .getBinaryMessenger (),
65- "cloud.tencent.com/txvodplayer/download/event" );
66- mEventChannel .setStreamHandler (new EventChannel .StreamHandler () {
67- @ Override
68- public void onListen (Object o , EventChannel .EventSink eventSink ) {
69- mEventSink .setEventSinkProxy (eventSink );
70- }
62+ TXFlutterDownloadApi .setUp (mFlutterPluginBinding .getBinaryMessenger (), this );
63+ mDownloadFlutterApi = new FtxMessages .TXDownloadFlutterAPI (flutterPluginBinding .getBinaryMessenger ());
64+ }
7165
72- @ Override
73- public void onCancel (Object o ) {
74- mEventSink .setEventSinkProxy (null );
75- }
76- });
77- TXVodDownloadManager .getInstance ().setListener (this );
66+ private void initDownloadListenerIfNeed () {
67+ if (!isInitDownloadListener ) {
68+ isInitDownloadListener = true ;
69+ TXVodDownloadManager .getInstance ().setListener (this );
70+ }
7871 }
7972
80- private void onStartEvent (long tmpTaskId , int taskId , String fileId , String url , Bundle params ) {
73+ private void onPreLoadStartEvent (long tmpTaskId , int taskId , String fileId , String url , Bundle params ) {
8174 Bundle bundle = new Bundle ();
8275 bundle .putLong ("tmpTaskId" , tmpTaskId );
8376 bundle .putInt ("taskId" , taskId );
8477 bundle .putString ("fileId" , fileId );
8578 bundle .putString ("url" , url );
8679 Map <String , Object > result = TXCommonUtil .getParams (FTXEvent .EVENT_PREDOWNLOAD_ON_START , bundle );
8780 result .put ("params" , TXCommonUtil .transToMap (params ));
88- sendSuccessEvent (result );
81+ onPreloadCallback (result );
8982 }
9083
91- private void onCompleteEvent (int taskId , String url ) {
84+ private void onPreLoadCompleteEvent (int taskId , String url ) {
9285 Bundle bundle = new Bundle ();
9386 bundle .putInt ("taskId" , taskId );
9487 bundle .putString ("url" , url );
95- sendSuccessEvent (TXCommonUtil .getParams (FTXEvent .EVENT_PREDOWNLOAD_ON_COMPLETE , bundle ));
88+ onPreloadCallback (TXCommonUtil .getParams (FTXEvent .EVENT_PREDOWNLOAD_ON_COMPLETE , bundle ));
9689 }
9790
98- private void onErrorEvent (long tmpTaskId , int taskId , String url , int code , String msg ) {
91+ private void onPreLoadErrorEvent (long tmpTaskId , int taskId , String url , int code , String msg ) {
9992 Bundle bundle = new Bundle ();
10093 if (tmpTaskId >= 0 ) {
10194 bundle .putLong ("tmpTaskId" , tmpTaskId );
@@ -104,21 +97,19 @@ private void onErrorEvent(long tmpTaskId, int taskId, String url, int code, Stri
10497 bundle .putInt ("code" , code );
10598 bundle .putString ("url" , url );
10699 bundle .putString ("msg" , msg );
107- sendSuccessEvent (TXCommonUtil .getParams (FTXEvent .EVENT_PREDOWNLOAD_ON_ERROR , bundle ));
100+ onPreloadCallback (TXCommonUtil .getParams (FTXEvent .EVENT_PREDOWNLOAD_ON_ERROR , bundle ));
108101 }
109102
110- private void sendSuccessEvent ( final Object event ) {
103+ private void onPreloadCallback ( Map < String , Object > eventArg ) {
111104 mMainHandler .post (new Runnable () {
112105 @ Override
113106 public void run () {
114- mEventSink . success ( event );
107+ mDownloadFlutterApi . onPreDownloadEvent ( eventArg , FTXDownloadManager . this );
115108 }
116109 });
117110 }
118111
119-
120112 public void destroy () {
121- mEventChannel .setStreamHandler (null );
122113 TXVodDownloadManager .getInstance ().setListener (null );
123114 }
124115
@@ -239,34 +230,42 @@ private int optQuality(Integer quality) {
239230 @ Override
240231 public void onDownloadStart (TXVodDownloadMediaInfo txVodDownloadMediaInfo ) {
241232 Bundle bundle = buildCommonDownloadBundle (txVodDownloadMediaInfo );
242- sendSuccessEvent (TXCommonUtil .getParams (FTXEvent .EVENT_DOWNLOAD_START , bundle ));
233+ onDownloadCallback (TXCommonUtil .getParams (FTXEvent .EVENT_DOWNLOAD_START , bundle ));
243234 }
244235
245-
246236 @ Override
247237 public void onDownloadProgress (TXVodDownloadMediaInfo txVodDownloadMediaInfo ) {
248238 Bundle bundle = buildCommonDownloadBundle (txVodDownloadMediaInfo );
249- sendSuccessEvent (TXCommonUtil .getParams (FTXEvent .EVENT_DOWNLOAD_PROGRESS , bundle ));
239+ onDownloadCallback (TXCommonUtil .getParams (FTXEvent .EVENT_DOWNLOAD_PROGRESS , bundle ));
250240 }
251241
252242 @ Override
253243 public void onDownloadStop (TXVodDownloadMediaInfo txVodDownloadMediaInfo ) {
254244 Bundle bundle = buildCommonDownloadBundle (txVodDownloadMediaInfo );
255- sendSuccessEvent (TXCommonUtil .getParams (FTXEvent .EVENT_DOWNLOAD_STOP , bundle ));
245+ onDownloadCallback (TXCommonUtil .getParams (FTXEvent .EVENT_DOWNLOAD_STOP , bundle ));
256246 }
257247
258248 @ Override
259249 public void onDownloadFinish (TXVodDownloadMediaInfo txVodDownloadMediaInfo ) {
260250 Bundle bundle = buildCommonDownloadBundle (txVodDownloadMediaInfo );
261- sendSuccessEvent (TXCommonUtil .getParams (FTXEvent .EVENT_DOWNLOAD_FINISH , bundle ));
251+ onDownloadCallback (TXCommonUtil .getParams (FTXEvent .EVENT_DOWNLOAD_FINISH , bundle ));
262252 }
263253
264254 @ Override
265255 public void onDownloadError (TXVodDownloadMediaInfo txVodDownloadMediaInfo , int i , String s ) {
266256 Bundle bundle = buildCommonDownloadBundle (txVodDownloadMediaInfo );
267257 bundle .putInt ("errorCode" , i );
268258 bundle .putString ("errorMsg" , s );
269- sendSuccessEvent (TXCommonUtil .getParams (FTXEvent .EVENT_DOWNLOAD_ERROR , bundle ));
259+ onDownloadCallback (TXCommonUtil .getParams (FTXEvent .EVENT_DOWNLOAD_ERROR , bundle ));
260+ }
261+
262+ private void onDownloadCallback (Map <String , Object > eventArg ) {
263+ mMainHandler .post (new Runnable () {
264+ @ Override
265+ public void run () {
266+ mDownloadFlutterApi .onDownloadEvent (eventArg , FTXDownloadManager .this );
267+ }
268+ });
270269 }
271270
272271 /**
@@ -291,20 +290,19 @@ public IntMsg startPreLoad(@NonNull PreLoadMsg msg) {
291290 new ITXVodPreloadListener () {
292291 @ Override
293292 public void onComplete (int taskID , String url ) {
294- onCompleteEvent (taskID , url );
293+ onPreLoadCompleteEvent (taskID , url );
295294 }
296295
297296 @ Override
298297 public void onError (int taskID , String url , int code , String msg ) {
299- onErrorEvent (-1 , taskID , url , code , msg );
298+ onPreLoadErrorEvent (-1 , taskID , url , code , msg );
300299 }
301300 });
302301 IntMsg res = new IntMsg ();
303302 res .setValue ((long ) retTaskID );
304303 return res ;
305304 }
306305
307- @ NonNull
308306 @ Override
309307 public void startPreLoadByParams (@ NonNull FtxMessages .PreLoadInfoMsg msg ) {
310308 mPreloadPool .execute (new Runnable () {
@@ -329,22 +327,22 @@ public void run() {
329327 @ Override
330328 public void onStart (int taskID , String fileId , String url , Bundle bundle ) {
331329 if (tmpTaskId >= 0 ) {
332- onStartEvent (tmpTaskId , taskID , fileId , url , bundle );
330+ onPreLoadStartEvent (tmpTaskId , taskID , fileId , url , bundle );
333331 }
334332 }
335333
336334 @ Override
337335 public void onComplete (int taskID , String url ) {
338- onCompleteEvent (taskID , url );
336+ onPreLoadCompleteEvent (taskID , url );
339337 }
340338
341339 @ Override
342340 public void onError (int taskID , String url , int code , String msg ) {
343- onErrorEvent (tmpTaskId , taskID , url , code , msg );
341+ onPreLoadErrorEvent (tmpTaskId , taskID , url , code , msg );
344342 }
345343 });
346344 if (isUrlPreload && tmpTaskId >= 0 ) {
347- onStartEvent (tmpTaskId , retTaskID , msg .getFileId (), msg .getPlayUrl (), new Bundle ());
345+ onPreLoadStartEvent (tmpTaskId , retTaskID , msg .getFileId (), msg .getPlayUrl (), new Bundle ());
348346 }
349347 }
350348 });
@@ -361,6 +359,7 @@ public void stopPreLoad(@NonNull IntMsg msg) {
361359
362360 @ Override
363361 public void startDownload (@ NonNull TXVodDownloadMediaMsg msg ) {
362+ initDownloadListenerIfNeed ();
364363 Integer quality = null != msg .getQuality () ? msg .getQuality ().intValue () : 0 ;
365364 String videoUrl = msg .getUrl ();
366365 Integer appId = null != msg .getAppId () ? msg .getAppId ().intValue () : null ;
@@ -378,6 +377,7 @@ public void startDownload(@NonNull TXVodDownloadMediaMsg msg) {
378377
379378 @ Override
380379 public void resumeDownload (@ NonNull TXVodDownloadMediaMsg msg ) {
380+ initDownloadListenerIfNeed ();
381381 TXVodDownloadMediaInfo mediaInfo = getDownloadInfoFromMsg (msg );
382382 if (null != mediaInfo ) {
383383 TXVodDownloadDataSource dataSource = mediaInfo .getDataSource ();
@@ -391,6 +391,7 @@ public void resumeDownload(@NonNull TXVodDownloadMediaMsg msg) {
391391
392392 @ Override
393393 public void stopDownload (@ NonNull TXVodDownloadMediaMsg msg ) {
394+ initDownloadListenerIfNeed ();
394395 TXVodDownloadMediaInfo mediaInfo = getDownloadInfoFromMsg (msg );
395396 TXVodDownloadManager .getInstance ().stopDownload (mediaInfo );
396397 }
@@ -426,10 +427,20 @@ public BoolMsg deleteDownloadMediaInfo(@NonNull TXVodDownloadMediaMsg msg) {
426427 TXVodDownloadMediaInfo mediaInfo = getDownloadInfoFromMsg (msg );
427428 boolean deleteResult = false ;
428429 if (mediaInfo != null ) {
430+ TXVodDownloadManager .getInstance ().stopDownload (mediaInfo );
429431 deleteResult = TXVodDownloadManager .getInstance ().deleteDownloadMediaInfo (mediaInfo );
430432 }
431433 BoolMsg res = new BoolMsg ();
432434 res .setValue (deleteResult );
433435 return res ;
434436 }
437+
438+ @ Override
439+ public void success () {
440+ }
441+
442+ @ Override
443+ public void error (@ NonNull Throwable error ) {
444+ LiteavLog .e (TAG , "callback message error:" + error );
445+ }
435446}
0 commit comments