Skip to content

Commit

Permalink
v1.1 commit
Browse files Browse the repository at this point in the history
  • Loading branch information
acmo0 committed Nov 14, 2021
1 parent 0966bac commit edbeb4a
Show file tree
Hide file tree
Showing 26 changed files with 240 additions and 83 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ build
captures
.externalNativeBuild
app/src/main/python/__pycache__
.gradle
Binary file modified .gradle/7.0.2/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.0.2/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.0.2/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.0.2/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/7.0.2/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/7.0.2/javaCompile/classAnalysis.bin
Binary file not shown.
Binary file modified .gradle/7.0.2/javaCompile/jarAnalysis.bin
Binary file not shown.
Binary file modified .gradle/7.0.2/javaCompile/javaCompile.lock
Binary file not shown.
Binary file modified .gradle/7.0.2/javaCompile/taskHistory.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/outputFiles.bin
Binary file not shown.
Binary file modified .gradle/checksums/checksums.lock
Binary file not shown.
Binary file modified .gradle/checksums/md5-checksums.bin
Binary file not shown.
Binary file modified .gradle/checksums/sha1-checksums.bin
Binary file not shown.
Binary file not shown.
9 changes: 8 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.1" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.acmo0.youtubedownloader">
<uses-permission android:name="android.permission.INTERNET" />
Expand Down Expand Up @@ -34,7 +34,14 @@
<category android:name="android.intent.category.DEFAULT" />
<data android:host="www.youtube.com" android:mimeType="text/*" />
</intent-filter>



</activity>
<receiver
android:name=".StopDownloadBroadcast"
android:enabled="true" />

</application>

</manifest>
68 changes: 58 additions & 10 deletions app/src/main/java/com/acmo0/youtubedownloader/DownloaderWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
Expand All @@ -23,6 +26,8 @@
import com.chaquo.python.Python;

import java.io.File;
import java.sql.Time;
import java.util.concurrent.TimeUnit;

import io.reactivex.annotations.NonNull;

