2022-06-03 00:40:16 +05:30
|
|
|
package com.github.libretube.dialogs
|
2022-03-04 21:27:10 +05:30
|
|
|
|
2022-06-03 02:02:23 +05:30
|
|
|
import android.Manifest
|
2022-03-04 21:27:10 +05:30
|
|
|
import android.app.Dialog
|
|
|
|
import android.content.Intent
|
2022-06-03 02:02:23 +05:30
|
|
|
import android.content.pm.PackageManager
|
|
|
|
import android.os.Build
|
2022-03-04 21:27:10 +05:30
|
|
|
import android.os.Bundle
|
2022-06-03 02:02:23 +05:30
|
|
|
import android.os.Environment
|
2022-03-04 21:27:10 +05:30
|
|
|
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
|
2022-05-21 13:32:04 +05:30
|
|
|
import android.widget.AdapterView
|
|
|
|
import android.widget.ArrayAdapter
|
|
|
|
import android.widget.Button
|
|
|
|
import android.widget.Spinner
|
|
|
|
import android.widget.TextView
|
2022-06-03 02:02:23 +05:30
|
|
|
import androidx.core.app.ActivityCompat
|
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-06-03 00:40:16 +05:30
|
|
|
import com.github.libretube.DownloadService
|
2022-06-03 02:02:23 +05:30
|
|
|
import com.github.libretube.MainActivity
|
2022-06-03 00:40:16 +05:30
|
|
|
import com.github.libretube.R
|
2022-06-03 02:02:23 +05:30
|
|
|
import com.github.libretube.obj.Streams
|
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"
|
2022-06-03 02:02:23 +05:30
|
|
|
var streams: Streams = Streams()
|
2022-03-04 21:27:10 +05:30
|
|
|
var selectedVideo = 0
|
|
|
|
var selectedAudio = 0
|
|
|
|
var duration = 0
|
|
|
|
private lateinit var videoId: String
|
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
|
|
|
return activity?.let {
|
2022-06-03 02:02:23 +05:30
|
|
|
streams = arguments?.getParcelable("streams")!!
|
|
|
|
videoId = arguments?.getString("video_id")!!
|
|
|
|
|
|
|
|
val mainActivity = activity as MainActivity
|
|
|
|
Log.e(TAG, "download button clicked!")
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
|
|
Log.d("myz", "" + Build.VERSION.SDK_INT)
|
|
|
|
if (!Environment.isExternalStorageManager()) {
|
|
|
|
ActivityCompat.requestPermissions(
|
|
|
|
mainActivity,
|
|
|
|
arrayOf(
|
|
|
|
Manifest.permission.READ_EXTERNAL_STORAGE,
|
|
|
|
Manifest.permission.MANAGE_EXTERNAL_STORAGE
|
|
|
|
),
|
|
|
|
1
|
|
|
|
) // permission request code is just an int
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ActivityCompat.checkSelfPermission(
|
|
|
|
requireContext(),
|
|
|
|
Manifest.permission.READ_EXTERNAL_STORAGE
|
|
|
|
) != PackageManager.PERMISSION_GRANTED ||
|
|
|
|
ActivityCompat.checkSelfPermission(
|
|
|
|
requireContext(),
|
|
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
|
|
|
) != PackageManager.PERMISSION_GRANTED
|
|
|
|
) {
|
|
|
|
ActivityCompat.requestPermissions(
|
|
|
|
mainActivity,
|
|
|
|
arrayOf(
|
|
|
|
Manifest.permission.READ_EXTERNAL_STORAGE,
|
|
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
|
|
|
),
|
|
|
|
1
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var vidName = arrayListOf<String>()
|
2022-06-17 20:09:05 +05:30
|
|
|
vidName.add(getString(R.string.no_video))
|
2022-06-03 02:02:23 +05:30
|
|
|
var vidUrl = arrayListOf<String>()
|
|
|
|
vidUrl.add("")
|
2022-06-03 22:21:27 +05:30
|
|
|
for (vid in streams.videoStreams!!) {
|
2022-06-03 02:02:23 +05:30
|
|
|
val name = vid.quality + " " + vid.format
|
|
|
|
vidName.add(name)
|
|
|
|
vidUrl.add(vid.url!!)
|
|
|
|
}
|
|
|
|
var audioName = arrayListOf<String>()
|
2022-06-17 20:09:05 +05:30
|
|
|
audioName.add(getString(R.string.no_audio))
|
2022-06-03 02:02:23 +05:30
|
|
|
var audioUrl = arrayListOf<String>()
|
|
|
|
audioUrl.add("")
|
2022-06-03 22:21:27 +05:30
|
|
|
for (audio in streams.audioStreams!!) {
|
2022-06-03 02:02:23 +05:30
|
|
|
val name = audio.quality + " " + audio.format
|
|
|
|
audioName.add(name)
|
|
|
|
audioUrl.add(audio.url!!)
|
|
|
|
}
|
|
|
|
|
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-21 13:32:04 +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
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
|
2022-03-04 21:27:10 +05:30
|
|
|
override fun onNothingSelected(parent: AdapterView<*>?) {}
|
|
|
|
}
|
|
|
|
val audioSpinner = view.findViewById<Spinner>(R.id.audio_spinner)
|
2022-05-21 13:32:04 +05:30
|
|
|
val audioArrayAdapter = ArrayAdapter(
|
|
|
|
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
|
|
|
}
|
2022-05-21 13:32:04 +05:30
|
|
|
|
2022-03-04 21:27:10 +05:30
|
|
|
override fun onNothingSelected(parent: AdapterView<*>?) {}
|
|
|
|
}
|
2022-06-07 16:12:59 +05:30
|
|
|
|
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("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")
|
|
|
|
}
|
|
|
|
}
|