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())
.subscribe(youtubeDLResponse -> {
downloadInfo.setDownloadPath(youtubeDLDir.getAbsolutePath());
downloadInfo.setDownloadType(type);
try{
for (Activity activity: activities.keySet()){
activity.runOnUiThread(() -> {

@ -110,21 +110,26 @@ public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerVi
if (video.isDownloading()){
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 {
progressBar.setVisibility(View.GONE);
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){
musicBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_music_downloaded));
}else{
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){
videoBtn.setIcon(ContextCompat.getDrawable(activity, R.drawable.ic_video_downloaded));
}else{
@ -132,6 +137,7 @@ public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerVi
}
}
if(checkedVideos.contains(position)){
card.setChecked(true);
card.setStrokeWidth(5);

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

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

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

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

@ -11,6 +11,7 @@ public class DownloadInfo {
private String outputLine;
private String downloadStatus;
private String downloadPath;
private String downloadType;
public DownloadInfo(){}
@ -63,4 +64,11 @@ public class DownloadInfo {
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.widget.Toast;
import androidx.annotation.Nullable;
import com.deniscerri.ytdlnis.R;
import com.deniscerri.ytdlnis.database.DBManager;
import com.deniscerri.ytdlnis.database.Video;
import com.yausername.youtubedl_android.YoutubeDL;
@ -191,9 +193,9 @@ public class InfoUtil {
String url = "https://www.youtube.com/watch?v=" + id;
int downloadedAudio = dbManager.checkDownloaded(url, "audio");
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){
Log.e(TAG, e.toString());
}
@ -308,6 +310,8 @@ public class InfoUtil {
if (jsonObject.has("ie_key")) website = jsonObject.getString("ie_key");
else website = jsonObject.getString("extractor");
String playlistTitle = "";
if (jsonObject.has("playlist")) playlistTitle = jsonObject.getString("playlist");
videos.add(new Video(
jsonObject.getString("id"),
@ -321,7 +325,8 @@ public class InfoUtil {
isPlaylist,
website,
0,
0)
0,
playlistTitle)
);
}
}catch(Exception e){
@ -361,6 +366,7 @@ public class InfoUtil {
if(v.getThumb().isEmpty()){
continue;
}
v.setPlaylistTitle(context.getString(R.string.trendingPlaylist));
videos.add(v);
}
return videos;

@ -98,4 +98,11 @@
<string name="downloads">Shkarkimet</string>
<string name="filter_history">Filtro Historinë</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>

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

Loading…
Cancel
Save