Skip to content

Commit

Permalink
feat(youtube/copy-video-url): add tap and hold functionality to copy …
Browse files Browse the repository at this point in the history
…video url buttons (#403)
  • Loading branch information
LisoUseInAIKyrios committed May 19, 2023
1 parent 71e94e8 commit 80689ef
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 20 deletions.
Expand Up @@ -2,22 +2,46 @@

import static app.revanced.integrations.utils.StringRef.str;

import android.os.Build;

import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;

public class CopyVideoUrlPatch {
public static void copyUrl(Boolean withTimestamp) {

public static void copyUrl(boolean withTimestamp) {
try {
String url = String.format("https://youtu.be/%s", VideoInformation.getVideoId());
if (withTimestamp) {
long seconds = VideoInformation.getVideoTime() / 1000;
url += String.format("?t=%s", seconds);
StringBuilder builder = new StringBuilder("https://youtu.be/");
builder.append(VideoInformation.getVideoId());
final long currentVideoTimeInSeconds = VideoInformation.getVideoTime() / 1000;
if (withTimestamp && currentVideoTimeInSeconds > 0) {
final long hour = currentVideoTimeInSeconds / (60 * 60);
final long minute = (currentVideoTimeInSeconds / 60) % 60;
final long second = currentVideoTimeInSeconds % 60;
builder.append("?t=");
if (hour > 0) {
builder.append(hour).append("h");
}
if (minute > 0) {
builder.append(minute).append("m");
}
if (second > 0) {
builder.append(second).append("s");
}
}

ReVancedUtils.setClipboard(url);
ReVancedUtils.showToastShort(str("share_copy_url_success"));
ReVancedUtils.setClipboard(builder.toString());
// Do not show a toast if using Android 13+ as it shows it's own toast.
// But if the user copied with a timestamp then show a toast.
// Unfortunately this will show 2 toasts on Android 13+, but no way around this.
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2 || (withTimestamp && currentVideoTimeInSeconds > 0)) {
ReVancedUtils.showToastShort(withTimestamp && currentVideoTimeInSeconds > 0
? str("revanced_share_copy_url_timestamp_success")
: str("revanced_share_copy_url_success"));
}
} catch (Exception e) {
LogHelper.printException(() -> "Failed to generate video url", e);
}
}

}
Expand Up @@ -37,7 +37,7 @@ public enum SettingsEnum {
"org.schabi.newpipe" /* NewPipe */, parents(EXTERNAL_DOWNLOADER)),

// Copy video URL
COPY_VIDEO_URL("revanced_copy_video_url", BOOLEAN, TRUE),
COPY_VIDEO_URL("revanced_copy_video_url", BOOLEAN, FALSE),
COPY_VIDEO_URL_TIMESTAMP("revanced_copy_video_url_timestamp", BOOLEAN, TRUE),

// Video
Expand Down
Expand Up @@ -4,14 +4,17 @@
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;
import androidx.annotation.Nullable;

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

import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;

public abstract class BottomControlButton {
private static final Animation fadeIn;
private static final Animation fadeOut;
Expand Down Expand Up @@ -40,7 +43,8 @@ public static Animation getButtonFadeOut() {
}

public BottomControlButton(@NonNull ViewGroup bottomControlsViewGroup, @NonNull String imageViewButtonId,
@NonNull SettingsEnum booleanSetting, @NonNull View.OnClickListener onClickListener) {
@NonNull SettingsEnum booleanSetting, @NonNull View.OnClickListener onClickListener,
@Nullable View.OnLongClickListener longClickListener) {
LogHelper.printDebug(() -> "Initializing button: " + imageViewButtonId);

if (booleanSetting.returnType != SettingsEnum.ReturnType.BOOLEAN) {
Expand All @@ -53,6 +57,9 @@ public BottomControlButton(@NonNull ViewGroup bottomControlsViewGroup, @NonNull
ReVancedUtils.getResourceIdentifier(imageViewButtonId, "id")
));
imageView.setOnClickListener(onClickListener);
if (longClickListener != null) {
imageView.setOnLongClickListener(longClickListener);
}
imageView.setVisibility(View.GONE);

buttonRef = new WeakReference<>(imageView);
Expand Down
@@ -1,5 +1,6 @@
package app.revanced.integrations.videoplayer;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
Expand All @@ -17,16 +18,20 @@ public CopyVideoUrlButton(ViewGroup viewGroup) {
viewGroup,
"copy_video_url_button",
SettingsEnum.COPY_VIDEO_URL,
view -> CopyVideoUrlPatch.copyUrl(false)
view -> CopyVideoUrlPatch.copyUrl(false),
view -> {
CopyVideoUrlPatch.copyUrl(true);
return true;
}
);
}

/**
* Injection point.
*/
public static void initializeButton(Object obj) {
public static void initializeButton(View view) {
try {
instance = new CopyVideoUrlButton((ViewGroup) obj);
instance = new CopyVideoUrlButton((ViewGroup) view);
} catch (Exception ex) {
LogHelper.printException(() -> "initializeButton failure", ex);
}
Expand Down
@@ -1,5 +1,6 @@
package app.revanced.integrations.videoplayer;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
Expand All @@ -17,14 +18,18 @@ public CopyVideoUrlTimestampButton(ViewGroup bottomControlsViewGroup) {
bottomControlsViewGroup,
"copy_video_url_timestamp_button",
SettingsEnum.COPY_VIDEO_URL_TIMESTAMP,
view -> CopyVideoUrlPatch.copyUrl(true)
view -> CopyVideoUrlPatch.copyUrl(true),
view -> {
CopyVideoUrlPatch.copyUrl(false);
return true;
}
);
}

/**
* Injection point.
*/
public static void initializeButton(Object bottomControlsViewGroup) {
public static void initializeButton(View bottomControlsViewGroup) {
try {
instance = new CopyVideoUrlTimestampButton((ViewGroup) bottomControlsViewGroup);
} catch (Exception ex) {
Expand Down
Expand Up @@ -22,16 +22,17 @@ public DownloadButton(ViewGroup viewGroup) {
viewGroup,
"download_button",
SettingsEnum.EXTERNAL_DOWNLOADER,
DownloadButton::onDownloadClick
DownloadButton::onDownloadClick,
null
);
}

/**
* Injection point.
*/
public static void initializeButton(Object obj) {
public static void initializeButton(View view) {
try {
instance = new DownloadButton((ViewGroup) obj);
instance = new DownloadButton((ViewGroup) view);
} catch (Exception ex) {
LogHelper.printException(() -> "initializeButton failure", ex);
}
Expand Down

0 comments on commit 80689ef

Please sign in to comment.