fixed bug when queued audio and video were of same source

2nd click downloaded the same type as the first. Now they download as expected
pull/1/head
Denis Çerri 4 years ago
parent 42deee3f2e
commit a3f0667c7d
No known key found for this signature in database
GPG Key ID: 3F50F14A8E7F7A13

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="192.168.1.131:46883" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2022-10-04T21:20:22.834747800Z" />
</component>
</project>

@ -328,6 +328,7 @@ public class DownloaderService extends Service {
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(youtubeDLResponse -> { .subscribe(youtubeDLResponse -> {
downloadInfo.setDownloadPath(youtubeDLDir.getAbsolutePath()); downloadInfo.setDownloadPath(youtubeDLDir.getAbsolutePath());
downloadInfo.setDownloadType(type);
try{ try{
for (Activity activity: activities.keySet()){ for (Activity activity: activities.keySet()){
activity.runOnUiThread(() -> { activity.runOnUiThread(() -> {

@ -110,21 +110,26 @@ public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerVi
if (video.isDownloading()){ if (video.isDownloading()){
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
if (video.isDownloadingAudio()){
musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel));
musicBtn.setTag(R.id.cancelDownload, "true");
}else if (video.isDownloadingVideo()){
videoBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel));
videoBtn.setTag(R.id.cancelDownload, "true");
}
}else { }else {
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
progressBar.setIndeterminate(true); progressBar.setIndeterminate(true);
}
if (video.isDownloadingAudio()) {
musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel));
musicBtn.setTag(R.id.cancelDownload, "true");
}else{
if(video.isAudioDownloaded() == 1){ if(video.isAudioDownloaded() == 1){
musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music_downloaded)); musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music_downloaded));
}else{ }else{
musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music)); musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music));
} }
}
if (video.isDownloadingVideo()){
videoBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_cancel));
videoBtn.setTag(R.id.cancelDownload, "true");
}else{
if(video.isVideoDownloaded() == 1){ if(video.isVideoDownloaded() == 1){
videoBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_video_downloaded)); videoBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_video_downloaded));
}else{ }else{
@ -132,6 +137,7 @@ public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerVi
} }
} }
if(checkedVideos.contains(position)){ if(checkedVideos.contains(position)){
card.setChecked(true); card.setChecked(true);
card.setStrokeWidth(5); card.setStrokeWidth(5);

@ -1,5 +1,6 @@
package com.deniscerri.ytdlnis.database; package com.deniscerri.ytdlnis.database;
import android.annotation.SuppressLint;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
@ -7,6 +8,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.ColorMatrix; import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter; import android.graphics.ColorMatrixColorFilter;
import android.util.Log;
import com.deniscerri.ytdlnis.R; import com.deniscerri.ytdlnis.R;
@ -16,7 +18,7 @@ import java.util.ArrayList;
public class DBManager extends SQLiteOpenHelper { public class DBManager extends SQLiteOpenHelper {
public static final String db_name = "ytdlnis_db"; public static final String db_name = "ytdlnis_db";
public static final int db_version = 9; public static final int db_version = 10;
public static final String results_table_name = "results"; public static final String results_table_name = "results";
public static final String history_table_name = "history"; public static final String history_table_name = "history";
public static final String id = "id"; public static final String id = "id";
@ -35,6 +37,7 @@ public class DBManager extends SQLiteOpenHelper {
public static final String downloadPath = "downloadPath"; public static final String downloadPath = "downloadPath";
public static final String downloadingAudio = "downloadingAudio"; public static final String downloadingAudio = "downloadingAudio";
public static final String downloadingVideo = "downloadingVideo"; public static final String downloadingVideo = "downloadingVideo";
public static final String playlistTitle = "playlistTitle";
public DBManager(Context context){ public DBManager(Context context){
@ -56,7 +59,8 @@ public class DBManager extends SQLiteOpenHelper {
+ isPlaylistItem + " INTENGER," + isPlaylistItem + " INTENGER,"
+ website + " TEXT," + website + " TEXT,"
+ downloadingAudio + " INTEGER," + downloadingAudio + " INTEGER,"
+ downloadingVideo + " INTEGER)"; + downloadingVideo + " INTEGER,"
+ playlistTitle + " TEXT)";
sqLiteDatabase.execSQL(query); sqLiteDatabase.execSQL(query);
@ -140,6 +144,7 @@ public class DBManager extends SQLiteOpenHelper {
} }
@SuppressLint("Range")
public ArrayList<Video> getResults(){ public ArrayList<Video> getResults(){
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + results_table_name, null); Cursor cursor = db.rawQuery("SELECT * FROM " + results_table_name, null);
@ -148,18 +153,19 @@ public class DBManager extends SQLiteOpenHelper {
if(cursor.moveToFirst()){ if(cursor.moveToFirst()){
do { do {
// on below line we are adding the data from cursor to our array list. // on below line we are adding the data from cursor to our array list.
list.add(new Video(cursor.getString(1), //videoId list.add(new Video(cursor.getString(cursor.getColumnIndex(videoId)),
cursor.getString(2), //url cursor.getString(cursor.getColumnIndex(url)),
cursor.getString(3), //title cursor.getString(cursor.getColumnIndex(title)),
cursor.getString(4), //author cursor.getString(cursor.getColumnIndex(author)),
cursor.getString(5), //duration cursor.getString(cursor.getColumnIndex(duration)),
cursor.getString(6), //thumb cursor.getString(cursor.getColumnIndex(thumb)),
cursor.getInt(7), //downloadedAudio cursor.getInt(cursor.getColumnIndex(downloadedAudio)),
cursor.getInt(8), //downloadedVideo cursor.getInt(cursor.getColumnIndex(downloadedVideo)),
cursor.getInt(9), //isPlaylistItem cursor.getInt(cursor.getColumnIndex(isPlaylistItem)),
cursor.getString(10), //website cursor.getString(cursor.getColumnIndex(website)),
cursor.getInt(11), //isDownloadingAudio cursor.getInt(cursor.getColumnIndex(downloadedAudio)),
cursor.getInt(12))); //isDownloadingVideo cursor.getInt(cursor.getColumnIndex(downloadingVideo)),
cursor.getString(cursor.getColumnIndex(playlistTitle))));
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
@ -167,14 +173,15 @@ public class DBManager extends SQLiteOpenHelper {
return list; return list;
} }
public ArrayList<Video> getHistory(String query, String format, String website, String sort){ @SuppressLint("Range")
public ArrayList<Video> getHistory(String query, String format, String site, String sort){
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
if (sort == null || sort.isEmpty()) sort = "DESC"; if (sort == null || sort.isEmpty()) sort = "DESC";
Cursor cursor = db.rawQuery("SELECT * FROM " + history_table_name Cursor cursor = db.rawQuery("SELECT * FROM " + history_table_name
+ " WHERE title LIKE '%"+query+"%'"+ + " WHERE title LIKE '%"+query+"%'"+
" AND type LIKE '%"+format+"%'"+ " AND type LIKE '%"+format+"%'"+
" AND website LIKE '%"+website+"%'"+ " AND website LIKE '%"+site+"%'"+
" ORDER BY id "+sort, " ORDER BY id "+sort,
null); null);
ArrayList<Video> list = new ArrayList<>(); ArrayList<Video> list = new ArrayList<>();
@ -182,16 +189,16 @@ public class DBManager extends SQLiteOpenHelper {
if(cursor.moveToFirst()){ if(cursor.moveToFirst()){
do { do {
// on below line we are adding the data from cursor to our array list. // on below line we are adding the data from cursor to our array list.
list.add(new Video(cursor.getInt(0), list.add(new Video(cursor.getInt(cursor.getColumnIndex(id)),
cursor.getString(1), cursor.getString(cursor.getColumnIndex(url)),
cursor.getString(2), cursor.getString(cursor.getColumnIndex(title)),
cursor.getString(3), cursor.getString(cursor.getColumnIndex(author)),
cursor.getString(4), cursor.getString(cursor.getColumnIndex(duration)),
cursor.getString(5), cursor.getString(cursor.getColumnIndex(thumb)),
cursor.getString(6), cursor.getString(cursor.getColumnIndex(type)),
cursor.getString(7), cursor.getString(cursor.getColumnIndex(time)),
cursor.getString(8), cursor.getString(cursor.getColumnIndex(downloadPath)),
cursor.getString(9))); cursor.getString(cursor.getColumnIndex(website))));
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
@ -216,6 +223,7 @@ public class DBManager extends SQLiteOpenHelper {
values.put(website, v.getWebsite()); values.put(website, v.getWebsite());
values.put(downloadingAudio, 0); values.put(downloadingAudio, 0);
values.put(downloadingVideo, 0); values.put(downloadingVideo, 0);
values.put(playlistTitle, v.getPlaylistTitle());
db.insert(results_table_name, null, values); db.insert(results_table_name, null, values);
} }
@ -275,7 +283,13 @@ public class DBManager extends SQLiteOpenHelper {
url + "' AND type='"+downloadType + "' LIMIT 1", null); url + "' AND type='"+downloadType + "' LIMIT 1", null);
if(cursor.moveToFirst()){ if(cursor.moveToFirst()){
return 1; String path = cursor.getString(8);
File file = new File(path);
if(!file.exists() && !path.isEmpty()){
return 0;
}else {
return 1;
}
} }
return 0; return 0;
} }

@ -20,10 +20,11 @@ public class Video implements Parcelable {
private String website; private String website;
private boolean downloadingAudio; private boolean downloadingAudio;
private boolean downloadingVideo; private boolean downloadingVideo;
private String playlistTitle;
// RESULTS OBJECT // RESULTS OBJECT
public Video(String videoId, String url, String title, String author, String duration, String thumb, public Video(String videoId, String url, String title, String author, String duration, String thumb,
int downloadedAudio, int downloadedVideo, int isPlaylistItem, String website, int downloadingAudio, int downloadingVideo) { int downloadedAudio, int downloadedVideo, int isPlaylistItem, String website, int downloadingAudio, int downloadingVideo, String playlistTitle) {
this.videoId = videoId; this.videoId = videoId;
this.url = url; this.url = url;
this.title = title; this.title = title;
@ -36,6 +37,7 @@ public class Video implements Parcelable {
this.website = website; this.website = website;
this.downloadingAudio = intToBoolean(downloadingAudio); this.downloadingAudio = intToBoolean(downloadingAudio);
this.downloadingVideo = intToBoolean(downloadingVideo); this.downloadingVideo = intToBoolean(downloadingVideo);
this.playlistTitle = playlistTitle;
} }
//HISTORY OBJECT //HISTORY OBJECT
@ -72,6 +74,7 @@ public class Video implements Parcelable {
isPlaylistItem = in.readInt(); isPlaylistItem = in.readInt();
downloadPath = in.readString(); downloadPath = in.readString();
website = in.readString(); website = in.readString();
playlistTitle = in.readString();
} }
public static final Creator<Video> CREATOR = new Creator<Video>() { public static final Creator<Video> CREATOR = new Creator<Video>() {
@ -226,6 +229,14 @@ public class Video implements Parcelable {
return this.downloadingAudio || this.downloadingVideo; return this.downloadingAudio || this.downloadingVideo;
} }
public String getPlaylistTitle() {
return playlistTitle;
}
public void setPlaylistTitle(String playlistTitle) {
this.playlistTitle = playlistTitle;
}
@Override @Override
public int describeContents() { public int describeContents() {
return 0; return 0;

@ -270,6 +270,8 @@ public class DownloadsFragment extends Fragment implements DownloadsRecyclerView
delete_dialog.setPositiveButton(getString(R.string.ok), (dialogInterface, i) -> { delete_dialog.setPositiveButton(getString(R.string.ok), (dialogInterface, i) -> {
dbManager.clearHistory(); dbManager.clearHistory();
downloadsRecyclerViewAdapter.clear(); downloadsRecyclerViewAdapter.clear();
downloadsObjects.clear();
downloadsRecyclerViewAdapter.setVideoList(downloadsObjects);
no_results.setVisibility(View.VISIBLE); no_results.setVisibility(View.VISIBLE);
selectionChips.setVisibility(View.GONE); selectionChips.setVisibility(View.GONE);
websiteGroup.removeAllViews(); websiteGroup.removeAllViews();
@ -437,6 +439,7 @@ public class DownloadsFragment extends Fragment implements DownloadsRecyclerView
delete_dialog.setPositiveButton(getString(R.string.ok), (dialogInterface, i) -> { delete_dialog.setPositiveButton(getString(R.string.ok), (dialogInterface, i) -> {
downloadsObjects.remove(position); downloadsObjects.remove(position);
downloadsRecyclerViewAdapter.notifyItemRemoved(position); downloadsRecyclerViewAdapter.notifyItemRemoved(position);
downloadsRecyclerViewAdapter.setVideoList(downloadsObjects);
downloadsRecyclerViewAdapter.setWebsiteList(); downloadsRecyclerViewAdapter.setWebsiteList();
updateWebsiteChips(); updateWebsiteChips();
dbManager.clearHistoryItem(v); dbManager.clearHistoryItem(v);

@ -134,14 +134,15 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
public void onDownloadEnd(DownloadInfo info) { public void onDownloadEnd(DownloadInfo info) {
downloadInfo = info; downloadInfo = info;
Video item = downloadInfo.getVideo();
String id = item.getVideoId();
String type = downloadInfo.getDownloadType();
item = findVideo(id);
try{ try{
Video item = downloadInfo.getVideo();
String id = item.getVideoId();
String type = item.getDownloadedType();
item = findVideo(id);
updateDownloadingStatusOnResult(item, type, false); updateDownloadingStatusOnResult(item, type, false);
homeRecyclerViewAdapter.notifyItemChanged(resultObjects.indexOf(item)); if (type.equals("audio")) item.setDownloadedAudio(1);
else item.setDownloadedVideo(1);
homeRecyclerViewAdapter.notifyItemChanged(resultObjects.indexOf(findVideo(id)));
// MEDIA SCAN // MEDIA SCAN
ArrayList<File> files = new ArrayList<>(); ArrayList<File> files = new ArrayList<>();
@ -160,7 +161,11 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
String[] paths = new String[files.size()]; String[] paths = new String[files.size()];
for (int i = 0; i < files.size(); i++) paths[i] = files.get(i).getAbsolutePath(); for (int i = 0; i < files.size(); i++) paths[i] = files.get(i).getAbsolutePath();
MediaScannerConnection.scanFile(context, paths, null, null); MediaScannerConnection.scanFile(context, paths, null, null);
String typeTmp = item.getDownloadedType();
item.setDownloadedType(type);
addToHistory(item, new Date(), paths); addToHistory(item, new Date(), paths);
item.setDownloadedType(typeTmp);
updateDownloadStatusOnResult(item, type, true); updateDownloadStatusOnResult(item, type, true);
mainActivity.updateHistoryFragment(); mainActivity.updateHistoryFragment();
@ -269,7 +274,11 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
Handler uiHandler = new Handler(Looper.getMainLooper()); Handler uiHandler = new Handler(Looper.getMainLooper());
dbManager = new DBManager(context); dbManager = new DBManager(context);
resultObjects = dbManager.getResults(); resultObjects = dbManager.getResults();
if (resultObjects.size() == 0) { String playlistTitle = "";
try {
playlistTitle = resultObjects.get(0).getPlaylistTitle();
}catch(Exception ignored){}
if (resultObjects.size() == 0 || (playlistTitle.equals(getString(R.string.trendingPlaylist)) && !downloading)) {
try { try {
infoUtil = new InfoUtil(context); infoUtil = new InfoUtil(context);
resultObjects = infoUtil.getTrending(context); resultObjects = infoUtil.getTrending(context);
@ -279,6 +288,7 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
} }
}else { }else {
if (!downloading){ if (!downloading){
homeRecyclerViewAdapter.setVideoList(resultObjects, true);
for (int i = 0; i < resultObjects.size(); i++){ for (int i = 0; i < resultObjects.size(); i++){
Video tmp = resultObjects.get(i); Video tmp = resultObjects.get(i);
if(tmp.isDownloading()){ if(tmp.isDownloading()){
@ -642,7 +652,6 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
}catch(Exception e){ }catch(Exception e){
e.printStackTrace(); e.printStackTrace();
} }
if (video != null) { if (video != null) {
dbManager = new DBManager(context); dbManager = new DBManager(context);
try { try {
@ -666,14 +675,14 @@ public class HomeFragment extends Fragment implements HomeRecyclerViewAdapter.On
} }
} }
public void updateDownloadingStatusOnResult(Video v, String type, boolean downloading) { public void updateDownloadingStatusOnResult(Video v, String type, boolean isDownloading) {
if (v != null) { if (v != null) {
if (type.equals("audio")) v.setDownloadingAudio(downloading); if (type.equals("audio")) v.setDownloadingAudio(isDownloading);
else if (type.equals("video")) v.setDownloadingVideo(downloading); else if (type.equals("video")) v.setDownloadingVideo(isDownloading);
homeRecyclerViewAdapter.updateVideoListItem(v, resultObjects.indexOf(v)); homeRecyclerViewAdapter.updateVideoListItem(v, resultObjects.indexOf(v));
dbManager = new DBManager(context); dbManager = new DBManager(context);
try { try {
dbManager.updateDownloadingStatusOnResult(v.getVideoId(), type, downloading); dbManager.updateDownloadingStatusOnResult(v.getVideoId(), type, isDownloading);
dbManager.close(); dbManager.close();
} catch (Exception ignored) {} } catch (Exception ignored) {}
} }

@ -11,6 +11,7 @@ public class DownloadInfo {
private String outputLine; private String outputLine;
private String downloadStatus; private String downloadStatus;
private String downloadPath; private String downloadPath;
private String downloadType;
public DownloadInfo(){} public DownloadInfo(){}
@ -63,4 +64,11 @@ public class DownloadInfo {
this.downloadPath = path; this.downloadPath = path;
} }
public String getDownloadType() {
return downloadType;
}
public void setDownloadType(String downloadType) {
this.downloadType = downloadType;
}
} }

@ -9,6 +9,8 @@ import android.text.Html;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.deniscerri.ytdlnis.R;
import com.deniscerri.ytdlnis.database.DBManager; import com.deniscerri.ytdlnis.database.DBManager;
import com.deniscerri.ytdlnis.database.Video; import com.deniscerri.ytdlnis.database.Video;
import com.yausername.youtubedl_android.YoutubeDL; import com.yausername.youtubedl_android.YoutubeDL;
@ -191,9 +193,9 @@ public class InfoUtil {
String url = "https://www.youtube.com/watch?v=" + id; String url = "https://www.youtube.com/watch?v=" + id;
int downloadedAudio = dbManager.checkDownloaded(url, "audio"); int downloadedAudio = dbManager.checkDownloaded(url, "audio");
int downloadedVideo = dbManager.checkDownloaded(url, "video"); int downloadedVideo = dbManager.checkDownloaded(url, "video");
int isPLaylist = 0; int isPlaylist = 0;
video = new Video(id, url, title, author, duration, thumb, downloadedAudio, downloadedVideo, isPLaylist, "youtube", 0, 0); video = new Video(id, url, title, author, duration, thumb, downloadedAudio, downloadedVideo, isPlaylist, "youtube", 0, 0, "");
}catch(Exception e){ }catch(Exception e){
Log.e(TAG, e.toString()); Log.e(TAG, e.toString());
} }
@ -308,6 +310,8 @@ public class InfoUtil {
if (jsonObject.has("ie_key")) website = jsonObject.getString("ie_key"); if (jsonObject.has("ie_key")) website = jsonObject.getString("ie_key");
else website = jsonObject.getString("extractor"); else website = jsonObject.getString("extractor");
String playlistTitle = "";
if (jsonObject.has("playlist")) playlistTitle = jsonObject.getString("playlist");
videos.add(new Video( videos.add(new Video(
jsonObject.getString("id"), jsonObject.getString("id"),
@ -321,7 +325,8 @@ public class InfoUtil {
isPlaylist, isPlaylist,
website, website,
0, 0,
0) 0,
playlistTitle)
); );
} }
}catch(Exception e){ }catch(Exception e){
@ -361,6 +366,7 @@ public class InfoUtil {
if(v.getThumb().isEmpty()){ if(v.getThumb().isEmpty()){
continue; continue;
} }
v.setPlaylistTitle(context.getString(R.string.trendingPlaylist));
videos.add(v); videos.add(v);
} }
return videos; return videos;

@ -98,4 +98,11 @@
<string name="downloads">Shkarkimet</string> <string name="downloads">Shkarkimet</string>
<string name="filter_history">Filtro Historinë</string> <string name="filter_history">Filtro Historinë</string>
<string name="removed_from_queue">u hoq nga rradha!</string> <string name="removed_from_queue">u hoq nga rradha!</string>
<string name="confirm_delete_history_deleted_desc">Kjo do të fshijë çdo element të historisë që nuk gjendet si skedar!</string>
<string name="confirm_delete_history_duplicates_desc">Kjo do të fshijë çdo element duplikat të historisë (url dhe tipi i njëjtë), duke mbajtur versionin e fundit!</string>
<string name="remove_deleted">Hiq të fshirat</string>
<string name="remove_duplicates">Hiq duplikatet</string>
<string name="sort_by">Rendit</string>
<string name="newest_first">Të rejat në fillim</string>
<string name="oldest_first">Të vjetrat në fillim</string>
</resources> </resources>

@ -109,4 +109,5 @@
<string name="newest_first">Newest First</string> <string name="newest_first">Newest First</string>
<string name="oldest_first">Oldest First</string> <string name="oldest_first">Oldest First</string>
<item type="id" name="cancelDownload" /> <item type="id" name="cancelDownload" />
<string name="trendingPlaylist" translatable="false">ytdlnis-TRENDING</string>
</resources> </resources>

Loading…
Cancel
Save