From e802ea118b9dc14870467b740483d60e2987e524 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 2 Jun 2022 22:32:23 +0200 Subject: [PATCH] move download action from player fragment to download dialog --- .../libretube/dialogs/DownloadDialog.kt | 78 +++++++++++++++---- .../libretube/fragments/PlayerFragment.kt | 68 +--------------- .../java/com/github/libretube/obj/Streams.kt | 64 ++++++++++++++- 3 files changed, 131 insertions(+), 79 deletions(-) diff --git a/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt b/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt index 99bf915a9..9af8c717a 100644 --- a/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt @@ -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() var vidUrl = arrayListOf() var audioName = arrayListOf() @@ -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 - vidUrl = arguments?.getStringArrayList("videoUrl") as ArrayList - audioName = arguments?.getStringArrayList("audioName") as ArrayList - audioUrl = arguments?.getStringArrayList("audioUrl") as ArrayList - 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() + vidName.add("No video") + var vidUrl = arrayListOf() + vidUrl.add("") + for (vid in streams?.videoStreams!!) { + val name = vid.quality + " " + vid.format + vidName.add(name) + vidUrl.add(vid.url!!) + } + var audioName = arrayListOf() + audioName.add("No audio") + var audioUrl = arrayListOf() + 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() - } } diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index b0a529f3f..e1722e48b 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -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() - vidName.add("No video") - var vidUrl = arrayListOf() - vidUrl.add("") - for (vid in response.videoStreams) { - val name = vid.quality + " " + vid.format - vidName.add(name) - vidUrl.add(vid.url!!) - } - var audioName = arrayListOf() - audioName.add("No audio") - var audioUrl = arrayListOf() - 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 { diff --git a/app/src/main/java/com/github/libretube/obj/Streams.kt b/app/src/main/java/com/github/libretube/obj/Streams.kt index 5837b680d..f127ae160 100644 --- a/app/src/main/java/com/github/libretube/obj/Streams.kt +++ b/app/src/main/java/com/github/libretube/obj/Streams.kt @@ -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? -) { +) : 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 { + override fun createFromParcel(parcel: Parcel): Streams { + return Streams(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } }