This repository has been archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add announcement checker and UI tweaks
- Loading branch information
Showing
4 changed files
with
158 additions
and
5 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
osumer-lib/src/main/java/com/github/mob41/osumer/updater/Announcement.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,24 @@ | ||
package com.github.mob41.osumer.updater; | ||
|
||
import java.util.Calendar; | ||
|
||
public class Announcement { | ||
|
||
private final Calendar time; | ||
|
||
private final String text; | ||
|
||
public Announcement(Calendar time, String text) { | ||
this.time = time; | ||
this.text = text; | ||
} | ||
|
||
public Calendar getTime() { | ||
return time; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
|
||
} |
121 changes: 121 additions & 0 deletions
121
osumer-lib/src/main/java/com/github/mob41/osumer/updater/AnnouncementChecker.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,121 @@ | ||
package com.github.mob41.osumer.updater; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Calendar; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import com.github.mob41.osumer.debug.WithDumpException; | ||
|
||
public class AnnouncementChecker { | ||
|
||
private static final String ANNOUNCEMENT_LIST = "https://mob41.github.io/osumer-updater/announcements.json"; | ||
|
||
public Announcement[] getAnnouncements() throws WithDumpException{ | ||
URL url = null; | ||
|
||
try { | ||
url = new URL(ANNOUNCEMENT_LIST + "?update=" + Calendar.getInstance().getTimeInMillis()); | ||
} catch (MalformedURLException e){ | ||
throw new WithDumpException( | ||
ANNOUNCEMENT_LIST, | ||
"URL url = null;", | ||
"new URL(VERSION_LIST);", | ||
"URLConnection conn = url.openConnection();", | ||
"", | ||
false, e); | ||
} | ||
|
||
String data = ""; | ||
try { | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
|
||
conn.setRequestMethod("GET"); | ||
conn.setRequestProperty("Connection", "close"); | ||
conn.setUseCaches(false); | ||
conn.setConnectTimeout(10000); | ||
conn.setReadTimeout(10000); | ||
|
||
InputStream in = conn.getInputStream(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); | ||
|
||
String line; | ||
while ((line = reader.readLine()) != null){ | ||
data += line; | ||
} | ||
} catch (IOException e) { | ||
throw new WithDumpException( | ||
ANNOUNCEMENT_LIST, | ||
"URL url = new URL(VERSION_LIST);", | ||
"(lots of code) -- Connecting and fetch data", | ||
"Data validating (isEmpty / null)", | ||
"", | ||
false, e); | ||
} | ||
|
||
if (data == null || data.isEmpty()){ | ||
throw new WithDumpException( | ||
ANNOUNCEMENT_LIST, | ||
"(lots of code) -- Connecting and fetch data", | ||
"Data validating (isEmpty / null)", | ||
"Create JSONObject", | ||
"No data fetched. \"data\" is null/isEmpty", | ||
false); | ||
} | ||
|
||
JSONObject json = null; | ||
try { | ||
json = new JSONObject(data); | ||
} catch (JSONException e){ | ||
throw new WithDumpException( | ||
ANNOUNCEMENT_LIST, | ||
"Data validating (isEmpty / null)", | ||
"Create JSONObject", | ||
"JSONObject validating \"announcements\" parameter", | ||
"Structure invalid", | ||
false, e); | ||
} | ||
|
||
if (json.isNull("announcements")){ | ||
throw new WithDumpException( | ||
ANNOUNCEMENT_LIST, | ||
"Create JSONObject", | ||
"JSONObject validating \"sources\" parameter", | ||
"Parsing announcement json to objects", | ||
"Structure invalid, missing \"announcements\" parameter", | ||
false); | ||
} | ||
|
||
JSONArray arr = json.getJSONArray("announcements"); | ||
|
||
Calendar cal; | ||
JSONObject obj; | ||
Announcement[] out = new Announcement[arr.length()]; | ||
for (int i = 0; i < out.length; i++) { | ||
obj = arr.getJSONObject(i); | ||
if (obj.isNull("time") || obj.isNull("text")) { | ||
throw new WithDumpException( | ||
data, | ||
"JSONObject validating \"sources\" parameter", | ||
"Parsing announcement json to objects", | ||
"Output array", | ||
"Structure invalid, announcement json missing some parameters", | ||
false); | ||
} | ||
cal = Calendar.getInstance(); | ||
cal.setTimeInMillis(obj.getLong("time")); | ||
out[i] = new Announcement(cal, obj.getString("text")); | ||
} | ||
|
||
return out; | ||
} | ||
|
||
} |
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