From a1de20bccd510782d6df8d1919f3593632a9db63 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sat, 1 Nov 2014 03:10:52 +0100 Subject: [PATCH] WeatherInformation: widget, remove storaged data on delete widget --- .../de/example/exampletdd/WidgetIntentService.java | 12 ++++-- .../exampletdd/service/PermanentStorage.java | 44 ++++++++++++++++------ .../example/exampletdd/widget/WidgetProvider.java | 1 + 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/WidgetIntentService.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/WidgetIntentService.java index d71506c..ab27004 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/WidgetIntentService.java +++ b/Android/WeatherInformation/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.getCurrentWidget(appWidgetId); + final Current current = store.getWidgetCurrentData(appWidgetId); if (current != null) { // Update UI. view = this.makeView(current, weatherLocation, appWidgetId); @@ -91,7 +91,7 @@ public class WidgetIntentService extends IntentService { view = this.makeView(current, weatherLocation, appWidgetId); final PermanentStorage store = new PermanentStorage(this.getApplicationContext()); - store.saveCurrentWidget(current, appWidgetId); + store.saveWidgetCurrentData(current, appWidgetId); } else { // Show error. view = this.makeErrorView(appWidgetId); @@ -99,7 +99,13 @@ public class WidgetIntentService extends IntentService { this.updateWidget(view, appWidgetId); } } - + + public static void deleteWidgetCurrentData(final Context context, final int appWidgetId) { + final PermanentStorage store = new PermanentStorage(context.getApplicationContext()); + + store.removeWidgetCurrentData(appWidgetId); + } + private Current getRemoteCurrent(final WeatherLocation weatherLocation) { final ServiceParser weatherService = new ServiceParser(new JPOSWeatherParser()); diff --git a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/service/PermanentStorage.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/service/PermanentStorage.java index 6da0c80..db64873 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/service/PermanentStorage.java +++ b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/service/PermanentStorage.java @@ -59,36 +59,47 @@ public class PermanentStorage { return null; } - public void saveCurrentWidget(final Current current, final int appWidgetId) { + public void saveWidgetCurrentData(final Current current, final int appWidgetId) { - final String file = MessageFormat.format(WIDGET_CURRENT_DATA_FILE, appWidgetId); + final String fileName = MessageFormat.format(WIDGET_CURRENT_DATA_FILE, appWidgetId); try { - this.saveObject(file, current); + this.saveObject(fileName, current); } catch (FileNotFoundException e) { - Log.e(TAG, "saveCurrent exception: ", e); + Log.e(TAG, "saveWidgetCurrentData exception: ", e); } catch (IOException e) { - Log.e(TAG, "saveCurrent exception: ", e); + Log.e(TAG, "saveWidgetCurrentData exception: ", e); } } - public Current getCurrentWidget(final int appWidgetId) { + public Current getWidgetCurrentData(final int appWidgetId) { - final String file = MessageFormat.format(WIDGET_CURRENT_DATA_FILE, appWidgetId); + final String fileName = MessageFormat.format(WIDGET_CURRENT_DATA_FILE, appWidgetId); try { - return (Current) this.getObject(file); + return (Current) this.getObject(fileName); } catch (final StreamCorruptedException e) { - Log.e(TAG, "getCurrent exception: ", e); + Log.e(TAG, "getWidgetCurrentData exception: ", e); } catch (final FileNotFoundException e) { - Log.e(TAG, "getCurrent exception: ", e); + Log.e(TAG, "getWidgetCurrentData exception: ", e); } catch (final IOException e) { - Log.e(TAG, "getCurrent exception: ", e); + Log.e(TAG, "getWidgetCurrentData exception: ", e); } catch (final ClassNotFoundException e) { - Log.e(TAG, "getCurrent exception: ", e); + Log.e(TAG, "getWidgetCurrentData exception: ", e); } return null; } + public void removeWidgetCurrentData(final int appWidgetId) { + + final String fileName = MessageFormat.format(WIDGET_CURRENT_DATA_FILE, appWidgetId); + + try { + this.removeFile(fileName); + } catch (final IOException e) { + Log.e(TAG, "removeWidgetCurrentData exception: ", e); + } + } + public void saveForecast(final Forecast forecast) { try { @@ -168,5 +179,14 @@ public class PermanentStorage { throw new IOException("PermanentStorage, rename file error"); } } + + private void removeFile(final String fileName) throws IOException { + final File filesDir = this.context.getFilesDir(); + final File file = new File(filesDir, fileName); + + if (!file.delete()) { + throw new IOException("PermanentStorage, remove file error"); + } + } } diff --git a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java index 3ad5b0b..6e9df77 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java +++ b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java @@ -35,6 +35,7 @@ public class WidgetProvider extends AppWidgetProvider { final int N = appWidgetIds.length; for (int i=0; i