Skip to content

Commit edbeb4a

Browse files
committed
v1.1 commit
1 parent 0966bac commit edbeb4a

26 files changed

+240
-83
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ build
77
captures
88
.externalNativeBuild
99
app/src/main/python/__pycache__
10+
.gradle
Binary file not shown.
Binary file not shown.
6.88 KB
Binary file not shown.
0 Bytes
Binary file not shown.
204 Bytes
Binary file not shown.
63.9 KB
Binary file not shown.
290 KB
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
247 KB
Binary file not shown.

.gradle/checksums/checksums.lock

0 Bytes
Binary file not shown.

.gradle/checksums/md5-checksums.bin

27.8 KB
Binary file not shown.

.gradle/checksums/sha1-checksums.bin

11.8 KB
Binary file not shown.
Binary file not shown.

app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.1" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.acmo0.youtubedownloader">
44
<uses-permission android:name="android.permission.INTERNET" />
@@ -34,7 +34,14 @@
3434
<category android:name="android.intent.category.DEFAULT" />
3535
<data android:host="www.youtube.com" android:mimeType="text/*" />
3636
</intent-filter>
37+
38+
39+
3740
</activity>
41+
<receiver
42+
android:name=".StopDownloadBroadcast"
43+
android:enabled="true" />
44+
3845
</application>
3946

4047
</manifest>

app/src/main/java/com/acmo0/youtubedownloader/DownloaderWorker.java

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
import android.app.NotificationManager;
77
import android.app.PendingIntent;
88
import android.app.TaskStackBuilder;
9+
import android.content.BroadcastReceiver;
910
import android.content.Context;
1011
import android.content.Intent;
12+
import android.content.IntentFilter;
1113
import android.content.pm.PackageManager;
1214
import android.os.Build;
1315
import android.os.Environment;
16+
import android.widget.Toast;
1417

1518
import androidx.annotation.RequiresApi;
1619
import androidx.core.app.NotificationCompat;
@@ -23,6 +26,8 @@
2326
import com.chaquo.python.Python;
2427

2528
import java.io.File;
29+
import java.sql.Time;
30+
import java.util.concurrent.TimeUnit;
2631

2732
import io.reactivex.annotations.NonNull;
2833

