Skip to content
This repository has been archived by the owner on Oct 26, 2024. It is now read-only.

Commit

Permalink
feat(YouTube - Downloads): Use external downloader when selecting 'Do…
Browse files Browse the repository at this point in the history
…wnload' in home feed flyout menu (#587)

fix(YouTube - Downloads): Use external downloader when selecting 'Download' from home feed flyout menu
  • Loading branch information
LisoUseInAIKyrios authored Mar 15, 2024
1 parent 7c40c94 commit 254da31
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
import android.content.pm.PackageManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.lang.ref.WeakReference;
import java.util.Objects;

import app.revanced.integrations.shared.Logger;
import app.revanced.integrations.shared.StringRef;
import app.revanced.integrations.shared.Utils;
import app.revanced.integrations.youtube.patches.spoof.SpoofAppVersionPatch;
import app.revanced.integrations.youtube.settings.Settings;

@SuppressWarnings("unused")
Expand All @@ -31,25 +30,13 @@ public static void activityCreated(Activity mainActivity) {
/**
* Injection point.
*
* Call if download playlist is pressed, or if download button is used
* for old spoofed version (both playlists and the player action button).
* Called from the in app download hook,
* for both the player action button (below the video)
* and the 'Download video' flyout option for feed videos.
*
* Downloading playlists is not supported yet,
* as the hooked code does not easily expose the playlist id.
* Appears to always be called from the main thread.
*/
public static boolean inAppDownloadPlaylistLegacyOnClick(@Nullable 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() {
public static boolean inAppDownloadButtonOnClick(@NonNull String videoId) {
try {
if (!Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.get()) {
return false;
Expand All @@ -65,7 +52,7 @@ public static boolean inAppDownloadButtonOnClick() {
isActivityContext = false;
}

launchExternalDownloader(context, isActivityContext);
launchExternalDownloader(videoId, context, isActivityContext);
return true;
} catch (Exception ex) {
Logger.printException(() -> "inAppDownloadButtonOnClick failure", ex);
Expand All @@ -77,29 +64,29 @@ public static boolean inAppDownloadButtonOnClick() {
* @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).
*/
public static void launchExternalDownloader(@NonNull Context context, boolean isActivityContext) {
Logger.printDebug(() -> "Launching external downloader with context: " + context);

// Trim string to avoid any accidental whitespace.
var downloaderPackageName = Settings.EXTERNAL_DOWNLOADER_PACKAGE_NAME.get().trim();

boolean packageEnabled = false;
public static void launchExternalDownloader(@NonNull String videoId,
@NonNull Context context, boolean isActivityContext) {
try {
packageEnabled = context.getPackageManager().getApplicationInfo(downloaderPackageName, 0).enabled;
} catch (PackageManager.NameNotFoundException error) {
Logger.printDebug(() -> "External downloader could not be found: " + error);
}
Objects.requireNonNull(videoId);
Logger.printDebug(() -> "Launching external downloader with context: " + context);

// If the package is not installed, show the toast
if (!packageEnabled) {
Utils.showToastLong(StringRef.str("revanced_external_downloader_not_installed_warning", downloaderPackageName));
return;
}
// Trim string to avoid any accidental whitespace.
var downloaderPackageName = Settings.EXTERNAL_DOWNLOADER_PACKAGE_NAME.get().trim();

// Launch intent
try {
String content = String.format("https://youtu.be/%s", VideoInformation.getVideoId());
boolean packageEnabled = false;
try {
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.setType("text/plain");
intent.setPackage(downloaderPackageName);
Expand All @@ -109,8 +96,8 @@ public static void launchExternalDownloader(@NonNull Context context, boolean is
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
context.startActivity(intent);
} catch (Exception error) {
Logger.printException(() -> "Failed to launch intent: " + error, error);
} catch (Exception ex) {
Logger.printException(() -> "launchExternalDownloader failure", ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import app.revanced.integrations.shared.Logger;
import app.revanced.integrations.youtube.patches.DownloadsPatch;
import app.revanced.integrations.youtube.patches.VideoInformation;
import app.revanced.integrations.youtube.settings.Settings;

@SuppressWarnings("unused")
Expand Down Expand Up @@ -43,7 +44,10 @@ public static void changeVisibility(boolean showing) {
}

private static void onDownloadClick(View view) {
DownloadsPatch.launchExternalDownloader(view.getContext(), true);
DownloadsPatch.launchExternalDownloader(
VideoInformation.getVideoId(),
view.getContext(),
true);
}
}

0 comments on commit 254da31

Please sign in to comment.