added preferred audio format id

This commit is contained in:
deniscerri 2023-06-16 19:11:05 +02:00
parent 259b3b27dd
commit 7131e3e1e8
No known key found for this signature in database
GPG key ID: 95C43D517D830350
12 changed files with 70 additions and 35 deletions

View file

@ -12,6 +12,7 @@ import android.graphics.drawable.ColorDrawable
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Handler
import android.provider.Settings import android.provider.Settings
import android.util.Log import android.util.Log
import android.view.View import android.view.View
@ -67,6 +68,7 @@ class MainActivity : BaseActivity() {
private lateinit var navigationView: View private lateinit var navigationView: View
private lateinit var navHostFragment : NavHostFragment private lateinit var navHostFragment : NavHostFragment
private lateinit var navController : NavController private lateinit var navController : NavController
private lateinit var sharedPreferences: SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -105,13 +107,18 @@ class MainActivity : BaseActivity() {
} }
} }
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val graph = navController.navInflater.inflate(R.navigation.nav_graph) val graph = navController.navInflater.inflate(R.navigation.nav_graph)
graph.setStartDestination(R.id.homeFragment)
when(sharedPreferences.getString("start_destination", "")) {
"History" -> graph.setStartDestination(R.id.historyFragment)
"More" -> if (navigationView is NavigationBarView) graph.setStartDestination(R.id.moreFragment)
}
navController.graph = graph navController.graph = graph
if (navigationView is NavigationBarView){ if (navigationView is NavigationBarView){
(navigationView as NavigationBarView).selectedItemId = graph.startDestinationId
(navigationView as NavigationBarView).setupWithNavController(navController) (navigationView as NavigationBarView).setupWithNavController(navController)
(navigationView as NavigationBarView).setOnItemReselectedListener { (navigationView as NavigationBarView).setOnItemReselectedListener {
when (it.itemId) { when (it.itemId) {
@ -142,7 +149,6 @@ class MainActivity : BaseActivity() {
window.navigationBarColor = SurfaceColors.SURFACE_2.getColor(this) window.navigationBarColor = SurfaceColors.SURFACE_2.getColor(this)
} }
if (navigationView is NavigationView){ if (navigationView is NavigationView){
(navigationView as NavigationView).setCheckedItem(graph.startDestinationId)
(navigationView as NavigationView).setupWithNavController(navController) (navigationView as NavigationView).setupWithNavController(navController)
//terminate button //terminate button
(navigationView as NavigationView).menu.getItem(7).setOnMenuItemClickListener { (navigationView as NavigationView).menu.getItem(7).setOnMenuItemClickListener {
@ -194,13 +200,14 @@ class MainActivity : BaseActivity() {
} }
} }
when(sharedPreferences.getString("start_destination", "")) { when(sharedPreferences.getString("start_destination", "")) {
"History" -> navController.navigate(R.id.historyFragment)
"Queue" -> navController.navigate(R.id.downloadQueueMainFragment) "Queue" -> navController.navigate(R.id.downloadQueueMainFragment)
"More" -> if (navigationView is NavigationBarView) graph.setStartDestination(R.id.moreFragment)
} }
} }
private fun View.visibilityChanged(action: (View) -> Unit) { private fun View.visibilityChanged(action: (View) -> Unit) {
this.viewTreeObserver.addOnGlobalLayoutListener { this.viewTreeObserver.addOnGlobalLayoutListener {
val newVis: Int = this.visibility val newVis: Int = this.visibility

View file

@ -64,6 +64,7 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
private val videoQualityPreference: String private val videoQualityPreference: String
private val formatIDPreference: String private val formatIDPreference: String
private val audioFormatIDPreference: String
private val resources : Resources private val resources : Resources
enum class Type { enum class Type {
audio, video, command audio, video, command
@ -86,6 +87,7 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
videoQualityPreference = sharedPreferences.getString("video_quality", application.getString(R.string.best_quality)).toString() videoQualityPreference = sharedPreferences.getString("video_quality", application.getString(R.string.best_quality)).toString()
formatIDPreference = sharedPreferences.getString("format_id", "").toString() formatIDPreference = sharedPreferences.getString("format_id", "").toString()
audioFormatIDPreference = sharedPreferences.getString("format_id_audio", "").toString()
val confTmp = Configuration(application.resources.configuration) val confTmp = Configuration(application.resources.configuration)
confTmp.locale = Locale(sharedPreferences.getString("app_language", "en")!!) confTmp.locale = Locale(sharedPreferences.getString("app_language", "en")!!)
@ -165,7 +167,7 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
val sponsorblock = sharedPreferences.getStringSet("sponsorblock_filters", emptySet()) val sponsorblock = sharedPreferences.getStringSet("sponsorblock_filters", emptySet())
val audioPreferences = AudioPreferences(embedThumb, false, ArrayList(sponsorblock!!)) val audioPreferences = AudioPreferences(embedThumb, false, ArrayList(sponsorblock!!))
val videoPreferences = VideoPreferences(embedSubs, addChapters, false, ArrayList(sponsorblock), saveSubs) val videoPreferences = VideoPreferences(embedSubs, addChapters, false, ArrayList(sponsorblock), saveSubs, audioFormatIDs = ArrayList(resultItem.formats.filter { it.format_id == audioFormatIDPreference }.map { it.format_id }))
return DownloadItem(0, return DownloadItem(0,
resultItem.url, resultItem.url,
@ -312,7 +314,7 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
return cloneFormat ( return cloneFormat (
try { try {
try{ try{
formats.first { it.format_note.contains("audio", ignoreCase = true) && it.format_id == formatIDPreference } formats.first { it.format_note.contains("audio", ignoreCase = true) && it.format_id == audioFormatIDPreference }
}catch (e: Exception){ }catch (e: Exception){
formats.last { it.format_note.contains("audio", ignoreCase = true) } formats.last { it.format_note.contains("audio", ignoreCase = true) }
} }

View file

@ -118,9 +118,7 @@ class ShareActivity : BaseActivity() {
val inputQuery = intent.getStringExtra(Intent.EXTRA_TEXT)!! val inputQuery = intent.getStringExtra(Intent.EXTRA_TEXT)!!
lifecycleScope.launch { lifecycleScope.launch {
try { try {
val result = withContext(Dispatchers.IO){ val result = resultViewModel.getItemByURL(inputQuery)
resultViewModel.getItemByURL(inputQuery)
}
loadingBottomSheet.dismiss() loadingBottomSheet.dismiss()
showDownloadSheet(result) showDownloadSheet(result)
}catch (e: Exception){ }catch (e: Exception){
@ -130,20 +128,18 @@ class ShareActivity : BaseActivity() {
loadingBottomSheet.dismiss() loadingBottomSheet.dismiss()
showDownloadSheet(result) showDownloadSheet(result)
}else{ }else{
lifecycleScope.launch { val res = withContext(Dispatchers.IO){
val res = withContext(Dispatchers.IO){ resultViewModel.parseQuery(inputQuery, true)
resultViewModel.parseQuery(inputQuery, true) }
} if (res.isEmpty()) {
if (res.isEmpty()) { Toast.makeText(this@ShareActivity, "No Results Found!", Toast.LENGTH_SHORT).show()
Toast.makeText(this@ShareActivity, "No Results Found!", Toast.LENGTH_SHORT).show() exit()
exit() }else{
loadingBottomSheet.dismiss()
if (res.size == 1){
showDownloadSheet(res[0]!!)
}else{ }else{
loadingBottomSheet.dismiss() showSelectPlaylistItems(res.toList())
if (res.size == 1){
showDownloadSheet(res[0]!!)
}else{
showSelectPlaylistItems(res.toList())
}
} }
} }
} }

View file

@ -176,7 +176,9 @@ class DownloadAudioFragment(private var resultItem: ResultItem, private var curr
) )
) )
downloadItem.container = if (containerPreference == getString(R.string.defaultValue)) "" else containerPreference!! if (currentDownloadItem == null || !containers.contains(downloadItem.container)){
downloadItem.container = if (containerPreference == getString(R.string.defaultValue)) "" else containerPreference!!
}
containerAutoCompleteTextView.setText(downloadItem.container.ifEmpty { getString(R.string.defaultValue) }, false) containerAutoCompleteTextView.setText(downloadItem.container.ifEmpty { getString(R.string.defaultValue) }, false)
(container!!.editText as AutoCompleteTextView?)!!.onItemClickListener = (container!!.editText as AutoCompleteTextView?)!!.onItemClickListener =

View file

@ -180,7 +180,9 @@ class DownloadVideoFragment(private val resultItem: ResultItem, private var curr
containers containers
) )
) )
downloadItem.container = if (containerPreference == getString(R.string.defaultValue)) "" else containerPreference!! if (currentDownloadItem == null || !containers.contains(downloadItem.container)){
downloadItem.container = if (containerPreference == getString(R.string.defaultValue)) "" else containerPreference!!
}
containerAutoCompleteTextView!!.setText( containerAutoCompleteTextView!!.setText(
downloadItem.container.ifEmpty { getString(R.string.defaultValue) }, downloadItem.container.ifEmpty { getString(R.string.defaultValue) },
false) false)

View file

@ -541,7 +541,7 @@ class MainSettingsFragment : PreferenceFragmentCompat() {
val intent: Intent? = requireContext().packageManager val intent: Intent? = requireContext().packageManager
.getLaunchIntentForPackage(requireContext().packageName) .getLaunchIntentForPackage(requireContext().packageName)
intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent) startActivity(intent!!)
if(json.has("settings")){ if(json.has("settings")){
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(preferences.getString("app_language", "en"))) AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(preferences.getString("app_language", "en")))
} }

View file

@ -9,6 +9,15 @@ class ProcessingSettingsFragment : BaseSettingsFragment() {
override val title: Int = R.string.processing override val title: Int = R.string.processing
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.processing_preferences, rootKey) setPreferencesFromResource(R.xml.processing_preferences, rootKey)
val preferredFormatID : EditTextPreference? = findPreference("format_id")
val preferredFormatIDAudio : EditTextPreference? = findPreference("format_id_audio")
preferredFormatID?.title = "${getString(R.string.preferred_format_id)} [${getString(R.string.video)}]"
preferredFormatID?.dialogTitle = "${getString(R.string.file_name_template)} [${getString(R.string.video)}]"
preferredFormatIDAudio?.title = "${getString(R.string.preferred_format_id)} [${getString(R.string.audio)}]"
preferredFormatIDAudio?.dialogTitle = "${getString(R.string.file_name_template)} [${getString(R.string.audio)}]"
} }
} }

View file

@ -69,7 +69,7 @@ class DownloadWorker(
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
//update item if its incomplete //update item if its incomplete
updateDownloadItem(downloadItem, infoUtil, dao, resultDao, false, notificationUtil) updateDownloadItem(downloadItem, infoUtil, dao, resultDao)
} }
val pendingIntent = NavDeepLinkBuilder(context) val pendingIntent = NavDeepLinkBuilder(context)
@ -173,6 +173,8 @@ class DownloadWorker(
when(type){ when(type){
DownloadViewModel.Type.audio -> { DownloadViewModel.Type.audio -> {
val supportedContainers = context.resources.getStringArray(R.array.audio_containers)
var audioQualityId : String = downloadItem.format.format_id var audioQualityId : String = downloadItem.format.format_id
if (audioQualityId.isBlank() || audioQualityId == "0" || audioQualityId == context.getString(R.string.best_quality) || audioQualityId == "best") audioQualityId = "" if (audioQualityId.isBlank() || audioQualityId == "0" || audioQualityId == context.getString(R.string.best_quality) || audioQualityId == "best") audioQualityId = ""
else if (audioQualityId == context.getString(R.string.worst_quality) || audioQualityId == "worst") audioQualityId = "worstaudio" else if (audioQualityId == context.getString(R.string.worst_quality) || audioQualityId == "worst") audioQualityId = "worstaudio"
@ -182,7 +184,7 @@ class DownloadWorker(
request.addOption("-x") request.addOption("-x")
if(ext.isNotBlank()){ if(ext.isNotBlank()){
if(!ext.matches("(webm)|(Default)|(${context.getString(R.string.defaultValue)})".toRegex())){ if(!ext.matches("(webm)|(Default)|(${context.getString(R.string.defaultValue)})".toRegex()) && supportedContainers.contains(ext)){
request.addOption("--audio-format", ext) request.addOption("--audio-format", ext)
} }
} }
@ -220,6 +222,8 @@ class DownloadWorker(
} }
DownloadViewModel.Type.video -> { DownloadViewModel.Type.video -> {
val supportedContainers = context.resources.getStringArray(R.array.video_containers)
if (downloadItem.videoPreferences.addChapters) { if (downloadItem.videoPreferences.addChapters) {
request.addOption("--sponsorblock-mark", "all") request.addOption("--sponsorblock-mark", "all")
request.addOption("--embed-chapters") request.addOption("--embed-chapters")
@ -250,7 +254,7 @@ class DownloadWorker(
Log.e(TAG, formatArgument) Log.e(TAG, formatArgument)
request.addOption("-f", formatArgument) request.addOption("-f", formatArgument)
val outputFormat = downloadItem.container val outputFormat = downloadItem.container
if(outputFormat.isNotEmpty() && outputFormat != "Default" && outputFormat != context.getString(R.string.defaultValue)){ if(outputFormat.isNotEmpty() && outputFormat != "Default" && outputFormat != context.getString(R.string.defaultValue) && supportedContainers.contains(outputFormat)){
request.addOption("--merge-output-format", outputFormat.lowercase()) request.addOption("--merge-output-format", outputFormat.lowercase())
if (outputFormat != "webm") { if (outputFormat != "webm") {
val embedThumb = sharedPreferences.getBoolean("embed_thumbnail", false) val embedThumb = sharedPreferences.getBoolean("embed_thumbnail", false)
@ -343,7 +347,7 @@ class DownloadWorker(
}, 1000) }, 1000)
} }
val wasQuickDownloaded = updateDownloadItem(downloadItem, infoUtil, dao, resultDao, false, notificationUtil) val wasQuickDownloaded = updateDownloadItem(downloadItem, infoUtil, dao, resultDao)
//put download in history //put download in history
val incognito = sharedPreferences.getBoolean("incognito", false) val incognito = sharedPreferences.getBoolean("incognito", false)
@ -359,7 +363,9 @@ class DownloadWorker(
notificationUtil.cancelDownloadNotification(downloadItem.id.toInt()) notificationUtil.cancelDownloadNotification(downloadItem.id.toInt())
notificationUtil.createDownloadFinished( notificationUtil.createDownloadFinished(
downloadItem.title, if (finalPaths?.first().equals(context.getString(R.string.unfound_file))) null else finalPaths, downloadItem.title, if (finalPaths?.first().equals(context.getString(R.string.unfound_file))) null else listOf(
finalPaths!!.maxByOrNull { it.length }!!
),
NotificationUtil.DOWNLOAD_FINISHED_CHANNEL_ID NotificationUtil.DOWNLOAD_FINISHED_CHANNEL_ID
) )
@ -420,10 +426,14 @@ class DownloadWorker(
return Result.success() return Result.success()
} }
private fun updateDownloadItem(downloadItem: DownloadItem, infoUtil: InfoUtil, dao: DownloadDao, resultDao: ResultDao, logDownloads: Boolean, notificationUtil: NotificationUtil) : Boolean { private fun updateDownloadItem(
downloadItem: DownloadItem,
infoUtil: InfoUtil,
dao: DownloadDao,
resultDao: ResultDao
) : Boolean {
var wasQuickDownloaded = false var wasQuickDownloaded = false
if (downloadItem.title.isEmpty() || downloadItem.author.isEmpty() || downloadItem.thumb.isEmpty()){ if (downloadItem.title.isEmpty() || downloadItem.author.isEmpty() || downloadItem.thumb.isEmpty()){
if (logDownloads) notificationUtil.createUpdatingItemNotification(NotificationUtil.DOWNLOAD_SERVICE_CHANNEL_ID)
runCatching { runCatching {
setProgressAsync(workDataOf("progress" to 0, "output" to context.getString(R.string.updating_download_data), "id" to downloadItem.id, "log" to false)) setProgressAsync(workDataOf("progress" to 0, "output" to context.getString(R.string.updating_download_data), "id" to downloadItem.id, "log" to false))
val info = infoUtil.getMissingInfo(downloadItem.url) val info = infoUtil.getMissingInfo(downloadItem.url)
@ -437,7 +447,6 @@ class DownloadWorker(
dao.update(downloadItem) dao.update(downloadItem)
} }
} }
if (logDownloads) notificationUtil.cancelDownloadNotification(NotificationUtil.DOWNLOAD_UPDATING_NOTIFICATION_ID)
} }
return wasQuickDownloaded return wasQuickDownloaded
} }

View file

@ -132,7 +132,7 @@
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<TextView <TextView
android:id="@+id/codec" android:id="@+id/container"
style="@style/Widget.Material3.FloatingActionButton.Large.Secondary" style="@style/Widget.Material3.FloatingActionButton.Large.Secondary"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View file

@ -107,6 +107,7 @@
<item>nb</item> <item>nb</item>
<item>pl</item> <item>pl</item>
<item>pt</item> <item>pt</item>
<item>pt-BR</item>
<item>ru</item> <item>ru</item>
<item>ro</item> <item>ro</item>
<item>sq</item> <item>sq</item>

View file

@ -17,6 +17,7 @@
<locale android:name="ms"/> <locale android:name="ms"/>
<locale android:name="nb-NO"/> <locale android:name="nb-NO"/>
<locale android:name="pl"/> <locale android:name="pl"/>
<locale android:name="pt"/>
<locale android:name="pt-BR" /> <locale android:name="pt-BR" />
<locale android:name="ru"/> <locale android:name="ru"/>
<locale android:name="ro-RO"/> <locale android:name="ro-RO"/>

View file

@ -126,4 +126,10 @@
app:defaultValue="" app:defaultValue=""
app:summary="@string/preferred_format_id_summary" app:summary="@string/preferred_format_id_summary"
app:title="@string/preferred_format_id" /> app:title="@string/preferred_format_id" />
<EditTextPreference
app:key="format_id_audio"
app:defaultValue=""
app:summary="@string/preferred_format_id_summary"
app:title="@string/preferred_format_id" />
</PreferenceScreen> </PreferenceScreen>