Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for external download apps #6084

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ object PreferenceKeys {
const val CONFIRM_UNSUBSCRIBE = "confirm_unsubscribing"
const val CLEAR_BOOKMARKS = "clear_bookmarks"
const val MAX_CONCURRENT_DOWNLOADS = "max_parallel_downloads"
const val EXTERNAL_DOWNLOAD_PROVIDER = "external_download_provider"
const val DISABLE_VIDEO_IMAGE_PROXY = "disable_video_image_proxy"
const val CONTRIBUTE_TO_SB = "sb_contribute_key"
const val CONTRIBUTE_TO_DEARROW = "dearrow_contribute_key"
Expand Down
54 changes: 53 additions & 1 deletion app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
import android.content.Intent
import android.os.Build
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentManager
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.obj.DownloadItem
import com.github.libretube.enums.PlaylistType
import com.github.libretube.parcelable.DownloadData
import com.github.libretube.services.DownloadService
import com.github.libretube.ui.dialogs.DownloadDialog
import com.github.libretube.ui.dialogs.DownloadPlaylistDialog
import com.github.libretube.ui.dialogs.ShareDialog
import java.nio.file.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.div
Expand All @@ -17,11 +24,11 @@
const val VIDEO_DIR = "video"
const val AUDIO_DIR = "audio"
const val SUBTITLE_DIR = "subtitle"
const val METADATA_DIR = "metadata"
const val THUMBNAIL_DIR = "thumbnail"
const val DOWNLOAD_CHUNK_SIZE = 8L * 1024
const val DEFAULT_TIMEOUT = 15 * 1000
const val DEFAULT_RETRY = 3
private const val videoMimeType = "video/*"

Check failure on line 31 in app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Property name should use the screaming snake case notation when the value can not be changed Raw Output: app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt:31:23: error: Property name should use the screaming snake case notation when the value can not be changed (standard:property-naming)

fun getDownloadDir(context: Context, path: String): Path {
val storageDir = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Expand Down Expand Up @@ -53,4 +60,49 @@
fun DownloadItem.getNotificationId(): Int {
return Int.MAX_VALUE - id
}

fun startDownloadDialog(context: Context, fragmentManager: FragmentManager, videoId: String) {

Check failure on line 64 in app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected after opening parenthesis Raw Output: app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt:64:29: error: Newline expected after opening parenthesis (standard:function-signature)

Check failure on line 64 in app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt:64:47: error: Parameter should start on a newline (standard:function-signature)

Check failure on line 64 in app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt:64:81: error: Parameter should start on a newline (standard:function-signature)

Check failure on line 64 in app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected before closing parenthesis Raw Output: app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt:64:96: error: Newline expected before closing parenthesis (standard:function-signature)
val externalProviderPackageName =
PreferenceHelper.getString(PreferenceKeys.EXTERNAL_DOWNLOAD_PROVIDER, "")

if (externalProviderPackageName.isBlank()) {
DownloadDialog().apply {
arguments = bundleOf(IntentData.videoId to videoId)
}.show(fragmentManager, DownloadDialog::class.java.name)
} else {
val intent = Intent(Intent.ACTION_VIEW)

Check failure on line 73 in app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt:73:26: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
.setPackage(externalProviderPackageName)
.setDataAndType("${ShareDialog.YOUTUBE_FRONTEND_URL}/watch?v=$videoId".toUri(), videoMimeType)

runCatching { context.startActivity(intent) }
}
}

fun startDownloadPlaylistDialog(
context: Context,
fragmentManager: FragmentManager,
playlistId: String,
playlistName: String,
playlistType: PlaylistType

Check failure on line 86 in app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Missing trailing comma before ")" Raw Output: app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt:86:35: error: Missing trailing comma before ")" (standard:trailing-comma-on-declaration-site)
) {
val externalProviderPackageName =
PreferenceHelper.getString(PreferenceKeys.EXTERNAL_DOWNLOAD_PROVIDER, "")

if (externalProviderPackageName.isBlank()) {
val downloadPlaylistDialog = DownloadPlaylistDialog().apply {

Check failure on line 92 in app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt:92:42: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
arguments = bundleOf(

Check failure on line 93 in app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/helpers/DownloadHelper.kt:93:29: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
IntentData.playlistId to playlistId,
IntentData.playlistName to playlistName,
IntentData.playlistType to playlistType
)
}
downloadPlaylistDialog.show(fragmentManager, null)
} else {
val intent = Intent(Intent.ACTION_VIEW)
.setPackage(externalProviderPackageName)
.setDataAndType("${ShareDialog.YOUTUBE_FRONTEND_URL}/playlist?list=$playlistId".toUri(), videoMimeType)

runCatching { context.startActivity(intent) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import com.github.libretube.extensions.togglePlayPauseState
import com.github.libretube.extensions.updateIfChanged
import com.github.libretube.extensions.updateParameters
import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.DownloadHelper
import com.github.libretube.helpers.ImageHelper
import com.github.libretube.helpers.IntentHelper
import com.github.libretube.helpers.NavBarHelper
Expand All @@ -92,7 +93,6 @@ import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.adapters.VideosAdapter
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.dialogs.AddToPlaylistDialog
import com.github.libretube.ui.dialogs.DownloadDialog
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.ui.extensions.animateDown
import com.github.libretube.ui.extensions.setupSubscriptionButton
Expand Down Expand Up @@ -625,9 +625,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions {
if (streams.duration <= 0) {
Toast.makeText(context, R.string.cannotDownload, Toast.LENGTH_SHORT).show()
} else {
val newFragment = DownloadDialog()
newFragment.arguments = bundleOf(IntentData.videoId to videoId)
newFragment.show(childFragmentManager, DownloadDialog::class.java.name)
DownloadHelper.startDownloadDialog(requireContext(), childFragmentManager, videoId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import com.github.libretube.extensions.serializable
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.DownloadHelper
import com.github.libretube.obj.ShareData
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.dialogs.DeletePlaylistDialog
import com.github.libretube.ui.dialogs.DownloadPlaylistDialog
import com.github.libretube.ui.dialogs.PlaylistDescriptionDialog
import com.github.libretube.ui.dialogs.RenamePlaylistDialog
import com.github.libretube.ui.dialogs.ShareDialog
Expand Down Expand Up @@ -139,14 +139,7 @@ class PlaylistOptionsBottomSheet : BaseBottomSheet() {
}

R.string.download -> {
val downloadPlaylistDialog = DownloadPlaylistDialog().apply {
arguments = bundleOf(
IntentData.playlistId to playlistId,
IntentData.playlistName to playlistName,
IntentData.playlistType to playlistType
)
}
downloadPlaylistDialog.show(mFragmentManager, null)
DownloadHelper.startDownloadPlaylistDialog(requireContext(), mFragmentManager, playlistId, playlistName, playlistType)
}

else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.parcelable
import com.github.libretube.extensions.toID
import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.DownloadHelper
import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.obj.ShareData
import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.dialogs.AddToPlaylistDialog
import com.github.libretube.ui.dialogs.DownloadDialog
import com.github.libretube.ui.dialogs.ShareDialog
import com.github.libretube.ui.fragments.SubscriptionsFragment
import com.github.libretube.util.PlayingQueue
Expand Down Expand Up @@ -71,9 +71,7 @@ class VideoOptionsBottomSheet : BaseBottomSheet() {
}

R.string.download -> {
val newFragment = DownloadDialog()
newFragment.arguments = bundleOf(IntentData.videoId to videoId)
newFragment.show(parentFragmentManager, DownloadDialog::class.java.name)
DownloadHelper.startDownloadDialog(requireContext(), parentFragmentManager, videoId)
}

R.string.share -> {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@
<string name="uptime">%.2f%% uptime</string>
<string name="change">Change</string>
<string name="gestures">Gestures</string>
<string name="external_download_provider">External download provider</string>
<string name="external_download_provider_summary">Enter the package name of the app you want to use for downloading videos. Leave blank to use LibreTube\'s inbuilt downloader.</string>

<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>
Expand Down
28 changes: 19 additions & 9 deletions app/src/main/res/xml/advanced_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@
app:key="play_automatically"
app:title="@string/play_automatically" />

<com.github.libretube.ui.views.SliderPreference
android:icon="@drawable/ic_download"
android:key="max_parallel_downloads"
android:title="@string/concurrent_downloads"
app:defValue="6"
app:stepSize="1"
app:valueFrom="1"
app:valueTo="6" />

<ListPreference
android:entries="@array/cacheSize"
android:entryValues="@array/cacheSizeValues"
Expand All @@ -58,6 +49,25 @@

</PreferenceCategory>

<PreferenceCategory app:title="@string/downloads">

<com.github.libretube.ui.views.SliderPreference
android:icon="@drawable/ic_download"
android:key="max_parallel_downloads"
android:title="@string/concurrent_downloads"
app:defValue="6"
app:stepSize="1"
app:valueFrom="1"
app:valueTo="6" />

<EditTextPreference
android:icon="@drawable/ic_open"
android:key="external_download_provider"
android:summary="@string/external_download_provider_summary"
android:title="@string/external_download_provider" />

</PreferenceCategory>

<PreferenceCategory app:title="@string/misc">

<Preference
Expand Down
Loading