forked from ReVanced/revanced-integrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReturnYouTubeDislikes.java
148 lines (123 loc) 路 5.98 KB
/
ReturnYouTubeDislikes.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package app.revanced.integrations.ryd;
import static app.revanced.integrations.videoplayer.VideoInformation.currentVideoId;
import static app.revanced.integrations.videoplayer.VideoInformation.dislikeCount;
import android.content.Context;
import android.icu.text.CompactDecimalFormat;
import android.os.Build;
import android.text.SpannableString;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import app.revanced.integrations.ryd.requests.RYDRequester;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;
import app.revanced.integrations.utils.SharedPrefHelper;
public class ReturnYouTubeDislikes {
private static boolean isEnabled;
private static Thread _dislikeFetchThread = null;
private static Thread _votingThread = null;
private static Registration registration;
private static Voting voting;
private static CompactDecimalFormat compactNumberFormatter;
static {
Context context = ReVancedUtils.getContext();
isEnabled = SettingsEnum.RYD_ENABLED.getBoolean();
if (isEnabled) {
registration = new Registration();
voting = new Voting(registration);
}
Locale locale = context.getResources().getConfiguration().locale;
LogHelper.debug(ReturnYouTubeDislikes.class, "locale - " + locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
compactNumberFormatter = CompactDecimalFormat.getInstance(
locale,
CompactDecimalFormat.CompactStyle.SHORT
);
}
}
public static void onEnabledChange(boolean enabled) {
isEnabled = enabled;
if (registration == null) {
registration = new Registration();
}
if (voting == null) {
voting = new Voting(registration);
}
}
public static void newVideoLoaded(String videoId) {
LogHelper.debug(ReturnYouTubeDislikes.class, "newVideoLoaded - " + videoId);
dislikeCount = null;
if (!isEnabled) return;
try {
if (_dislikeFetchThread != null && _dislikeFetchThread.getState() != Thread.State.TERMINATED) {
LogHelper.debug(ReturnYouTubeDislikes.class, "Interrupting the thread. Current state " + _dislikeFetchThread.getState());
_dislikeFetchThread.interrupt();
}
} catch (Exception ex) {
LogHelper.printException(ReturnYouTubeDislikes.class, "Error in the dislike fetch thread", ex);
}
_dislikeFetchThread = new Thread(() -> RYDRequester.fetchDislikes(videoId));
_dislikeFetchThread.start();
}
public static void onComponentCreated(Object conversionContext, AtomicReference<Object> textRef) {
if (!isEnabled) return;
try {
// Contains a pathBuilder string, used to distinguish from other litho components
if (!conversionContext.toString().contains("dislike_button")) return;
LogHelper.debug(ReturnYouTubeDislikes.class, "dislike button was created");
// Have to block the current thread until fetching is done
// There's no known way to edit the text after creation yet
if (_dislikeFetchThread != null) _dislikeFetchThread.join();
if (dislikeCount != null) {
updateDislikeText(textRef, formatDislikes(dislikeCount));
}
} catch (Exception ex) {
LogHelper.printException(ReturnYouTubeDislikes.class, "Error while trying to set dislikes text", ex);
}
}
public static void sendVote(int vote) {
if (!isEnabled) return;
Context context = ReVancedUtils.getContext();
if (SharedPrefHelper.getBoolean(Objects.requireNonNull(context), SharedPrefHelper.SharedPrefNames.YOUTUBE, "user_signed_out", true))
return;
LogHelper.debug(ReturnYouTubeDislikes.class, "sending vote - " + vote + " for video " + currentVideoId);
try {
if (_votingThread != null && _votingThread.getState() != Thread.State.TERMINATED) {
LogHelper.debug(ReturnYouTubeDislikes.class, "Interrupting the thread. Current state " + _votingThread.getState());
_votingThread.interrupt();
}
} catch (Exception ex) {
LogHelper.printException(ReturnYouTubeDislikes.class, "Error in the voting thread", ex);
}
_votingThread = new Thread(() -> {
try {
boolean result = voting.sendVote(currentVideoId, vote);
LogHelper.debug(ReturnYouTubeDislikes.class, "sendVote status " + result);
} catch (Exception ex) {
LogHelper.printException(ReturnYouTubeDislikes.class, "Failed to send vote", ex);
}
});
_votingThread.start();
}
private static void updateDislikeText(AtomicReference<Object> textRef, String text) {
SpannableString oldString = (SpannableString) textRef.get();
SpannableString newString = new SpannableString(text);
// Copy style (foreground color, etc) to new string
Object[] spans = oldString.getSpans(0, oldString.length(), Object.class);
for (Object span : spans) {
int flags = oldString.getSpanFlags(span);
newString.setSpan(span, 0, newString.length(), flags);
}
textRef.set(newString);
}
private static String formatDislikes(int dislikes) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && compactNumberFormatter != null) {
final String formatted = compactNumberFormatter.format(dislikes);
LogHelper.debug(ReturnYouTubeDislikes.class, "Formatting dislikes - " + dislikes + " - " + formatted);
return formatted;
}
LogHelper.debug(ReturnYouTubeDislikes.class, "Couldn't format dislikes, using the unformatted count - " + dislikes);
return String.valueOf(dislikes);
}
}