6
6
import android .app .NotificationManager ;
7
7
import android .app .PendingIntent ;
8
8
import android .app .TaskStackBuilder ;
9
+ import android .content .BroadcastReceiver ;
9
10
import android .content .Context ;
10
11
import android .content .Intent ;
12
+ import android .content .IntentFilter ;
11
13
import android .content .pm .PackageManager ;
12
14
import android .os .Build ;
13
15
import android .os .Environment ;
16
+ import android .widget .Toast ;
14
17
15
18
import androidx .annotation .RequiresApi ;
16
19
import androidx .core .app .NotificationCompat ;
23
26
import com .chaquo .python .Python ;
24
27
25
28
import java .io .File ;
29
+ import java .sql .Time ;
30
+ import java .util .concurrent .TimeUnit ;
26
31
27
32
import io .reactivex .annotations .NonNull ;
28
33
@@ -32,6 +37,11 @@ public class DownloaderWorker extends Worker {
32
37
public static final String DIRECTORY = "DIRECTORY" ;
33
38
public static final String MAX_QUALITY = "MAX_QUALITY" ;
34
39
public static final String VIDEO_URL = "VIDEO_URL" ;
40
+ public static PyObject downloader ;
41
+ NotificationManagerCompat mNotificationManager ;
42
+ NotificationCompat .Builder notifBuilder ;
43
+ PendingIntent activityPendingIntent ;
44
+
35
45
public DownloaderWorker (@ NonNull Context context , @ NonNull WorkerParameters params ){
36
46
super (context , params );
37
47
}
@@ -43,9 +53,10 @@ public Result doWork(){
43
53
String videoUrl = getInputData ().getString (VIDEO_URL );
44
54
File downloadDirectory = new File (directory );
45
55
boolean success ;
56
+
46
57
makeNotificationChannel ("CHANNEL" , "Download status" , NotificationManager .IMPORTANCE_DEFAULT );
47
- NotificationManagerCompat mNotificationManager = NotificationManagerCompat .from (getApplicationContext ());
48
- NotificationCompat . Builder notifBuilder = new NotificationCompat .Builder (getApplicationContext (), "CHANNEL" );
58
+ mNotificationManager = NotificationManagerCompat .from (getApplicationContext ());
59
+ notifBuilder = new NotificationCompat .Builder (getApplicationContext (), "CHANNEL" );
49
60
50
61
51
62
Intent activityIntent = new Intent (getApplicationContext (), MainActivity .class );
@@ -55,7 +66,7 @@ public Result doWork(){
55
66
activityIntent .addFlags (Intent .FLAG_ACTIVITY_CLEAR_TOP );
56
67
TaskStackBuilder stackBuilder = TaskStackBuilder .create (getApplicationContext ());
57
68
stackBuilder .addNextIntentWithParentStack (activityIntent );
58
- PendingIntent activityPendingIntent = PendingIntent .getActivity (getApplicationContext (),0 ,activityIntent , 0 );
69
+ activityPendingIntent = PendingIntent .getActivity (getApplicationContext (),0 ,activityIntent , 0 );
59
70
60
71
if (!downloadDirectory .exists ()){
61
72
success = downloadDirectory .mkdirs ();
@@ -67,27 +78,60 @@ public Result doWork(){
67
78
68
79
Python py = Python .getInstance ();
69
80
PyObject pyf = py .getModule ("downloader" );
70
- PyObject downloader ;
71
81
72
82
73
- notifyDownloading (mNotificationManager , notifBuilder , getApplicationContext ().getResources ().getString (R .string .text_notif_downloading ), activityPendingIntent );
74
- downloader = pyf .callAttr ("download" , videoUrl , format , directory , maxQuality , Environment .getExternalStorageDirectory ().getPath ()+"/Android/data/com.acmo0.youtubedownloader" );
75
83
76
- if (downloader .toBoolean () == false ){
77
- notifyFail (mNotificationManager , notifBuilder , activityPendingIntent );
78
- return Result .failure ();
84
+ notifyDownloading (mNotificationManager , notifBuilder , getApplicationContext ().getResources ().getString (R .string .text_notif_downloading ), activityPendingIntent );
85
+ downloader = pyf .callAttr ("downloader" , videoUrl , format , directory , maxQuality , Environment .getExternalStorageDirectory ().getPath ()+"/Android/data/com.acmo0.youtubedownloader" );
86
+ PyObject download = downloader .callAttr ("download" );
87
+ System .out .println ("Started downloading" );
88
+ while (downloader .callAttr ("state" ).toBoolean ()!=false ){
89
+ notifBuilder .setContentText (downloader .get ("status" ).toString ());
90
+ mNotificationManager .notify (1 ,notifBuilder .build ());
91
+ if (downloader .get ("fail" ).toBoolean () == true ){
92
+ mNotificationManager .cancelAll ();
93
+ notifyFail (mNotificationManager , notifBuilder , activityPendingIntent );
94
+ return Result .failure ();
95
+ }
96
+ try {
97
+ TimeUnit .MILLISECONDS .sleep (1000 );
98
+ } catch (InterruptedException e ) {
99
+ e .printStackTrace ();
100
+ }
79
101
}
80
- else {
102
+ if ( downloader . get ( "fail" ). toBoolean () == false ) {
81
103
notifySuccess (mNotificationManager , notifBuilder , activityPendingIntent );
82
104
return Result .success ();
83
105
}
106
+ mNotificationManager .cancelAll ();
107
+ notifyFail (mNotificationManager , notifBuilder , activityPendingIntent );
108
+ return Result .failure ();
109
+ }
110
+
111
+ @ Override
112
+ public void onStopped (){
113
+ System .out .println ("STOOOOOOP" );
114
+ downloader .put ("stop" ,true );
115
+ try {
116
+ TimeUnit .MILLISECONDS .sleep (3000 );
117
+ } catch (InterruptedException e ) {
118
+ e .printStackTrace ();
119
+ }
120
+ mNotificationManager .cancelAll ();
121
+ notifyStop (mNotificationManager , notifBuilder , activityPendingIntent );
122
+ super .onStopped ();
84
123
}
85
124
void notifyDownloading (NotificationManagerCompat mNotificationManager , NotificationCompat .Builder notifBuilder , String text , PendingIntent mPendingIntent ){
125
+ Intent stopIntent = new Intent (getApplicationContext (), StopDownloadBroadcast .class );
126
+ PendingIntent stopPIntent = PendingIntent .getBroadcast (getApplicationContext (),1 ,stopIntent ,0 );
86
127
notifBuilder
128
+ .setContentIntent (mPendingIntent )
87
129
.setSmallIcon (R .mipmap .ic_launcher )
88
130
.setContentTitle ("Video Downloader" )
89
131
.setContentText (text )
90
132
.setProgress (0 , 0 , true )
133
+ .setOnlyAlertOnce (true )
134
+ .addAction (R .drawable .ic_background ,"Cancel" ,stopPIntent )
91
135
.setPriority (NotificationCompat .PRIORITY_DEFAULT );
92
136
mNotificationManager .notify (1 , notifBuilder .build ());
93
137
}
@@ -99,6 +143,10 @@ void notifySuccess(NotificationManagerCompat mNotificationManager, NotificationC
99
143
notifBuilder .setContentIntent (mPendingIntent ).setSmallIcon (R .mipmap .ic_launcher ).setContentText (getApplicationContext ().getResources ().getString (R .string .text_notif_download_finished )).setProgress (0 ,0 ,false );
100
144
mNotificationManager .notify (1 , notifBuilder .build ());
101
145
}
146
+ void notifyStop (NotificationManagerCompat mNotificationManager , NotificationCompat .Builder notifBuilder , PendingIntent mPendingIntent ){
147
+ notifBuilder .setContentIntent (mPendingIntent ).setSmallIcon (R .mipmap .ic_launcher ).setContentText (getApplicationContext ().getResources ().getString (R .string .text_notif_stop )).setProgress (0 ,0 ,false );
148
+ mNotificationManager .notify (1 , notifBuilder .build ());
149
+ }
102
150
@ RequiresApi (api = Build .VERSION_CODES .O )
103
151
void makeNotificationChannel (String id , String name , int importance )
104
152
{
0 commit comments