@@ -32,6 +37,11 @@ public class DownloaderWorker extends Worker {
3237
public static final String DIRECTORY = "DIRECTORY";
3338
public static final String MAX_QUALITY = "MAX_QUALITY";
3439
public static final String VIDEO_URL = "VIDEO_URL";
40+
public static PyObject downloader;
41+
NotificationManagerCompat mNotificationManager;
42+
NotificationCompat.Builder notifBuilder;
43+
PendingIntent activityPendingIntent;
44+
3545
public DownloaderWorker(@NonNull Context context, @NonNull WorkerParameters params){
3646
super(context, params);
3747
}
@@ -43,9 +53,10 @@ public Result doWork(){
4353
String videoUrl = getInputData().getString(VIDEO_URL);
4454
File downloadDirectory = new File(directory);
4555
boolean success;
56+
4657
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");
4960

5061

5162
Intent activityIntent = new Intent(getApplicationContext(), MainActivity.class);
@@ -55,7 +66,7 @@ public Result doWork(){
5566
activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
5667
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
5768
stackBuilder.addNextIntentWithParentStack(activityIntent);
58-
PendingIntent activityPendingIntent = PendingIntent.getActivity(getApplicationContext(),0,activityIntent, 0);
69+
activityPendingIntent = PendingIntent.getActivity(getApplicationContext(),0,activityIntent, 0);
5970

6071
if(!downloadDirectory.exists()){
6172
success = downloadDirectory.mkdirs();
@@ -67,27 +78,60 @@ public Result doWork(){
6778

6879
Python py = Python.getInstance();
6980
PyObject pyf = py.getModule("downloader");
70-
PyObject downloader;
7181

7282

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");
7583

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+
}
79101
}
80-
else{
102+
if(downloader.get("fail").toBoolean() == false) {
81103
notifySuccess(mNotificationManager, notifBuilder, activityPendingIntent);
82104
return Result.success();
83105
}
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();
84123
}
85124
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);
86127
notifBuilder
128+
.setContentIntent(mPendingIntent)
87129
.setSmallIcon(R.mipmap.ic_launcher)
88130
.setContentTitle("Video Downloader")
89131
.setContentText(text)
90132
.setProgress(0, 0, true)
133+
.setOnlyAlertOnce(true)
134+
.addAction(R.drawable.ic_background,"Cancel",stopPIntent)
91135
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
92136
mNotificationManager.notify(1, notifBuilder.build());
93137
}
@@ -99,6 +143,10 @@ void notifySuccess(NotificationManagerCompat mNotificationManager, NotificationC
99143
notifBuilder.setContentIntent(mPendingIntent).setSmallIcon(R.mipmap.ic_launcher).setContentText(getApplicationContext().getResources().getString(R.string.text_notif_download_finished)).setProgress(0,0,false);
100144
mNotificationManager.notify(1, notifBuilder.build());
101145
}
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+
}
102150
@RequiresApi(api = Build.VERSION_CODES.O)
103151
void makeNotificationChannel(String id, String name, int importance)
104152
{

app/src/main/java/com/acmo0/youtubedownloader/MainActivity.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.io.File;
5656
import java.util.ArrayList;
5757
import java.util.List;
58+
import java.util.UUID;
5859

5960
@RequiresApi(api = Build.VERSION_CODES.R)
6061
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
@@ -67,6 +68,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
6768
RadioButton radioButton360p;
6869
RadioButton radioButtonAudio;
6970
ProgressBar progressBar;
71+
Button StopDownloadButton;
7072
TextView textViewWait;
7173
Button infoButton;
7274
Button infoButton2;
@@ -103,7 +105,7 @@ protected void onCreate(Bundle savedInstanceState) {
103105
radioButtonAudio = findViewById(R.id.radioButtonAudio);
104106
radioLayout = findViewById(R.id.radioLayout);
105107
textViewWait = findViewById(R.id.textViewWait);
106-
108+
StopDownloadButton = findViewById(R.id.stopbutton);
107109
editTextLink.setText(sharedUrl);
108110

109111
if (!editTextLink.getText().toString().equals("")) {
@@ -259,7 +261,10 @@ public void onClick(View view) {
259261
.putString(DIRECTORY, directory)
260262
.putString(MAX_QUALITY, maxQuality)
261263
.putString(VIDEO_URL, videoUrl).build();
264+
StopDownloadButton.setVisibility(View.VISIBLE);
265+
262266
OneTimeWorkRequest downloaderWorkRequest = new OneTimeWorkRequest.Builder(DownloaderWorker.class).setInputData(arguments).build();
267+
263268
WorkManager.getInstance(getApplicationContext()).getWorkInfoByIdLiveData(downloaderWorkRequest.getId())
264269
.observe(me, new Observer<WorkInfo>() {
265270
@Override
@@ -269,18 +274,21 @@ public void onChanged(@Nullable WorkInfo workInfo) {
269274
}
270275
if (workInfo.getState().equals(WorkInfo.State.RUNNING)) {
271276
textViewWait.setText(R.string.text_downloading);
277+
cancelButton(downloaderWorkManager, downloaderWorkRequest.getId());
272278
}
273279
if (workInfo.getState().equals(WorkInfo.State.SUCCEEDED)) {
274280
Toast.makeText(getApplicationContext(), getResources().getString(R.string.text_download_suceedeed), Toast.LENGTH_SHORT).show();
275281
textViewWait.setVisibility(View.INVISIBLE);
276282
progressBar.setVisibility(View.INVISIBLE);
277283
buttonDownload.setVisibility(View.VISIBLE);
284+
StopDownloadButton.setVisibility(View.GONE);
278285
}
279286
if (workInfo.getState().equals(WorkInfo.State.FAILED)) {
280287
Toast.makeText(getApplicationContext(), getResources().getString(R.string.text_download_fail), Toast.LENGTH_SHORT).show();
281288
textViewWait.setVisibility(View.INVISIBLE);
282289
progressBar.setVisibility(View.INVISIBLE);
283290
buttonDownload.setVisibility(View.VISIBLE);
291+
StopDownloadButton.setVisibility(View.GONE);
284292
}
285293
}
286294
});
@@ -321,6 +329,18 @@ public void onBackPressed(){
321329
super.onBackPressed();
322330
}
323331
}
332+
private void cancelButton(WorkManager worker, UUID workId){
333+
this.StopDownloadButton.setOnClickListener(new View.OnClickListener() {
334+
@Override
335+
public void onClick(View view) {
336+
worker.cancelWorkById(workId);
337+
textViewWait.setVisibility(View.INVISIBLE);
338+
progressBar.setVisibility(View.INVISIBLE);
339+
buttonDownload.setVisibility(View.VISIBLE);
340+
StopDownloadButton.setVisibility(View.INVISIBLE);
341+
}
342+
});
343+
}
324344
private void configureToolBar(){
325345
this.toolBar = (Toolbar) findViewById(R.id.myToolBar);
326346

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.acmo0.youtubedownloader;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.widget.Toast;
7+
8+
import static com.acmo0.youtubedownloader.DownloaderWorker.downloader;
9+
10+
public class StopDownloadBroadcast extends BroadcastReceiver {
11+
@Override
12+
public void onReceive(Context context, Intent intent) {
13+
downloader.put("stop",true);
14+
System.out.println("Clicked");
15+
}
16+
}

0 commit comments

Comments
 (0)