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;
import org.json.JSONException;
import android.app.Fragment;
+import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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,
final WeatherDataAdapter adapter = new WeatherDataAdapter(this.getActivity(),
R.layout.weather_data_entry_list);
- final Collection<WeatherDataEntry> entries = createEmptyEntriesList();
+ final Collection<WeatherDataEntry> entries = this.createEmptyEntriesList();
adapter.addAll(entries);
listWeatherView.setAdapter(adapter);
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z");
final double tempUnits = this.isFahrenheit ? 0 : 273.15;
- final List<WeatherDataEntry> entries = createEmptyEntriesList();
+ final List<WeatherDataEntry> entries = this.createEmptyEntriesList();
final ListView listWeatherView = (ListView) this.getActivity().findViewById(
R.id.weather_data_list_view);
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);
}
public void onResume() {
super.onResume();
+
final SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this.getActivity());
} 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<Object, Void, WeatherData> {
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();
}
@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();
return weatherData;
}
+
+ private void onPostExecuteThrowable(final WeatherData weatherData)
+ throws FileNotFoundException, IOException {
+ WeatherInformationDataFragment.this.storeWeatherData(weatherData);
+
+ WeatherInformationDataFragment.this.updateWeatherData(weatherData);
+ }
}
private List<WeatherDataEntry> createEmptyEntriesList() {
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();
+ }
+ }
+ }
}
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;
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;
}
}
- 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)
}
}
- 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
}
- 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;
}
}
- 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;
}
}
- 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) {
}
}
- 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;
}
}
- public static class Clouds {
+ public static class Clouds implements Serializable {
+ private static final long serialVersionUID = 8787321588449154348L;
// Cloudiness in %
private final double cloudiness;
}
}
- 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;