Skip to content

Commit

Permalink
add formats bottom sheet;
Browse files Browse the repository at this point in the history
  • Loading branch information
tastelessjolt committed Aug 27, 2018
1 parent 905bacd commit bf19e69
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 123 deletions.
41 changes: 41 additions & 0 deletions app/src/main/java/me/harshithgoka/youtubedl/Format.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package me.harshithgoka.youtubedl;

import android.app.DownloadManager;
import android.content.ContentProvider;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import java.io.File;

import me.harshithgoka.youtubedl.Utils.Utils;

/**
* Created by harshithgoka on 12/16/2017 AD.
Expand All @@ -14,6 +24,9 @@ public class Format implements Parcelable {
public String quality;
public String type;

public String extension;
public String description;

public Format (String title) {
this.title = title;
}
Expand All @@ -23,6 +36,10 @@ protected Format(Parcel in) {
url = in.readString();
quality = in.readString();
type = in.readString();

extension = Utils.getExtension(this);
title = Utils.getTitle(this);
description = Utils.getDescription(this);
}

public static final Creator<Format> CREATOR = new Creator<Format>() {
Expand All @@ -49,4 +66,28 @@ public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(quality);
parcel.writeString(type);
}

public void download (Context context) {
String extension = Utils.getExtension(this);
String name = title;
Log.d("Filename", title + "." + extension);

DownloadManager.Request req = new DownloadManager.Request(Uri.parse(url));
req.setTitle(name)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, File.separator + name + "." + extension)
.allowScanningByMediaScanner();
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

DownloadManager dm =null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
File[] files = context.getExternalMediaDirs();
if (files.length > 0) {
Log.d(files[0].getAbsolutePath(), files.length > 1 ? files[1].getAbsolutePath(): "");
}
dm = context.getSystemService(DownloadManager.class);
}else{
dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
}
dm.enqueue(req);
}
}
40 changes: 18 additions & 22 deletions app/src/main/java/me/harshithgoka/youtubedl/FormatAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,34 @@ public class FormatAdapter extends RecyclerView.Adapter<FormatAdapter.MyViewHold
List<Format> formats;
Context context;

class MyViewHolder extends RecyclerView.ViewHolder {
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView quality, type, itag, url;
public View parentView;

public MyViewHolder(View itemView) {
super(itemView);
quality = itemView.findViewById(R.id.format_quality);
type = itemView.findViewById(R.id.format_type);
itag = itemView.findViewById(R.id.format_itag);
url = itemView.findViewById(R.id.format_url);
parentView = itemView;
}

@Override
public void onClick(View view) {
String finalurl = ((TextView) view.findViewById(R.id.format_url)).getText().toString();

ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
assert clipboard != null;
ClipData clip = ClipData.newRawUri("DownloadURL", Uri.parse(finalurl));
clipboard.setPrimaryClip(clip);

Toast.makeText(context, String.format("(%s) Quality link copied to Clipboard", ((TextView) view.findViewById(R.id.format_quality)).getText().toString()), Toast.LENGTH_SHORT).show();

formats.get(getLayoutPosition()).download(context);
}
}

public FormatAdapter( Context context, List<Format> formats ) {
this.formats = new ArrayList<>();
this.formats.addAll(formats);

this.formats = formats;
this.context = context;
}

Expand All @@ -60,30 +70,16 @@ public void onBindViewHolder(final FormatAdapter.MyViewHolder holder, int positi
else
holder.quality.setText("Quality not defined");
if (format.type != null)
holder.type.setText(format.type);
holder.type.setText(format.title);
else
holder.type.setText("Type not defined");

if (format.url != null)
holder.url.setText(format.url);
holder.url.setText(format.description);
else
holder.url.setText("URL not found");

holder.itag.setText(format.itag + "");

holder.parentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String finalurl = ((TextView) view.findViewById(R.id.format_url)).getText().toString();

ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
assert clipboard != null;
ClipData clip = ClipData.newRawUri("DownloadURL", Uri.parse(finalurl));
clipboard.setPrimaryClip(clip);

Toast.makeText(context, String.format("(%s) Quality link copied to Clipboard", ((TextView) view.findViewById(R.id.format_quality)).getText().toString()), Toast.LENGTH_SHORT).show();
}
});
}

@Override
Expand Down
75 changes: 38 additions & 37 deletions app/src/main/java/me/harshithgoka/youtubedl/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.code.regexp.Matcher;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import me.harshithgoka.youtubedl.Utils.Utils;


Expand All @@ -45,12 +47,18 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
FloatingActionButton btnCopy;
Button btnDownload, btnAllFormats;

List<Format> curr_formats;
List<Format> formats;

Extractor extractor;

Pattern youtubeUrlPattern;

RecyclerView recyclerView;
FormatAdapter adapter;
LinearLayoutManager linearLayoutManager;

BottomSheetBehavior<View> bottomSheetBehavior;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
Expand Down Expand Up @@ -95,11 +103,29 @@ protected void onCreate(Bundle savedInstanceState) {

urlEdit = (EditText) findViewById(R.id.url);

curr_formats = new ArrayList<>();
formats = new ArrayList<>();
extractor = new Extractor();

youtubeUrlPattern = Pattern.compile(extractor._VALID_URL);

bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottom_sheet));
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

recyclerView = findViewById(R.id.recycler_view);
adapter = new FormatAdapter(getApplicationContext(), formats);
recyclerView.setAdapter(adapter);
linearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(linearLayoutManager);

BottomAppBar bar = (BottomAppBar) findViewById(R.id.bar);
bar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Handle the navigation click by showing a BottomDrawer etc.
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});

// ATTENTION: This was auto-generated to handle app links.
Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Expand Down Expand Up @@ -163,23 +189,23 @@ public void pasteFromClipboard(View button) {
if (clipboard.hasPrimaryClip()) {
ClipData clipData = clipboard.getPrimaryClip();
Uri uri;
String url;
CharSequence url;
if ( clipData.getItemCount() > 0 ) {
if ((uri = clipData.getItemAt(0).getUri()) != null) {
urlEdit.setText(uri.toString());
}
else if ((url = clipData.getItemAt(0).getText().toString()) != null ) {
urlEdit.setText(url);
else if ((url = clipData.getItemAt(0).getText()) != null ) {
urlEdit.setText(url.toString());
}
startPoint(button);
}
}
}

public void showAllFormats(View view) {
if (curr_formats.size() > 0) {
if (formats.size() > 0) {
Intent intent = new Intent(getApplicationContext(), FormatsActivity.class);
intent.putParcelableArrayListExtra(FormatsActivity.FORMATS, (ArrayList<? extends Parcelable>) curr_formats);
intent.putParcelableArrayListExtra(FormatsActivity.FORMATS, (ArrayList<? extends Parcelable>) formats);
startActivity(intent);
}
}
Expand Down Expand Up @@ -217,33 +243,13 @@ protected List<Format> doInBackground(String... strings) {
return ytextractor.getFormats(you_url);
}

public void download (String url, String name, String extension) {

DownloadManager.Request req = new DownloadManager.Request(Uri.parse(url));
req.setTitle(name)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, File.separator + name + "." + extension)
.allowScanningByMediaScanner();
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

DownloadManager dm =null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
File[] files = context.getExternalMediaDirs();
if (files.length > 0) {
Log.d(files[0].getAbsolutePath(), files.length > 1 ? files[1].getAbsolutePath(): "");
}
dm = context.getSystemService(DownloadManager.class);
}else{
dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
}
dm.enqueue(req);
}

@Override
protected void onPostExecute(List<Format> formats) {
if (formats != null) {
if (formats.size() > 0) {
curr_formats.clear();
curr_formats.addAll(formats);
MainActivity.this.formats.clear();
MainActivity.this.formats.addAll(formats);
MainActivity.this.adapter.notifyDataSetChanged();

String finalurl = formats.get(0).url;
println(finalurl);
Expand All @@ -254,11 +260,6 @@ protected void onPostExecute(List<Format> formats) {
clipboard.setPrimaryClip(clip);

Toast.makeText(getApplicationContext(), String.format("Best quality link (%s) copied to Clipboard", formats.get(0).quality), Toast.LENGTH_SHORT).show();
String extension = "";
Format format = formats.get(0);
download(finalurl, format.title, Utils.getExtension(format));
Log.d("Filename", format.title + "." + Utils.getExtension(format));

}
else {
println("No. of formats: 0");
Expand Down
50 changes: 50 additions & 0 deletions app/src/main/java/me/harshithgoka/youtubedl/Utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -606,5 +606,55 @@ public static String getExtension (Format format) {
return "";
}

public static String getTitle (Format format) {
String ret = "";
if (formats != null) {
try {
JSONObject fmt = (JSONObject) formats.get(format.itag + "");
boolean audio = fmt.has("acodec");
boolean video = fmt.has("vcodec");

if ( audio && video) {
ret = "Video + Audio ";

if (fmt.has("height")) {
ret += fmt.getInt("height") + "p";
}

if (fmt.has("abr")) {
ret += fmt.getInt("abr") + "kbps audio";
}
}
else if (video) {
ret = "Video Only ";
if (fmt.has("height")) {
ret += fmt.getInt("height") + "p";
}
}
else if (audio) {
ret = "Audio Only ";
if (fmt.has("abr")) {
ret += fmt.getInt("abr") + "kbps";
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return ret;
}

public static String getDescription(Format format) {
if (formats != null) {
try {
JSONObject fmt = (JSONObject) formats.get(format.itag + "");
return fmt.getString("ext");
} catch (JSONException e) {
e.printStackTrace();
}
}
return "";
}


}
Loading

0 comments on commit bf19e69

Please sign in to comment.