move download action from player fragment to download dialog

This commit is contained in:
Bnyro 2022-06-02 22:32:23 +02:00
parent 73276894f0
commit e802ea118b
3 changed files with 131 additions and 79 deletions

View File

@ -1,8 +1,12 @@
package com.github.libretube.dialogs
import android.Manifest
import android.app.Dialog
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.util.TypedValue
import android.view.View
@ -13,14 +17,18 @@ import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Spinner
import android.widget.TextView
import androidx.core.app.ActivityCompat
import androidx.core.text.HtmlCompat
import androidx.fragment.app.DialogFragment
import com.github.libretube.DownloadService
import com.github.libretube.MainActivity
import com.github.libretube.R
import com.github.libretube.obj.Streams
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class DownloadDialog : DialogFragment() {
private val TAG = "DownloadDialog"
var streams: Streams = Streams()
var vidName = arrayListOf<String>()
var vidUrl = arrayListOf<String>()
var audioName = arrayListOf<String>()
@ -32,12 +40,62 @@ class DownloadDialog : DialogFragment() {
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")!!
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>()
vidName.add("No video")
var vidUrl = arrayListOf<String>()
vidUrl.add("")
for (vid in streams?.videoStreams!!) {
val name = vid.quality + " " + vid.format
vidName.add(name)
vidUrl.add(vid.url!!)
}
var audioName = arrayListOf<String>()
audioName.add("No audio")
var audioUrl = arrayListOf<String>()
audioUrl.add("")
for (audio in streams?.audioStreams!!) {
val name = audio.quality + " " + audio.format
audioName.add(name)
audioUrl.add(audio.url!!)
}
val builder = MaterialAlertDialogBuilder(it)
// Get the layout inflater
val inflater = requireActivity().layoutInflater
@ -115,12 +173,4 @@ class DownloadDialog : DialogFragment() {
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
override fun onDestroy() {
vidName.clear()
vidUrl.clear()
audioUrl.clear()
audioName.clear()
super.onDestroy()
}
}

View File

@ -1,19 +1,16 @@
package com.github.libretube.fragments
import android.Manifest
import android.annotation.SuppressLint
import android.app.NotificationManager
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.graphics.Rect
import android.net.Uri
import android.os.Build
import android.os.Build.VERSION.SDK_INT
import android.os.Bundle
import android.os.Environment
import android.support.v4.media.session.MediaSessionCompat
import android.text.Html
import android.text.TextUtils
@ -34,7 +31,6 @@ import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.app.ActivityCompat
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
@ -85,11 +81,11 @@ import com.google.android.exoplayer2.util.RepeatModeUtil
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.squareup.picasso.Picasso
import org.chromium.net.CronetEngine
import retrofit2.HttpException
import java.io.IOException
import java.util.concurrent.Executors
import kotlin.math.abs
import org.chromium.net.CronetEngine
import retrofit2.HttpException
var isFullScreen = false
@ -729,66 +725,10 @@ class PlayerFragment : Fragment() {
// download clicked
relDownloadVideo.setOnClickListener {
if (!IS_DOWNLOAD_RUNNING) {
val mainActivity = activity as MainActivity
Log.e(TAG, "download button clicked!")
if (SDK_INT >= Build.VERSION_CODES.R) {
Log.d("myz", "" + 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>()
vidName.add("No video")
var vidUrl = arrayListOf<String>()
vidUrl.add("")
for (vid in response.videoStreams) {
val name = vid.quality + " " + vid.format
vidName.add(name)
vidUrl.add(vid.url!!)
}
var audioName = arrayListOf<String>()
audioName.add("No audio")
var audioUrl = arrayListOf<String>()
audioUrl.add("")
for (audio in response.audioStreams!!) {
val name = audio.quality + " " + audio.format
audioName.add(name)
audioUrl.add(audio.url!!)
}
val newFragment = DownloadDialog()
var bundle = Bundle()
bundle.putStringArrayList("videoName", vidName)
bundle.putStringArrayList("videoUrl", vidUrl)
bundle.putStringArrayList("audioName", audioName)
bundle.putStringArrayList("audioUrl", audioUrl)
bundle.putString("videoId", videoId)
bundle.putInt("duration", response.duration)
bundle.putString("video_id", videoId)
bundle.putParcelable("streams", response)
newFragment.arguments = bundle
newFragment.show(childFragmentManager, "Download")
} else {

View File

@ -1,5 +1,7 @@
package com.github.libretube.obj
import android.os.Parcel
import android.os.Parcelable
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
@JsonIgnoreProperties(ignoreUnknown = true)
@ -26,9 +28,69 @@ data class Streams(
val livestream: Boolean?,
val proxyUrl: String?,
val chapters: List<ChapterSegment>?
) {
) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readString(),
parcel.readValue(Boolean::class.java.classLoader) as? Boolean,
parcel.readValue(Int::class.java.classLoader) as? Int,
parcel.readValue(Long::class.java.classLoader) as? Long,
parcel.readValue(Long::class.java.classLoader) as? Long,
parcel.readValue(Int::class.java.classLoader) as? Int,
TODO("audioStreams"),
TODO("videoStreams"),
TODO("relatedStreams"),
TODO("subtitles"),
parcel.readValue(Boolean::class.java.classLoader) as? Boolean,
parcel.readString(),
TODO("chapters")
) {
}
constructor() : this(
"", "", "", "", "", "", "", "", "", "", null, -1, -1, -1, -1, emptyList(), emptyList(),
emptyList(), emptyList(), null, "", emptyList()
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(title)
parcel.writeString(description)
parcel.writeString(uploadDate)
parcel.writeString(uploader)
parcel.writeString(uploaderUrl)
parcel.writeString(uploaderAvatar)
parcel.writeString(thumbnailUrl)
parcel.writeString(hls)
parcel.writeString(dash)
parcel.writeString(lbryId)
parcel.writeValue(uploaderVerified)
parcel.writeValue(duration)
parcel.writeValue(views)
parcel.writeValue(likes)
parcel.writeValue(dislikes)
parcel.writeValue(livestream)
parcel.writeString(proxyUrl)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<Streams> {
override fun createFromParcel(parcel: Parcel): Streams {
return Streams(parcel)
}
override fun newArray(size: Int): Array<Streams?> {
return arrayOfNulls(size)
}
}
}