1 package de.example.exampletdd.fragment.specific;
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.io.StreamCorruptedException;
6 import java.text.DecimalFormat;
7 import java.text.NumberFormat;
8 import java.text.SimpleDateFormat;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.Date;
12 import java.util.List;
13 import java.util.Locale;
15 import android.app.DialogFragment;
16 import android.app.Fragment;
17 import android.content.SharedPreferences;
18 import android.graphics.Bitmap;
19 import android.graphics.BitmapFactory;
20 import android.os.Bundle;
21 import android.preference.PreferenceManager;
22 import android.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.ImageView;
27 import android.widget.ListView;
28 import de.example.exampletdd.R;
29 import de.example.exampletdd.activityinterface.GetWeather;
30 import de.example.exampletdd.fragment.ErrorDialogFragment;
31 import de.example.exampletdd.model.WeatherData;
32 import de.example.exampletdd.service.WeatherServicePersistenceFile;
34 public class WeatherInformationSpecificDataFragment extends Fragment implements GetWeather {
35 private static final String TAG = "WeatherInformationDataFragment";
36 private boolean mIsFahrenheit;
37 private String mLanguage;
38 private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
41 public void onCreate(final Bundle savedInstanceState) {
42 super.onCreate(savedInstanceState);
44 this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
47 final SharedPreferences sharedPreferences = PreferenceManager
48 .getDefaultSharedPreferences(this.getActivity());
49 final String keyPreference = this.getResources().getString(
50 R.string.weather_preferences_language_key);
51 this.mLanguage = sharedPreferences.getString(
56 public View onCreateView(final LayoutInflater inflater,
57 final ViewGroup container, final Bundle savedInstanceState) {
58 final View rootView = inflater.inflate(R.layout.weather_data_list,
65 public void onActivityCreated(final Bundle savedInstanceState) {
66 super.onActivityCreated(savedInstanceState);
68 final ListView listWeatherView = (ListView) this.getActivity().findViewById(
69 R.id.weather_data_list_view);
71 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
72 R.layout.weather_data_entry_list);
74 final Collection<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
76 adapter.addAll(entries);
77 listWeatherView.setAdapter(adapter);
79 if (savedInstanceState != null) {
81 final WeatherData weatherData = (WeatherData) savedInstanceState
82 .getSerializable("weatherData");
84 this.mWeatherServicePersistenceFile.storeWeatherData(weatherData);
85 } catch (final IOException e) {
86 final DialogFragment newFragment = ErrorDialogFragment
87 .newInstance(R.string.error_dialog_generic_error);
88 newFragment.show(this.getFragmentManager(), "errorDialog");
94 public void onSaveInstanceState(final Bundle savedInstanceState) {
97 WeatherData weatherData = null;
99 weatherData = this.mWeatherServicePersistenceFile.getWeatherData();
100 } catch (final StreamCorruptedException e) {
101 Log.e(TAG, "onResume exception: ", e);
102 } catch (final FileNotFoundException e) {
103 Log.e(TAG, "onResume exception: ", e);
104 } catch (final IOException e) {
105 Log.e(TAG, "onResume exception: ", e);
106 } catch (final ClassNotFoundException e) {
107 Log.e(TAG, "onResume exception: ", e);
110 if (weatherData != null) {
111 savedInstanceState.putSerializable("weatherData", weatherData);
114 super.onSaveInstanceState(savedInstanceState);
118 public void getWeather() {
119 WeatherData weatherData = null;
121 weatherData = this.mWeatherServicePersistenceFile.getWeatherData();
122 } catch (final StreamCorruptedException e) {
123 Log.e(TAG, "onResume exception: ", e);
124 } catch (final FileNotFoundException e) {
125 Log.e(TAG, "onResume exception: ", e);
126 } catch (final IOException e) {
127 Log.e(TAG, "onResume exception: ", e);
128 } catch (final ClassNotFoundException e) {
129 Log.e(TAG, "onResume exception: ", e);
131 if (weatherData != null) {
132 this.updateWeatherData(weatherData);
136 public void updateWeatherData(final WeatherData weatherData) {
137 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault());
138 tempFormatter.applyPattern("#####.#####");
139 final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z", Locale.getDefault());
141 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
143 final List<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
145 final ListView listWeatherView = (ListView) this.getActivity().findViewById(
146 R.id.weather_data_list_view);
148 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
149 R.layout.weather_data_entry_list);
151 if (weatherData.getWeather() != null) {
152 entries.set(0, new WeatherSpecificDataEntry(this.getString(R.string.text_field_description), weatherData.getWeather()
154 double conversion = weatherData.getMain().getTemp();
155 conversion = conversion - tempUnits;
156 entries.set(1, new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem), tempFormatter.format(conversion)));
157 conversion = weatherData.getMain().getMaxTemp();
158 conversion = conversion - tempUnits;
159 entries.set(2, new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_max), tempFormatter.format(conversion)));
160 conversion = weatherData.getMain().getMinTemp();
161 conversion = conversion - tempUnits;
162 entries.set(3, new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_min), tempFormatter.format(conversion)));
165 if (weatherData.getSystem() != null) {
166 long unixTime = weatherData.getSystem().getSunRiseTime();
167 Date unixDate = new Date(unixTime * 1000L);
168 String dateFormatUnix = dateFormat.format(unixDate);
169 entries.set(4, new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_rise), dateFormatUnix));
171 unixTime = weatherData.getSystem().getSunSetTime();
172 unixDate = new Date(unixTime * 1000L);
173 dateFormatUnix = dateFormat.format(unixDate);
174 entries.set(5, new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_set), dateFormatUnix));
177 if (weatherData.getClouds() != null) {
178 final double cloudiness = weatherData.getClouds().getCloudiness();
179 entries.set(6, new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness), tempFormatter.format(cloudiness)));
182 if (weatherData.getWeather().getIcon() != null) {
183 final Bitmap icon = BitmapFactory.decodeByteArray(
184 weatherData.getIconData(), 0,
185 weatherData.getIconData().length);
186 final ImageView imageIcon = (ImageView) this.getActivity()
187 .findViewById(R.id.weather_picture);
188 imageIcon.setImageBitmap(icon);
193 listWeatherView.setAdapter(null);
194 adapter.addAll(entries);
195 listWeatherView.setAdapter(adapter);
199 public void onResume() {
202 final SharedPreferences sharedPreferences = PreferenceManager
203 .getDefaultSharedPreferences(this.getActivity());
205 // 1. Update units of measurement.
206 String keyPreference = this.getResources().getString(
207 R.string.weather_preferences_units_key);
208 final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
209 final String celsius = this.getResources().getString(
210 R.string.weather_preferences_units_celsius);
211 if (unitsPreferenceValue.equals(celsius)) {
212 this.mIsFahrenheit = false;
214 this.mIsFahrenheit = true;
218 // 2. Update current data on display.
219 WeatherData weatherData = null;
221 weatherData = this.mWeatherServicePersistenceFile.getWeatherData();
222 } catch (final StreamCorruptedException e) {
223 Log.e(TAG, "onResume exception: ", e);
224 } catch (final FileNotFoundException e) {
225 Log.e(TAG, "onResume exception: ", e);
226 } catch (final IOException e) {
227 Log.e(TAG, "onResume exception: ", e);
228 } catch (final ClassNotFoundException e) {
229 Log.e(TAG, "onResume exception: ", e);
231 if (weatherData != null) {
232 this.updateWeatherData(weatherData);
236 // 3. If language changed, try to retrieve new data for new language
237 // (new strings with the chosen language)
238 keyPreference = this.getResources().getString(
239 R.string.weather_preferences_language_key);
240 final String languagePreferenceValue = sharedPreferences.getString(
242 if (!languagePreferenceValue.equals(this.mLanguage)) {
243 this.mLanguage = languagePreferenceValue;
248 private List<WeatherSpecificDataEntry> createEmptyEntriesList() {
249 final List<WeatherSpecificDataEntry> entries = new ArrayList<WeatherSpecificDataEntry>();
250 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_description), null));
251 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem), null));
252 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_max), null));
253 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_min), null));
254 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_rise), null));
255 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_set), null));
256 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness), null));
257 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_time), null));
258 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_amount), null));
259 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_wind_speed), null));
260 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_humidity), null));