1
mirror of https://github.com/revanced/revanced-integrations synced 2024-11-22 05:02:19 +01:00

feat(YouTube - External downloader): Add ability to use in-app download button

This commit is contained in:
oSumAtrIX 2024-03-04 20:36:24 +01:00
parent 6aacd1a225
commit 771a0de3bc
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
3 changed files with 60 additions and 37 deletions

View File

@ -0,0 +1,57 @@
package app.revanced.integrations.youtube.patches;
import android.content.Intent;
import android.content.pm.PackageManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import app.revanced.integrations.shared.Logger;
import app.revanced.integrations.shared.StringRef;
import app.revanced.integrations.shared.Utils;
import app.revanced.integrations.youtube.settings.Settings;
public final class DownloadsPatch {
public static boolean inAppDownloadButtonOnClick() {
if (!Settings.USE_IN_APP_DOWNLOAD_BUTTON.get())
return false;
launchExternalDownloader();
return true;
}
public static void launchExternalDownloader() {
Logger.printDebug(() -> "Launching external downloader");
final var context = Utils.getContext();
// Trim string to avoid any accidental whitespace.
var downloaderPackageName = Settings.EXTERNAL_DOWNLOADER_PACKAGE_NAME.get().trim();
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;
}
// Launch intent
try {
String content = String.format("https://youtu.be/%s", VideoInformation.getVideoId());
Intent intent = new Intent("android.intent.action.SEND");
intent.setType("text/plain");
intent.setPackage(downloaderPackageName);
intent.putExtra("android.intent.extra.TEXT", content);
context.startActivity(intent);
Logger.printDebug(() -> "Launched the intent with the content: " + content);
} catch (Exception error) {
Logger.printException(() -> "Failed to launch the intent: " + error, error);
}
}
}

View File

@ -18,6 +18,7 @@ public class Settings extends BaseSettings {
public static final BooleanSetting EXTERNAL_DOWNLOADER = new BooleanSetting("revanced_external_downloader", FALSE);
public static final StringSetting EXTERNAL_DOWNLOADER_PACKAGE_NAME = new StringSetting("revanced_external_downloader_name",
"org.schabi.newpipe" /* NewPipe */, parent(EXTERNAL_DOWNLOADER));
public static final BooleanSetting USE_IN_APP_DOWNLOAD_BUTTON = new BooleanSetting("revanced_use_in_app_download_button", TRUE);
// Copy video URL
public static final BooleanSetting COPY_VIDEO_URL = new BooleanSetting("revanced_copy_video_url", FALSE);

View File

@ -1,17 +1,14 @@
package app.revanced.integrations.youtube.videoplayer;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
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.shared.Logger;
import app.revanced.integrations.shared.Utils;
import app.revanced.integrations.shared.StringRef;
@SuppressWarnings("unused")
public class ExternalDownloadButton extends BottomControlButton {
@ -47,39 +44,7 @@ public class ExternalDownloadButton extends BottomControlButton {
}
private static void onDownloadClick(View view) {
Logger.printDebug(() -> "External download button clicked");
final var context = view.getContext();
// Trim string to avoid any accidental whitespace.
var downloaderPackageName = Settings.EXTERNAL_DOWNLOADER_PACKAGE_NAME.get().trim();
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;
}
// Launch PowerTube intent
try {
String content = String.format("https://youtu.be/%s", VideoInformation.getVideoId());
Intent intent = new Intent("android.intent.action.SEND");
intent.setType("text/plain");
intent.setPackage(downloaderPackageName);
intent.putExtra("android.intent.extra.TEXT", content);
context.startActivity(intent);
Logger.printDebug(() -> "Launched the intent with the content: " + content);
} catch (Exception error) {
Logger.printException(() -> "Failed to launch the intent: " + error, error);
}
DownloadsPatch.launchExternalDownloader();
}
}