From fa341a53f2b21cdc390d6f6a3f6835155bce2f0a Mon Sep 17 00:00:00 2001 From: deniscerri <64997243+deniscerri@users.noreply.github.com> Date: Sat, 6 May 2023 12:15:10 +0200 Subject: [PATCH] added ability keep cached files when download is finished --- .../5.json | 428 ++++++++++++++++++ .../ytdlnis/adapter/ActiveDownloadAdapter.kt | 4 +- .../deniscerri/ytdlnis/database/DBManager.kt | 5 +- .../ytdlnis/database/models/HistoryItem.kt | 5 +- .../database/viewmodel/DownloadViewModel.kt | 2 +- .../ui/more/settings/SettingsFragment.kt | 51 +-- .../com/deniscerri/ytdlnis/util/FileUtil.kt | 12 +- .../ytdlnis/util/NotificationUtil.kt | 4 +- .../deniscerri/ytdlnis/work/DownloadWorker.kt | 46 +- .../ytdlnis/work/TerminalDownloadWorker.kt | 17 +- .../main/res/drawable/baseline_save_24.xml | 5 + app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/root_preferences.xml | 8 + 13 files changed, 495 insertions(+), 94 deletions(-) create mode 100644 app/schemas/com.deniscerri.ytdlnis.database.DBManager/5.json create mode 100644 app/src/main/res/drawable/baseline_save_24.xml diff --git a/app/schemas/com.deniscerri.ytdlnis.database.DBManager/5.json b/app/schemas/com.deniscerri.ytdlnis.database.DBManager/5.json new file mode 100644 index 00000000..63a2eb1a --- /dev/null +++ b/app/schemas/com.deniscerri.ytdlnis.database.DBManager/5.json @@ -0,0 +1,428 @@ +{ + "formatVersion": 1, + "database": { + "version": 5, + "identityHash": "740a11120b61827e949e8cecb28b1d1d", + "entities": [ + { + "tableName": "results", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `url` TEXT NOT NULL, `title` TEXT NOT NULL, `author` TEXT NOT NULL, `duration` TEXT NOT NULL, `thumb` TEXT NOT NULL, `website` TEXT NOT NULL, `playlistTitle` TEXT NOT NULL, `formats` TEXT NOT NULL, `creationTime` INTEGER NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "url", + "columnName": "url", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "title", + "columnName": "title", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "author", + "columnName": "author", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "duration", + "columnName": "duration", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "thumb", + "columnName": "thumb", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "website", + "columnName": "website", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "playlistTitle", + "columnName": "playlistTitle", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "formats", + "columnName": "formats", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "creationTime", + "columnName": "creationTime", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "history", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `url` TEXT NOT NULL, `title` TEXT NOT NULL, `author` TEXT NOT NULL, `duration` TEXT NOT NULL, `thumb` TEXT NOT NULL, `type` TEXT NOT NULL, `time` INTEGER NOT NULL, `downloadPath` TEXT NOT NULL, `website` TEXT NOT NULL, `format` TEXT NOT NULL, `downloadId` INTEGER NOT NULL DEFAULT 0)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "url", + "columnName": "url", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "title", + "columnName": "title", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "author", + "columnName": "author", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "duration", + "columnName": "duration", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "thumb", + "columnName": "thumb", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "type", + "columnName": "type", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "time", + "columnName": "time", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "downloadPath", + "columnName": "downloadPath", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "website", + "columnName": "website", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "format", + "columnName": "format", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "downloadId", + "columnName": "downloadId", + "affinity": "INTEGER", + "notNull": true, + "defaultValue": "0" + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "downloads", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `url` TEXT NOT NULL, `title` TEXT NOT NULL, `author` TEXT NOT NULL, `thumb` TEXT NOT NULL, `duration` TEXT NOT NULL, `type` TEXT NOT NULL, `format` TEXT NOT NULL, `downloadSections` TEXT NOT NULL DEFAULT '', `allFormats` TEXT NOT NULL, `downloadPath` TEXT NOT NULL, `website` TEXT NOT NULL, `downloadSize` TEXT NOT NULL, `playlistTitle` TEXT NOT NULL, `audioPreferences` TEXT NOT NULL, `videoPreferences` TEXT NOT NULL, `customFileNameTemplate` TEXT NOT NULL, `SaveThumb` INTEGER NOT NULL, `status` TEXT NOT NULL DEFAULT 'Queued', `downloadStartTime` INTEGER NOT NULL DEFAULT 0)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "url", + "columnName": "url", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "title", + "columnName": "title", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "author", + "columnName": "author", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "thumb", + "columnName": "thumb", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "duration", + "columnName": "duration", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "type", + "columnName": "type", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "format", + "columnName": "format", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "downloadSections", + "columnName": "downloadSections", + "affinity": "TEXT", + "notNull": true, + "defaultValue": "''" + }, + { + "fieldPath": "allFormats", + "columnName": "allFormats", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "downloadPath", + "columnName": "downloadPath", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "website", + "columnName": "website", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "downloadSize", + "columnName": "downloadSize", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "playlistTitle", + "columnName": "playlistTitle", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "audioPreferences", + "columnName": "audioPreferences", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "videoPreferences", + "columnName": "videoPreferences", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "customFileNameTemplate", + "columnName": "customFileNameTemplate", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "SaveThumb", + "columnName": "SaveThumb", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "status", + "columnName": "status", + "affinity": "TEXT", + "notNull": true, + "defaultValue": "'Queued'" + }, + { + "fieldPath": "downloadStartTime", + "columnName": "downloadStartTime", + "affinity": "INTEGER", + "notNull": true, + "defaultValue": "0" + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "commandTemplates", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `title` TEXT NOT NULL, `content` TEXT NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "title", + "columnName": "title", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "content", + "columnName": "content", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "searchHistory", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `query` TEXT NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "query", + "columnName": "query", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "templateShortcuts", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `content` TEXT NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "content", + "columnName": "content", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "cookies", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `url` TEXT NOT NULL, `content` TEXT NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "url", + "columnName": "url", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "content", + "columnName": "content", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + } + ], + "views": [], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '740a11120b61827e949e8cecb28b1d1d')" + ] + } +} \ No newline at end of file diff --git a/app/src/main/java/com/deniscerri/ytdlnis/adapter/ActiveDownloadAdapter.kt b/app/src/main/java/com/deniscerri/ytdlnis/adapter/ActiveDownloadAdapter.kt index 5a30853a..d4e5788a 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/adapter/ActiveDownloadAdapter.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/adapter/ActiveDownloadAdapter.kt @@ -120,10 +120,10 @@ class ActiveDownloadAdapter(onItemClickListener: OnItemClickListener, activity: if (fileSize.visibility == View.VISIBLE && codec.visibility == View.GONE){ fileSize.shapeAppearanceModel = ShapeAppearanceModel.builder() - .setTopRightCorner(CornerFamily.ROUNDED, 40F) - .setBottomRightCorner(CornerFamily.ROUNDED, 40F) .setTopLeftCorner(CornerFamily.ROUNDED, 0F) .setBottomLeftCorner(CornerFamily.ROUNDED, 0F) + .setTopRightCorner(CornerFamily.ROUNDED, 20F) + .setBottomRightCorner(CornerFamily.ROUNDED, 20F) .build() } diff --git a/app/src/main/java/com/deniscerri/ytdlnis/database/DBManager.kt b/app/src/main/java/com/deniscerri/ytdlnis/database/DBManager.kt index 2e3acd68..294e4c4d 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/database/DBManager.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/database/DBManager.kt @@ -8,11 +8,12 @@ import com.deniscerri.ytdlnis.database.models.* @TypeConverters(Converters::class) @Database( entities = [ResultItem::class, HistoryItem::class, DownloadItem::class, CommandTemplate::class, SearchHistoryItem::class, TemplateShortcut::class, CookieItem::class], - version = 4, + version = 5, autoMigrations = [ AutoMigration (from = 1, to = 2), AutoMigration (from = 2, to = 3), - AutoMigration (from = 3, to = 4) + AutoMigration (from = 3, to = 4), + AutoMigration (from = 4, to = 5) ] ) abstract class DBManager : RoomDatabase(){ diff --git a/app/src/main/java/com/deniscerri/ytdlnis/database/models/HistoryItem.kt b/app/src/main/java/com/deniscerri/ytdlnis/database/models/HistoryItem.kt index 40b128f7..5f9385ed 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/database/models/HistoryItem.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/database/models/HistoryItem.kt @@ -1,5 +1,6 @@ package com.deniscerri.ytdlnis.database.models +import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.PrimaryKey import com.deniscerri.ytdlnis.database.viewmodel.DownloadViewModel @@ -17,5 +18,7 @@ data class HistoryItem( val time: Long, val downloadPath: String, val website: String, - val format: Format + val format: Format, + @ColumnInfo(defaultValue = "0") + val downloadId: Long ) \ No newline at end of file diff --git a/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/DownloadViewModel.kt b/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/DownloadViewModel.kt index 2a2c58b5..4b56e6a9 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/DownloadViewModel.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/DownloadViewModel.kt @@ -218,7 +218,7 @@ class DownloadViewModel(private val application: Application) : AndroidViewModel val audioPreferences = AudioPreferences(embedThumb, false, ArrayList(sponsorblock!!)) val videoPreferences = VideoPreferences(embedSubs, addChapters, false, ArrayList(sponsorblock), saveSubs) - return DownloadItem(0, + return DownloadItem(historyItem.downloadId, historyItem.url, historyItem.title, historyItem.author, diff --git a/app/src/main/java/com/deniscerri/ytdlnis/ui/more/settings/SettingsFragment.kt b/app/src/main/java/com/deniscerri/ytdlnis/ui/more/settings/SettingsFragment.kt index c4064943..7c739125 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/ui/more/settings/SettingsFragment.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/ui/more/settings/SettingsFragment.kt @@ -48,6 +48,7 @@ class SettingsFragment : PreferenceFragmentCompat() { private var commandPath: Preference? = null private var accessAllFiles : Preference? = null private var clearCache: Preference? = null + private var keepCache: SwitchPreferenceCompat? = null private var incognito: SwitchPreferenceCompat? = null private var preferredDownloadType : ListPreference? = null private var searchEngine : ListPreference? = null @@ -123,6 +124,7 @@ class SettingsFragment : PreferenceFragmentCompat() { commandPath = findPreference("command_path") accessAllFiles = findPreference("access_all_files") clearCache = findPreference("clear_cache") + keepCache = findPreference("keep_cache") incognito = findPreference("incognito") preferredDownloadType = findPreference("preferred_download_type") searchEngine = findPreference("search_engine") @@ -171,8 +173,6 @@ class SettingsFragment : PreferenceFragmentCompat() { entries.add(Locale(it).getDisplayName(Locale(it))) } language!!.entries = entries.toTypedArray() - val lang = if (values.contains(Locale.getDefault().language)) Locale.getDefault().language else "en" - editor.putString("app_language", lang) }else{ language!!.isVisible = false } @@ -183,9 +183,6 @@ class SettingsFragment : PreferenceFragmentCompat() { ignoreBatteryOptimization!!.isVisible = false } - editor.putString("ytdlnis_theme", theme!!.value) - editor.putString("theme_accent", accent!!.value) - editor.putBoolean("high_contrast", highContrast!!.isChecked) if (preferences.getString("music_path", "")!!.isEmpty()) { editor.putString("music_path", getString(R.string.music_path)) @@ -197,44 +194,11 @@ class SettingsFragment : PreferenceFragmentCompat() { editor.putString("command_path", getString(R.string.command_path)) } - if((Build.VERSION.SDK_INT >= 30 && Environment.isExternalStorageManager()) || - Build.VERSION.SDK_INT < 30) { + if((VERSION.SDK_INT >= 30 && Environment.isExternalStorageManager()) || + VERSION.SDK_INT < 30) { accessAllFiles!!.isVisible = false } - editor.putBoolean("incognito", incognito!!.isChecked) - editor.putString("preferred_download_type", preferredDownloadType!!.value) - editor.putString("search_engine", searchEngine!!.value) - editor.putString("start_destination", startDestination!!.value) - editor.putBoolean("download_card", downloadCard!!.isChecked) - editor.putBoolean("quick_download", quickDownload!!.isChecked) - editor.putBoolean("metered_networks", meteredNetwork!!.isChecked) - editor.putString("api_key", apiKey!!.text) - editor.putBoolean("home_recommendations", homeRecommendations!!.isChecked) - editor.putString("locale", locale!!.value) - editor.putInt("concurrent_fragments", concurrentFragments!!.value) - editor.putInt("concurrent_downloads", concurrentDownloads!!.value) - editor.putString("limit_rate", limitRate!!.text) - editor.putBoolean("aria2", aria2!!.isChecked) - editor.putBoolean("log_downloads", logDownloads!!.isChecked) - editor.putStringSet("sponsorblock_filters", sponsorblockFilters!!.values) - editor.putString("file_name_template", filenameTemplate!!.text) - editor.putBoolean("restrict_filenames", restrictFilenames!!.isChecked) - editor.putBoolean("mtime", mtime!!.isChecked) - editor.putBoolean("embed_subtitles", embedSubtitles!!.isChecked) - editor.putBoolean("write_subtitles", writeSubtitles!!.isChecked) - editor.putBoolean("embed_thumbnail", embedThumbnail!!.isChecked) - editor.putBoolean("add_chapters", addChapters!!.isChecked) - editor.putBoolean("write_thumbnail", writeThumbnail!!.isChecked) - editor.putString("audio_format", audioFormat!!.value) - editor.putString("video_format", videoFormat!!.value) - editor.putInt("audio_quality", audioQuality!!.value) - editor.putString("video_quality", videoQuality!!.value) - editor.putString("format_id", formatID!!.text) - editor.putBoolean("nightly_ytdl", updateNightlyYTDL!!.isChecked) - editor.putBoolean("update_formats", updateFormats!!.isChecked) - editor.putString("formats_source", formatSource!!.value) - editor.putBoolean("update_app", updateApp!!.isChecked) editor.apply() } @@ -366,6 +330,13 @@ class SettingsFragment : PreferenceFragmentCompat() { } true } + keepCache!!.onPreferenceChangeListener = + Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any -> + val enable = newValue as Boolean + editor.putBoolean("keep_cache", enable) + editor.apply() + true + } incognito!!.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any -> val enable = newValue as Boolean diff --git a/app/src/main/java/com/deniscerri/ytdlnis/util/FileUtil.kt b/app/src/main/java/com/deniscerri/ytdlnis/util/FileUtil.kt index f5b808ad..a1c061e0 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/util/FileUtil.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/util/FileUtil.kt @@ -5,6 +5,7 @@ import android.media.MediaScannerConnection import android.net.Uri import android.os.Build import android.provider.DocumentsContract +import android.util.Log import android.webkit.MimeTypeMap import com.deniscerri.ytdlnis.R import com.deniscerri.ytdlnis.database.models.DownloadItem @@ -77,19 +78,22 @@ class FileUtil() { return context.getString(R.string.unfound_file); } @Throws(Exception::class) - fun moveFile(originDir: File, context: Context, destDir: String, progress: (p: Int) -> Unit) : String { + fun moveFile(originDir: File, context: Context, destDir: String, keepCache: Boolean, progress: (p: Int) -> Unit) : String { val fileList = mutableListOf() val dir = File(formatPath(destDir)) if (!dir.exists()) dir.mkdirs() originDir.listFiles()?.forEach { + Log.e("zz", it.name) val destFile = File(dir.absolutePath + "/${it.name}") try { - if (it.name.equals("rList")){ + if (it.name.matches("(^config.*.\\.txt\$)|(rList)|(part-Frag)".toRegex())){ it.delete() return@forEach } + if(it.name.contains(".part-Frag")) return@forEach + val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(it.extension) ?: "*/*" @@ -132,7 +136,9 @@ class FileUtil() { } } - originDir.deleteRecursively() + if (!keepCache){ + originDir.deleteRecursively() + } return scanMedia(fileList, context) } diff --git a/app/src/main/java/com/deniscerri/ytdlnis/util/NotificationUtil.kt b/app/src/main/java/com/deniscerri/ytdlnis/util/NotificationUtil.kt index 5ba4c409..26fa2582 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/util/NotificationUtil.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/util/NotificationUtil.kt @@ -148,11 +148,11 @@ class NotificationUtil(var context: Context) { notificationBuilder .setContentTitle(context.getString(R.string.updating_download_data)) - .setSmallIcon(R.drawable.ic_launcher_foreground_large) + .setSmallIcon(android.R.drawable.stat_sys_download) .setLargeIcon( BitmapFactory.decodeResource( context.resources, - R.drawable.ic_launcher_foreground_large + android.R.drawable.stat_sys_download ) ) .setPriority(NotificationCompat.PRIORITY_DEFAULT) diff --git a/app/src/main/java/com/deniscerri/ytdlnis/work/DownloadWorker.kt b/app/src/main/java/com/deniscerri/ytdlnis/work/DownloadWorker.kt index 855a4a6f..03c5c16f 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/work/DownloadWorker.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/work/DownloadWorker.kt @@ -170,6 +170,12 @@ class DownloadWorker( request.addOption("--cookies", cookiesFile.absolutePath) } + val keepCache = sharedPreferences.getBoolean("keep_cache", false) + if(keepCache){ + request.addOption("--part") + request.addOption("--keep-fragments") + } + when(type){ DownloadViewModel.Type.audio -> { var audioQualityId : String = downloadItem.format.format_id @@ -177,33 +183,23 @@ class DownloadWorker( else if (audioQualityId == context.getString(R.string.worst_quality)) audioQualityId = "worstaudio" val ext = downloadItem.format.container - if (audioQualityId.isBlank()) request.addOption("-x") - else request.addOption("-f", audioQualityId) - - if(ext != "webm"){ - val codec = when(downloadItem.format.container){ - "aac" -> "aac" - "mp3" -> "libmp3lame" - "flac" -> "flac" - "opus" -> "libopus" - else -> "" - } - Log.e("aa", codec) - if (codec.isEmpty()){ - request.addOption("-x") - }else{ - request.addOption("--remux-video", ext) - request.addOption("--ppa", "VideoRemuxer:-vn -c:a $codec") + if (audioQualityId.isNotBlank()) request.addOption("-f", audioQualityId) + request.addOption("-x") + + if(ext.isNotBlank()){ + if(!ext.matches("(webm)|(Default)|(${context.getString(R.string.defaultValue)})".toRegex())){ + request.addOption("--audio-format", downloadItem.format.container) } } + request.addOption("--embed-metadata") if (downloadItem.audioPreferences.embedThumb) { request.addOption("--embed-thumbnail") request.addOption("--convert-thumbnails", "jpg") try { - val config = File(context.cacheDir.absolutePath + "/downloads/config" + downloadItem.title + "##" + downloadItem.format.format_id + ".txt") + val config = File(context.cacheDir.absolutePath + "/downloads/${downloadItem.id}/config" + downloadItem.title + "##" + downloadItem.format.format_id + ".txt") val configData = "--ppa \"ffmpeg: -c:v mjpeg -vf crop=\\\"'if(gt(ih,iw),iw,ih)':'if(gt(iw,ih),ih,iw)'\\\"\"" config.writeText(configData) request.addOption("--ppa", "ThumbnailsConvertor:-qmin 1 -q:v 1") @@ -326,8 +322,8 @@ class DownloadWorker( setProgressAsync(workDataOf("progress" to 100, "output" to "Moving file to ${fileUtil.formatPath(downloadLocation)}", "id" to downloadItem.id, "log" to logDownloads)) var finalPath : String? try { - finalPath = moveFile(tempFileDir.absoluteFile, downloadLocation){ progress -> - setProgressAsync(workDataOf("progress" to progress, "output" to "Moving file to ${fileUtil.formatPath(downloadLocation)}", "id" to downloadItem.id, "log" to logDownloads)) + finalPath = fileUtil.moveFile(tempFileDir.absoluteFile,context, downloadLocation, keepCache){ p -> + setProgressAsync(workDataOf("progress" to p, "output" to "Moving file to ${fileUtil.formatPath(downloadLocation)}", "id" to downloadItem.id, "log" to logDownloads)) } setProgressAsync(workDataOf("progress" to 100, "output" to "Moved file to $finalPath", "id" to downloadItem.id, "log" to logDownloads)) }catch (e: Exception){ @@ -343,7 +339,7 @@ class DownloadWorker( val unixtime = System.currentTimeMillis() / 1000 val file = File(finalPath!!) downloadItem.format.filesize = if (file.exists()) file.length() else 0L - val historyItem = HistoryItem(0, downloadItem.url, downloadItem.title, downloadItem.author, downloadItem.duration, downloadItem.thumb, downloadItem.type, unixtime, finalPath, downloadItem.website, downloadItem.format) + val historyItem = HistoryItem(0, downloadItem.url, downloadItem.title, downloadItem.author, downloadItem.duration, downloadItem.thumb, downloadItem.type, unixtime, finalPath, downloadItem.website, downloadItem.format, downloadItem.id) runBlocking { historyDao.insert(historyItem) } @@ -418,14 +414,6 @@ class DownloadWorker( super.onStopped() } - @Throws(Exception::class) - private fun moveFile(originDir: File, downLocation: String, progress: (progress: Int) -> Unit) : String{ - val fileUtil = FileUtil() - val path = fileUtil.moveFile(originDir, context, downLocation){ p -> - progress(p) - } - return path - } companion object { var itemId: Long = 0 diff --git a/app/src/main/java/com/deniscerri/ytdlnis/work/TerminalDownloadWorker.kt b/app/src/main/java/com/deniscerri/ytdlnis/work/TerminalDownloadWorker.kt index bdc3a004..a2dd1815 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/work/TerminalDownloadWorker.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/work/TerminalDownloadWorker.kt @@ -93,20 +93,18 @@ class TerminalDownloadWorker( } }.onSuccess { //move file from internal to set download directory - var finalPath : String? try { - finalPath = moveFile(tempFileDir.absoluteFile, downloadLocation!!){ progress -> - setProgressAsync(workDataOf("progress" to progress)) + fileUtil.moveFile(tempFileDir.absoluteFile,context, downloadLocation!!, false){ p -> + setProgressAsync(workDataOf("progress" to p)) } }catch (e: Exception){ - finalPath = context.getString(R.string.unfound_file) e.printStackTrace() handler.postDelayed({ Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show() }, 1000) } - if (it.out.length > 500){ + if (it.out.length > 200){ outputFile.appendText("${it.out}\n") if (logDownloads && logFile.exists()){ logFile.appendText("${it.out}\n") @@ -138,15 +136,6 @@ class TerminalDownloadWorker( super.onStopped() } - @Throws(Exception::class) - private fun moveFile(originDir: File, downLocation: String, progress: (progress: Int) -> Unit) : String{ - val fileUtil = FileUtil() - val path = fileUtil.moveFile(originDir, context, downLocation){ p -> - progress(p) - } - return path - } - companion object { const val TAG = "DownloadWorker" } diff --git a/app/src/main/res/drawable/baseline_save_24.xml b/app/src/main/res/drawable/baseline_save_24.xml new file mode 100644 index 00000000..30047ce7 --- /dev/null +++ b/app/src/main/res/drawable/baseline_save_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0c7d3653..009faeb1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -251,4 +251,6 @@ Pause\n Resume Updating Download Item Data + Keep Cache + Don\'t delete temporary files after the download is finished \ No newline at end of file diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index e1a0ac59..2465a9b4 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -72,6 +72,14 @@ android:summary="@string/clear_temporary_files_summary" app:title="@string/clear_temporary_files" /> + +