forked from ReVanced/revanced-integrations
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(YouTube): add
Watch history
patch inotia00/ReVanced_Extended#2275
- Loading branch information
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
app/src/main/java/app/revanced/integrations/youtube/patches/misc/WatchHistoryPatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...a/app/revanced/integrations/youtube/settings/preference/WatchHistoryStatusPreference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |