Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
feat(Overlay Button - Always Repeat): Re-implement pause after repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
YT-Advanced committed Feb 3, 2024
1 parent 94beab4 commit e7fd995
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import static app.revanced.integrations.youtube.utils.ResourceUtils.integer;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.view.animation.Animation;
Expand All @@ -21,6 +25,7 @@ public class AlwaysRepeat {
volatile static boolean isShowing;
volatile static boolean isScrubbed;
static WeakReference<ImageView> buttonView = new WeakReference<>(null);
static final ColorFilter cf = new PorterDuffColorFilter(Color.parseColor("#fffffc79"), PorterDuff.Mode.SRC_ATOP);
@SuppressLint("StaticFieldLeak")
static ConstraintLayout constraintLayout;
static int fadeDurationFast;
Expand All @@ -35,7 +40,12 @@ public static void initialize(Object obj) {
ImageView imageView = findView(constraintLayout, "always_repeat_button");
imageView.setSelected(SettingsEnum.ALWAYS_REPEAT.getBoolean());
imageView.setOnClickListener(view -> AlwaysRepeat.changeSelected(!view.isSelected(), false));
imageView.setOnLongClickListener(view -> {
AlwaysRepeat.changeColorFilter();
return true;
});
buttonView = new WeakReference<>(imageView);
AlwaysRepeat.setColorFilter(SettingsEnum.ALWAYS_REPEAT_PAUSE.getBoolean());

fadeDurationFast = integer("fade_duration_fast");
fadeDurationScheduled = integer("fade_duration_scheduled");
Expand Down Expand Up @@ -91,17 +101,40 @@ public static void changeVisibilityNegatedImmediate(boolean isUserScrubbing) {

public static void changeSelected(boolean selected, boolean onlyView) {
ImageView imageView = buttonView.get();
if (constraintLayout == null || imageView == null)
if (constraintLayout == null || imageView == null || imageView.getColorFilter() == cf)
return;

imageView.setSelected(selected);
if (!onlyView) SettingsEnum.ALWAYS_REPEAT.saveValue(selected);
}

private static void changeColorFilter() {
ImageView imageView = buttonView.get();
if (constraintLayout == null || imageView == null) return;

imageView.setSelected(true);
SettingsEnum.ALWAYS_REPEAT.saveValue(true);

final boolean newValue = !SettingsEnum.ALWAYS_REPEAT_PAUSE.getBoolean();
SettingsEnum.ALWAYS_REPEAT_PAUSE.saveValue(newValue);
setColorFilter(newValue);
}

public static void refreshVisibility() {
isButtonEnabled = setValue();
}

private static void setColorFilter(boolean selected) {
ImageView imageView = buttonView.get();
if (constraintLayout == null || imageView == null)
return;

if (selected)
imageView.setColorFilter(cf);
else
imageView.clearColorFilter();
}

private static boolean setValue() {
return SettingsEnum.OVERLAY_BUTTON_ALWAYS_REPEAT.getBoolean();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
package app.revanced.integrations.youtube.patches.utils;

import android.content.Context;
import android.media.AudioManager;
import android.view.KeyEvent;

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

public class AlwaysRepeatPatch {

public static boolean enableAlwaysRepeat(boolean original) {
return !SettingsEnum.ALWAYS_REPEAT.getBoolean() && original;
}

public static void shouldRepeatAndPause() {
Context context = ReVancedUtils.getContext();
if (context == null)
return;

pauseMedia(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE), context);
pauseMedia(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PAUSE), context);

AudioManager audioManager = (AudioManager) context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);

if (audioManager == null)
return;

audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}

public static void pauseMedia(KeyEvent keyEvent, Context context) {
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
context.sendOrderedBroadcast(intent, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static app.revanced.integrations.youtube.utils.StringRef.str;
import static app.revanced.integrations.youtube.patches.video.PlaybackSpeedPatch.overrideSpeed;
import static app.revanced.integrations.youtube.patches.utils.AlwaysRepeatPatch.shouldRepeatAndPause

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -129,9 +130,13 @@ public static boolean videoEnded() {
if (!SettingsEnum.ALWAYS_REPEAT.getBoolean())
return false;

ReVancedUtils.runOnMainThreadDelayed(() -> seekTo(0), 0);
final boolean seekResult = false;
ReVancedUtils.runOnMainThreadDelayed(() -> seekResult = seekTo(0), 0);

return true;
if (SettingsEnum.ALWAYS_REPEAT_PAUSE.getBoolean() && seekResult)
shouldRepeatAndPause();

return seekResult;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ public enum SettingsEnum {

// Overlay Button
ALWAYS_REPEAT("revanced_always_repeat", BOOLEAN, FALSE),
ALWAYS_REPEAT_PAUSE("revanced_always_repeat_pause", BOOLEAN, FALSE),
OVERLAY_BUTTON_ALWAYS_REPEAT("revanced_overlay_button_always_repeat", BOOLEAN, FALSE),
OVERLAY_BUTTON_COPY_VIDEO_URL("revanced_overlay_button_copy_video_url", BOOLEAN, FALSE),
OVERLAY_BUTTON_COPY_VIDEO_URL_TIMESTAMP("revanced_overlay_button_copy_video_url_timestamp", BOOLEAN, FALSE),
Expand Down

0 comments on commit e7fd995

Please sign in to comment.