default selection downloads

This commit is contained in:
Bnyro 2022-06-17 17:03:17 +02:00
parent 9c32112dde
commit 10d15a900a
2 changed files with 32 additions and 42 deletions

View File

@ -152,7 +152,7 @@ class DownloadService : Service() {
if (downloadId == id) { if (downloadId == id) {
if (downloadType == "mux") { if (downloadType == "mux") {
downloadManagerRequest( downloadManagerRequest(
getString(R.string.video), getString(R.string.audio),
getString(R.string.downloading), getString(R.string.downloading),
audioUrl, audioUrl,
audioDir audioDir

View File

@ -10,7 +10,6 @@ import android.os.Environment
import android.util.Log import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.view.View import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import android.widget.Button import android.widget.Button
import android.widget.Spinner import android.widget.Spinner
@ -26,18 +25,23 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
class DownloadDialog : DialogFragment() { class DownloadDialog : DialogFragment() {
private val TAG = "DownloadDialog" private val TAG = "DownloadDialog"
var streams: Streams = Streams()
var selectedVideo = 0 private lateinit var streams: Streams
var selectedAudio = 0
var duration = 0
private lateinit var videoId: String private lateinit var videoId: String
private var duration = 0
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let { return activity?.let {
streams = arguments?.getParcelable("streams")!! streams = arguments?.getParcelable("streams")!!
videoId = arguments?.getString("video_id")!! videoId = arguments?.getString("video_id")!!
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
Log.e(TAG, "download button clicked!") val builder = MaterialAlertDialogBuilder(it)
// Get the layout inflater
val inflater = requireActivity().layoutInflater
var view: View = inflater.inflate(R.layout.dialog_download, null)
// request storage permissions if not granted yet
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Log.d("myz", "" + Build.VERSION.SDK_INT) Log.d("myz", "" + Build.VERSION.SDK_INT)
if (!Environment.isExternalStorageManager()) { if (!Environment.isExternalStorageManager()) {
@ -70,50 +74,46 @@ class DownloadDialog : DialogFragment() {
) )
} }
} }
var vidName = arrayListOf<String>() var vidName = arrayListOf<String>()
vidName.add(getString(R.string.no_video))
var vidUrl = arrayListOf<String>() var vidUrl = arrayListOf<String>()
// add empty selection
vidName.add(getString(R.string.no_video))
vidUrl.add("") vidUrl.add("")
// add all available video streams
for (vid in streams.videoStreams!!) { for (vid in streams.videoStreams!!) {
val name = vid.quality + " " + vid.format val name = vid.quality + " " + vid.format
vidName.add(name) vidName.add(name)
vidUrl.add(vid.url!!) vidUrl.add(vid.url!!)
} }
var audioName = arrayListOf<String>() var audioName = arrayListOf<String>()
audioName.add(getString(R.string.no_audio))
var audioUrl = arrayListOf<String>() var audioUrl = arrayListOf<String>()
// add empty selection
audioName.add(getString(R.string.no_audio))
audioUrl.add("") audioUrl.add("")
// add all available audio streams
for (audio in streams.audioStreams!!) { for (audio in streams.audioStreams!!) {
val name = audio.quality + " " + audio.format val name = audio.quality + " " + audio.format
audioName.add(name) audioName.add(name)
audioUrl.add(audio.url!!) audioUrl.add(audio.url!!)
} }
val builder = MaterialAlertDialogBuilder(it)
// Get the layout inflater
val inflater = requireActivity().layoutInflater
var view: View = inflater.inflate(R.layout.dialog_download, null)
val videoSpinner = view.findViewById<Spinner>(R.id.video_spinner) val videoSpinner = view.findViewById<Spinner>(R.id.video_spinner)
val videoArrayAdapter = ArrayAdapter<String>( val videoArrayAdapter = ArrayAdapter(
requireContext(), requireContext(),
android.R.layout.simple_spinner_item, android.R.layout.simple_spinner_item,
vidName vidName
) )
videoArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) videoArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
videoSpinner.adapter = videoArrayAdapter videoSpinner.adapter = videoArrayAdapter
videoSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { videoSpinner.setSelection(1)
override fun onItemSelected(
parent: AdapterView<*>,
view: View,
position: Int,
id: Long
) {
selectedVideo = position
Log.d(TAG, selectedVideo.toString())
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
val audioSpinner = view.findViewById<Spinner>(R.id.audio_spinner) val audioSpinner = view.findViewById<Spinner>(R.id.audio_spinner)
val audioArrayAdapter = ArrayAdapter( val audioArrayAdapter = ArrayAdapter(
requireContext(), requireContext(),
@ -122,27 +122,17 @@ class DownloadDialog : DialogFragment() {
) )
audioArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) audioArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
audioSpinner.adapter = audioArrayAdapter audioSpinner.adapter = audioArrayAdapter
audioSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { audioSpinner.setSelection(1)
override fun onItemSelected(
parent: AdapterView<*>,
view: View,
position: Int,
id: Long
) {
selectedAudio = position
Log.d(TAG, selectedAudio.toString())
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
view.findViewById<Button>(R.id.download).setOnClickListener { view.findViewById<Button>(R.id.download).setOnClickListener {
val selectedAudioUrl = audioUrl[audioSpinner.selectedItemPosition]
val selectedVideoUrl = vidUrl[videoSpinner.selectedItemPosition]
val intent = Intent(context, DownloadService::class.java) val intent = Intent(context, DownloadService::class.java)
intent.putExtra("videoId", videoId) intent.putExtra("videoId", videoId)
intent.putExtra("videoUrl", vidUrl[selectedVideo]) intent.putExtra("videoUrl", selectedVideoUrl)
intent.putExtra("audioUrl", audioUrl[selectedAudio]) intent.putExtra("audioUrl", selectedAudioUrl)
intent.putExtra("duration", duration) intent.putExtra("duration", duration)
// intent.putExtra("command","-y -i ${response.videoStreams[which].url} -i ${response.audioStreams!![0].url} -c copy ${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)}/${videoId}.mkv")
context?.startService(intent) context?.startService(intent)
dismiss() dismiss()
} }