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

Commit 6aa02e5

Browse files
authored
feat: downloads patch (#89)
1 parent 6abc481 commit 6aa02e5

11 files changed

Lines changed: 233 additions & 15 deletions

File tree

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ android {
3434
}
3535

3636
dependencies {
37+
//implementation 'com.github.TeamNewPipe:NewPipeExtractor:v0.22.1'
38+
3739
compileOnly 'androidx.annotation:annotation:1.4.0'
38-
compileOnly 'androidx.constraintlayout:constraintlayout:2.1.4'
3940
}
4041

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package android.support.constraint;
2+
3+
import android.content.Context;
4+
import android.view.ViewGroup;
5+
6+
/**
7+
* "CompileOnly" class
8+
* because android.support.constraint.ConstraintLayout is deprecated
9+
* in favour of androidx.constraintlayout.widget.ConstraintLayout.
10+
*
11+
* This class will not be included and "replaced" by the real package's class.
12+
*/
13+
public class ConstraintLayout extends ViewGroup {
14+
public ConstraintLayout(Context context) {
15+
super(context);
16+
}
17+
18+
@Override
19+
protected void onLayout(boolean changed, int l, int t, int r, int b) { }
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package app.revanced.integrations.patches.downloads;
2+
3+
import app.revanced.integrations.utils.LogHelper;
4+
5+
/**
6+
* Used by app.revanced.patches.youtube.interaction.downloads.bytecode.patch.DownloadsBytecodePatch
7+
*/
8+
public class DownloadsPatch {
9+
private static String videoId;
10+
11+
/**
12+
* Called when the video changes
13+
* @param videoId The current video id
14+
*/
15+
public static void setVideoId(String videoId) {
16+
LogHelper.debug(DownloadsPatch.class, "newVideoLoaded - " + videoId);
17+
18+
DownloadsPatch.videoId = videoId;
19+
}
20+
21+
/**
22+
* @return The current video id
23+
*/
24+
public static String getCurrentVideoId() {
25+
return videoId;
26+
}
27+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package app.revanced.integrations.patches.downloads.views
2+
3+
class DownloadOptions {
4+
}

app/src/main/java/app/revanced/integrations/settings/SettingsEnum.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app.revanced.integrations.settings;
22

33
import android.content.Context;
4+
import android.os.Environment;
45
import android.util.Log;
56

67
import java.util.ArrayList;
@@ -11,6 +12,9 @@
1112
import app.revanced.integrations.utils.SharedPrefHelper;
1213

1314
public enum SettingsEnum {
15+
//Download Settings
16+
// TODO: DOWNLOAD_PATH("revanced_download_path", Environment.getExternalStorageDirectory().getPath() + "/Download", ReturnType.STRING),
17+
DOWNLOAD_BUTTON_SHOWN("revanced_downloads", true, ReturnType.BOOLEAN),
1418

1519
//Video Settings
1620
OLD_STYLE_QUALITY_SETTINGS("revanced_use_old_style_quality_settings", true, ReturnType.BOOLEAN),

app/src/main/java/app/revanced/integrations/settingsmenu/ReVancedSettingsFragment.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import app.revanced.integrations.utils.ReVancedUtils;
2929
import app.revanced.integrations.utils.SharedPrefHelper;
3030
import app.revanced.integrations.videoplayer.AutoRepeat;
31+
import app.revanced.integrations.videoplayer.Copy;
32+
import app.revanced.integrations.videoplayer.CopyWithTimeStamp;
33+
import app.revanced.integrations.videoplayer.DownloadButton;
3134

3235
public class ReVancedSettingsFragment extends PreferenceFragment {
3336

@@ -91,13 +94,14 @@ public class ReVancedSettingsFragment extends PreferenceFragment {
9194
} else {
9295
LogHelper.printException(ReVancedSettingsFragment.class, "No valid setting found: " + setting.toString());
9396
}
94-
/*
95-
if ("pref_copy_video_url_timestamp_button_list".equals(str)) {
96-
CopyWithTimeStamp.refreshShouldBeShown();
97-
} else if ("pref_copy_video_url_button_list".equals(str)) {
98-
Copy.refreshShouldBeShown();
99-
}
100-
*/
97+
98+
if ("pref_copy_video_url_timestamp_button_list".equals(str)) {
99+
CopyWithTimeStamp.refreshShouldBeShown();
100+
} else if ("pref_copy_video_url_button_list".equals(str)) {
101+
Copy.refreshShouldBeShown();
102+
} else if ("pref_download_button_list".equals(str)) {
103+
DownloadButton.refreshShouldBeShown();
104+
}
101105
} else {
102106
LogHelper.printException(ReVancedSettingsFragment.class, "Setting cannot be handled! " + pref.toString());
103107
}

app/src/main/java/app/revanced/integrations/videoplayer/AutoRepeat.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import android.content.Context;
44

5+
import android.support.constraint.ConstraintLayout;
56
import android.view.View;
67
import android.view.animation.Animation;
78
import android.view.animation.AnimationUtils;
89
import android.widget.ImageView;
910

10-
import androidx.constraintlayout.widget.ConstraintLayout;
11-
1211
import app.revanced.integrations.settings.SettingsEnum;
1312
import app.revanced.integrations.utils.LogHelper;
1413
import app.revanced.integrations.utils.ReVancedUtils;

app/src/main/java/app/revanced/integrations/videoplayer/Copy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package app.revanced.integrations.videoplayer;
22

33
import android.content.Context;
4+
import android.support.constraint.ConstraintLayout;
45
import android.view.View;
56
import android.view.animation.Animation;
67
import android.view.animation.AnimationUtils;
78
import android.widget.ImageView;
89

9-
import androidx.constraintlayout.widget.ConstraintLayout;
10-
1110
import app.revanced.integrations.utils.LogHelper;
1211
import app.revanced.integrations.sponsorblock.player.VideoHelpers;
1312
import app.revanced.integrations.utils.ReVancedUtils;

app/src/main/java/app/revanced/integrations/videoplayer/CopyWithTimeStamp.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import android.content.Context;
44

5+
import android.support.constraint.ConstraintLayout;
56
import android.view.View;
67
import android.view.animation.Animation;
78
import android.view.animation.AnimationUtils;
89
import android.widget.ImageView;
910

10-
import androidx.constraintlayout.widget.ConstraintLayout;
11-
1211
import app.revanced.integrations.utils.LogHelper;
1312
import app.revanced.integrations.sponsorblock.player.VideoHelpers;
1413
import app.revanced.integrations.utils.ReVancedUtils;
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package app.revanced.integrations.videoplayer;
2+
3+
import android.app.AlertDialog;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.content.pm.PackageManager;
7+
import android.support.constraint.ConstraintLayout;
8+
import android.view.View;
9+
import android.view.animation.Animation;
10+
import android.view.animation.AnimationUtils;
11+
import android.widget.ImageView;
12+
import android.widget.Toast;
13+
14+
15+
import java.lang.ref.WeakReference;
16+
17+
import app.revanced.integrations.patches.downloads.DownloadsPatch;
18+
import app.revanced.integrations.settings.SettingsEnum;
19+
import app.revanced.integrations.sponsorblock.StringRef;
20+
import app.revanced.integrations.utils.LogHelper;
21+
import app.revanced.integrations.utils.ReVancedUtils;
22+
import app.revanced.integrations.utils.SharedPrefHelper;
23+
24+
/* loaded from: classes6.dex */
25+
//ToDo: Refactor
26+
public class DownloadButton {
27+
static WeakReference<ImageView> _button = new WeakReference<>(null);
28+
static ConstraintLayout _constraintLayout;
29+
static int fadeDurationFast;
30+
static int fadeDurationScheduled;
31+
static Animation fadeIn;
32+
static Animation fadeOut;
33+
public static boolean isDownloadButtonEnabled;
34+
static boolean isShowing;
35+
36+
public static void initializeDownloadButton(Object obj) {
37+
try {
38+
LogHelper.debug(DownloadButton.class, "initializing");
39+
_constraintLayout = (ConstraintLayout) obj;
40+
isDownloadButtonEnabled = shouldBeShown();
41+
ImageView imageView = _constraintLayout.findViewById(getIdentifier("download_button", "id"));
42+
if (imageView == null) {
43+
LogHelper.debug(DownloadButton.class, "Couldn't find imageView with id \"download_button\"");
44+
return;
45+
}
46+
47+
imageView.setOnClickListener(view -> {
48+
LogHelper.debug(DownloadButton.class, "Download button clicked");
49+
50+
final var context = view.getContext();
51+
final var powerTubePackageName = "ussr.razar.youtube_dl";
52+
53+
boolean packageEnabled = false;
54+
try {
55+
assert context != null;
56+
packageEnabled = context.getPackageManager().getApplicationInfo(powerTubePackageName, 0).enabled;
57+
} catch (PackageManager.NameNotFoundException error) {
58+
LogHelper.debug(DownloadButton.class, "PowerTube could not be found: " + error);
59+
}
60+
61+
// If the package is not installed, show the toast
62+
if (!packageEnabled) {
63+
Toast.makeText(context, StringRef.str("powertube_not_installed_warning"), Toast.LENGTH_SHORT).show();
64+
Toast.makeText(context, StringRef.str("powertube_not_installed_notice"), Toast.LENGTH_LONG).show();
65+
return;
66+
}
67+
68+
// Launch PowerTube intent
69+
try {
70+
String content = String.format("https://youtu.be/%s", DownloadsPatch.getCurrentVideoId());
71+
72+
Intent intent = new Intent("android.intent.action.SEND");
73+
intent.setType("text/plain");
74+
intent.setPackage(powerTubePackageName);
75+
intent.putExtra("android.intent.extra.TEXT", content);
76+
context.startActivity(intent);
77+
78+
LogHelper.debug(DownloadButton.class, "Launched the intent with the content: " + content);
79+
} catch (Exception error) {
80+
LogHelper.debug(DownloadButton.class, "Failed to launch the intent: " + error);
81+
}
82+
83+
//var options = Arrays.asList("Video", "Audio").toArray(new CharSequence[0]);
84+
//
85+
//new AlertDialog.Builder(view.getContext())
86+
// .setItems(options, (dialog, which) -> {
87+
// LogHelper.debug(DownloadButton.class, String.valueOf(options[which]));
88+
// })
89+
// .show();
90+
// TODO: show popup and download via newpipe
91+
});
92+
_button = new WeakReference<>(imageView);
93+
fadeDurationFast = getInteger("fade_duration_fast");
94+
fadeDurationScheduled = getInteger("fade_duration_scheduled");
95+
Animation animation = getAnimation("fade_in");
96+
fadeIn = animation;
97+
animation.setDuration(fadeDurationFast);
98+
Animation animation2 = getAnimation("fade_out");
99+
fadeOut = animation2;
100+
animation2.setDuration(fadeDurationScheduled);
101+
isShowing = true;
102+
changeVisibility(false);
103+
104+
} catch (Exception e) {
105+
LogHelper.printException(DownloadButton.class, "Unable to set FrameLayout", e);
106+
}
107+
}
108+
109+
public static void changeVisibility(boolean z) {
110+
if (isShowing == z) return;
111+
112+
isShowing = z;
113+
ImageView imageView = _button.get();
114+
if (_constraintLayout != null && imageView != null) {
115+
if (z && isDownloadButtonEnabled) {
116+
LogHelper.debug(DownloadButton.class, "Fading in");
117+
imageView.setVisibility(View.VISIBLE);
118+
imageView.startAnimation(fadeIn);
119+
} else if (imageView.getVisibility() == View.VISIBLE) {
120+
LogHelper.debug(DownloadButton.class, "Fading out");
121+
imageView.startAnimation(fadeOut);
122+
imageView.setVisibility(View.GONE);
123+
}
124+
}
125+
}
126+
127+
public static void refreshShouldBeShown() {
128+
isDownloadButtonEnabled = shouldBeShown();
129+
}
130+
131+
private static boolean shouldBeShown() {
132+
if (!SettingsEnum.DOWNLOAD_BUTTON_SHOWN.getBoolean()) {
133+
return false;
134+
}
135+
136+
Context appContext = ReVancedUtils.getContext();
137+
if (appContext == null) {
138+
LogHelper.printException(DownloadButton.class, "shouldBeShown - context is null!");
139+
return false;
140+
}
141+
String string = SharedPrefHelper.getString(appContext, SharedPrefHelper.SharedPrefNames.YOUTUBE, "pref_download_button_list", "PLAYER" /* TODO: set the default to null, as this will be set by the settings page later */);
142+
if (string == null || string.isEmpty()) {
143+
return false;
144+
}
145+
return string.equalsIgnoreCase("PLAYER");
146+
}
147+
148+
private static int getIdentifier(String str, String str2) {
149+
Context appContext = ReVancedUtils.getContext();
150+
return appContext.getResources().getIdentifier(str, str2, appContext.getPackageName());
151+
}
152+
153+
private static int getInteger(String str) {
154+
return ReVancedUtils.getContext().getResources().getInteger(getIdentifier(str, "integer"));
155+
}
156+
private static Animation getAnimation(String str) {
157+
return AnimationUtils.loadAnimation(ReVancedUtils.getContext(), getIdentifier(str, "anim"));
158+
}
159+
}
160+

0 commit comments

Comments
 (0)