1 package name.gumartinm.weather.information.service;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.ObjectInputStream;
9 import java.io.ObjectOutputStream;
10 import java.io.StreamCorruptedException;
11 import java.text.MessageFormat;
13 import android.content.Context;
14 import android.util.Log;
16 import name.gumartinm.weather.information.model.currentweather.Current;
17 import name.gumartinm.weather.information.model.forecastweather.Forecast;
20 public class PermanentStorage {
21 private static final String TAG = "PermanentStorage";
22 private static final String CURRENT_DATA_FILE = "current.file";
23 private static final String FORECAST_DATA_FILE = "forecast.file";
24 private static final String WIDGET_CURRENT_DATA_FILE = "current.{0}.file";
25 private final Context context;
27 public PermanentStorage(final Context context) {
28 this.context = context;
31 public void saveCurrent(final Current current) {
34 this.saveObject(CURRENT_DATA_FILE, current);
35 } catch (FileNotFoundException e) {
36 Log.e(TAG, "saveCurrent exception: ", e);
37 } catch (IOException e) {
38 Log.e(TAG, "saveCurrent exception: ", e);
42 public Current getCurrent() {
45 return (Current) this.getObject(CURRENT_DATA_FILE);
46 } catch (final StreamCorruptedException e) {
47 Log.e(TAG, "getCurrent exception: ", e);
48 } catch (final FileNotFoundException e) {
49 Log.e(TAG, "getCurrent exception: ", e);
50 } catch (final IOException e) {
51 Log.e(TAG, "getCurrent exception: ", e);
52 } catch (final ClassNotFoundException e) {
53 Log.e(TAG, "getCurrent exception: ", e);
59 public void saveWidgetCurrentData(final Current current, final int appWidgetId) {
61 final String fileName = MessageFormat.format(WIDGET_CURRENT_DATA_FILE, appWidgetId);
63 this.saveObject(fileName, current);
64 } catch (FileNotFoundException e) {
65 Log.e(TAG, "saveWidgetCurrentData exception: ", e);
66 } catch (IOException e) {
67 Log.e(TAG, "saveWidgetCurrentData exception: ", e);
71 public Current getWidgetCurrentData(final int appWidgetId) {
73 final String fileName = MessageFormat.format(WIDGET_CURRENT_DATA_FILE, appWidgetId);
75 return (Current) this.getObject(fileName);
76 } catch (final StreamCorruptedException e) {
77 Log.e(TAG, "getWidgetCurrentData exception: ", e);
78 } catch (final FileNotFoundException e) {
79 Log.e(TAG, "getWidgetCurrentData exception: ", e);
80 } catch (final IOException e) {
81 Log.e(TAG, "getWidgetCurrentData exception: ", e);
82 } catch (final ClassNotFoundException e) {
83 Log.e(TAG, "getWidgetCurrentData exception: ", e);
89 public void removeWidgetCurrentData(final int appWidgetId) {
91 final String fileName = MessageFormat.format(WIDGET_CURRENT_DATA_FILE, appWidgetId);
94 this.removeFile(fileName);
95 } catch (final IOException e) {
96 Log.e(TAG, "removeWidgetCurrentData exception: ", e);
100 public void saveForecast(final Forecast forecast) {
103 this.saveObject(FORECAST_DATA_FILE, forecast);
104 } catch (FileNotFoundException e) {
105 Log.e(TAG, "saveForecast exception: ", e);
106 } catch (IOException e) {
107 Log.e(TAG, "saveForecast exception: ", e);
111 public Forecast getForecast() {
114 return (Forecast) this.getObject(FORECAST_DATA_FILE);
115 } catch (final StreamCorruptedException e) {
116 Log.e(TAG, "getForecast exception: ", e);
117 } catch (final FileNotFoundException e) {
118 Log.e(TAG, "getForecast exception: ", e);
119 } catch (final IOException e) {
120 Log.e(TAG, "getForecast exception: ", e);
121 } catch (final ClassNotFoundException e) {
122 Log.e(TAG, "getForecast exception: ", e);
128 private void saveObject(final String fileName, final Object objectToStore)
129 throws FileNotFoundException, IOException {
130 final String temporaryFileName = fileName.concat(".tmp");
132 final FileOutputStream tmpPersistFile = this.context.openFileOutput(
133 temporaryFileName, Context.MODE_PRIVATE);
135 final ObjectOutputStream oos = new ObjectOutputStream(tmpPersistFile);
137 oos.writeObject(objectToStore);
139 // Don't fear the fsync!
140 // http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
141 tmpPersistFile.flush();
142 tmpPersistFile.getFD().sync();
147 tmpPersistFile.close();
150 this.renameFile(temporaryFileName, fileName);
153 private Object getObject(final String fileName) throws StreamCorruptedException, FileNotFoundException,
154 IOException, ClassNotFoundException {
155 final InputStream persistFile = this.context.openFileInput(fileName);
157 final ObjectInputStream ois = new ObjectInputStream(persistFile);
159 return ois.readObject();
168 private void renameFile(final String fromFileName, final String toFileName) throws IOException {
169 final File filesDir = this.context.getFilesDir();
170 final File fromFile = new File(filesDir, fromFileName);
171 final File toFile = new File(filesDir, toFileName);
172 if (!fromFile.renameTo(toFile)) {
173 if (!fromFile.delete()) {
174 throw new IOException("PermanentStorage, delete file error");
176 throw new IOException("PermanentStorage, rename file error");
180 private void removeFile(final String fileName) throws IOException {
181 final File filesDir = this.context.getFilesDir();
182 final File file = new File(filesDir, fileName);
184 if (!file.delete()) {
185 throw new IOException("PermanentStorage, remove file error");