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

147 lines
5.7 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-05-21 13:32:04 +05:30
import android.widget.ArrayAdapter
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
import com.github.libretube.MainActivity
2022-06-03 00:40:16 +05:30
import com.github.libretube.R
2022-07-01 13:49:00 +05:30
import com.github.libretube.databinding.DialogDownloadBinding
import com.github.libretube.obj.Streams
2022-06-28 20:02:26 +05:30
import com.github.libretube.services.DownloadService
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-07-01 13:49:00 +05:30
private lateinit var binding: DialogDownloadBinding
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)
2022-07-01 13:49:00 +05:30
binding = DialogDownloadBinding.inflate(layoutInflater)
2022-06-17 20:33:17 +05:30
// 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-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-07-01 13:49:00 +05:30
binding.videoSpinner.adapter = videoArrayAdapter
binding.videoSpinner.setSelection(1)
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)
2022-07-01 13:49:00 +05:30
binding.audioSpinner.adapter = audioArrayAdapter
binding.audioSpinner.setSelection(1)
2022-06-07 16:12:59 +05:30
2022-07-01 13:49:00 +05:30
binding.download.setOnClickListener {
val selectedAudioUrl = audioUrl[binding.audioSpinner.selectedItemPosition]
val selectedVideoUrl = vidUrl[binding.videoSpinner.selectedItemPosition]
2022-06-17 20:33:17 +05:30
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
)
2022-07-01 13:49:00 +05:30
binding.title.text = appName
2022-05-20 15:50:36 +05:30
2022-07-01 13:49:00 +05:30
builder.setView(binding.root)
2022-03-04 21:27:10 +05:30
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}