From: gu.martinm@gmail.com Date: Fri, 11 Apr 2014 07:30:09 +0000 (+0200) Subject: WeatherServicePersistenceFile X-Git-Tag: weatherinformation-1.0~177 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=6088ec0ca0a6ee9af63baaa196099d6a0b038351;p=AndroidWeatherInformation WeatherServicePersistenceFile --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 3cb28cd..488258d 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -57,6 +57,16 @@ + + + + + + + + diff --git a/src/de/example/exampletdd/WeatherInformationActivity.java b/src/de/example/exampletdd/WeatherInformationActivity.java index 978160c..8a3554b 100644 --- a/src/de/example/exampletdd/WeatherInformationActivity.java +++ b/src/de/example/exampletdd/WeatherInformationActivity.java @@ -2,8 +2,6 @@ package de.example.exampletdd; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; import java.io.StreamCorruptedException; import android.app.ActionBar; @@ -18,11 +16,12 @@ import android.view.MenuItem; import de.example.exampletdd.activityinterface.GetWeather; import de.example.exampletdd.fragment.overview.WeatherInformationOverviewFragment; import de.example.exampletdd.model.GeocodingData; +import de.example.exampletdd.service.WeatherServicePersistenceFile; public class WeatherInformationActivity extends Activity { - private static final String WEATHER_GEOCODING_FILE = "weathergeocoding.file"; private static final String TAG = "WeatherInformationActivity"; private GetWeather mGetWeather; + private WeatherServicePersistenceFile mWeatherServicePersistenceFile; @Override protected void onCreate(final Bundle savedInstanceState) { @@ -44,10 +43,13 @@ public class WeatherInformationActivity extends Activity { // this.getFragmentManager().beginTransaction() // .add(R.id.container, weatherDataFragment).commit(); // } - final WeatherInformationOverviewFragment weatherDataFragment = (WeatherInformationOverviewFragment) this + final WeatherInformationOverviewFragment weatherOverviewFragment = (WeatherInformationOverviewFragment) this .getFragmentManager().findFragmentById(R.id.weather_overview_fragment); - this.mGetWeather = weatherDataFragment; + this.mGetWeather = weatherOverviewFragment; + + this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile( + this); } @Override @@ -97,7 +99,8 @@ public class WeatherInformationActivity extends Activity { GeocodingData geocodingData = null; try { - geocodingData = this.restoreGeocodingDataFromFile(); + geocodingData = this.mWeatherServicePersistenceFile + .getGeocodingData(); } catch (final StreamCorruptedException e) { Log.e(TAG, "onCreate exception: ", e); } catch (final FileNotFoundException e) { @@ -121,22 +124,4 @@ public class WeatherInformationActivity extends Activity { public void getWeather() { this.mGetWeather.getWeather(); } - - private GeocodingData restoreGeocodingDataFromFile() - throws StreamCorruptedException, FileNotFoundException, - IOException, ClassNotFoundException { - final InputStream persistenceFile = this.openFileInput( - WEATHER_GEOCODING_FILE); - - ObjectInputStream ois = null; - try { - ois = new ObjectInputStream(persistenceFile); - - return (GeocodingData) ois.readObject(); - } finally { - if (ois != null) { - ois.close(); - } - } - } } diff --git a/src/de/example/exampletdd/WeatherInformationMapActivity.java b/src/de/example/exampletdd/WeatherInformationMapActivity.java index d934465..1651a13 100644 --- a/src/de/example/exampletdd/WeatherInformationMapActivity.java +++ b/src/de/example/exampletdd/WeatherInformationMapActivity.java @@ -2,17 +2,12 @@ package de.example.exampletdd; 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.util.List; import java.util.Locale; import android.app.Activity; import android.app.DialogFragment; -import android.content.Context; import android.location.Address; import android.location.Geocoder; import android.os.AsyncTask; @@ -31,12 +26,13 @@ import com.google.android.gms.maps.model.MarkerOptions; import de.example.exampletdd.fragment.ErrorDialogFragment; import de.example.exampletdd.fragment.ProgressDialogFragment; import de.example.exampletdd.model.GeocodingData; +import de.example.exampletdd.service.WeatherServicePersistenceFile; public class WeatherInformationMapActivity extends Activity { - private static final String WEATHER_GEOCODING_FILE = "weathergeocoding.file"; private static final String TAG = "WeatherInformationMapActivity"; private GoogleMap mMap; private Marker mMarker; + private WeatherServicePersistenceFile mWeatherServicePersistenceFile; @Override protected void onCreate(final Bundle savedInstanceState) { @@ -51,9 +47,11 @@ public class WeatherInformationMapActivity extends Activity { this.mMap.getUiSettings().setCompassEnabled(false); this.mMap.setOnMapLongClickListener(new LongClickListener()); + this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this); + GeocodingData geocodingData = null; try { - geocodingData = this.restoreGeocodingDataFromFile(); + geocodingData = this.mWeatherServicePersistenceFile.getGeocodingData(); } catch (final StreamCorruptedException e) { Log.e(TAG, "onResume exception: ", e); } catch (final FileNotFoundException e) { @@ -84,41 +82,6 @@ public class WeatherInformationMapActivity extends Activity { } } - private void storeGeocodingDataToFile(final GeocodingData geocodingData) - throws FileNotFoundException, IOException { - final OutputStream persistenceFile = this.openFileOutput( - WEATHER_GEOCODING_FILE, Context.MODE_PRIVATE); - - ObjectOutputStream oos = null; - try { - oos = new ObjectOutputStream(persistenceFile); - - oos.writeObject(geocodingData); - } finally { - if (oos != null) { - oos.close(); - } - } - } - - private GeocodingData restoreGeocodingDataFromFile() - throws StreamCorruptedException, FileNotFoundException, - IOException, ClassNotFoundException { - final InputStream persistenceFile = this - .openFileInput(WEATHER_GEOCODING_FILE); - - ObjectInputStream ois = null; - try { - ois = new ObjectInputStream(persistenceFile); - - return (GeocodingData) ois.readObject(); - } finally { - if (ois != null) { - ois.close(); - } - } - } - private class LongClickListener implements OnMapLongClickListener { @Override @@ -186,26 +149,26 @@ public class WeatherInformationMapActivity extends Activity { private void onPostExecuteThrowable(final GeocodingData geocodingData) throws FileNotFoundException, IOException { - WeatherInformationMapActivity.this.storeGeocodingDataToFile(geocodingData); + WeatherInformationMapActivity.this.mWeatherServicePersistenceFile + .storeGeocodingData(geocodingData); final String city = (geocodingData.getCity() == null) ? WeatherInformationMapActivity.this.getString(R.string.city_not_found) : geocodingData.getCity(); - final String country = (geocodingData.getCountry() == null) ? - WeatherInformationMapActivity.this.getString(R.string.country_not_found) - : geocodingData.getCountry(); - final TextView cityCountry = (TextView) WeatherInformationMapActivity.this + final String country = (geocodingData.getCountry() == null) ? + WeatherInformationMapActivity.this.getString(R.string.country_not_found) + : geocodingData.getCountry(); + final TextView cityCountry = (TextView) WeatherInformationMapActivity.this .findViewById(R.id.weather_map_citycountry_data); - cityCountry.setText(city + "," + country); + cityCountry.setText(city + "," + country); - final LatLng point = new LatLng(geocodingData.getLatitude(), geocodingData.getLongitude()); - if (WeatherInformationMapActivity.this.mMarker == null) { - WeatherInformationMapActivity.this.mMarker = - WeatherInformationMapActivity.this.mMap.addMarker + final LatLng point = new LatLng(geocodingData.getLatitude(), geocodingData.getLongitude()); + if (WeatherInformationMapActivity.this.mMarker == null) { + WeatherInformationMapActivity.this.mMarker = WeatherInformationMapActivity.this.mMap.addMarker (new MarkerOptions().position(point).draggable(true)); - } else { - WeatherInformationMapActivity.this.mMarker.setPosition(point); - } + } else { + WeatherInformationMapActivity.this.mMarker.setPosition(point); + } } private GeocodingData getGeocodingData(final double latitude, final double longitude) throws IOException { diff --git a/src/de/example/exampletdd/WeatherInformationSpecificDataActivity.java b/src/de/example/exampletdd/WeatherInformationSpecificDataActivity.java index ab7b326..3916103 100644 --- a/src/de/example/exampletdd/WeatherInformationSpecificDataActivity.java +++ b/src/de/example/exampletdd/WeatherInformationSpecificDataActivity.java @@ -2,8 +2,6 @@ package de.example.exampletdd; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; import java.io.StreamCorruptedException; import android.app.ActionBar; @@ -13,10 +11,11 @@ import android.preference.PreferenceManager; import android.util.Log; import de.example.exampletdd.fragment.specific.WeatherInformationSpecificDataFragment; import de.example.exampletdd.model.GeocodingData; +import de.example.exampletdd.service.WeatherServicePersistenceFile; public class WeatherInformationSpecificDataActivity extends Activity { - private static final String WEATHER_GEOCODING_FILE = "weathergeocoding.file"; private static final String TAG = "WeatherInformationSpecificDataActivity"; + private WeatherServicePersistenceFile mWeatherServicePersistenceFile; @Override protected void onCreate(final Bundle savedInstanceState) { @@ -39,6 +38,7 @@ public class WeatherInformationSpecificDataActivity extends Activity { .add(R.id.container, fragment).commit(); } + this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this); } @Override @@ -49,7 +49,8 @@ public class WeatherInformationSpecificDataActivity extends Activity { GeocodingData geocodingData = null; try { - geocodingData = this.restoreGeocodingDataFromFile(); + geocodingData = this.mWeatherServicePersistenceFile + .getGeocodingData(); } catch (final StreamCorruptedException e) { Log.e(TAG, "onCreate exception: ", e); } catch (final FileNotFoundException e) { @@ -68,22 +69,4 @@ public class WeatherInformationSpecificDataActivity extends Activity { } } - - private GeocodingData restoreGeocodingDataFromFile() - throws StreamCorruptedException, FileNotFoundException, - IOException, ClassNotFoundException { - final InputStream persistenceFile = this.openFileInput( - WEATHER_GEOCODING_FILE); - - ObjectInputStream ois = null; - try { - ois = new ObjectInputStream(persistenceFile); - - return (GeocodingData) ois.readObject(); - } finally { - if (ois != null) { - ois.close(); - } - } - } } diff --git a/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java b/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java index 5061dd5..30402dc 100644 --- a/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java +++ b/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java @@ -2,10 +2,6 @@ package de.example.exampletdd.fragment.overview; 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; @@ -26,7 +22,6 @@ import org.json.JSONException; import android.app.DialogFragment; import android.app.ListFragment; import android.content.ComponentName; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; @@ -49,27 +44,28 @@ import de.example.exampletdd.model.WeatherData; import de.example.exampletdd.parser.IJPOSWeatherParser; import de.example.exampletdd.parser.JPOSWeatherParser; import de.example.exampletdd.service.WeatherService; +import de.example.exampletdd.service.WeatherServicePersistenceFile; -public class WeatherInformationOverviewFragment extends ListFragment implements -GetWeather { - private static final String WEATHER_DATA_FILE = "weatherdata.file"; - private static final String WEATHER_GEOCODING_FILE = "weathergeocoding.file"; +public class WeatherInformationOverviewFragment extends ListFragment implements GetWeather { private static final String TAG = "WeatherInformationOverviewFragment"; private boolean mIsFahrenheit; private String mLanguage; + private WeatherServicePersistenceFile mWeatherServicePersistenceFile; @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); - this.getActivity().deleteFile(WEATHER_DATA_FILE); - final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this.getActivity()); final String keyPreference = this.getResources().getString( R.string.weather_preferences_language_key); this.mLanguage = sharedPreferences.getString( keyPreference, ""); + + this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile( + this.getActivity()); + this.mWeatherServicePersistenceFile.removeWeatherData(); } @Override @@ -85,7 +81,8 @@ GetWeather { final WeatherData weatherData = (WeatherData) savedInstanceState .getSerializable("weatherData"); try { - this.storeWeatherDataToFile(weatherData); + this.mWeatherServicePersistenceFile + .storeWeatherData(weatherData); } catch (final IOException e) { final DialogFragment newFragment = ErrorDialogFragment .newInstance(R.string.error_dialog_generic_error); @@ -110,13 +107,13 @@ GetWeather { @Override public void onListItemClick(final ListView l, final View v, final int position, final long id) { - final WeatherInformationSpecificDataFragment fragment = (WeatherInformationSpecificDataFragment) getFragmentManager() + final WeatherInformationSpecificDataFragment fragment = (WeatherInformationSpecificDataFragment) this.getFragmentManager() .findFragmentById(R.id.weather_specific_data__fragment); if (fragment == null) { // handset layout final Intent intent = new Intent("de.example.exampletdd.WEATHERINFO"). setComponent(new ComponentName("de.example.exampletdd", - "de.example.exampletdd.specific.WeatherInformationSpecificDataActivity")); + "de.example.exampletdd.WeatherInformationSpecificDataActivity")); WeatherInformationOverviewFragment.this.getActivity().startActivity(intent); } else { // tablet layout @@ -130,7 +127,7 @@ GetWeather { // Save state WeatherData weatherData = null; try { - weatherData = this.restoreWeatherDataFromFile(); + weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); } catch (final StreamCorruptedException e) { Log.e(TAG, "onResume exception: ", e); } catch (final FileNotFoundException e) { @@ -153,7 +150,8 @@ GetWeather { GeocodingData geocodingData = null; try { - geocodingData = this.restoreGeocodingDataFromFile(); + geocodingData = this.mWeatherServicePersistenceFile + .getGeocodingData(); } catch (final StreamCorruptedException e) { Log.e(TAG, "onResume exception: ", e); } catch (final FileNotFoundException e) { @@ -246,7 +244,7 @@ GetWeather { // 2. Update current data on display. WeatherData weatherData = null; try { - weatherData = this.restoreWeatherDataFromFile(); + weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); } catch (final StreamCorruptedException e) { Log.e(TAG, "onResume exception: ", e); } catch (final FileNotFoundException e) { @@ -391,7 +389,8 @@ GetWeather { private void onPostExecuteThrowable(final WeatherData weatherData) throws FileNotFoundException, IOException { - WeatherInformationOverviewFragment.this.storeWeatherDataToFile(weatherData); + WeatherInformationOverviewFragment.this.mWeatherServicePersistenceFile + .storeWeatherData(weatherData); WeatherInformationOverviewFragment.this.updateWeatherData(weatherData); } @@ -411,56 +410,4 @@ GetWeather { return entries; } - - private void storeWeatherDataToFile(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 restoreWeatherDataFromFile() 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(); - } - } - } - - private GeocodingData restoreGeocodingDataFromFile() - throws StreamCorruptedException, FileNotFoundException, - IOException, ClassNotFoundException { - final InputStream persistenceFile = this.getActivity() - .openFileInput(WEATHER_GEOCODING_FILE); - - ObjectInputStream ois = null; - try { - ois = new ObjectInputStream(persistenceFile); - - return (GeocodingData) ois.readObject(); - } finally { - if (ois != null) { - ois.close(); - } - } - } } diff --git a/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java b/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java index b53d4a6..fce0356 100644 --- a/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java +++ b/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java @@ -2,10 +2,6 @@ package de.example.exampletdd.fragment.specific; 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.text.DecimalFormat; import java.text.NumberFormat; @@ -18,7 +14,6 @@ import java.util.Locale; import android.app.DialogFragment; import android.app.Fragment; -import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -34,18 +29,20 @@ import de.example.exampletdd.R; import de.example.exampletdd.activityinterface.GetWeather; import de.example.exampletdd.fragment.ErrorDialogFragment; import de.example.exampletdd.model.WeatherData; +import de.example.exampletdd.service.WeatherServicePersistenceFile; public class WeatherInformationSpecificDataFragment extends Fragment implements GetWeather { - private static final String WEATHER_DATA_FILE = "weatherdata.file"; private static final String TAG = "WeatherInformationDataFragment"; private boolean mIsFahrenheit; private String mLanguage; + private WeatherServicePersistenceFile mWeatherServicePersistenceFile; @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); - this.getActivity().deleteFile(WEATHER_DATA_FILE); + this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile( + this.getActivity()); final SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(this.getActivity()); @@ -84,7 +81,7 @@ public class WeatherInformationSpecificDataFragment extends Fragment implements final WeatherData weatherData = (WeatherData) savedInstanceState .getSerializable("weatherData"); try { - this.storeWeatherDataToFile(weatherData); + this.mWeatherServicePersistenceFile.storeWeatherData(weatherData); } catch (final IOException e) { final DialogFragment newFragment = ErrorDialogFragment .newInstance(R.string.error_dialog_generic_error); @@ -99,7 +96,7 @@ public class WeatherInformationSpecificDataFragment extends Fragment implements // Save state WeatherData weatherData = null; try { - weatherData = this.restoreWeatherDataFromFile(); + weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); } catch (final StreamCorruptedException e) { Log.e(TAG, "onResume exception: ", e); } catch (final FileNotFoundException e) { @@ -121,7 +118,7 @@ public class WeatherInformationSpecificDataFragment extends Fragment implements public void getWeather() { WeatherData weatherData = null; try { - weatherData = this.restoreWeatherDataFromFile(); + weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); } catch (final StreamCorruptedException e) { Log.e(TAG, "onResume exception: ", e); } catch (final FileNotFoundException e) { @@ -221,7 +218,7 @@ public class WeatherInformationSpecificDataFragment extends Fragment implements // 2. Update current data on display. WeatherData weatherData = null; try { - weatherData = this.restoreWeatherDataFromFile(); + weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); } catch (final StreamCorruptedException e) { Log.e(TAG, "onResume exception: ", e); } catch (final FileNotFoundException e) { @@ -264,38 +261,4 @@ public class WeatherInformationSpecificDataFragment extends Fragment implements return entries; } - - private void storeWeatherDataToFile(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 restoreWeatherDataFromFile() 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/service/WeatherServicePersistence.java b/src/de/example/exampletdd/service/WeatherServicePersistence.java deleted file mode 100644 index e76e625..0000000 --- a/src/de/example/exampletdd/service/WeatherServicePersistence.java +++ /dev/null @@ -1,93 +0,0 @@ -package de.example.exampletdd.service; - -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 android.content.Context; -import de.example.exampletdd.model.GeocodingData; -import de.example.exampletdd.model.WeatherData; - -public class WeatherServicePersistence { - private static final String WEATHER_DATA_FILE = "weatherdata.file"; - private static final String WEATHER_GEOCODING_FILE = "weathergeocoding.file"; - private final Context context; - - public WeatherServicePersistence(final Context context) { - this.context = context; - } - - public void storeGeocodingDataToFile(final GeocodingData geocodingData) - throws FileNotFoundException, IOException { - final OutputStream persistenceFile = context.openFileOutput( - WEATHER_GEOCODING_FILE, Context.MODE_PRIVATE); - - ObjectOutputStream oos = null; - try { - oos = new ObjectOutputStream(persistenceFile); - - oos.writeObject(geocodingData); - } finally { - if (oos != null) { - oos.close(); - } - } - } - - public GeocodingData restoreGeocodingDataFromFile() - throws StreamCorruptedException, FileNotFoundException, - IOException, ClassNotFoundException { - final InputStream persistenceFile = context.openFileInput( - WEATHER_GEOCODING_FILE); - - ObjectInputStream ois = null; - try { - ois = new ObjectInputStream(persistenceFile); - - return (GeocodingData) ois.readObject(); - } finally { - if (ois != null) { - ois.close(); - } - } - } - - public void storeWeatherDataToFile(final WeatherData weatherData) - throws FileNotFoundException, IOException { - final OutputStream persistenceFile = context.openFileOutput( - WEATHER_DATA_FILE, Context.MODE_PRIVATE); - - ObjectOutputStream oos = null; - try { - oos = new ObjectOutputStream(persistenceFile); - - oos.writeObject(weatherData); - } finally { - if (oos != null) { - oos.close(); - } - } - } - - public WeatherData restoreWeatherDataFromFile() - throws StreamCorruptedException, - FileNotFoundException, IOException, ClassNotFoundException { - final InputStream persistenceFile = context.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/service/WeatherServicePersistenceFile.java b/src/de/example/exampletdd/service/WeatherServicePersistenceFile.java new file mode 100644 index 0000000..aecd422 --- /dev/null +++ b/src/de/example/exampletdd/service/WeatherServicePersistenceFile.java @@ -0,0 +1,101 @@ +package de.example.exampletdd.service; + +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 android.content.Context; +import de.example.exampletdd.model.GeocodingData; +import de.example.exampletdd.model.WeatherData; + +public class WeatherServicePersistenceFile { + private static final String WEATHER_DATA_FILE = "weatherdata.file"; + private static final String WEATHER_GEOCODING_FILE = "weathergeocoding.file"; + private final Context context; + + public WeatherServicePersistenceFile(final Context context) { + this.context = context; + } + + public void storeGeocodingData(final GeocodingData geocodingData) + throws FileNotFoundException, IOException { + final OutputStream persistenceFile = this.context.openFileOutput( + WEATHER_GEOCODING_FILE, Context.MODE_PRIVATE); + + ObjectOutputStream oos = null; + try { + oos = new ObjectOutputStream(persistenceFile); + + oos.writeObject(geocodingData); + } finally { + if (oos != null) { + oos.close(); + } + } + } + + public GeocodingData getGeocodingData() + throws StreamCorruptedException, FileNotFoundException, + IOException, ClassNotFoundException { + final InputStream persistenceFile = this.context.openFileInput( + WEATHER_GEOCODING_FILE); + + ObjectInputStream ois = null; + try { + ois = new ObjectInputStream(persistenceFile); + + return (GeocodingData) ois.readObject(); + } finally { + if (ois != null) { + ois.close(); + } + } + } + + public void removeGeocodingData() { + this.context.deleteFile(WEATHER_GEOCODING_FILE); + } + + public void storeWeatherData(final WeatherData weatherData) + throws FileNotFoundException, IOException { + final OutputStream persistenceFile = this.context.openFileOutput( + WEATHER_DATA_FILE, Context.MODE_PRIVATE); + + ObjectOutputStream oos = null; + try { + oos = new ObjectOutputStream(persistenceFile); + + oos.writeObject(weatherData); + } finally { + if (oos != null) { + oos.close(); + } + } + } + + public WeatherData getWeatherData() + throws StreamCorruptedException, + FileNotFoundException, IOException, ClassNotFoundException { + final InputStream persistenceFile = this.context.openFileInput( + WEATHER_DATA_FILE); + + ObjectInputStream ois = null; + try { + ois = new ObjectInputStream(persistenceFile); + + return (WeatherData) ois.readObject(); + } finally { + if (ois != null) { + ois.close(); + } + } + } + + public void removeWeatherData() { + this.context.deleteFile(WEATHER_DATA_FILE); + } +}