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 @Nullable
private static String timeWithoutSegments; private static String timeWithoutSegments;
private static float sponsorBarLeft = 1f; private static int sponsorBarAbsoluteLeft;
private static float sponsorBarRight = 1f; private static int sponsorAbsoluteBarRight;
private static float sponsorBarThickness = 2f; private static int sponsorBarThickness;
@Nullable @Nullable
static SponsorSegment[] getSegments() { static SponsorSegment[] getSegments() {
@ -235,7 +235,7 @@ public class SegmentPlaybackController {
SponsorSegment[] segments = SBRequester.getSegments(videoId); SponsorSegment[] segments = SBRequester.getSegments(videoId);
ReVancedUtils.runOnMainThread(()-> { ReVancedUtils.runOnMainThread(()-> {
if (!videoId.equals(SegmentPlaybackController.currentVideoId)) { if (!videoId.equals(currentVideoId)) {
// user changed videos before get segments network call could complete // user changed videos before get segments network call could complete
LogHelper.printDebug(() -> "Ignoring segments for prior video: " + videoId); LogHelper.printDebug(() -> "Ignoring segments for prior video: " + videoId);
return; return;
@ -525,7 +525,7 @@ public class SegmentPlaybackController {
if (!userManuallySkipped) { if (!userManuallySkipped) {
// check for any smaller embedded segments, and count those as autoskipped // check for any smaller embedded segments, and count those as autoskipped
final boolean showSkipToast = SettingsEnum.SB_SHOW_TOAST_ON_SKIP.getBoolean(); 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) { if (segmentToSkip.end < otherSegment.start) {
break; // no other segments can be contained 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 * Injection point
*/ */
@ -620,13 +606,9 @@ public class SegmentPlaybackController {
try { try {
Field field = self.getClass().getDeclaredField("replaceMeWithsetSponsorBarRect"); Field field = self.getClass().getDeclaredField("replaceMeWithsetSponsorBarRect");
field.setAccessible(true); field.setAccessible(true);
Rect rect = (Rect) field.get(self); Rect rect = (Rect) Objects.requireNonNull(field.get(self));
if (rect == null) { setSponsorBarAbsoluteLeft(rect);
LogHelper.printException(() -> "Could not find sponsorblock rect"); setSponsorBarAbsoluteRight(rect);
} else {
setSponsorBarAbsoluteLeft(rect.left);
setSponsorBarAbsoluteRight(rect.right);
}
} catch (Exception ex) { } catch (Exception ex) {
LogHelper.printException(() -> "setSponsorBarRect failure", ex); LogHelper.printException(() -> "setSponsorBarRect failure", ex);
} }
@ -635,27 +617,31 @@ public class SegmentPlaybackController {
/** /**
* Injection point. * Injection point.
*/ */
public static void setSponsorBarAbsoluteRight(final Rect rect) { public static void setSponsorBarAbsoluteLeft(Rect rect) {
setSponsorBarAbsoluteRight(rect.right); final int left = rect.left;
if (sponsorBarAbsoluteLeft != left) {
LogHelper.printDebug(() -> "setSponsorBarAbsoluteLeft: " + left);
sponsorBarAbsoluteLeft = left;
}
} }
public static void setSponsorBarAbsoluteRight(final float right) { /**
if (sponsorBarRight != right) { * Injection point.
LogHelper.printDebug(() -> String.format("setSponsorBarAbsoluteRight: right=%.2f", right)); */
sponsorBarRight = right; public static void setSponsorBarAbsoluteRight(Rect rect) {
final int right = rect.right;
if (sponsorAbsoluteBarRight != right) {
LogHelper.printDebug(() -> "setSponsorBarAbsoluteRight: " + right);
sponsorAbsoluteBarRight = right;
} }
} }
/** /**
* Injection point * Injection point
*/ */
public static void setSponsorBarThickness(final int thickness) { public static void setSponsorBarThickness(int thickness) {
setSponsorBarThickness((float) thickness);
}
public static void setSponsorBarThickness(final float thickness) {
if (sponsorBarThickness != thickness) { if (sponsorBarThickness != thickness) {
LogHelper.printDebug(() -> String.format("setSponsorBarThickness: %.2f", thickness)); LogHelper.printDebug(() -> "setSponsorBarThickness: " + thickness);
sponsorBarThickness = thickness; sponsorBarThickness = thickness;
} }
} }
@ -736,25 +722,23 @@ public class SegmentPlaybackController {
*/ */
public static void drawSponsorTimeBars(final Canvas canvas, final float posY) { public static void drawSponsorTimeBars(final Canvas canvas, final float posY) {
try { try {
if (sponsorBarThickness < 0.1) return;
if (segments == null) return; if (segments == null) return;
final long videoLength = VideoInformation.getVideoLength(); final long videoLength = VideoInformation.getVideoLength();
if (videoLength <= 0) return; if (videoLength <= 0) return;
final float thicknessDiv2 = sponsorBarThickness / 2; final int thicknessDiv2 = sponsorBarThickness / 2; // rounds down
final float top = posY - thicknessDiv2; final float top = posY - (sponsorBarThickness - thicknessDiv2);
final float bottom = posY + thicknessDiv2; final float bottom = posY + thicknessDiv2;
final float absoluteLeft = sponsorBarLeft; final float videoMillisecondsToPixels = (1f / videoLength) * (sponsorAbsoluteBarRight - sponsorBarAbsoluteLeft);
final float absoluteRight = sponsorBarRight; final float leftPadding = sponsorBarAbsoluteLeft;
final float tmp1 = (1f / videoLength) * (absoluteRight - absoluteLeft);
for (SponsorSegment segment : segments) { for (SponsorSegment segment : segments) {
final float left = segment.start * tmp1 + absoluteLeft; final float left = leftPadding + segment.start * videoMillisecondsToPixels;
final float right; final float right;
if (segment.category == SegmentCategory.HIGHLIGHT) { if (segment.category == SegmentCategory.HIGHLIGHT) {
right = left + getHighlightSegmentTimeBarScreenWidth(); right = left + getHighlightSegmentTimeBarScreenWidth();
} else { } else {
right = segment.end * tmp1 + absoluteLeft; right = leftPadding + segment.end * videoMillisecondsToPixels;
} }
canvas.drawRect(left, top, right, bottom, segment.category.paint); 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 static app.revanced.integrations.utils.ReVancedUtils.getResourceIdentifier;
import android.view.View; import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.RelativeLayout;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.Objects;
import app.revanced.integrations.patches.VideoInformation; import app.revanced.integrations.patches.VideoInformation;
import app.revanced.integrations.settings.SettingsEnum; import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils; import app.revanced.integrations.utils.ReVancedUtils;
import app.revanced.integrations.videoplayer.BottomControlButton;
public class CreateSegmentButtonController { public class CreateSegmentButtonController {
private static WeakReference<ImageView> buttonReference = new WeakReference<>(null); private static WeakReference<ImageView> buttonReference = new WeakReference<>(null);
private static Animation fadeIn;
private static Animation fadeOut;
private static boolean isShowing; private static boolean isShowing;
/** /**
* injection point * injection point
*/ */
public static void initialize(Object viewStub) { public static void initialize(View youtubeControlsLayout) {
try { try {
LogHelper.printDebug(() -> "initializing new segment button"); LogHelper.printDebug(() -> "initializing new segment button");
ImageView imageView = Objects.requireNonNull(youtubeControlsLayout.findViewById(
RelativeLayout youtubeControlsLayout = (RelativeLayout) viewStub; getResourceIdentifier("sb_sponsorblock_button", "id")));
String buttonIdentifier = "sb_sponsorblock_button"; imageView.setVisibility(View.GONE);
ImageView imageView = youtubeControlsLayout.findViewById(getResourceIdentifier(buttonIdentifier, "id"));
if (imageView == null) {
LogHelper.printException(() -> "Couldn't find imageView with \"" + buttonIdentifier + "\"");
return;
}
imageView.setOnClickListener(v -> { imageView.setOnClickListener(v -> {
LogHelper.printDebug(() -> "New segment button clicked");
SponsorBlockViewController.toggleNewSegmentLayoutVisibility(); SponsorBlockViewController.toggleNewSegmentLayoutVisibility();
}); });
buttonReference = new WeakReference<>(imageView);
// Animations buttonReference = new WeakReference<>(imageView);
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);
} catch (Exception ex) { } catch (Exception ex) {
LogHelper.printException(() -> "initialize failure", ex); LogHelper.printException(() -> "initialize failure", ex);
} }
@ -86,7 +69,7 @@ public class CreateSegmentButtonController {
return; return;
} }
if (!immediate) { if (!immediate) {
iView.startAnimation(fadeIn); iView.startAnimation(BottomControlButton.getButtonFadeIn());
} }
iView.setVisibility(View.VISIBLE); iView.setVisibility(View.VISIBLE);
return; return;
@ -95,7 +78,7 @@ public class CreateSegmentButtonController {
if (iView.getVisibility() == View.VISIBLE) { if (iView.getVisibility() == View.VISIBLE) {
iView.clearAnimation(); iView.clearAnimation();
if (!immediate) { if (!immediate) {
iView.startAnimation(fadeOut); iView.startAnimation(BottomControlButton.getButtonFadeOut());
} }
iView.setVisibility(View.GONE); iView.setVisibility(View.GONE);
} }

View File

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

View File

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

View File

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