LibreTube/app/src/main/java/com/github/libretube/DownloadDialog.kt

108 lines
5.0 KiB
Kotlin
Raw Normal View History

2022-03-04 21:27:10 +05:30
package com.github.libretube
import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.util.Log
2022-05-20 15:50:36 +05:30
import android.util.TypedValue
2022-03-04 21:27:10 +05:30
import android.view.View
import android.widget.*
2022-05-20 15:50:36 +05:30
import androidx.core.text.HtmlCompat
2022-03-04 21:27:10 +05:30
import androidx.fragment.app.DialogFragment
2022-05-20 15:50:36 +05:30
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2022-03-04 21:27:10 +05:30
class DownloadDialog : DialogFragment() {
private val TAG = "DownloadDialog"
var vidName = arrayListOf<String>()
var vidUrl = arrayListOf<String>()
var audioName = arrayListOf<String>()
var audioUrl = arrayListOf<String>()
var selectedVideo = 0
var selectedAudio = 0
var extension = ".mkv"
var duration = 0
private lateinit var videoId: String
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
vidName = arguments?.getStringArrayList("videoName") as ArrayList<String>
vidUrl = arguments?.getStringArrayList("videoUrl") as ArrayList<String>
audioName = arguments?.getStringArrayList("audioName") as ArrayList<String>
audioUrl = arguments?.getStringArrayList("audioUrl") as ArrayList<String>
duration = arguments?.getInt("duration")!!
videoId = arguments?.getString("videoId")!!
2022-05-20 15:50:36 +05:30
val builder = MaterialAlertDialogBuilder(it)
2022-03-04 21:27:10 +05:30
// 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)
2022-05-20 03:52:10 +05:30
val videoArrayAdapter = ArrayAdapter<String>(requireContext(), android.R.layout.simple_spinner_item, vidName)
2022-03-04 21:27:10 +05:30
videoArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
2022-05-20 03:52:10 +05:30
videoSpinner.adapter = videoArrayAdapter
2022-03-04 21:27:10 +05:30
videoSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>,
view: View,
position: Int,
id: Long
) {
selectedVideo = position
2022-05-20 03:52:10 +05:30
Log.d(TAG, selectedVideo.toString())
2022-03-04 21:27:10 +05:30
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
val audioSpinner = view.findViewById<Spinner>(R.id.audio_spinner)
2022-05-20 03:52:10 +05:30
val audioArrayAdapter = ArrayAdapter<String>(requireContext(), android.R.layout.simple_spinner_item, audioName)
2022-03-04 21:27:10 +05:30
audioArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
audioSpinner.adapter = audioArrayAdapter
audioSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>,
view: View,
position: Int,
id: Long
) {
selectedAudio = position
2022-05-20 03:52:10 +05:30
Log.d(TAG, selectedAudio.toString())
2022-03-04 21:27:10 +05:30
}
override fun onNothingSelected(parent: AdapterView<*>?) {}
}
val radioGroup = view.findViewById<RadioGroup>(R.id.radioGp)
radioGroup.setOnCheckedChangeListener { group, checkedId ->
val radio: RadioButton = view.findViewById(checkedId)
extension = radio.text.toString()
2022-05-20 03:52:10 +05:30
Log.d(TAG, extension)
2022-03-04 21:27:10 +05:30
}
view.findViewById<Button>(R.id.download).setOnClickListener {
2022-05-20 03:52:10 +05:30
val intent = Intent(context, DownloadService::class.java)
intent.putExtra("videoId", videoId)
intent.putExtra("videoUrl", vidUrl[selectedVideo])
intent.putExtra("audioUrl", audioUrl[selectedAudio])
intent.putExtra("duration", duration)
intent.putExtra("extension", extension)
// intent.putExtra("command","-y -i ${response.videoStreams[which].url} -i ${response.audioStreams!![0].url} -c copy ${Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)}/${videoId}.mkv")
2022-03-04 21:27:10 +05:30
context?.startService(intent)
dismiss()
}
2022-05-20 15:50:36 +05:30
val typedValue = TypedValue()
this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true)
val hexColor = String.format("#%06X", (0xFFFFFF and typedValue.data))
val appName = HtmlCompat.fromHtml(
"Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT
)
view.findViewById<TextView>(R.id.title).text = appName
2022-03-04 21:27:10 +05:30
builder.setView(view)
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
override fun onDestroy() {
vidName.clear()
vidUrl.clear()
audioUrl.clear()
audioName.clear()
super.onDestroy()
}
}