Skip to content

Commit

Permalink
feat(YouTube): add Watch history patch inotia00/ReVanced_Extended#2275
Browse files Browse the repository at this point in the history
  • Loading branch information
inotia00 committed Jul 27, 2024
1 parent 64a5aeb commit c7f2cea
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package app.revanced.integrations.youtube.patches.misc;

import app.revanced.integrations.shared.utils.Logger;
import app.revanced.integrations.youtube.settings.Settings;

@SuppressWarnings("unused")
public final class WatchHistoryPatch {

public enum WatchHistoryType {
ORIGINAL,
REPLACE,
BLOCK
}

private static final String UNREACHABLE_HOST_URI_STRING = "https://127.0.0.0";
private static final String YOUTUBE_DOMAIN = "www.youtube.com";
private static final String YOUTUBE_TRACKING_DOMAIN = "s.youtube.com";

public static String replaceTrackingUrl(String originalUrl) {
final WatchHistoryType watchHistoryType = Settings.WATCH_HISTORY_TYPE.get();
if (watchHistoryType != WatchHistoryType.ORIGINAL) {
try {
if (originalUrl.contains(YOUTUBE_TRACKING_DOMAIN)) {
if (watchHistoryType == WatchHistoryType.REPLACE) {
final String replacement = originalUrl.replaceAll(YOUTUBE_TRACKING_DOMAIN, YOUTUBE_DOMAIN);
if (!replacement.equals(originalUrl)) {
Logger.printDebug(() -> "Replaced: '" + originalUrl + "'\nwith: '" + replacement + "'");
}
return replacement;
} else if (watchHistoryType == WatchHistoryType.BLOCK) {
Logger.printDebug(() -> "Blocking: " + originalUrl + " by returning: " + UNREACHABLE_HOST_URI_STRING);
return UNREACHABLE_HOST_URI_STRING;
}
}
} catch (Exception ex) {
Logger.printException(() -> "replaceTrackingUrl failure", ex);
}
}

return originalUrl;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public static boolean SponsorBlock() {
return false;
}

public static boolean SpoofClient() {
// Replace this with true if the Spoof client patch succeeds
return false;
}

public static boolean ToolBarComponents() {
// Replace this with true if the Toolbar components patch succeeds
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import app.revanced.integrations.youtube.patches.alternativethumbnails.AlternativeThumbnailsPatch.StillImagesAvailability;
import app.revanced.integrations.youtube.patches.alternativethumbnails.AlternativeThumbnailsPatch.ThumbnailOption;
import app.revanced.integrations.youtube.patches.alternativethumbnails.AlternativeThumbnailsPatch.ThumbnailStillTime;
import app.revanced.integrations.youtube.patches.misc.WatchHistoryPatch.WatchHistoryType;
import app.revanced.integrations.youtube.patches.misc.requests.PlayerRoutes.ClientType;
import app.revanced.integrations.youtube.patches.shorts.AnimationFeedbackPatch.AnimationType;
import app.revanced.integrations.youtube.sponsorblock.SponsorBlockSettings;
Expand Down Expand Up @@ -462,6 +463,8 @@ public class Settings extends BaseSettings {
// Use {@code ClientType.ANDROID_TESTSUITE}.
ClientType.ANDROID_TESTSUITE);

public static final EnumSetting<WatchHistoryType> WATCH_HISTORY_TYPE = new EnumSetting<>("revanced_watch_history_type", WatchHistoryType.REPLACE);

public static final BooleanSetting ENABLE_OPUS_CODEC = new BooleanSetting("revanced_enable_opus_codec", FALSE, true);

@Deprecated
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package app.revanced.integrations.youtube.settings.preference;

import static app.revanced.integrations.shared.utils.StringRef.str;
import static app.revanced.integrations.youtube.patches.utils.PatchStatus.SpoofClient;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;

import app.revanced.integrations.shared.settings.Setting;
import app.revanced.integrations.shared.utils.Utils;
import app.revanced.integrations.youtube.patches.misc.WatchHistoryPatch.WatchHistoryType;
import app.revanced.integrations.youtube.patches.misc.requests.PlayerRoutes.ClientType;
import app.revanced.integrations.youtube.settings.Settings;

@SuppressWarnings("unused")
public class WatchHistoryStatusPreference extends Preference {

private final SharedPreferences.OnSharedPreferenceChangeListener listener = (sharedPreferences, str) -> {
// Because this listener may run before the ReVanced settings fragment updates SettingsEnum,
// this could show the prior config and not the current.
//
// Push this call to the end of the main run queue,
// so all other listeners are done and SettingsEnum is up to date.
Utils.runOnMainThread(this::updateUI);
};

public WatchHistoryStatusPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public WatchHistoryStatusPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public WatchHistoryStatusPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public WatchHistoryStatusPreference(Context context) {
super(context);
}

private void addChangeListener() {
Setting.preferences.preferences.registerOnSharedPreferenceChangeListener(listener);
}

private void removeChangeListener() {
Setting.preferences.preferences.unregisterOnSharedPreferenceChangeListener(listener);
}

@Override
protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
super.onAttachedToHierarchy(preferenceManager);
updateUI();
addChangeListener();
}

@Override
protected void onPrepareForRemoval() {
super.onPrepareForRemoval();
removeChangeListener();
}

private void updateUI() {
final ClientType clientTypeIOS = ClientType.IOS;
final boolean spoofClientEnabled = SpoofClient() && Settings.SPOOF_CLIENT.get();
final boolean containsClientTypeIOS =
Settings.SPOOF_CLIENT_GENERAL.get() == clientTypeIOS ||
Settings.SPOOF_CLIENT_LIVESTREAM.get() == clientTypeIOS ||
Settings.SPOOF_CLIENT_SHORTS.get() == clientTypeIOS ||
Settings.SPOOF_CLIENT_FALLBACK.get() == clientTypeIOS;

final WatchHistoryType watchHistoryType = Settings.WATCH_HISTORY_TYPE.get();
final boolean blockWatchHistory = watchHistoryType == WatchHistoryType.BLOCK;
final boolean replaceWatchHistory = watchHistoryType == WatchHistoryType.REPLACE;

final String summaryTextKey;
if (blockWatchHistory) {
summaryTextKey = "revanced_watch_history_about_status_blocked";
} else if (spoofClientEnabled && containsClientTypeIOS) {
summaryTextKey = replaceWatchHistory
? "revanced_watch_history_about_status_ios_replaced"
: "revanced_watch_history_about_status_ios_original";
} else {
summaryTextKey = replaceWatchHistory
? "revanced_watch_history_about_status_android_replaced"
: "revanced_watch_history_about_status_android_original";
}

setSummary(str(summaryTextKey));
}
}

0 comments on commit c7f2cea

Please sign in to comment.