From: Gustavo Martin Morcuende Date: Sat, 1 Nov 2014 01:30:53 +0000 (+0100) Subject: WeatherInformation: widget, refresh stores to permanent storage X-Git-Tag: weatherinformation-1.0~83 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=89e4c20efca276d4452f995985921d8240499768;p=AndroidWeatherInformation WeatherInformation: widget, refresh stores to permanent storage --- diff --git a/app/src/main/java/de/example/exampletdd/WidgetIntentService.java b/app/src/main/java/de/example/exampletdd/WidgetIntentService.java index 0653c73..d71506c 100644 --- a/app/src/main/java/de/example/exampletdd/WidgetIntentService.java +++ b/app/src/main/java/de/example/exampletdd/WidgetIntentService.java @@ -73,7 +73,7 @@ public class WidgetIntentService extends IntentService { RemoteViews view; final PermanentStorage store = new PermanentStorage(this.getApplicationContext()); - final Current current = store.getCurrent(); + final Current current = store.getCurrentWidget(appWidgetId); if (current != null) { // Update UI. view = this.makeView(current, weatherLocation, appWidgetId); @@ -89,6 +89,9 @@ public class WidgetIntentService extends IntentService { if (current != null) { // Update UI. view = this.makeView(current, weatherLocation, appWidgetId); + + final PermanentStorage store = new PermanentStorage(this.getApplicationContext()); + store.saveCurrentWidget(current, appWidgetId); } else { // Show error. view = this.makeErrorView(appWidgetId); diff --git a/app/src/main/java/de/example/exampletdd/service/PermanentStorage.java b/app/src/main/java/de/example/exampletdd/service/PermanentStorage.java index 49eac40..6da0c80 100644 --- a/app/src/main/java/de/example/exampletdd/service/PermanentStorage.java +++ b/app/src/main/java/de/example/exampletdd/service/PermanentStorage.java @@ -8,6 +8,7 @@ import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.StreamCorruptedException; +import java.text.MessageFormat; import android.content.Context; import android.util.Log; @@ -23,6 +24,7 @@ public class PermanentStorage { private static final String TAG = "PermanentStorage"; private static final String CURRENT_DATA_FILE = "current.file"; private static final String FORECAST_DATA_FILE = "forecast.file"; + private static final String WIDGET_CURRENT_DATA_FILE = "current.{0}.file"; private final Context context; public PermanentStorage(final Context context) { @@ -57,6 +59,36 @@ public class PermanentStorage { return null; } + public void saveCurrentWidget(final Current current, final int appWidgetId) { + + final String file = MessageFormat.format(WIDGET_CURRENT_DATA_FILE, appWidgetId); + try { + this.saveObject(file, current); + } catch (FileNotFoundException e) { + Log.e(TAG, "saveCurrent exception: ", e); + } catch (IOException e) { + Log.e(TAG, "saveCurrent exception: ", e); + } + } + + public Current getCurrentWidget(final int appWidgetId) { + + final String file = MessageFormat.format(WIDGET_CURRENT_DATA_FILE, appWidgetId); + try { + return (Current) this.getObject(file); + } catch (final StreamCorruptedException e) { + Log.e(TAG, "getCurrent exception: ", e); + } catch (final FileNotFoundException e) { + Log.e(TAG, "getCurrent exception: ", e); + } catch (final IOException e) { + Log.e(TAG, "getCurrent exception: ", e); + } catch (final ClassNotFoundException e) { + Log.e(TAG, "getCurrent exception: ", e); + } + + return null; + } + public void saveForecast(final Forecast forecast) { try {