@@ -23,6 +23,7 @@ public interface TaskWorkerThreadListener {
2323 private UnpackTask unpackTask ;
2424
2525 private boolean active = true ;
26+ private Object taskInProgress = new Object ();
2627
2728 public TaskWorkerThread (final Context context ) {
2829 this .context = context ;
@@ -32,12 +33,20 @@ public TaskWorkerThread(final Context context) {
3233 public void run () {
3334 while (active ) {
3435 if (!list .isEmpty ()) {
35- try {
36- performTask ();
37- }
38- catch (Exception e ) {
39- Timber .e (e , "ERROR in run()" );
40- }
36+
37+ final String stringUrl = list .get (0 );
38+ Timber .i ("performTask(), stringUrl: %s" , stringUrl );
39+
40+ final Thread thread = new Thread (new Runnable () {
41+ @ Override
42+ public void run () {
43+ performTask (stringUrl );
44+ wakeUpTaskInProgress ();
45+ }
46+ });
47+ thread .start ();
48+
49+ sleepTaskInProgress ();
4150 }
4251 else {
4352 Timber .i ("run() sleep" );
@@ -46,6 +55,25 @@ public void run() {
4655 }
4756 }
4857
58+ private void sleepTaskInProgress () {
59+ Timber .i ("sleepTaskInProgress() begin" );
60+ synchronized (taskInProgress ) {
61+ try {
62+ taskInProgress .wait (10000 );
63+ } catch (InterruptedException e ) {
64+ Timber .e (e , "EROR in sleepTaskInProgress() interrupted" );
65+ }
66+ }
67+ Timber .i ("sleepTaskInProgress() done" );
68+ }
69+
70+ private void wakeUpTaskInProgress () {
71+ Timber .i ("wakeUpTaskInProgress()" );
72+ synchronized (taskInProgress ) {
73+ taskInProgress .notify ();
74+ }
75+ }
76+
4977 public void cancel () {
5078 active = false ;
5179 }
@@ -67,35 +95,37 @@ private synchronized void sleep() {
6795 try {
6896 Timber .i ("sleep() begin ..." );
6997 wait ();
98+ Timber .i ("sleep() done ..." );
7099 } catch (InterruptedException e ) {
71- Timber .i ( " sleep() end ... " );
100+ Timber .e ( e , "ERROR in sleep()" );
72101 }
73102 }
74103
75- private void performTask () {
104+ private void performTask (final String stringUrl ) {
76105 Timber .i ("performTask()" );
77-
78- if (unpackTask == null ) {
79- unpackTask = new UnpackTask ();
80- if (!unpackTask .unpack (context .getApplicationContext ())) {
81- unpackTask = null ;
106+ try {
107+ if (unpackTask == null ) {
108+ unpackTask = new UnpackTask ();
109+ if (!unpackTask .unpack (context .getApplicationContext ())) {
110+ unpackTask = null ;
111+ }
82112 }
83- }
84113
85- final String stringUrl = list .get (0 );
86- Timber .i ("performTask(), stringUrl: %s" , stringUrl );
87-
88- final YoutubeDlWorker youtubeDlWorker = new YoutubeDlWorker ();
89- if (youtubeDlWorker .process (context .getApplicationContext (), stringUrl )) {
90- Timber .i ("performTask(), success" );
91- }
92- list .remove (0 );
93- if (list .isEmpty ()) {
94- Timber .i ("performTask(), list.isEmpty()" );
95- if (context instanceof TaskWorkerThreadListener ) {
96- Timber .i ("performTask(), context instanceof TaskWorkerThreadListener" );
97- ((TaskWorkerThreadListener )context ).onCompleteAllItems ();
114+ final YoutubeDlWorker youtubeDlWorker = new YoutubeDlWorker ();
115+ if (youtubeDlWorker .process (context .getApplicationContext (), stringUrl )) {
116+ Timber .i ("performTask(), success" );
98117 }
118+ list .remove (stringUrl );
119+ if (list .isEmpty ()) {
120+ Timber .i ("performTask(), list.isEmpty()" );
121+ if (context instanceof TaskWorkerThreadListener ) {
122+ Timber .i ("performTask(), context instanceof TaskWorkerThreadListener" );
123+ ((TaskWorkerThreadListener ) context ).onCompleteAllItems ();
124+ }
125+ }
126+ }
127+ catch (Exception e ) {
128+ Timber .e (e , "ERROR in performTask(%s)" , stringUrl );
99129 }
100130 }
101131}
0 commit comments