fix(youtube/sponsorblock): fix skip button in wrong location when full screen and comments visible (#387)

This commit is contained in:
LisoUseInAIKyrios 2023-05-03 11:29:29 +04:00 committed by GitHub
parent 680f17257d
commit 486b79b4e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 102 deletions

View File

@ -102,9 +102,9 @@ public class SegmentPlaybackController {
@Nullable
private static String timeWithoutSegments;
private static float sponsorBarLeft = 1f;
private static float sponsorBarRight = 1f;
private static float sponsorBarThickness = 2f;
private static int sponsorBarAbsoluteLeft;
private static int sponsorAbsoluteBarRight;
private static int sponsorBarThickness;
@Nullable
static SponsorSegment[] getSegments() {
@ -235,7 +235,7 @@ public class SegmentPlaybackController {
SponsorSegment[] segments = SBRequester.getSegments(videoId);
ReVancedUtils.runOnMainThread(()-> {
if (!videoId.equals(SegmentPlaybackController.currentVideoId)) {
if (!videoId.equals(currentVideoId)) {
// user changed videos before get segments network call could complete
LogHelper.printDebug(() -> "Ignoring segments for prior video: " + videoId);
return;
@ -525,7 +525,7 @@ public class SegmentPlaybackController {
if (!userManuallySkipped) {
// check for any smaller embedded segments, and count those as autoskipped
final boolean showSkipToast = SettingsEnum.SB_SHOW_TOAST_ON_SKIP.getBoolean();
for (final SponsorSegment otherSegment : segments) {
for (final SponsorSegment otherSegment : Objects.requireNonNull(segments)) {
if (segmentToSkip.end < otherSegment.start) {
break; // no other segments can be contained
}
@ -599,20 +599,6 @@ public class SegmentPlaybackController {
}
}
/**
* Injection point.
*/
public static void setSponsorBarAbsoluteLeft(final Rect rect) {
setSponsorBarAbsoluteLeft(rect.left);
}
public static void setSponsorBarAbsoluteLeft(final float left) {
if (sponsorBarLeft != left) {
LogHelper.printDebug(() -> String.format("setSponsorBarAbsoluteLeft: left=%.2f", left));
sponsorBarLeft = left;
}
}
/**
* Injection point
*/
@ -620,13 +606,9 @@ public class SegmentPlaybackController {
try {
Field field = self.getClass().getDeclaredField("replaceMeWithsetSponsorBarRect");
field.setAccessible(true);
Rect rect = (Rect) field.get(self);
if (rect == null) {
LogHelper.printException(() -> "Could not find sponsorblock rect");
} else {
setSponsorBarAbsoluteLeft(rect.left);
setSponsorBarAbsoluteRight(rect.right);
}
Rect rect = (Rect) Objects.requireNonNull(field.get(self));
setSponsorBarAbsoluteLeft(rect);
setSponsorBarAbsoluteRight(rect);
} catch (Exception ex) {
LogHelper.printException(() -> "setSponsorBarRect failure", ex);
}
@ -635,27 +617,31 @@ public class SegmentPlaybackController {
/**
* Injection point.
*/
public static void setSponsorBarAbsoluteRight(final Rect rect) {
setSponsorBarAbsoluteRight(rect.right);
public static void setSponsorBarAbsoluteLeft(Rect rect) {
final int left = rect.left;
if (sponsorBarAbsoluteLeft != left) {
LogHelper.printDebug(() -> "setSponsorBarAbsoluteLeft: " + left);
sponsorBarAbsoluteLeft = left;
}
}
public static void setSponsorBarAbsoluteRight(final float right) {
if (sponsorBarRight != right) {
LogHelper.printDebug(() -> String.format("setSponsorBarAbsoluteRight: right=%.2f", right));
sponsorBarRight = right;
/**
* Injection point.
*/
public static void setSponsorBarAbsoluteRight(Rect rect) {
final int right = rect.right;
if (sponsorAbsoluteBarRight != right) {
LogHelper.printDebug(() -> "setSponsorBarAbsoluteRight: " + right);
sponsorAbsoluteBarRight = right;
}
}
/**
* Injection point
*/
public static void setSponsorBarThickness(final int thickness) {
setSponsorBarThickness((float) thickness);
}
public static void setSponsorBarThickness(final float thickness) {
public static void setSponsorBarThickness(int thickness) {
if (sponsorBarThickness != thickness) {
LogHelper.printDebug(() -> String.format("setSponsorBarThickness: %.2f", thickness));
LogHelper.printDebug(() -> "setSponsorBarThickness: " + thickness);
sponsorBarThickness = thickness;
}
}
@ -736,25 +722,23 @@ public class SegmentPlaybackController {
*/
public static void drawSponsorTimeBars(final Canvas canvas, final float posY) {
try {
if (sponsorBarThickness < 0.1) return;
if (segments == null) return;
final long videoLength = VideoInformation.getVideoLength();
if (videoLength <= 0) return;
final float thicknessDiv2 = sponsorBarThickness / 2;
final float top = posY - thicknessDiv2;
final int thicknessDiv2 = sponsorBarThickness / 2; // rounds down
final float top = posY - (sponsorBarThickness - thicknessDiv2);
final float bottom = posY + thicknessDiv2;
final float absoluteLeft = sponsorBarLeft;
final float absoluteRight = sponsorBarRight;
final float videoMillisecondsToPixels = (1f / videoLength) * (sponsorAbsoluteBarRight - sponsorBarAbsoluteLeft);
final float leftPadding = sponsorBarAbsoluteLeft;
final float tmp1 = (1f / videoLength) * (absoluteRight - absoluteLeft);
for (SponsorSegment segment : segments) {
final float left = segment.start * tmp1 + absoluteLeft;
final float left = leftPadding + segment.start * videoMillisecondsToPixels;
final float right;
if (segment.category == SegmentCategory.HIGHLIGHT) {
right = left + getHighlightSegmentTimeBarScreenWidth();
} else {
right = segment.end * tmp1 + absoluteLeft;
right = leftPadding + segment.end * videoMillisecondsToPixels;
}
canvas.drawRect(left, top, right, bottom, segment.category.paint);
}

View File

@ -3,52 +3,35 @@ package app.revanced.integrations.sponsorblock.ui;
import static app.revanced.integrations.utils.ReVancedUtils.getResourceIdentifier;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import java.lang.ref.WeakReference;
import java.util.Objects;
import app.revanced.integrations.patches.VideoInformation;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;
import app.revanced.integrations.videoplayer.BottomControlButton;
public class CreateSegmentButtonController {
private static WeakReference<ImageView> buttonReference = new WeakReference<>(null);
private static Animation fadeIn;
private static Animation fadeOut;
private static boolean isShowing;
/**
* injection point
*/
public static void initialize(Object viewStub) {
public static void initialize(View youtubeControlsLayout) {
try {
LogHelper.printDebug(() -> "initializing new segment button");
RelativeLayout youtubeControlsLayout = (RelativeLayout) viewStub;
String buttonIdentifier = "sb_sponsorblock_button";
ImageView imageView = youtubeControlsLayout.findViewById(getResourceIdentifier(buttonIdentifier, "id"));
if (imageView == null) {
LogHelper.printException(() -> "Couldn't find imageView with \"" + buttonIdentifier + "\"");
return;
}
ImageView imageView = Objects.requireNonNull(youtubeControlsLayout.findViewById(
getResourceIdentifier("sb_sponsorblock_button", "id")));
imageView.setVisibility(View.GONE);
imageView.setOnClickListener(v -> {
LogHelper.printDebug(() -> "New segment button clicked");
SponsorBlockViewController.toggleNewSegmentLayoutVisibility();
});
buttonReference = new WeakReference<>(imageView);
// Animations
if (fadeIn == null) {
fadeIn = ReVancedUtils.getResourceAnimation("fade_in");
fadeIn.setDuration(ReVancedUtils.getResourceInteger("fade_duration_fast"));
fadeOut = ReVancedUtils.getResourceAnimation("fade_out");
fadeOut.setDuration(ReVancedUtils.getResourceInteger("fade_duration_scheduled"));
}
isShowing = true;
changeVisibilityImmediate(false);
buttonReference = new WeakReference<>(imageView);
} catch (Exception ex) {
LogHelper.printException(() -> "initialize failure", ex);
}
@ -86,7 +69,7 @@ public class CreateSegmentButtonController {
return;
}
if (!immediate) {
iView.startAnimation(fadeIn);
iView.startAnimation(BottomControlButton.getButtonFadeIn());
}
iView.setVisibility(View.VISIBLE);
return;
@ -95,7 +78,7 @@ public class CreateSegmentButtonController {
if (iView.getVisibility() == View.VISIBLE) {
iView.clearAnimation();
if (!immediate) {
iView.startAnimation(fadeOut);
iView.startAnimation(BottomControlButton.getButtonFadeOut());
}
iView.setVisibility(View.GONE);
}

View File

@ -51,7 +51,7 @@ public class SponsorBlockViewController {
/**
* Injection point.
*/
public static void initialize(Object obj) {
public static void initialize(ViewGroup viewGroup) {
try {
LogHelper.printDebug(() -> "initializing");
@ -64,7 +64,6 @@ public class SponsorBlockViewController {
LayoutInflater.from(context).inflate(getResourceIdentifier("inline_sponsor_overlay", "layout"), layout);
inlineSponsorOverlayRef = new WeakReference<>(layout);
ViewGroup viewGroup = (ViewGroup) obj;
viewGroup.addView(layout);
viewGroup.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
@Override

View File

@ -3,11 +3,10 @@ package app.revanced.integrations.sponsorblock.ui;
import static app.revanced.integrations.utils.ReVancedUtils.getResourceIdentifier;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import java.lang.ref.WeakReference;
import java.util.Objects;
import app.revanced.integrations.patches.VideoInformation;
import app.revanced.integrations.settings.SettingsEnum;
@ -15,40 +14,26 @@ import app.revanced.integrations.sponsorblock.SegmentPlaybackController;
import app.revanced.integrations.sponsorblock.SponsorBlockUtils;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;
import app.revanced.integrations.videoplayer.BottomControlButton;
public class VotingButtonController {
private static WeakReference<ImageView> buttonReference = new WeakReference<>(null);
private static Animation fadeIn;
private static Animation fadeOut;
private static boolean isShowing;
/**
* injection point
*/
public static void initialize(Object viewStub) {
public static void initialize(View youtubeControlsLayout) {
try {
LogHelper.printDebug(() -> "initializing voting button");
RelativeLayout controlsLayout = (RelativeLayout) viewStub;
String buttonResourceName = "sb_voting_button";
ImageView imageView = controlsLayout.findViewById(getResourceIdentifier(buttonResourceName, "id"));
if (imageView == null) {
LogHelper.printException(() -> "Couldn't find imageView with \"" + buttonResourceName + "\"");
return;
}
ImageView imageView = Objects.requireNonNull(youtubeControlsLayout.findViewById(
getResourceIdentifier("sb_voting_button", "id")));
imageView.setVisibility(View.GONE);
imageView.setOnClickListener(v -> {
SponsorBlockUtils.onVotingClicked(v.getContext());
});
buttonReference = new WeakReference<>(imageView);
// Animations
if (fadeIn == null) {
fadeIn = ReVancedUtils.getResourceAnimation("fade_in");
fadeIn.setDuration(ReVancedUtils.getResourceInteger("fade_duration_fast"));
fadeOut = ReVancedUtils.getResourceAnimation("fade_out");
fadeOut.setDuration(ReVancedUtils.getResourceInteger("fade_duration_scheduled"));
}
isShowing = true;
changeVisibilityImmediate(false);
buttonReference = new WeakReference<>(imageView);
} catch (Exception ex) {
LogHelper.printException(() -> "Unable to set RelativeLayout", ex);
}
@ -86,7 +71,7 @@ public class VotingButtonController {
return;
}
if (!immediate) {
iView.startAnimation(fadeIn);
iView.startAnimation(BottomControlButton.getButtonFadeIn());
}
iView.setVisibility(View.VISIBLE);
return;
@ -95,7 +80,7 @@ public class VotingButtonController {
if (iView.getVisibility() == View.VISIBLE) {
iView.clearAnimation();
if (!immediate) {
iView.startAnimation(fadeOut);
iView.startAnimation(BottomControlButton.getButtonFadeOut());
}
iView.setVisibility(View.GONE);
}
@ -116,7 +101,6 @@ public class VotingButtonController {
ReVancedUtils.verifyOnMainThread();
View v = buttonReference.get();
if (v == null) {
LogHelper.printDebug(() -> "Cannot hide voting button (value is null)");
return;
}
v.setVisibility(View.GONE);

View File

@ -13,18 +13,32 @@ import java.lang.ref.WeakReference;
import java.util.Objects;
public abstract class BottomControlButton {
private static final Animation fadeIn = ReVancedUtils.getResourceAnimation("fade_in");
private static final Animation fadeOut = ReVancedUtils.getResourceAnimation("fade_out");
private static final Animation fadeIn;
private static final Animation fadeOut;
private final WeakReference<ImageView> buttonRef;
private final SettingsEnum setting;
protected boolean isVisible;
static {
// TODO: check if these durations are correct.
fadeIn = ReVancedUtils.getResourceAnimation("fade_in");
fadeIn.setDuration(ReVancedUtils.getResourceInteger("fade_duration_fast"));
fadeOut = ReVancedUtils.getResourceAnimation("fade_out");
fadeOut.setDuration(ReVancedUtils.getResourceInteger("fade_duration_scheduled"));
}
@NonNull
public static Animation getButtonFadeIn() {
return fadeIn;
}
@NonNull
public static Animation getButtonFadeOut() {
return fadeOut;
}
public BottomControlButton(@NonNull ViewGroup bottomControlsViewGroup, @NonNull String imageViewButtonId,
@NonNull SettingsEnum booleanSetting, @NonNull View.OnClickListener onClickListener) {
LogHelper.printDebug(() -> "Initializing button: " + imageViewButtonId);
@ -32,7 +46,6 @@ public abstract class BottomControlButton {
if (booleanSetting.returnType != SettingsEnum.ReturnType.BOOLEAN) {
throw new IllegalArgumentException();
}
setting = booleanSetting;
// Create the button.