From 572aa6de3e7690ef39f5579268d462bd6c6af5d6 Mon Sep 17 00:00:00 2001 From: reduplicated <110570621+reduplicated@users.noreply.github.com> Date: Fri, 23 Sep 2022 22:05:06 +0200 Subject: [PATCH] shit not working due to android storage fuckery --- app/build.gradle | 2 +- .../lagradost/cloudstream3/CommonActivity.kt | 4 +- .../cloudstream3/ui/DownloadButton.kt | 17 +++- .../cloudstream3/ui/result/DownloadHelper.kt | 51 +++++----- .../cloudstream3/ui/result/ResultFragment.kt | 24 ++++- .../ui/result/ResultViewModel2.kt | 95 +++++++++++-------- .../utils/VideoDownloadManager.kt | 6 +- app/src/main/res/layout/download_button.xml | 9 -- .../res/layout/download_button_layout.xml | 6 +- app/src/main/res/layout/fragment_result.xml | 8 +- .../main/res/layout/fragment_result_tv.xml | 4 +- app/src/main/res/layout/result_episode.xml | 1 + .../main/res/layout/result_episode_large.xml | 1 + app/src/main/res/values/styles.xml | 1 + 14 files changed, 134 insertions(+), 95 deletions(-) delete mode 100644 app/src/main/res/layout/download_button.xml diff --git a/app/build.gradle b/app/build.gradle index 5bf72d99..5e34174b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -201,7 +201,7 @@ dependencies { implementation 'me.xdrop:fuzzywuzzy:1.4.0' // aria2c downloader - implementation 'com.github.LagradOst:Aria2cButton:v0.0.3' + implementation 'com.github.LagradOst:Aria2cButton:v0.0.4' } task androidSourcesJar(type: Jar) { diff --git a/app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt index dd3942f9..6281dbe1 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt @@ -137,9 +137,9 @@ object CommonActivity { Aria2Starter.start( act, Aria2Settings( - UUID.randomUUID().toString(), + "1337", //UUID.randomUUID().toString() 4337, - act.filesDir.path, + "/storage/emulated/0/Download",// act.filesDir.path, "${act.filesDir.path}/session" ) ) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/DownloadButton.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/DownloadButton.kt index b026eb9d..ce9969c5 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/DownloadButton.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/DownloadButton.kt @@ -4,6 +4,7 @@ import android.content.Context import android.util.AttributeSet import android.widget.TextView import androidx.core.view.isVisible +import com.google.android.material.button.MaterialButton import com.lagradost.cloudstream3.R import com.lagradost.fetchbutton.aria2c.DownloadStatusTell import com.lagradost.fetchbutton.aria2c.Metadata @@ -14,10 +15,22 @@ class DownloadButton(context: Context, attributeSet: AttributeSet) : var progressText: TextView? = null var mainText: TextView? = null - override fun onAttachedToWindow() { - super.onAttachedToWindow() + var bigButton: MaterialButton? = null + + override fun onInflate() { + overrideLayout = R.layout.download_button_layout + super.onInflate() progressText = findViewById(R.id.result_movie_download_text_precentage) mainText = findViewById(R.id.result_movie_download_text) + bigButton = findViewById(R.id.download_big_button) + } + + override fun setOnClickListener(l: OnClickListener?) { + bigButton?.setOnClickListener(l) + } + + override fun setOnLongClickListener(l: OnLongClickListener?) { + bigButton?.setOnLongClickListener(l) } override fun setStatus(status: DownloadStatusTell?) { diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/DownloadHelper.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/DownloadHelper.kt index 067874b1..b407bdec 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/DownloadHelper.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/DownloadHelper.kt @@ -1,12 +1,17 @@ package com.lagradost.cloudstream3.ui.result +import android.app.Activity import android.net.Uri +import android.widget.Toast +import com.lagradost.cloudstream3.CommonActivity.showToast import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.ui.download.DOWNLOAD_ACTION_DOWNLOAD +import com.lagradost.cloudstream3.ui.download.DOWNLOAD_ACTION_LONG_CLICK import com.lagradost.cloudstream3.ui.download.DownloadEpisodeClickEvent import com.lagradost.cloudstream3.ui.player.DownloadFileGenerator import com.lagradost.cloudstream3.utils.ExtractorUri import com.lagradost.cloudstream3.utils.UIHelper.popupMenuNoIcons +import com.lagradost.fetchbutton.aria2c.Aria2Starter import com.lagradost.fetchbutton.aria2c.DownloadStatusTell import com.lagradost.fetchbutton.ui.PieFetchButton @@ -39,9 +44,17 @@ object DownloadHelper { downloadClickCallback: (DownloadEpisodeClickEvent) -> Unit ) { setPersistentId(card.id.toLong()) + val play = if (card.episode <= 0) R.string.play_movie_button else R.string.play_episode - setOnClickListener { view -> - if (view !is PieFetchButton) return@setOnClickListener + setOnLongClickListener { //Aria2Starter.saveActivity.get() + downloadClickCallback.invoke(DownloadEpisodeClickEvent(DOWNLOAD_ACTION_LONG_CLICK, card)) + //showToast(it.context as? Activity, R.string.download, Toast.LENGTH_SHORT) + return@setOnLongClickListener true + } + + setOnClickListener { + val view = this + //if (view !is PieFetchButton) return@setOnClickListener when (view.currentStatus) { null, DownloadStatusTell.Removed -> { view.setStatus(DownloadStatusTell.Waiting) @@ -56,7 +69,7 @@ object DownloadHelper { view.popupMenuNoIcons( listOf( 1 to R.string.resume, - 2 to R.string.play_episode, + 2 to play, 3 to R.string.delete ) ) { @@ -69,29 +82,21 @@ object DownloadHelper { ) ) } - 2 -> { - play(card) - } - 3 -> { - view.deleteAllFiles() - } + 2 -> play(card) + 3 -> view.deleteAllFiles() } } } DownloadStatusTell.Complete -> { view.popupMenuNoIcons( listOf( - 2 to R.string.play_episode, + 2 to play, 3 to R.string.delete ) ) { when (itemId) { - 2 -> { - play(card) - } - 3 -> { - view.deleteAllFiles() - } + 2 -> play(card) + 3 -> view.deleteAllFiles() } } } @@ -99,20 +104,14 @@ object DownloadHelper { view.popupMenuNoIcons( listOf( 4 to R.string.pause, - 2 to R.string.play_episode, + 2 to play, 3 to R.string.delete ) ) { when (itemId) { - 4 -> { - view.pauseDownload() - } - 2 -> { - play(card) - } - 3 -> { - view.deleteAllFiles() - } + 4 -> view.pauseDownload() + 2 -> play(card) + 3 -> view.deleteAllFiles() } } } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt index e0f76509..552b3762 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultFragment.kt @@ -34,28 +34,32 @@ import com.lagradost.cloudstream3.TvType import com.lagradost.cloudstream3.mvvm.* import com.lagradost.cloudstream3.syncproviders.providers.Kitsu import com.lagradost.cloudstream3.ui.WatchType +import com.lagradost.cloudstream3.ui.download.DOWNLOAD_ACTION_DOWNLOAD +import com.lagradost.cloudstream3.ui.download.DOWNLOAD_ACTION_LONG_CLICK import com.lagradost.cloudstream3.ui.quicksearch.QuickSearchFragment import com.lagradost.cloudstream3.ui.result.DownloadHelper.setUp import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTrueTvSettings import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings -import com.lagradost.cloudstream3.utils.* import com.lagradost.cloudstream3.utils.AppUtils.getNameFull import com.lagradost.cloudstream3.utils.AppUtils.html import com.lagradost.cloudstream3.utils.AppUtils.loadCache import com.lagradost.cloudstream3.utils.AppUtils.openBrowser +import com.lagradost.cloudstream3.utils.DataStoreHelper import com.lagradost.cloudstream3.utils.DataStoreHelper.getViewPos +import com.lagradost.cloudstream3.utils.ExtractorLink import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog +import com.lagradost.cloudstream3.utils.UIHelper import com.lagradost.cloudstream3.utils.UIHelper.colorFromAttribute import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe import com.lagradost.cloudstream3.utils.UIHelper.fixPaddingStatusbar import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard -import kotlinx.android.synthetic.main.download_button.* import kotlinx.android.synthetic.main.fragment_result.* import kotlinx.android.synthetic.main.fragment_result.result_cast_items import kotlinx.android.synthetic.main.fragment_result.result_cast_text import kotlinx.android.synthetic.main.fragment_result.result_coming_soon import kotlinx.android.synthetic.main.fragment_result.result_data_holder import kotlinx.android.synthetic.main.fragment_result.result_description +import kotlinx.android.synthetic.main.fragment_result.result_download_movie import kotlinx.android.synthetic.main.fragment_result.result_episode_loading import kotlinx.android.synthetic.main.fragment_result.result_episodes import kotlinx.android.synthetic.main.fragment_result.result_error_text @@ -334,7 +338,13 @@ open class ResultFragment : ResultTrailerPlayer() { result_download_movie?.setUp(ep) { - viewModel.download(it.data) + when (it.action) { + DOWNLOAD_ACTION_DOWNLOAD -> viewModel.download(activity, it.data) + DOWNLOAD_ACTION_LONG_CLICK -> viewModel.handleAction( + activity, + EpisodeClickEvent(ACTION_DOWNLOAD_MIRROR, ep) + ) + } } result_download_movie?.isVisible = true /*main { @@ -509,7 +519,13 @@ open class ResultFragment : ResultTrailerPlayer() { viewModel.handleAction(activity, episodeClick) }, { clickEvent -> - viewModel.download(clickEvent.data) + when (clickEvent.action) { + DOWNLOAD_ACTION_DOWNLOAD -> viewModel.download(activity, clickEvent.data) + DOWNLOAD_ACTION_LONG_CLICK -> viewModel.handleAction( + activity, + EpisodeClickEvent(ACTION_DOWNLOAD_MIRROR, clickEvent.data) + ) + } //handleDownloadClick(activity, downloadClickEvent) } ) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel2.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel2.kt index 51caaf79..a5917a9f 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel2.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultViewModel2.kt @@ -56,10 +56,12 @@ import com.lagradost.cloudstream3.utils.UIHelper.colorFromAttribute import com.lagradost.cloudstream3.utils.UIHelper.navigate import com.lagradost.cloudstream3.utils.UIHelper.requestRW import com.lagradost.cloudstream3.utils.VideoDownloadManager.getBasePath +import com.lagradost.cloudstream3.utils.VideoDownloadManager.gotoDir import com.lagradost.fetchbutton.NotificationMetaData import com.lagradost.fetchbutton.aria2c.Aria2Starter import com.lagradost.fetchbutton.aria2c.UriRequest import com.lagradost.fetchbutton.aria2c.newUriRequest +import com.lagradost.fetchbutton.utils.Coroutines.mainThread import kotlinx.coroutines.* import java.io.File import java.lang.Math.abs @@ -534,24 +536,16 @@ class ResultViewModel2 : ViewModel() { } private fun downloadSubtitle( - context: Context?, link: ExtractorSubtitleLink, fileName: String, folder: String ) { - ioSafe { - VideoDownloadManager.downloadThing( - context ?: return@ioSafe, - link, - "$fileName ${link.name}", - folder, - if (link.url.contains(".srt")) ".srt" else "vtt", - false, - null - ) { - // no notification - } - } + Aria2Starter.download( + newUriRequest( + null, link.url, fileName, folder, link.headers, + USER_AGENT + ) + ) } private fun getFolder(currentType: TvType, titleName: String): String { @@ -581,7 +575,6 @@ class ResultViewModel2 : ViewModel() { val fileName = VideoDownloadManager.getFileName(ctx, meta) val folder = getFolder(meta.type ?: return, meta.mainName) downloadSubtitle( - ctx, ExtractorSubtitleLink(link.name, link.url, ""), fileName, folder @@ -611,10 +604,12 @@ class ResultViewModel2 : ViewModel() { currentType ) - val topFolder = AcraApplication.context?.getBasePath()?.first?.filePath + val topFolder = VideoDownloadManager.getDownloadDir()?.filePath ?: throw RuntimeException("FUCK YOU")//AcraApplication.context?.getBasePath()?.first //?.second?.also { println("URIIIIII: $it") } ?: throw RuntimeException("FUCK YOU") + //?: VideoDownloadManager.getDownloadDir()?.filePath ?: return null - val folder = - topFolder + "/" + getFolder(currentType, currentHeaderName).replace(".", "") + val folder = topFolder//topFolder?.gotoDir(getFolder(currentType, currentHeaderName).replace(".", ""), true)?.uri?.toString() ?: throw RuntimeException("FUCK YOU") + //val folder = + // topFolder + "/" + getFolder(currentType, currentHeaderName).replace(".", "") //val src = "$DOWNLOAD_NAVIGATE_TO/$parentId" // url ?: return@let // SET VISUAL KEYS @@ -667,8 +662,7 @@ class ResultViewModel2 : ViewModel() { contentTitle = currentHeaderName, secondRow = rowTwo, subText = null, - linkName = currentHeaderName, - rowTwoExtra = null + linkName = null, ) } @@ -680,9 +674,16 @@ class ResultViewModel2 : ViewModel() { VideoDownloadManager.getFileName( AcraApplication.context ?: return null, meta - ), ".mp4" - ), folder, link.headers, USER_AGENT, - notificationMetaData = notification + ), "mp4" + ), null, + link.headers, USER_AGENT, + notificationMetaData = notification?.copy( + linkName = "${link.name} ${ + Qualities.getStringByInt( + link.quality + ) + }" + ) ) } @@ -694,17 +695,24 @@ class ResultViewModel2 : ViewModel() { true ) ) - }.distinctBy { it.url } - //.map { ExtractorSubtitleLink(it.name, it.url, "") } - .map { link -> - val fileName = VideoDownloadManager.getFileName( - AcraApplication.context ?: return null, - meta - ) + ".vtt" + }.distinctBy { it.url }.groupBy { link -> + SubtitleHelper.fromLanguageToTwoLetters( + link.name, + true + ) + }.map { + it.value.map { link -> + val fileName = VideoDownloadManager.getDisplayName( + VideoDownloadManager.getFileName( + AcraApplication.context ?: return null, + meta + ), "vtt" + ) - newUriRequest(0, link.url, fileName, folder, link.headers, USER_AGENT) + newUriRequest(null, link.url, fileName, folder, link.headers, USER_AGENT) //downloadSubtitle(context, link, fileName, folder) } + } return DownloadRequest(linkRequests, downloadSubsList) @@ -740,7 +748,7 @@ class ResultViewModel2 : ViewModel() { data class DownloadRequest( val links: List, - val subs: List, + val subs: List>, ) @@ -1093,16 +1101,24 @@ class ResultViewModel2 : ViewModel() { handleEpisodeClickEvent(activity, click) } - private fun downloadFromRequest(req: DownloadRequest) { + private fun downloadFromRequest(activity: Activity?, req: DownloadRequest) { Aria2Starter.download(req.links) - for (sub in req.subs.take(5)) { + for (sub in req.subs.take(4)) { // download max 4 langs to not block real download Aria2Starter.download(sub) } + val linksFound = req.links.isNotEmpty() + mainThread { + showToast( + activity, + if (linksFound) R.string.download_started else R.string.no_links_found_toast, + Toast.LENGTH_SHORT + ) + } } - fun download(card: ResultEpisode) = ioSafe { + fun download(activity: Activity?, card: ResultEpisode) = ioSafe { getRequest(card)?.let { req -> - downloadFromRequest(req) + downloadFromRequest(activity, req) } } @@ -1242,13 +1258,8 @@ class ResultViewModel2 : ViewModel() { listOf(result.links[index]), result.subs, ) ?: return@ioSafe - downloadFromRequest(req) + downloadFromRequest(activity, req) } - showToast( - activity, - R.string.download_started, - Toast.LENGTH_SHORT - ) } } ACTION_RELOAD_EPISODE -> { diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/VideoDownloadManager.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/VideoDownloadManager.kt index 32cb4b4f..60f5b8a1 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/VideoDownloadManager.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/VideoDownloadManager.kt @@ -426,7 +426,7 @@ object VideoDownloadManager { } private const val reservedChars = "|\\?*<\":>+[]/\'" - fun sanitizeFilename(name: String, removeSpaces: Boolean= false): String { + fun sanitizeFilename(name: String, removeSpaces: Boolean = false): String { var tempName = name for (c in reservedChars) { tempName = tempName.replace(c, ' ') @@ -941,7 +941,7 @@ object VideoDownloadManager { * @param directoryName if null will use the current path. * @return UniFile / null if createMissingDirectories = false and folder is not found. * */ - private fun UniFile.gotoDir( + fun UniFile.gotoDir( directoryName: String?, createMissingDirectories: Boolean = true ): UniFile? { @@ -1612,7 +1612,7 @@ object VideoDownloadManager { .mapIndexed { index, any -> DownloadQueueResumePackage(index, any) } .toTypedArray() setKey(KEY_RESUME_QUEUE_PACKAGES, dQueue) - } catch (t : Throwable) { + } catch (t: Throwable) { logError(t) } } diff --git a/app/src/main/res/layout/download_button.xml b/app/src/main/res/layout/download_button.xml deleted file mode 100644 index 9014102e..00000000 --- a/app/src/main/res/layout/download_button.xml +++ /dev/null @@ -1,9 +0,0 @@ - - \ No newline at end of file diff --git a/app/src/main/res/layout/download_button_layout.xml b/app/src/main/res/layout/download_button_layout.xml index 7f9caa1c..f1ba7fb9 100644 --- a/app/src/main/res/layout/download_button_layout.xml +++ b/app/src/main/res/layout/download_button_layout.xml @@ -9,6 +9,7 @@ + android:layout_width="35dp" + android:layout_height="35dp" /> --> - - +