From 563b988db87b6c8cb064886e2a0fea995df3e361 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Fri, 11 Apr 2014 18:09:32 +0200 Subject: [PATCH] WeatherInformation service improvements --- .../exampletdd/WeatherInformationActivity.java | 22 ++--------- .../exampletdd/WeatherInformationMapActivity.java | 19 ++------- .../WeatherInformationSpecificDataActivity.java | 21 +--------- .../WeatherInformationOverviewFragment.java | 45 +++------------------ .../WeatherInformationSpecificDataFragment.java | 46 +++------------------- src/de/example/exampletdd/model/GeocodingData.java | 1 - src/de/example/exampletdd/model/WeatherData.java | 36 +++++++++++------ .../service/WeatherServicePersistenceFile.java | 42 +++++++++++++++++++- 8 files changed, 84 insertions(+), 148 deletions(-) diff --git a/src/de/example/exampletdd/WeatherInformationActivity.java b/src/de/example/exampletdd/WeatherInformationActivity.java index 8a3554b..2de943e 100644 --- a/src/de/example/exampletdd/WeatherInformationActivity.java +++ b/src/de/example/exampletdd/WeatherInformationActivity.java @@ -1,16 +1,11 @@ package de.example.exampletdd; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.StreamCorruptedException; - import android.app.ActionBar; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.preference.PreferenceManager; -import android.util.Log; import android.view.Menu; import android.view.MenuItem; import de.example.exampletdd.activityinterface.GetWeather; @@ -19,7 +14,6 @@ import de.example.exampletdd.model.GeocodingData; import de.example.exampletdd.service.WeatherServicePersistenceFile; public class WeatherInformationActivity extends Activity { - private static final String TAG = "WeatherInformationActivity"; private GetWeather mGetWeather; private WeatherServicePersistenceFile mWeatherServicePersistenceFile; @@ -97,19 +91,9 @@ public class WeatherInformationActivity extends Activity { final ActionBar actionBar = this.getActionBar(); - GeocodingData geocodingData = null; - try { - geocodingData = this.mWeatherServicePersistenceFile - .getGeocodingData(); - } catch (final StreamCorruptedException e) { - Log.e(TAG, "onCreate exception: ", e); - } catch (final FileNotFoundException e) { - Log.e(TAG, "onCreate exception: ", e); - } catch (final IOException e) { - Log.e(TAG, "onCreate exception: ", e); - } catch (final ClassNotFoundException e) { - Log.e(TAG, "onCreate exception: ", e); - } + final GeocodingData geocodingData = + this.mWeatherServicePersistenceFile.getGeocodingData(); + if (geocodingData != null) { final String city = (geocodingData.getCity() == null) ? this.getString(R.string.city_not_found) : geocodingData.getCity(); diff --git a/src/de/example/exampletdd/WeatherInformationMapActivity.java b/src/de/example/exampletdd/WeatherInformationMapActivity.java index 1651a13..747a440 100644 --- a/src/de/example/exampletdd/WeatherInformationMapActivity.java +++ b/src/de/example/exampletdd/WeatherInformationMapActivity.java @@ -2,7 +2,6 @@ package de.example.exampletdd; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.StreamCorruptedException; import java.util.List; import java.util.Locale; @@ -29,7 +28,6 @@ import de.example.exampletdd.model.GeocodingData; import de.example.exampletdd.service.WeatherServicePersistenceFile; public class WeatherInformationMapActivity extends Activity { - private static final String TAG = "WeatherInformationMapActivity"; private GoogleMap mMap; private Marker mMarker; private WeatherServicePersistenceFile mWeatherServicePersistenceFile; @@ -49,18 +47,7 @@ public class WeatherInformationMapActivity extends Activity { this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this); - GeocodingData geocodingData = null; - try { - geocodingData = this.mWeatherServicePersistenceFile.getGeocodingData(); - } 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); - } + final GeocodingData geocodingData = this.mWeatherServicePersistenceFile.getGeocodingData(); if (geocodingData != null) { final LatLng point = new LatLng( @@ -150,7 +137,7 @@ public class WeatherInformationMapActivity extends Activity { throws FileNotFoundException, IOException { WeatherInformationMapActivity.this.mWeatherServicePersistenceFile - .storeGeocodingData(geocodingData); + .storeGeocodingData(geocodingData); final String city = (geocodingData.getCity() == null) ? WeatherInformationMapActivity.this.getString(R.string.city_not_found) @@ -165,7 +152,7 @@ public class WeatherInformationMapActivity extends Activity { 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)); + (new MarkerOptions().position(point).draggable(true)); } else { WeatherInformationMapActivity.this.mMarker.setPosition(point); } diff --git a/src/de/example/exampletdd/WeatherInformationSpecificDataActivity.java b/src/de/example/exampletdd/WeatherInformationSpecificDataActivity.java index 3916103..130831c 100644 --- a/src/de/example/exampletdd/WeatherInformationSpecificDataActivity.java +++ b/src/de/example/exampletdd/WeatherInformationSpecificDataActivity.java @@ -1,20 +1,14 @@ package de.example.exampletdd; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.StreamCorruptedException; - import android.app.ActionBar; import android.app.Activity; import android.os.Bundle; 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 TAG = "WeatherInformationSpecificDataActivity"; private WeatherServicePersistenceFile mWeatherServicePersistenceFile; @Override @@ -47,19 +41,8 @@ public class WeatherInformationSpecificDataActivity extends Activity { final ActionBar actionBar = this.getActionBar(); - GeocodingData geocodingData = null; - try { - geocodingData = this.mWeatherServicePersistenceFile - .getGeocodingData(); - } catch (final StreamCorruptedException e) { - Log.e(TAG, "onCreate exception: ", e); - } catch (final FileNotFoundException e) { - Log.e(TAG, "onCreate exception: ", e); - } catch (final IOException e) { - Log.e(TAG, "onCreate exception: ", e); - } catch (final ClassNotFoundException e) { - Log.e(TAG, "onCreate exception: ", e); - } + final GeocodingData geocodingData = this.mWeatherServicePersistenceFile.getGeocodingData(); + if (geocodingData != null) { final String city = (geocodingData.getCity() == null) ? this.getString(R.string.city_not_found) : geocodingData.getCity(); diff --git a/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java b/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java index 30402dc..05a10a9 100644 --- a/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java +++ b/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java @@ -2,7 +2,6 @@ package de.example.exampletdd.fragment.overview; import java.io.FileNotFoundException; import java.io.IOException; -import java.io.StreamCorruptedException; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; @@ -47,7 +46,6 @@ import de.example.exampletdd.service.WeatherService; import de.example.exampletdd.service.WeatherServicePersistenceFile; public class WeatherInformationOverviewFragment extends ListFragment implements GetWeather { - private static final String TAG = "WeatherInformationOverviewFragment"; private boolean mIsFahrenheit; private String mLanguage; private WeatherServicePersistenceFile mWeatherServicePersistenceFile; @@ -125,18 +123,7 @@ public class WeatherInformationOverviewFragment extends ListFragment implements public void onSaveInstanceState(final Bundle savedInstanceState) { // Save state - WeatherData weatherData = null; - try { - weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); - } 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); - } + final WeatherData weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); if (weatherData != null) { savedInstanceState.putSerializable("weatherData", weatherData); @@ -148,19 +135,7 @@ public class WeatherInformationOverviewFragment extends ListFragment implements @Override public void getWeather() { - GeocodingData geocodingData = null; - try { - geocodingData = this.mWeatherServicePersistenceFile - .getGeocodingData(); - } 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); - } + final GeocodingData geocodingData = this.mWeatherServicePersistenceFile.getGeocodingData(); if (geocodingData != null) { final IJPOSWeatherParser JPOSWeatherParser = new JPOSWeatherParser(); @@ -242,18 +217,8 @@ public class WeatherInformationOverviewFragment extends ListFragment implements // 2. Update current data on display. - WeatherData weatherData = null; - try { - weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); - } 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); - } + final WeatherData weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); + if (weatherData != null) { this.updateWeatherData(weatherData); } @@ -390,7 +355,7 @@ public class WeatherInformationOverviewFragment extends ListFragment implements private void onPostExecuteThrowable(final WeatherData weatherData) throws FileNotFoundException, IOException { WeatherInformationOverviewFragment.this.mWeatherServicePersistenceFile - .storeWeatherData(weatherData); + .storeWeatherData(weatherData); WeatherInformationOverviewFragment.this.updateWeatherData(weatherData); } diff --git a/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java b/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java index fce0356..831adaf 100644 --- a/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java +++ b/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java @@ -1,8 +1,6 @@ package de.example.exampletdd.fragment.specific; -import java.io.FileNotFoundException; import java.io.IOException; -import java.io.StreamCorruptedException; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; @@ -19,7 +17,6 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.preference.PreferenceManager; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -32,7 +29,6 @@ import de.example.exampletdd.model.WeatherData; import de.example.exampletdd.service.WeatherServicePersistenceFile; public class WeatherInformationSpecificDataFragment extends Fragment implements GetWeather { - private static final String TAG = "WeatherInformationDataFragment"; private boolean mIsFahrenheit; private String mLanguage; private WeatherServicePersistenceFile mWeatherServicePersistenceFile; @@ -94,18 +90,8 @@ public class WeatherInformationSpecificDataFragment extends Fragment implements public void onSaveInstanceState(final Bundle savedInstanceState) { // Save state - WeatherData weatherData = null; - try { - weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); - } 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); - } + final WeatherData weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); + if (weatherData != null) { savedInstanceState.putSerializable("weatherData", weatherData); @@ -116,18 +102,8 @@ public class WeatherInformationSpecificDataFragment extends Fragment implements @Override public void getWeather() { - WeatherData weatherData = null; - try { - weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); - } 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); - } + final WeatherData weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); + if (weatherData != null) { this.updateWeatherData(weatherData); } @@ -216,18 +192,8 @@ public class WeatherInformationSpecificDataFragment extends Fragment implements // 2. Update current data on display. - WeatherData weatherData = null; - try { - weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); - } 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); - } + final WeatherData weatherData = this.mWeatherServicePersistenceFile.getWeatherData(); + if (weatherData != null) { this.updateWeatherData(weatherData); } diff --git a/src/de/example/exampletdd/model/GeocodingData.java b/src/de/example/exampletdd/model/GeocodingData.java index 226aff3..c6a129e 100644 --- a/src/de/example/exampletdd/model/GeocodingData.java +++ b/src/de/example/exampletdd/model/GeocodingData.java @@ -42,7 +42,6 @@ public class GeocodingData implements Serializable { } private GeocodingData(final Builder builder) { - super(); this.city = builder.mCity; this.country = builder.mCountry; this.latitude = builder.mLatitude; diff --git a/src/de/example/exampletdd/model/WeatherData.java b/src/de/example/exampletdd/model/WeatherData.java index 89c5dad..d198e4b 100644 --- a/src/de/example/exampletdd/model/WeatherData.java +++ b/src/de/example/exampletdd/model/WeatherData.java @@ -1,10 +1,12 @@ package de.example.exampletdd.model; import java.io.Serializable; +import java.util.Arrays; +import java.util.Date; public class WeatherData implements Serializable { - private static final long serialVersionUID = -9174787242150282821L; + private static final long serialVersionUID = -3521472950666173278L; private final Main main; private final Wind wind; private final Rain rain; @@ -15,6 +17,7 @@ public class WeatherData implements Serializable { private final Clouds clouds; private final Weather weather; private byte[] iconData; + private final Date date; public static class Builder { @@ -28,6 +31,7 @@ public class WeatherData implements Serializable { private System mSystem; private Clouds mClouds; private Weather mWeather; + private Date mDate; public Builder setMain(final Main main) { @@ -76,6 +80,11 @@ public class WeatherData implements Serializable { return this; } + public Builder setDate(final Date date) { + this.mDate = date; + return this; + } + public WeatherData build() { return new WeatherData(this); } @@ -91,21 +100,22 @@ public class WeatherData implements Serializable { this.system = builder.mSystem; this.clouds = builder.mClouds; this.weather = builder.mWeather; + this.date = builder.mDate; } @Override public String toString() { - final StringBuilder builder2 = new StringBuilder(); - builder2.append("WeatherData [main=").append(this.main) - .append(", wind=").append(this.wind).append(", rain=") - .append(this.rain).append(", coord=").append(this.coord) - .append(", dataReceivingTime=").append(this.dataReceivingTime) - .append(", stationName=").append(this.stationName) - .append(", system=").append(this.system).append(", clouds=") - .append(this.clouds).append(", weather=").append(this.weather) - .append("]"); - return builder2.toString(); + final StringBuilder builder = new StringBuilder(); + builder.append("WeatherData [main=").append(main) + .append(", wind=").append(wind).append(", rain=").append(rain) + .append(", coord=").append(coord).append(", dataReceivingTime=") + .append(dataReceivingTime).append(", stationName=").append(stationName) + .append(", system=").append(system).append(", clouds=") + .append(clouds).append(", weather=").append(weather) + .append(", iconData=").append(Arrays.toString(iconData)) + .append(", date=").append(date).append("]"); + return builder.toString(); } public Main getMain() { @@ -152,6 +162,10 @@ public class WeatherData implements Serializable { return this.iconData; } + public Date getDate() { + return this.date; + } + public static class Main implements Serializable { private static final long serialVersionUID = -1632181871917583409L; private final double temp; diff --git a/src/de/example/exampletdd/service/WeatherServicePersistenceFile.java b/src/de/example/exampletdd/service/WeatherServicePersistenceFile.java index aecd422..e23c5de 100644 --- a/src/de/example/exampletdd/service/WeatherServicePersistenceFile.java +++ b/src/de/example/exampletdd/service/WeatherServicePersistenceFile.java @@ -9,10 +9,12 @@ import java.io.OutputStream; import java.io.StreamCorruptedException; import android.content.Context; +import android.util.Log; import de.example.exampletdd.model.GeocodingData; import de.example.exampletdd.model.WeatherData; public class WeatherServicePersistenceFile { + private static final String TAG = "WeatherServicePersistenceFile"; private static final String WEATHER_DATA_FILE = "weatherdata.file"; private static final String WEATHER_GEOCODING_FILE = "weathergeocoding.file"; private final Context context; @@ -38,7 +40,25 @@ public class WeatherServicePersistenceFile { } } - public GeocodingData getGeocodingData() + public GeocodingData getGeocodingData() { + GeocodingData geocodingData = null; + + try { + geocodingData = this.getGeocodingDataThrowable(); + } catch (final StreamCorruptedException e) { + Log.e(TAG, "getGeocodingData exception: ", e); + } catch (final FileNotFoundException e) { + Log.e(TAG, "getGeocodingData exception: ", e); + } catch (final IOException e) { + Log.e(TAG, "getGeocodingData exception: ", e); + } catch (final ClassNotFoundException e) { + Log.e(TAG, "getGeocodingData exception: ", e); + } + + return geocodingData; + } + + private GeocodingData getGeocodingDataThrowable() throws StreamCorruptedException, FileNotFoundException, IOException, ClassNotFoundException { final InputStream persistenceFile = this.context.openFileInput( @@ -77,7 +97,25 @@ public class WeatherServicePersistenceFile { } } - public WeatherData getWeatherData() + public WeatherData getWeatherData() { + WeatherData weatherData = null; + + try { + weatherData = getWeatherDataThrowable(); + } catch (final StreamCorruptedException e) { + Log.e(TAG, "getWeatherData exception: ", e); + } catch (final FileNotFoundException e) { + Log.e(TAG, "getWeatherData exception: ", e); + } catch (final IOException e) { + Log.e(TAG, "getWeatherData exception: ", e); + } catch (final ClassNotFoundException e) { + Log.e(TAG, "getWeatherData exception: ", e); + } + + return weatherData; + } + + private WeatherData getWeatherDataThrowable() throws StreamCorruptedException, FileNotFoundException, IOException, ClassNotFoundException { final InputStream persistenceFile = this.context.openFileInput( -- 2.1.4