Skip to content

Commit

Permalink
Fixed issues #10, #11, #12, #13
Browse files Browse the repository at this point in the history
  • Loading branch information
GiorgioBertolotti committed May 24, 2019
1 parent 1dddcfd commit 9674c0a
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 151 deletions.
Expand Up @@ -78,8 +78,18 @@ public void onUpdate(Context context, AppWidgetManager widgetManager, int[] appW
PendingIntent service = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
long refreshRate = sharedPrefs.getLong("flutter.widget_refresh_interval", 15);
manager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), refreshRate * 60000, service);
WidgetUpdateService.DataElaborator dataElaborator = new WidgetUpdateService.DataElaborator(context, source, id);
dataElaborator.execute();
if(source.getUrl().endsWith(".txt") || source.getUrl().endsWith(".xml")) {
WidgetUpdateService.DataElaborator dataElaborator = new WidgetUpdateService.DataElaborator(context, source, id);
dataElaborator.execute();
} else {
String originalSource = source.getUrl();
source.setUrl(originalSource + "/realtime.txt");
WidgetUpdateService.DataElaborator dataElaborator = new WidgetUpdateService.DataElaborator(context, source, id);
dataElaborator.execute();
source.setUrl(originalSource + "/realtime.xml");
dataElaborator = new WidgetUpdateService.DataElaborator(context, source, id);
dataElaborator.execute();
}
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
widgetManager.updateAppWidget(id, views);
Log.d("PWSWatcher", "Started Widget #" + id);
Expand Down
Expand Up @@ -102,8 +102,18 @@ public void onCreate(Bundle icicle) {
}
long refreshRate = sharedPrefs.getLong("flutter.widget_refresh_interval", 15);
manager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), refreshRate * 60000, service);
WidgetUpdateService.DataElaborator dataElaborator = new WidgetUpdateService.DataElaborator(getApplicationContext(), source, mAppWidgetId);
dataElaborator.execute();
if(source.getUrl().endsWith(".txt") || source.getUrl().endsWith(".xml")) {
WidgetUpdateService.DataElaborator dataElaborator = new WidgetUpdateService.DataElaborator(getApplicationContext(), source, mAppWidgetId);
dataElaborator.execute();
} else {
String originalSource = source.getUrl();
source.setUrl(originalSource + "/realtime.txt");
WidgetUpdateService.DataElaborator dataElaborator = new WidgetUpdateService.DataElaborator(getApplicationContext(), source, mAppWidgetId);
dataElaborator.execute();
source.setUrl(originalSource + "/realtime.xml");
dataElaborator = new WidgetUpdateService.DataElaborator(getApplicationContext(), source, mAppWidgetId);
dataElaborator.execute();
}
widgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
Expand Down
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;

