1
mirror of https://github.com/revanced/revanced-integrations synced 2024-10-29 13:07:22 +01:00

feat(YouTube - Downloads): Use external downloader when selecting 'Download' in home feed flyout menu (#587)

fix(YouTube - Downloads): Use external downloader when selecting 'Download' from home feed flyout menu
This commit is contained in:
LisoUseInAIKyrios 2024-03-15 16:32:48 +04:00 committed by GitHub
parent 7c40c947ef
commit 254da31d16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 42 deletions

View File

@ -6,14 +6,13 @@ import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.Objects;
import app.revanced.integrations.shared.Logger; import app.revanced.integrations.shared.Logger;
import app.revanced.integrations.shared.StringRef; import app.revanced.integrations.shared.StringRef;
import app.revanced.integrations.shared.Utils; import app.revanced.integrations.shared.Utils;
import app.revanced.integrations.youtube.patches.spoof.SpoofAppVersionPatch;
import app.revanced.integrations.youtube.settings.Settings; import app.revanced.integrations.youtube.settings.Settings;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ -31,25 +30,13 @@ public final class DownloadsPatch {
/** /**
* Injection point. * Injection point.
* *
* Call if download playlist is pressed, or if download button is used * Called from the in app download hook,
* for old spoofed version (both playlists and the player action button). * for both the player action button (below the video)
* and the 'Download video' flyout option for feed videos.
* *
* Downloading playlists is not supported yet, * Appears to always be called from the main thread.
* as the hooked code does not easily expose the playlist id.
*/ */
public static boolean inAppDownloadPlaylistLegacyOnClick(@Nullable String videoId) { public static boolean inAppDownloadButtonOnClick(@NonNull String videoId) {
if (videoId == null || videoId.isEmpty()) {
// videoId is null or empty if download playlist is pressed.
Logger.printDebug(() -> "Ignoring playlist download button press");
return false;
}
return inAppDownloadButtonOnClick();
}
/**
* Injection point.
*/
public static boolean inAppDownloadButtonOnClick() {
try { try {
if (!Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.get()) { if (!Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.get()) {
return false; return false;
@ -65,7 +52,7 @@ public final class DownloadsPatch {
isActivityContext = false; isActivityContext = false;
} }
launchExternalDownloader(context, isActivityContext); launchExternalDownloader(videoId, context, isActivityContext);
return true; return true;
} catch (Exception ex) { } catch (Exception ex) {
Logger.printException(() -> "inAppDownloadButtonOnClick failure", ex); Logger.printException(() -> "inAppDownloadButtonOnClick failure", ex);
@ -77,29 +64,29 @@ public final class DownloadsPatch {
* @param isActivityContext If the context parameter is for an Activity. If this is false, then * @param isActivityContext If the context parameter is for an Activity. If this is false, then
* the downloader is opened as a new task (which forces YT to minimize). * the downloader is opened as a new task (which forces YT to minimize).
*/ */
public static void launchExternalDownloader(@NonNull Context context, boolean isActivityContext) { public static void launchExternalDownloader(@NonNull String videoId,
Logger.printDebug(() -> "Launching external downloader with context: " + context); @NonNull Context context, boolean isActivityContext) {
// Trim string to avoid any accidental whitespace.
var downloaderPackageName = Settings.EXTERNAL_DOWNLOADER_PACKAGE_NAME.get().trim();
boolean packageEnabled = false;
try { try {
packageEnabled = context.getPackageManager().getApplicationInfo(downloaderPackageName, 0).enabled; Objects.requireNonNull(videoId);
} catch (PackageManager.NameNotFoundException error) { Logger.printDebug(() -> "Launching external downloader with context: " + context);
Logger.printDebug(() -> "External downloader could not be found: " + error);
}
// If the package is not installed, show the toast // Trim string to avoid any accidental whitespace.
if (!packageEnabled) { var downloaderPackageName = Settings.EXTERNAL_DOWNLOADER_PACKAGE_NAME.get().trim();
Utils.showToastLong(StringRef.str("revanced_external_downloader_not_installed_warning", downloaderPackageName));
return;
}
// Launch intent boolean packageEnabled = false;
try { try {
String content = String.format("https://youtu.be/%s", VideoInformation.getVideoId()); packageEnabled = context.getPackageManager().getApplicationInfo(downloaderPackageName, 0).enabled;
} catch (PackageManager.NameNotFoundException error) {
Logger.printDebug(() -> "External downloader could not be found: " + error);
}
// If the package is not installed, show the toast
if (!packageEnabled) {
Utils.showToastLong(StringRef.str("revanced_external_downloader_not_installed_warning", downloaderPackageName));
return;
}
String content = "https://youtu.be/" + videoId;
Intent intent = new Intent("android.intent.action.SEND"); Intent intent = new Intent("android.intent.action.SEND");
intent.setType("text/plain"); intent.setType("text/plain");
intent.setPackage(downloaderPackageName); intent.setPackage(downloaderPackageName);
@ -109,8 +96,8 @@ public final class DownloadsPatch {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} }
context.startActivity(intent); context.startActivity(intent);
} catch (Exception error) { } catch (Exception ex) {
Logger.printException(() -> "Failed to launch intent: " + error, error); Logger.printException(() -> "launchExternalDownloader failure", ex);
} }
} }
} }

View File

@ -7,6 +7,7 @@ import androidx.annotation.Nullable;
import app.revanced.integrations.shared.Logger; import app.revanced.integrations.shared.Logger;
import app.revanced.integrations.youtube.patches.DownloadsPatch; import app.revanced.integrations.youtube.patches.DownloadsPatch;
import app.revanced.integrations.youtube.patches.VideoInformation;
import app.revanced.integrations.youtube.settings.Settings; import app.revanced.integrations.youtube.settings.Settings;
@SuppressWarnings("unused") @SuppressWarnings("unused")
@ -43,7 +44,10 @@ public class ExternalDownloadButton extends BottomControlButton {
} }
private static void onDownloadClick(View view) { private static void onDownloadClick(View view) {
DownloadsPatch.launchExternalDownloader(view.getContext(), true); DownloadsPatch.launchExternalDownloader(
VideoInformation.getVideoId(),
view.getContext(),
true);
} }
} }