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

153 lines
6.0 KiB
Kotlin
Raw Normal View History

2022-06-03 00:40:16 +05:30
package com.github.libretube.dialogs
2022-03-04 21:27:10 +05:30
import android.Manifest
2022-03-04 21:27:10 +05:30
import android.app.Dialog
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
2022-03-04 21:27:10 +05:30
import android.os.Bundle
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.ArrayAdapter
import android.widget.Button
import android.widget.Spinner
import android.widget.TextView
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
import com.github.libretube.MainActivity
2022-06-03 00:40:16 +05:30
import com.github.libretube.R
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-17 20:33:17 +05:30
private lateinit var streams: Streams
2022-03-04 21:27:10 +05:30
private lateinit var videoId: String
2022-06-17 20:33:17 +05:30
private var duration = 0
2022-03-04 21:27:10 +05:30
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
streams = arguments?.getParcelable("streams")!!
videoId = arguments?.getString("video_id")!!
val mainActivity = activity as MainActivity
2022-06-17 20:33:17 +05:30
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) {
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
)
}
}
2022-06-17 20:33:17 +05:30
var vidName = arrayListOf<String>()
var vidUrl = arrayListOf<String>()
2022-06-17 20:33:17 +05:30
// add empty selection
vidName.add(getString(R.string.no_video))
vidUrl.add("")
2022-06-17 20:33:17 +05:30
// add all available video streams
2022-06-03 22:21:27 +05:30
for (vid in streams.videoStreams!!) {
val name = vid.quality + " " + vid.format
vidName.add(name)
vidUrl.add(vid.url!!)
}
2022-06-17 20:33:17 +05:30
var audioName = arrayListOf<String>()
var audioUrl = arrayListOf<String>()
2022-06-17 20:33:17 +05:30
// add empty selection
audioName.add(getString(R.string.no_audio))
audioUrl.add("")
2022-06-17 20:33:17 +05:30
// add all available audio streams
2022-06-03 22:21:27 +05:30
for (audio in streams.audioStreams!!) {
val name = audio.quality + " " + audio.format
audioName.add(name)
audioUrl.add(audio.url!!)
}
2022-03-04 21:27:10 +05:30
val videoSpinner = view.findViewById<Spinner>(R.id.video_spinner)
2022-06-17 20:33:17 +05:30
val videoArrayAdapter = ArrayAdapter(
2022-05-21 13:32:04 +05:30
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-06-17 20:33:17 +05:30
videoSpinner.setSelection(1)
2022-05-21 13:32:04 +05:30
2022-03-04 21:27:10 +05:30
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
2022-06-17 20:33:17 +05:30
audioSpinner.setSelection(1)
2022-06-07 16:12:59 +05:30
2022-03-04 21:27:10 +05:30
view.findViewById<Button>(R.id.download).setOnClickListener {
2022-06-17 20:33:17 +05:30
val selectedAudioUrl = audioUrl[audioSpinner.selectedItemPosition]
val selectedVideoUrl = vidUrl[videoSpinner.selectedItemPosition]
2022-05-20 03:52:10 +05:30
val intent = Intent(context, DownloadService::class.java)
intent.putExtra("videoId", videoId)
2022-06-17 20:33:17 +05:30
intent.putExtra("videoUrl", selectedVideoUrl)
intent.putExtra("audioUrl", selectedAudioUrl)
2022-05-20 03:52:10 +05:30
intent.putExtra("duration", duration)
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")
}
}