From 39d12122842962d6c70115fb7b348b973dc55a13 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Fri, 4 Apr 2014 03:22:05 +0200 Subject: [PATCH] Store weather data in file. --- .../fragment/WeatherInformationDataFragment.java | 103 ++++++++++++++++++--- src/de/example/exampletdd/model/WeatherData.java | 32 +++++-- 2 files changed, 114 insertions(+), 21 deletions(-) diff --git a/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java b/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java index 502a807..e955e93 100644 --- a/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java +++ b/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java @@ -1,6 +1,12 @@ package de.example.exampletdd.fragment; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.io.StreamCorruptedException; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; @@ -15,6 +21,7 @@ import org.apache.http.client.ClientProtocolException; import org.json.JSONException; import android.app.Fragment; +import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -39,7 +46,15 @@ import de.example.exampletdd.service.WeatherService; public class WeatherInformationDataFragment extends Fragment implements OnClickButtons { private boolean isFahrenheit; + private static final String WEATHER_DATA_FILE = "weatherdata.file"; + private static final String TAG = "WeatherInformationDataFragment"; + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + this.getActivity().deleteFile(WEATHER_DATA_FILE); + } @Override public View onCreateView(final LayoutInflater inflater, @@ -60,7 +75,7 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB final WeatherDataAdapter adapter = new WeatherDataAdapter(this.getActivity(), R.layout.weather_data_entry_list); - final Collection entries = createEmptyEntriesList(); + final Collection entries = this.createEmptyEntriesList(); adapter.addAll(entries); listWeatherView.setAdapter(adapter); @@ -88,7 +103,7 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z"); final double tempUnits = this.isFahrenheit ? 0 : 273.15; - final List entries = createEmptyEntriesList(); + final List entries = this.createEmptyEntriesList(); final ListView listWeatherView = (ListView) this.getActivity().findViewById( R.id.weather_data_list_view); @@ -131,7 +146,7 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB final Bitmap icon = BitmapFactory.decodeByteArray( weatherData.getIconData(), 0, weatherData.getIconData().length); - final ImageView imageIcon = (ImageView) getActivity().findViewById(R.id.weather_picture); + final ImageView imageIcon = (ImageView) this.getActivity().findViewById(R.id.weather_picture); imageIcon.setImageBitmap(icon); } @@ -146,6 +161,7 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB public void onResume() { super.onResume(); + final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this.getActivity()); @@ -159,6 +175,23 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB } else { this.isFahrenheit = true; } + + WeatherData weatherData = null; + try { + weatherData = this.restoreDataFromFile(); + } catch (final StreamCorruptedException e) { + Log.e(TAG, "onResume exception: ", e); + } catch (final FileNotFoundException e) { + Log.e(TAG, "onResume exception: ", e); + } catch (final IOException e) { + Log.e(TAG, "onResume exception: ", e); + } catch (final ClassNotFoundException e) { + Log.e(TAG, "onResume exception: ", e); + } + + if (weatherData != null) { + this.updateWeatherData(weatherData); + } } public class WeatherTask extends AsyncTask { @@ -179,15 +212,15 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB try { weatherData = this.doInBackgroundThrowable(params); } catch (final ClientProtocolException e) { - Log.e(TAG, "WeatherHTTPClient exception: ", e); + Log.e(TAG, "doInBackground exception: ", e); } catch (final MalformedURLException e) { - Log.e(TAG, "Syntax URL exception: ", e); + Log.e(TAG, "doInBackground exception: ", e); } catch (final URISyntaxException e) { - Log.e(TAG, "WeatherHTTPClient exception: ", e); + Log.e(TAG, "doInBackground exception: ", e); } catch (final IOException e) { - Log.e(TAG, "WeatherHTTPClient exception: ", e); + Log.e(TAG, "doInBackground exception: ", e); } catch (final JSONException e) { - Log.e(TAG, "WeatherService exception: ", e); + Log.e(TAG, "doInBackground exception: ", e); } finally { this.weatherHTTPClient.close(); } @@ -198,10 +231,17 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB @Override protected void onPostExecute(final WeatherData weatherData) { if (weatherData != null) { - WeatherInformationDataFragment.this.updateWeatherData(weatherData); + try { + this.onPostExecuteThrowable(weatherData); + } catch (final IOException e) { + ((ErrorMessage) WeatherInformationDataFragment.this + .getActivity()) + .createErrorDialog(R.string.error_dialog_generic_error); + } } else { - ((ErrorMessage) WeatherInformationDataFragment.this.getActivity()) - .createErrorDialog(R.string.error_dialog_generic_error); + ((ErrorMessage) WeatherInformationDataFragment.this + .getActivity()) + .createErrorDialog(R.string.error_dialog_generic_error); } this.weatherHTTPClient.close(); @@ -245,6 +285,13 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB return weatherData; } + + private void onPostExecuteThrowable(final WeatherData weatherData) + throws FileNotFoundException, IOException { + WeatherInformationDataFragment.this.storeWeatherData(weatherData); + + WeatherInformationDataFragment.this.updateWeatherData(weatherData); + } } private List createEmptyEntriesList() { @@ -263,4 +310,38 @@ public class WeatherInformationDataFragment extends Fragment implements OnClickB return entries; } + + private void storeWeatherData(final WeatherData weatherData) + throws FileNotFoundException, IOException { + final OutputStream persistenceFile = this.getActivity().openFileOutput( + WEATHER_DATA_FILE, Context.MODE_PRIVATE); + + ObjectOutputStream oos = null; + try { + oos = new ObjectOutputStream(persistenceFile); + + oos.writeObject(weatherData); + } finally { + if (oos != null) { + oos.close(); + } + } + } + + private WeatherData restoreDataFromFile() throws StreamCorruptedException, + FileNotFoundException, IOException, ClassNotFoundException { + final InputStream persistenceFile = this.getActivity().openFileInput( + WEATHER_DATA_FILE); + + ObjectInputStream ois = null; + try { + ois = new ObjectInputStream(persistenceFile); + + return (WeatherData) ois.readObject(); + } finally { + if (ois != null) { + ois.close(); + } + } + } } diff --git a/src/de/example/exampletdd/model/WeatherData.java b/src/de/example/exampletdd/model/WeatherData.java index c3187bc..89c5dad 100644 --- a/src/de/example/exampletdd/model/WeatherData.java +++ b/src/de/example/exampletdd/model/WeatherData.java @@ -1,7 +1,10 @@ package de.example.exampletdd.model; +import java.io.Serializable; -public class WeatherData { + +public class WeatherData implements Serializable { + private static final long serialVersionUID = -9174787242150282821L; private final Main main; private final Wind wind; private final Rain rain; @@ -149,7 +152,8 @@ public class WeatherData { return this.iconData; } - public static class Main { + public static class Main implements Serializable { + private static final long serialVersionUID = -1632181871917583409L; private final double temp; // Minimum temperature private final double minTemp; @@ -201,7 +205,8 @@ public class WeatherData { } } - public static class Wind { + public static class Wind implements Serializable { + private static final long serialVersionUID = -5960066625859236765L; // Wind speed in mps private final double speed; // Wind direction in degrees (meteorological) @@ -253,7 +258,8 @@ public class WeatherData { } } - public static class Rain { + public static class Rain implements Serializable { + private static final long serialVersionUID = 8975457888483866531L; // Period private final String time; // Precipitation volume for period @@ -282,7 +288,8 @@ public class WeatherData { } - public static class Coord { + public static class Coord implements Serializable { + private static final long serialVersionUID = 1462850471835376903L; // City location private final double longitude; private final double latitude; @@ -309,7 +316,8 @@ public class WeatherData { } } - public static class DataReceivingTime { + public static class DataReceivingTime implements Serializable { + private static final long serialVersionUID = 1953087163647283008L; // Time of data receiving in unixtime GMT private final double time; @@ -330,7 +338,8 @@ public class WeatherData { } } - public static class StationName { + public static class StationName implements Serializable { + private static final long serialVersionUID = -1064381425887709851L; private final String name; public StationName(final String name) { @@ -349,7 +358,8 @@ public class WeatherData { } } - public static class System { + public static class System implements Serializable { + private static final long serialVersionUID = 6066763201061358078L; private final String country; // Unixtime GMT private final long sunRiseTime; @@ -392,7 +402,8 @@ public class WeatherData { } } - public static class Clouds { + public static class Clouds implements Serializable { + private static final long serialVersionUID = 8787321588449154348L; // Cloudiness in % private final double cloudiness; @@ -413,7 +424,8 @@ public class WeatherData { } } - public static class Weather { + public static class Weather implements Serializable { + private static final long serialVersionUID = 6386458126182895773L; private final int weatherId; private final String main; private final String description; -- 2.1.4