19ea58f5b1d6e9f61103912d21705afb7a28c71b
[JavaForFun] /
1 package de.example.exampletdd.fragment.specific;
2
3 import java.io.IOException;
4 import java.text.DecimalFormat;
5 import java.text.NumberFormat;
6 import java.text.SimpleDateFormat;
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.Date;
10 import java.util.List;
11 import java.util.Locale;
12
13 import android.app.DialogFragment;
14 import android.app.Fragment;
15 import android.content.SharedPreferences;
16 import android.graphics.Bitmap;
17 import android.graphics.BitmapFactory;
18 import android.os.Bundle;
19 import android.preference.PreferenceManager;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.ImageView;
24 import android.widget.ListView;
25 import de.example.exampletdd.R;
26 import de.example.exampletdd.activityinterface.GetWeather;
27 import de.example.exampletdd.fragment.ErrorDialogFragment;
28 import de.example.exampletdd.model.currentweather.CurrentWeatherData;
29 import de.example.exampletdd.model.forecastweather.ForecastWeatherData;
30 import de.example.exampletdd.service.WeatherServicePersistenceFile;
31
32 public class WeatherInformationSpecificDataFragment extends Fragment implements GetWeather {
33     private boolean mIsFahrenheit;
34     private long mChosenDay;
35     private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
36
37     @Override
38     public void onCreate(final Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40
41         final Bundle extras = this.getActivity().getIntent().getExtras();
42
43         if (extras != null) {
44             this.mChosenDay = extras.getLong("CHOSEN_DAY", 0);
45         } else {
46             this.mChosenDay = 0;
47         }
48
49         this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
50                 this.getActivity());
51
52         // final SharedPreferences sharedPreferences = PreferenceManager
53         // .getDefaultSharedPreferences(this.getActivity());
54         // final String keyPreference = this.getResources().getString(
55         // R.string.weather_preferences_language_key);
56         // this.mLanguage = sharedPreferences.getString(
57         // keyPreference, "");
58     }
59
60     @Override
61     public View onCreateView(final LayoutInflater inflater,
62             final ViewGroup container, final Bundle savedInstanceState) {
63         final View rootView = inflater.inflate(R.layout.weather_data_list,
64                 container, false);
65
66         return rootView;
67     }
68
69     @Override
70     public void onActivityCreated(final Bundle savedInstanceState) {
71         super.onActivityCreated(savedInstanceState);
72
73         final ListView listWeatherView = (ListView) this.getActivity().findViewById(
74                 R.id.weather_data_list_view);
75
76         final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
77                 R.layout.weather_data_entry_list);
78
79         final Collection<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
80
81         adapter.addAll(entries);
82         listWeatherView.setAdapter(adapter);
83
84         if (savedInstanceState != null) {
85             // Restore state
86             final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
87                     .getSerializable("ForecastWeatherData");
88             final CurrentWeatherData currentWeatherData = (CurrentWeatherData) savedInstanceState
89                     .getSerializable("CurrentWeatherData");
90
91             if ((forecastWeatherData != null) && (currentWeatherData != null)) {
92                 try {
93                     this.mWeatherServicePersistenceFile
94                     .storeForecastWeatherData(forecastWeatherData);
95                     this.mWeatherServicePersistenceFile.storeCurrentWeatherData(currentWeatherData);
96                 } catch (final IOException e) {
97                     final DialogFragment newFragment = ErrorDialogFragment
98                             .newInstance(R.string.error_dialog_generic_error);
99                     newFragment.show(this.getFragmentManager(), "errorDialog");
100                 }
101             }
102         }
103     }
104
105     @Override
106     public void onSaveInstanceState(final Bundle savedInstanceState) {
107
108         // Save state
109         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
110                 .getForecastWeatherData();
111
112         final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
113                 .getCurrentWeatherData();
114
115         if ((forecastWeatherData != null) && (currentWeatherData != null)) {
116             savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
117             savedInstanceState.putSerializable("CurrentWeatherData", currentWeatherData);
118         }
119
120         super.onSaveInstanceState(savedInstanceState);
121     }
122
123     @Override
124     public void getWeatherByDay(final int chosenDay) {
125         if (chosenDay == 0) {
126             final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
127                     .getCurrentWeatherData();
128
129             if (currentWeatherData != null) {
130                 this.updateCurrentWeatherData(currentWeatherData);
131             }
132         } else {
133             final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
134                     .getForecastWeatherData();
135             if (forecastWeatherData != null) {
136                 this.updateForecastWeatherData(forecastWeatherData, chosenDay);
137             }
138         }
139     }
140
141     @Override
142     public void getRemoteWeatherInformation() {
143         // Nothing to do.
144     }
145
146     public void updateCurrentWeatherData(final CurrentWeatherData currentWeatherData) {
147         final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault());
148         tempFormatter.applyPattern("#####.#####");
149         final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z", Locale.getDefault());
150
151         final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
152
153         final List<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
154
155         final ListView listWeatherView = (ListView) this.getActivity().findViewById(
156                 R.id.weather_data_list_view);
157
158         final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
159                 R.layout.weather_data_entry_list);
160
161         if (currentWeatherData.getWeather().size() > 0) {
162             entries.set(0, new WeatherSpecificDataEntry(this.getString(R.string.text_field_description),
163                     currentWeatherData.getWeather().get(0).getDescription()));
164         }
165
166         if (currentWeatherData.getMain().getTemp() != null) {
167             double conversion = (Double) currentWeatherData.getMain().getTemp();
168             conversion = conversion - tempUnits;
169             entries.set(1, new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem),
170                     tempFormatter.format(conversion)));
171         }
172
173         if (currentWeatherData.getMain().getTemp_max() != null) {
174             double conversion = (Double) currentWeatherData.getMain().getTemp_max();
175             conversion = conversion - tempUnits;
176             entries.set(2, new WeatherSpecificDataEntry(
177                     this.getString(R.string.text_field_tem_max), tempFormatter.format(conversion)));
178         }
179
180         if (currentWeatherData.getMain().getTemp_max() != null) {
181             double conversion = (Double) currentWeatherData.getMain().getTemp_min();
182             conversion = conversion - tempUnits;
183             entries.set(3, new WeatherSpecificDataEntry(
184                     this.getString(R.string.text_field_tem_min), tempFormatter.format(conversion)));
185         }
186
187
188         if (currentWeatherData.getSys().getSunrise() != null) {
189             final long unixTime = (Long) currentWeatherData.getSys().getSunrise();
190             final Date unixDate = new Date(unixTime * 1000L);
191             final String dateFormatUnix = dateFormat.format(unixDate);
192             entries.set(4,
193                     new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_rise),
194                             dateFormatUnix));
195         }
196
197         if (currentWeatherData.getSys().getSunset() != null) {
198             final long unixTime = (Long) currentWeatherData.getSys().getSunset();
199             final Date unixDate = new Date(unixTime * 1000L);
200             final String dateFormatUnix = dateFormat.format(unixDate);
201             entries.set(5, new WeatherSpecificDataEntry(
202                     this.getString(R.string.text_field_sun_set), dateFormatUnix));
203         }
204
205         if (currentWeatherData.getClouds().getAll() != null) {
206             final double cloudiness = (Double) currentWeatherData.getClouds().getAll();
207             entries.set(6,
208                     new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness),
209                             tempFormatter.format(cloudiness)));
210         }
211
212         if (currentWeatherData.getIconData() != null) {
213             final Bitmap icon = BitmapFactory.decodeByteArray(
214                     currentWeatherData.getIconData(), 0,
215                     currentWeatherData.getIconData().length);
216             final ImageView imageIcon = (ImageView) this.getActivity()
217                     .findViewById(R.id.weather_picture);
218             imageIcon.setImageBitmap(icon);
219         }
220
221
222
223         listWeatherView.setAdapter(null);
224         adapter.addAll(entries);
225         listWeatherView.setAdapter(adapter);
226     }
227
228     public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData,
229             final int chosenDay) {
230         final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale
231                 .getDefault());
232         tempFormatter.applyPattern("#####.#####");
233         final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
234
235         final List<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
236         final ListView listWeatherView = (ListView) this.getActivity().findViewById(
237                 R.id.weather_data_list_view);
238         final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(
239                 this.getActivity(), R.layout.weather_data_entry_list);
240
241
242         final int forecastSize = forecastWeatherData.getList().size();
243         if (chosenDay > forecastSize) {
244             // Nothing to do.
245             return;
246         }
247
248
249         final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
250                 .getList().get((chosenDay - 1));
251
252         if (forecast.getWeather().size() > 0) {
253             entries.set(0,
254                     new WeatherSpecificDataEntry(this.getString(R.string.text_field_description),
255                     forecast.getWeather().get(0).getDescription()));
256         }
257
258         if (forecast.getTemp().getDay() != null) {
259             double conversion = (Double) forecast.getTemp().getDay();
260             conversion = conversion - tempUnits;
261             entries.set(1, new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem),
262                     tempFormatter.format(conversion)));
263         }
264
265         if (forecast.getTemp().getMax() != null) {
266             double conversion = (Double) forecast.getTemp().getMax();
267             conversion = conversion - tempUnits;
268             entries.set(2, new WeatherSpecificDataEntry(
269                     this.getString(R.string.text_field_tem_max), tempFormatter.format(conversion)));
270         }
271
272         if (forecast.getTemp().getMin() != null) {
273             double conversion = (Double) forecast.getTemp().getMin();
274             conversion = conversion - tempUnits;
275             entries.set(3, new WeatherSpecificDataEntry(
276                     this.getString(R.string.text_field_tem_min), tempFormatter.format(conversion)));
277         }
278
279
280         if (forecast.getClouds() != null) {
281             final double cloudiness = (Double) forecast.getClouds();
282             entries.set(6,
283                     new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness),
284                             tempFormatter.format(cloudiness)));
285         }
286
287         listWeatherView.setAdapter(null);
288         adapter.addAll(entries);
289         listWeatherView.setAdapter(adapter);
290     }
291
292     @Override
293     public void onResume() {
294         super.onResume();
295
296         final SharedPreferences sharedPreferences = PreferenceManager
297                 .getDefaultSharedPreferences(this.getActivity());
298
299         // 1. Update units of measurement.
300         final String keyPreference = this.getResources().getString(
301                 R.string.weather_preferences_units_key);
302         final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
303         final String celsius = this.getResources().getString(
304                 R.string.weather_preferences_units_celsius);
305         if (unitsPreferenceValue.equals(celsius)) {
306             this.mIsFahrenheit = false;
307         } else {
308             this.mIsFahrenheit = true;
309         }
310
311
312         // 2. Update weather data on display.
313         if (this.mChosenDay == 0) {
314             final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
315                     .getCurrentWeatherData();
316
317             if (currentWeatherData != null) {
318                 this.updateCurrentWeatherData(currentWeatherData);
319             }
320         } else {
321             final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
322                     .getForecastWeatherData();
323             if (forecastWeatherData != null) {
324                 this.updateForecastWeatherData(forecastWeatherData, (int) this.mChosenDay);
325             }
326         }
327
328
329         // 3. If language changed, try to retrieve new data for new language
330         // (new strings with the chosen language)
331         // keyPreference = this.getResources().getString(
332         // R.string.weather_preferences_language_key);
333         // final String languagePreferenceValue = sharedPreferences.getString(
334         // keyPreference, "");
335         // if (!languagePreferenceValue.equals(this.mLanguage)) {
336         // this.mLanguage = languagePreferenceValue;
337         // this.getWeather();
338         // }
339     }
340
341     private List<WeatherSpecificDataEntry> createEmptyEntriesList() {
342         final List<WeatherSpecificDataEntry> entries = new ArrayList<WeatherSpecificDataEntry>();
343         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_description), null));
344         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem), null));
345         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_max), null));
346         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_min), null));
347         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_rise), null));
348         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_set), null));
349         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness), null));
350         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_time), null));
351         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_amount), null));
352         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_wind_speed), null));
353         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_humidity), null));
354
355         return entries;
356     }
357 }