v0.8.2: Finished news implementation, including a full news scene
This commit is contained in:
@@ -29,10 +29,11 @@ import java.util.Date;
|
||||
public class DebugNews extends NewsService {
|
||||
|
||||
@Override
|
||||
public void checkForArticles(boolean useMetered, NewsResultCallback callback) {
|
||||
public void checkForArticles(boolean useMetered, boolean forceHTTPS, NewsResultCallback callback) {
|
||||
|
||||
if (!useMetered && !Game.platform.connectedToUnmeteredNetwork()){
|
||||
callback.onConnectionFailed();
|
||||
return;
|
||||
}
|
||||
|
||||
//turn on to test connection failure
|
||||
@@ -41,6 +42,11 @@ public class DebugNews extends NewsService {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean testUnread = false;
|
||||
//start placing articles either at the current time (if testing unread count)
|
||||
// or 10 days after 1st jan 1970
|
||||
long startTime = testUnread ? Game.realTime : 10*1000*60*60*24;
|
||||
|
||||
ArrayList<NewsArticle> articles = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++){
|
||||
NewsArticle article = new NewsArticle();
|
||||
@@ -51,10 +57,14 @@ public class DebugNews extends NewsService {
|
||||
"commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit " +
|
||||
"esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " +
|
||||
"non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
|
||||
// 10 to 1 days after Jan 1st 1970
|
||||
article.date = new Date(startTime - (i)*1000*60*60*24);
|
||||
|
||||
article.URL = "http://www.google.com";
|
||||
// a year in the past, plus one day for each article
|
||||
long daysBack = 365+i;
|
||||
article.date = new Date(System.currentTimeMillis() - (daysBack*1000*60*60*24));
|
||||
|
||||
//debug icon!
|
||||
article.icon = "sprites/spinner.png, 144, 0, 16, 16";
|
||||
|
||||
articles.add(article);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,14 +34,19 @@ import java.util.Locale;
|
||||
public class ShatteredNews extends NewsService {
|
||||
|
||||
@Override
|
||||
public void checkForArticles(boolean useMetered, NewsResultCallback callback) {
|
||||
public void checkForArticles(boolean useMetered, boolean preferHTTPS, NewsResultCallback callback) {
|
||||
|
||||
if (!useMetered && !Game.platform.connectedToUnmeteredNetwork()){
|
||||
callback.onConnectionFailed();
|
||||
return;
|
||||
}
|
||||
|
||||
Net.HttpRequest httpGet = new Net.HttpRequest(Net.HttpMethods.GET);
|
||||
httpGet.setUrl("http://shatteredpixel.com/feed");
|
||||
if (preferHTTPS) {
|
||||
httpGet.setUrl("https://shatteredpixel.com/feed");
|
||||
} else {
|
||||
httpGet.setUrl("http://shatteredpixel.com/feed");
|
||||
}
|
||||
|
||||
Gdx.net.sendHttpRequest(httpGet, new Net.HttpResponseListener() {
|
||||
@Override
|
||||
@@ -61,7 +66,17 @@ public class ShatteredNews extends NewsService {
|
||||
Game.reportException(e);
|
||||
}
|
||||
article.summary = xmlArticle.get("summary");
|
||||
article.URL = xmlArticle.get("id").replace("https://", "http://");
|
||||
article.URL = xmlArticle.getChildByName("link").getAttribute("href");
|
||||
if (!preferHTTPS) {
|
||||
article.URL = article.URL.replace("https://", "http://");
|
||||
}
|
||||
|
||||
try {
|
||||
article.icon = xmlArticle.getChildByName("category").getAttribute("term");
|
||||
} catch (Exception e){
|
||||
article.icon = null;
|
||||
}
|
||||
|
||||
articles.add(article);
|
||||
}
|
||||
callback.onArticlesFound(articles);
|
||||
|
||||
@@ -26,8 +26,12 @@ import java.util.Date;
|
||||
public class NewsArticle {
|
||||
|
||||
public String title;
|
||||
public String summary;
|
||||
public String URL;
|
||||
public Date date;
|
||||
public String summary;
|
||||
|
||||
public String URL;
|
||||
|
||||
//the icon is stored as a string here so it can be decoded to an image later
|
||||
//See News.java for supported formats
|
||||
public String icon;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,6 @@ public abstract class NewsService {
|
||||
public abstract void onConnectionFailed();
|
||||
}
|
||||
|
||||
public abstract void checkForArticles(boolean useMetered, NewsResultCallback callback);
|
||||
public abstract void checkForArticles(boolean useMetered, boolean forceHTTPS, NewsResultCallback callback);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user