Expand All @@ -32,6 +37,11 @@ public class DownloaderWorker extends Worker {
public static final String DIRECTORY = "DIRECTORY";
public static final String MAX_QUALITY = "MAX_QUALITY";
public static final String VIDEO_URL = "VIDEO_URL";
public static PyObject downloader;
NotificationManagerCompat mNotificationManager;
NotificationCompat.Builder notifBuilder;
PendingIntent activityPendingIntent;

public DownloaderWorker(@NonNull Context context, @NonNull WorkerParameters params){
super(context, params);
}
Expand All @@ -43,9 +53,10 @@ public Result doWork(){
String videoUrl = getInputData().getString(VIDEO_URL);
File downloadDirectory = new File(directory);
boolean success;

makeNotificationChannel("CHANNEL", "Download status", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(getApplicationContext());
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(getApplicationContext(), "CHANNEL");
mNotificationManager = NotificationManagerCompat.from(getApplicationContext());
notifBuilder = new NotificationCompat.Builder(getApplicationContext(), "CHANNEL");


Intent activityIntent = new Intent(getApplicationContext(), MainActivity.class);
Expand All @@ -55,7 +66,7 @@ public Result doWork(){
activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
stackBuilder.addNextIntentWithParentStack(activityIntent);
PendingIntent activityPendingIntent = PendingIntent.getActivity(getApplicationContext(),0,activityIntent, 0);
activityPendingIntent = PendingIntent.getActivity(getApplicationContext(),0,activityIntent, 0);

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

Python py = Python.getInstance();
PyObject pyf = py.getModule("downloader");
PyObject downloader;


notifyDownloading(mNotificationManager, notifBuilder, getApplicationContext().getResources().getString(R.string.text_notif_downloading), activityPendingIntent);
downloader = pyf.callAttr("download", videoUrl, format, directory, maxQuality, Environment.getExternalStorageDirectory().getPath()+"/Android/data/com.acmo0.youtubedownloader");

if(downloader.toBoolean() == false){
notifyFail(mNotificationManager, notifBuilder, activityPendingIntent);
return Result.failure();
notifyDownloading(mNotificationManager, notifBuilder, getApplicationContext().getResources().getString(R.string.text_notif_downloading), activityPendingIntent);
downloader = pyf.callAttr("downloader", videoUrl, format, directory, maxQuality, Environment.getExternalStorageDirectory().getPath()+"/Android/data/com.acmo0.youtubedownloader");
PyObject download = downloader.callAttr("download");
System.out.println("Started downloading");
while (downloader.callAttr("state").toBoolean()!=false){
notifBuilder.setContentText(downloader.get("status").toString());
mNotificationManager.notify(1,notifBuilder.build());
if(downloader.get("fail").toBoolean() == true){
mNotificationManager.cancelAll();
notifyFail(mNotificationManager, notifBuilder, activityPendingIntent);
return Result.failure();
}
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
else{
if(downloader.get("fail").toBoolean() == false) {
notifySuccess(mNotificationManager, notifBuilder, activityPendingIntent);
return Result.success();
}
mNotificationManager.cancelAll();
notifyFail(mNotificationManager, notifBuilder, activityPendingIntent);
return Result.failure();
}

@Override
public void onStopped(){
System.out.println("STOOOOOOP");
downloader.put("stop",true);
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mNotificationManager.cancelAll();
notifyStop(mNotificationManager, notifBuilder, activityPendingIntent);
super.onStopped();
}
void notifyDownloading(NotificationManagerCompat mNotificationManager, NotificationCompat.Builder notifBuilder, String text, PendingIntent mPendingIntent){
Intent stopIntent = new Intent(getApplicationContext(), StopDownloadBroadcast.class);
PendingIntent stopPIntent = PendingIntent.getBroadcast(getApplicationContext(),1,stopIntent,0);
notifBuilder
.setContentIntent(mPendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Video Downloader")
.setContentText(text)
.setProgress(0, 0, true)
.setOnlyAlertOnce(true)
.addAction(R.drawable.ic_background,"Cancel",stopPIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
mNotificationManager.notify(1, notifBuilder.build());
}
Expand All @@ -99,6 +143,10 @@ void notifySuccess(NotificationManagerCompat mNotificationManager, NotificationC
notifBuilder.setContentIntent(mPendingIntent).setSmallIcon(R.mipmap.ic_launcher).setContentText(getApplicationContext().getResources().getString(R.string.text_notif_download_finished)).setProgress(0,0,false);
mNotificationManager.notify(1, notifBuilder.build());
}
void notifyStop(NotificationManagerCompat mNotificationManager, NotificationCompat.Builder notifBuilder, PendingIntent mPendingIntent){
notifBuilder.setContentIntent(mPendingIntent).setSmallIcon(R.mipmap.ic_launcher).setContentText(getApplicationContext().getResources().getString(R.string.text_notif_stop)).setProgress(0,0,false);
mNotificationManager.notify(1, notifBuilder.build());
}
@RequiresApi(api = Build.VERSION_CODES.O)
void makeNotificationChannel(String id, String name, int importance)
{
Expand Down
22 changes: 21 additions & 1 deletion app/src/main/java/com/acmo0/youtubedownloader/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RequiresApi(api = Build.VERSION_CODES.R)
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
Expand All @@ -67,6 +68,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
RadioButton radioButton360p;
RadioButton radioButtonAudio;
ProgressBar progressBar;
Button StopDownloadButton;
TextView textViewWait;
Button infoButton;
Button infoButton2;
Expand Down Expand Up @@ -103,7 +105,7 @@ protected void onCreate(Bundle savedInstanceState) {
radioButtonAudio = findViewById(R.id.radioButtonAudio);
radioLayout = findViewById(R.id.radioLayout);
textViewWait = findViewById(R.id.textViewWait);

StopDownloadButton = findViewById(R.id.stopbutton);
editTextLink.setText(sharedUrl);

if (!editTextLink.getText().toString().equals("")) {
Expand Down Expand Up @@ -259,7 +261,10 @@ public void onClick(View view) {
.putString(DIRECTORY, directory)
.putString(MAX_QUALITY, maxQuality)
.putString(VIDEO_URL, videoUrl).build();
StopDownloadButton.setVisibility(View.VISIBLE);

OneTimeWorkRequest downloaderWorkRequest = new OneTimeWorkRequest.Builder(DownloaderWorker.class).setInputData(arguments).build();

WorkManager.getInstance(getApplicationContext()).getWorkInfoByIdLiveData(downloaderWorkRequest.getId())
.observe(me, new Observer<WorkInfo>() {
@Override
Expand All @@ -269,18 +274,21 @@ public void onChanged(@Nullable WorkInfo workInfo) {
}
if (workInfo.getState().equals(WorkInfo.State.RUNNING)) {
textViewWait.setText(R.string.text_downloading);
cancelButton(downloaderWorkManager, downloaderWorkRequest.getId());
}
if (workInfo.getState().equals(WorkInfo.State.SUCCEEDED)) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.text_download_suceedeed), Toast.LENGTH_SHORT).show();
textViewWait.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.INVISIBLE);
buttonDownload.setVisibility(View.VISIBLE);
StopDownloadButton.setVisibility(View.GONE);
}
if (workInfo.getState().equals(WorkInfo.State.FAILED)) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.text_download_fail), Toast.LENGTH_SHORT).show();
textViewWait.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.INVISIBLE);
buttonDownload.setVisibility(View.VISIBLE);
StopDownloadButton.setVisibility(View.GONE);
}
}
});
Expand Down Expand Up @@ -321,6 +329,18 @@ public void onBackPressed(){
super.onBackPressed();
}
}
private void cancelButton(WorkManager worker, UUID workId){
this.StopDownloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
worker.cancelWorkById(workId);
textViewWait.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.INVISIBLE);
buttonDownload.setVisibility(View.VISIBLE);
StopDownloadButton.setVisibility(View.INVISIBLE);
}
});
}
private void configureToolBar(){
this.toolBar = (Toolbar) findViewById(R.id.myToolBar);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.acmo0.youtubedownloader;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import static com.acmo0.youtubedownloader.DownloaderWorker.downloader;

public class StopDownloadBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
downloader.put("stop",true);
System.out.println("Clicked");
}
}
Loading

0 comments on commit edbeb4a

Please sign in to comment.