Skip to content

Commit cfd7780

Browse files
MarcaDianLisoUseInAIKyrios
andauthored
feat(YouTube - External downloads): Improve the selection of the external downloader package (#5504)
Co-authored-by: LisoUseInAIKyrios <[email protected]>
1 parent 707deae commit cfd7780

File tree

10 files changed

+493
-48
lines changed

10 files changed

+493
-48
lines changed

extensions/shared/library/src/main/java/app/revanced/extension/shared/Utils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,28 @@ public static int dipToPixels(float dip) {
14381438
);
14391439
}
14401440

1441+
/**
1442+
* Converts a percentage of the screen height to actual device pixels.
1443+
*
1444+
* @param percentage The percentage of the screen height (e.g., 30 for 30%).
1445+
* @return The device pixel value corresponding to the percentage of screen height.
1446+
*/
1447+
public static int percentageHeightToPixels(int percentage) {
1448+
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
1449+
return (int) (metrics.heightPixels * (percentage / 100.0f));
1450+
}
1451+
1452+
/**
1453+
* Converts a percentage of the screen width to actual device pixels.
1454+
*
1455+
* @param percentage The percentage of the screen width (e.g., 30 for 30%).
1456+
* @return The device pixel value corresponding to the percentage of screen width.
1457+
*/
1458+
public static int percentageWidthToPixels(int percentage) {
1459+
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
1460+
return (int) (metrics.widthPixels * (percentage / 100.0f));
1461+
}
1462+
14411463
/**
14421464
* Adjusts the brightness of a color by lightening or darkening it based on the given factor.
14431465
* <p>

extensions/shared/library/src/main/java/app/revanced/extension/shared/settings/preference/CustomDialogListPreference.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package app.revanced.extension.shared.settings.preference;
22

3-
import static app.revanced.extension.shared.Utils.dipToPixels;
4-
53
import android.app.Dialog;
64
import android.content.Context;
75
import android.os.Bundle;
@@ -26,7 +24,7 @@ public class CustomDialogListPreference extends ListPreference {
2624
/**
2725
* Custom ArrayAdapter to handle checkmark visibility.
2826
*/
29-
private static class ListPreferenceArrayAdapter extends ArrayAdapter<CharSequence> {
27+
public static class ListPreferenceArrayAdapter extends ArrayAdapter<CharSequence> {
3028
private static class SubViewDataContainer {
3129
ImageView checkIcon;
3230
View placeholder;

extensions/shared/library/src/main/java/app/revanced/extension/shared/settings/preference/ResettableEditTextPreference.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
package app.revanced.extension.shared.settings.preference;
22

33
import static app.revanced.extension.shared.StringRef.str;
4-
import static app.revanced.extension.shared.Utils.dipToPixels;
54

65
import android.app.Dialog;
76
import android.content.Context;
8-
import android.graphics.Color;
9-
import android.graphics.drawable.Drawable;
10-
import android.graphics.drawable.LayerDrawable;
11-
import android.graphics.drawable.shapes.RectShape;
12-
import android.graphics.drawable.shapes.RoundRectShape;
13-
import android.graphics.drawable.ShapeDrawable;
14-
import android.graphics.Paint.Style;
157
import android.os.Bundle;
168
import android.preference.EditTextPreference;
17-
import android.text.TextUtils;
189
import android.util.AttributeSet;
1910
import android.util.Pair;
20-
import android.view.ViewGroup;
21-
import android.widget.Button;
2211
import android.widget.EditText;
2312
import android.widget.LinearLayout;
24-
import android.widget.LinearLayout;
25-
import android.widget.TextView;
2613

2714
import androidx.annotation.Nullable;
2815

extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/DownloadsPatch.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package app.revanced.extension.youtube.patches;
22

3+
import static app.revanced.extension.youtube.settings.preference.ExternalDownloaderPreference.showDialogIfAppIsNotInstalled;
4+
35
import android.app.Activity;
46
import android.content.Context;
57
import android.content.Intent;
6-
import android.content.pm.PackageManager;
7-
8-
import androidx.annotation.NonNull;
98

109
import java.lang.ref.WeakReference;
1110
import java.util.Objects;
1211

1312
import app.revanced.extension.shared.Logger;
14-
import app.revanced.extension.shared.StringRef;
1513
import app.revanced.extension.shared.Utils;
1614
import app.revanced.extension.youtube.settings.Settings;
1715

@@ -36,7 +34,7 @@ public static void activityCreated(Activity mainActivity) {
3634
*
3735
* Appears to always be called from the main thread.
3836
*/
39-
public static boolean inAppDownloadButtonOnClick(@NonNull String videoId) {
37+
public static boolean inAppDownloadButtonOnClick(String videoId) {
4038
try {
4139
if (!Settings.EXTERNAL_DOWNLOADER_ACTION_BUTTON.get()) {
4240
return false;
@@ -48,6 +46,9 @@ public static boolean inAppDownloadButtonOnClick(@NonNull String videoId) {
4846
boolean isActivityContext = true;
4947
if (context == null) {
5048
// Utils context is the application context, and not an activity context.
49+
//
50+
// Edit: This check may no longer be needed since YT can now
51+
// only be launched from the main Activity (embedded usage in other apps no longer works).
5152
context = Utils.getContext();
5253
isActivityContext = false;
5354
}
@@ -64,25 +65,16 @@ public static boolean inAppDownloadButtonOnClick(@NonNull String videoId) {
6465
* @param isActivityContext If the context parameter is for an Activity. If this is false, then
6566
* the downloader is opened as a new task (which forces YT to minimize).
6667
*/
67-
public static void launchExternalDownloader(@NonNull String videoId,
68-
@NonNull Context context, boolean isActivityContext) {
68+
public static void launchExternalDownloader(String videoId, Context context, boolean isActivityContext) {
6969
try {
7070
Objects.requireNonNull(videoId);
7171
Logger.printDebug(() -> "Launching external downloader with context: " + context);
7272

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

76-
boolean packageEnabled = false;
77-
try {
78-
packageEnabled = context.getPackageManager().getApplicationInfo(downloaderPackageName, 0).enabled;
79-
} catch (PackageManager.NameNotFoundException error) {
80-
Logger.printDebug(() -> "External downloader could not be found: " + error);
81-
}
82-
83-
// If the package is not installed, show the toast
84-
if (!packageEnabled) {
85-
Utils.showToastLong(StringRef.str("revanced_external_downloader_not_installed_warning", downloaderPackageName));
76+
// If the package is not installed, show a dialog.
77+
if (showDialogIfAppIsNotInstalled(context, downloaderPackageName)) {
8678
return;
8779
}
8880

extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public class Settings extends BaseSettings {
191191
public static final BooleanSetting EXTERNAL_DOWNLOADER = new BooleanSetting("revanced_external_downloader", FALSE);
192192
public static final BooleanSetting EXTERNAL_DOWNLOADER_ACTION_BUTTON = new BooleanSetting("revanced_external_downloader_action_button", FALSE);
193193
public static final StringSetting EXTERNAL_DOWNLOADER_PACKAGE_NAME = new StringSetting("revanced_external_downloader_name",
194-
"org.schabi.newpipe" /* NewPipe */, parentsAny(EXTERNAL_DOWNLOADER, EXTERNAL_DOWNLOADER_ACTION_BUTTON));
194+
"com.deniscerri.ytdl" /* YTDLnis */, parentsAny(EXTERNAL_DOWNLOADER, EXTERNAL_DOWNLOADER_ACTION_BUTTON));
195195

196196
// Comments
197197
public static final BooleanSetting HIDE_COMMENTS_AI_CHAT_SUMMARY = new BooleanSetting("revanced_hide_comments_ai_chat_summary", FALSE);

extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/preference/CustomVideoSpeedListPreference.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
@SuppressWarnings({"unused", "deprecation"})
1717
public final class CustomVideoSpeedListPreference extends CustomDialogListPreference {
1818

19-
/**
20-
* Initialize a settings preference list with the available playback speeds.
21-
*/
22-
private void initializeEntryValues() {
19+
{
20+
// Initialize a settings preference list with the available playback speeds.
2321
float[] customPlaybackSpeeds = CustomPlaybackSpeedPatch.customPlaybackSpeeds;
2422
final int numberOfEntries = customPlaybackSpeeds.length + 1;
2523
String[] preferenceListEntries = new String[numberOfEntries];
@@ -41,10 +39,6 @@ private void initializeEntryValues() {
4139
setEntryValues(preferenceListEntryValues);
4240
}
4341

44-
{
45-
initializeEntryValues();
46-
}
47-
4842
public CustomVideoSpeedListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
4943
super(context, attrs, defStyleAttr, defStyleRes);
5044
}

0 commit comments

Comments
 (0)