import okhttp3.Call;
import okhttp3.OkHttpClient;
Expand All @@ -43,8 +44,18 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}
}
if (source != null && id != -1) {
DataElaborator dataElaborator = new DataElaborator(this, source, id);
dataElaborator.execute();
if(source.getUrl().endsWith(".txt") || source.getUrl().endsWith(".xml")) {
DataElaborator dataElaborator = new DataElaborator(this, source, id);
dataElaborator.execute();
} else {
String originalSource = source.getUrl();
source.setUrl(originalSource + "/realtime.txt");
DataElaborator dataElaborator = new DataElaborator(this, source, id);
dataElaborator.execute();
source.setUrl(originalSource + "/realtime.xml");
dataElaborator = new DataElaborator(this, source, id);
dataElaborator.execute();
}
}
return super.onStartCommand(intent, flags, startId);
}
Expand All @@ -62,12 +73,12 @@ public DataElaborator(Context context, Source source, int id) {

@Override
protected String doInBackground(String... params) {
OkHttpClient client = new OkHttpClient.Builder().build();
Request request = new Request.Builder()
.url(this.source.getUrl())
.build();
Call call = client.newCall(request);
try {
OkHttpClient client = new OkHttpClient.Builder().build();
Request request = new Request.Builder()
.url(this.source.getUrl())
.build();
Call call = client.newCall(request);
Response response = call.execute();
return response.body().string();
} catch (IOException e) {
Expand All @@ -81,59 +92,85 @@ protected void onPostExecute(String resp) {
if (resp == null)
return;
RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.widget);
boolean done = false;
try {
if (this.source.getUrl().endsWith(".txt")) {
String[] values = resp.split(" ");
view.setTextViewText(R.id.tv_location, this.source.getName());
view.setTextViewText(R.id.tv_temperature, values[2] + values[14]);
view.setTextViewText(R.id.tv_datetime, values[0] + " " + values[1]);
done = visualizeRealtimeTXT(resp, view);
} else if (this.source.getUrl().endsWith(".xml")) {
XmlPullParserFactory parserFactory;
parserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = parserFactory.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(new StringReader(resp));
int eventType = parser.getEventType();
String date = null, time = null, temp = null, tempunit = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
String eltName = null;
switch (eventType) {
case XmlPullParser.START_TAG:
eltName = parser.getName();
if ("misc".equals(eltName)) {
for (int i = 0; i < parser.getAttributeCount(); i++) {
if (parser.getAttributeName(i).equals("data") && parser.getAttributeValue(i).equals("station_location")) {
view.setTextViewText(R.id.tv_location, parser.getText());
}
done = visualizeRealtimeXML(resp, view);
}
} catch (Exception e) {
e.printStackTrace();
}
if(done) {
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(this.id, view);
}
}

private boolean visualizeRealtimeTXT(String resp, RemoteViews view) {
try {
String[] values = resp.split(" ");
view.setTextViewText(R.id.tv_location, this.source.getName());
view.setTextViewText(R.id.tv_temperature, values[2] + values[14]);
view.setTextViewText(R.id.tv_datetime, values[0] + " " + values[1]);
return true;
} catch(Exception ignored) {}
return false;
}

private boolean visualizeRealtimeXML(String resp, RemoteViews view) {
try {
XmlPullParserFactory parserFactory;
parserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = parserFactory.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(new StringReader(resp));
int eventType = parser.getEventType();
String date = null, time = null, temp = null, tempunit = null;
String[] attributes = {"misc","realtime","today","yesterday","record"};
while (eventType != XmlPullParser.END_DOCUMENT) {
String eltName = null;
switch (eventType) {
case XmlPullParser.START_TAG:
eltName = parser.getName();
if ("misc".equals(eltName)) {
for (int i = 0; i < parser.getAttributeCount(); i++) {
if (parser.getAttributeName(i).equals("data") && parser.getAttributeValue(i).equals("station_location")) {
view.setTextViewText(R.id.tv_location, parser.nextText());
}
}
if ("data".equals(eltName)) {
for (int i = 0; i < parser.getAttributeCount(); i++) {
if (parser.getAttributeName(i).equals("realtime")) {
if (parser.getAttributeValue(i).equals("temp")) {
temp = parser.getText();
} else if (parser.getAttributeValue(i).equals("tempunit")) {
tempunit = parser.getText();
} else if (parser.getAttributeValue(i).equals("station_date")) {
date = parser.getText();
} else if (parser.getAttributeValue(i).equals("station_time")) {
time = parser.getText();
}
}
if ("data".equals(eltName)) {
for (int i = 0; i < parser.getAttributeCount(); i++) {
if (Arrays.asList(attributes).contains(parser.getAttributeName(i))) {
if (parser.getAttributeValue(i).equals("temp")) {
temp = parser.nextText();
} else if (parser.getAttributeValue(i).equals("tempunit")) {
tempunit = parser.nextText();
} else if (parser.getAttributeValue(i).equals("station_date")) {
date = parser.nextText();
} else if (parser.getAttributeValue(i).equals("station_time")) {
time = parser.nextText();
} else if (parser.getAttributeValue(i).equals("location")) {
view.setTextViewText(R.id.tv_location, parser.nextText());
} else if (parser.getAttributeValue(i).equals("refresh_time")) {
date = parser.nextText();
}
}
}
break;
}
eventType = parser.next();
}
break;
}
view.setTextViewText(R.id.tv_temperature, ((temp != null) ? temp : "") + ((tempunit != null) ? tempunit : ""));
view.setTextViewText(R.id.tv_datetime, ((date != null) ? date + " " : "") + ((time != null) ? time : ""));
eventType = parser.next();
}
} catch (XmlPullParserException e) {
} catch (IOException e) {
view.setTextViewText(R.id.tv_temperature, ((temp != null) ? temp : "") + ((tempunit != null) ? tempunit : ""));
view.setTextViewText(R.id.tv_datetime, ((date != null) ? date + " " : "") + ((time != null) ? time : ""));
return true;
} catch(Exception e) {
e.printStackTrace();
}
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(this.id, view);
return false;
}
}
}
Expand Up @@ -26,6 +26,10 @@ public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String toJSON() throws JSONException {
JSONObject root = new JSONObject();
root.put("id", this.id);
Expand Down

0 comments on commit 9674c0a

Please sign in to comment.