2
2
3
3
import static android .text .Html .FROM_HTML_MODE_COMPACT ;
4
4
import static app .revanced .extension .shared .StringRef .str ;
5
- import static app .revanced .extension .youtube .patches .announcements .requests .AnnouncementsRoutes .GET_LATEST_ANNOUNCEMENT ;
5
+ import static app .revanced .extension .youtube .patches .announcements .requests .AnnouncementsRoutes .GET_LATEST_ANNOUNCEMENTS ;
6
+ import static app .revanced .extension .youtube .patches .announcements .requests .AnnouncementsRoutes .GET_LATEST_ANNOUNCEMENT_IDS ;
6
7
7
8
import android .app .Activity ;
8
9
import android .app .AlertDialog ;
13
14
14
15
import androidx .annotation .RequiresApi ;
15
16
17
+ import org .json .JSONArray ;
16
18
import org .json .JSONObject ;
17
19
18
20
import java .io .IOException ;
19
21
import java .net .HttpURLConnection ;
22
+ import java .time .LocalDateTime ;
20
23
import java .util .Locale ;
24
+ import java .util .logging .Level ;
21
25
22
26
import app .revanced .extension .shared .Logger ;
23
27
import app .revanced .extension .shared .Utils ;
@@ -30,6 +34,45 @@ public final class AnnouncementsPatch {
30
34
private AnnouncementsPatch () {
31
35
}
32
36
37
+ @ RequiresApi (api = Build .VERSION_CODES .O )
38
+ private static boolean isLatestAlready () throws IOException {
39
+ HttpURLConnection connection =
40
+ AnnouncementsRoutes .getAnnouncementsConnectionFromRoute (GET_LATEST_ANNOUNCEMENT_IDS );
41
+
42
+ Logger .printDebug (() -> "Get latest announcement IDs route connection url: " + connection .getURL ());
43
+
44
+ try {
45
+ // Do not show the announcement if the request failed.
46
+ if (connection .getResponseCode () != 200 ) {
47
+ if (Settings .ANNOUNCEMENT_LAST_ID .isSetToDefault ())
48
+ return true ;
49
+
50
+ Settings .ANNOUNCEMENT_LAST_ID .resetToDefault ();
51
+ Utils .showToastLong (str ("revanced_announcements_connection_failed" ));
52
+
53
+ return true ;
54
+ }
55
+ } catch (IOException ex ) {
56
+ Logger .printException (() -> "Could not connect to announcements provider" , ex );
57
+ return true ;
58
+ }
59
+
60
+ var jsonString = Requester .parseStringAndDisconnect (connection );
61
+
62
+ // Parse the ID. Fall-back to raw string if it fails.
63
+ int id = Settings .ANNOUNCEMENT_LAST_ID .defaultValue ;
64
+ try {
65
+ final var announcementIds = new JSONArray (jsonString );
66
+ id = announcementIds .getJSONObject (0 ).getInt ("id" );
67
+
68
+ } catch (Throwable ex ) {
69
+ Logger .printException (() -> "Failed to parse announcement IDs" , ex );
70
+ }
71
+
72
+ // Do not show the announcement, if the last announcement id is the same as the current one.
73
+ return Settings .ANNOUNCEMENT_LAST_ID .get () == id ;
74
+ }
75
+
33
76
@ RequiresApi (api = Build .VERSION_CODES .O )
34
77
public static void showAnnouncement (final Activity context ) {
35
78
if (!Settings .ANNOUNCEMENTS .get ()) return ;
@@ -39,51 +82,45 @@ public static void showAnnouncement(final Activity context) {
39
82
40
83
Utils .runOnBackgroundThread (() -> {
41
84
try {
42
- HttpURLConnection connection = AnnouncementsRoutes .getAnnouncementsConnectionFromRoute (
43
- GET_LATEST_ANNOUNCEMENT , Locale .getDefault ().toLanguageTag ());
44
-
45
- Logger .printDebug (() -> "Get latest announcement route connection url: " + connection .getURL ());
85
+ if (isLatestAlready ()) return ;
46
86
47
- try {
48
- // Do not show the announcement if the request failed.
49
- if (connection .getResponseCode () != 200 ) {
50
- if (Settings .ANNOUNCEMENT_LAST_ID .isSetToDefault ())
51
- return ;
87
+ HttpURLConnection connection = AnnouncementsRoutes .getAnnouncementsConnectionFromRoute (
88
+ GET_LATEST_ANNOUNCEMENTS , Locale .getDefault ().toLanguageTag ());
52
89
53
- Settings .ANNOUNCEMENT_LAST_ID .resetToDefault ();
54
- Utils .showToastLong (str ("revanced_announcements_connection_failed" ));
55
-
56
- return ;
57
- }
58
- } catch (IOException ex ) {
59
- Logger .printException (() -> "Could not connect to announcements provider" , ex );
60
- return ;
61
- }
90
+ Logger .printDebug (() -> "Get latest announcements route connection url: " + connection .getURL ());
62
91
63
92
var jsonString = Requester .parseStringAndDisconnect (connection );
64
93
65
94
// Parse the announcement. Fall-back to raw string if it fails.
66
95
int id = Settings .ANNOUNCEMENT_LAST_ID .defaultValue ;
67
96
String title ;
68
97
String message ;
98
+ LocalDateTime archivedAt = LocalDateTime .MAX ;
69
99
Level level = Level .INFO ;
70
100
try {
71
101
final var announcement = new JSONObject (jsonString );
72
102
73
103
id = announcement .getInt ("id" );
74
104
title = announcement .getString ("title" );
75
105
message = announcement .getJSONObject ("content" ).getString ("message" );
76
- if (!announcement .isNull ("level" )) level = Level .fromInt (announcement .getInt ("level" ));
77
-
106
+ if (!announcement .isNull ("archived_at" )) {
107
+ archivedAt = LocalDateTime .parse (announcement .getString ("archived_at" ));
108
+ }
109
+ if (!announcement .isNull ("level" )) {
110
+ level = Level .fromInt (announcement .getInt ("level" ));
111
+ }
78
112
} catch (Throwable ex ) {
79
113
Logger .printException (() -> "Failed to parse announcement. Fall-backing to raw string" , ex );
80
114
81
115
title = "Announcement" ;
82
116
message = jsonString ;
83
117
}
84
118
85
- // Do not show the announcement, if the last announcement id is the same as the current one.
86
- if (Settings .ANNOUNCEMENT_LAST_ID .get () == id ) return ;
119
+ // If the announcement is archived, do not show it.
120
+ if (archivedAt .isBefore (LocalDateTime .now ())) {
121
+ Settings .ANNOUNCEMENT_LAST_ID .save (id );
122
+ return ;
123
+ }
87
124
88
125
int finalId = id ;
89
126
final var finalTitle = title ;
0 commit comments