1 package de.example.exampletdd.fragment.specific;
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;
10 import java.util.List;
11 import java.util.Locale;
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;
32 public class WeatherInformationSpecificDataFragment extends Fragment implements GetWeather {
33 private boolean mIsFahrenheit;
34 private long mChosenDay;
35 private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
38 public void onCreate(final Bundle savedInstanceState) {
39 super.onCreate(savedInstanceState);
41 final Bundle extras = this.getActivity().getIntent().getExtras();
44 this.mChosenDay = extras.getLong("CHOSEN_DAY", 0);
49 this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
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, "");
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,
70 public void onActivityCreated(final Bundle savedInstanceState) {
71 super.onActivityCreated(savedInstanceState);
73 final ListView listWeatherView = (ListView) this.getActivity().findViewById(
74 R.id.weather_data_list_view);
76 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
77 R.layout.weather_data_entry_list);
79 final Collection<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
81 adapter.addAll(entries);
82 listWeatherView.setAdapter(adapter);
84 if (savedInstanceState != null) {
86 final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
87 .getSerializable("ForecastWeatherData");
88 final CurrentWeatherData currentWeatherData = (CurrentWeatherData) savedInstanceState
89 .getSerializable("CurrentWeatherData");
91 if ((forecastWeatherData != null) && (currentWeatherData != null)) {
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");
106 public void onSaveInstanceState(final Bundle savedInstanceState) {
109 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
110 .getForecastWeatherData();
112 final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
113 .getCurrentWeatherData();
115 if ((forecastWeatherData != null) && (currentWeatherData != null)) {
116 savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
117 savedInstanceState.putSerializable("CurrentWeatherData", currentWeatherData);
120 super.onSaveInstanceState(savedInstanceState);
124 public void getWeatherByDay(final int chosenDay) {
125 if (chosenDay == 0) {
126 final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
127 .getCurrentWeatherData();
129 if (currentWeatherData != null) {
130 this.updateCurrentWeatherData(currentWeatherData);
133 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
134 .getForecastWeatherData();
135 if (forecastWeatherData != null) {
136 this.updateForecastWeatherData(forecastWeatherData, chosenDay);
142 public void getRemoteWeatherInformation() {
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());
151 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
153 final List<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
155 final ListView listWeatherView = (ListView) this.getActivity().findViewById(
156 R.id.weather_data_list_view);
158 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
159 R.layout.weather_data_entry_list);
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()));
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)));
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)));
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)));
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);
193 new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_rise),
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));
205 if (currentWeatherData.getClouds().getAll() != null) {
206 final double cloudiness = (Double) currentWeatherData.getClouds().getAll();
208 new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness),
209 tempFormatter.format(cloudiness)));
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);
223 listWeatherView.setAdapter(null);
224 adapter.addAll(entries);
225 listWeatherView.setAdapter(adapter);
228 public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData,
229 final int chosenDay) {
230 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale
232 tempFormatter.applyPattern("#####.#####");
233 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
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);
242 final int forecastSize = forecastWeatherData.getList().size();
243 if (chosenDay > forecastSize) {
249 final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
250 .getList().get((chosenDay - 1));
252 if (forecast.getWeather().size() > 0) {
254 new WeatherSpecificDataEntry(this.getString(R.string.text_field_description),
255 forecast.getWeather().get(0).getDescription()));
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)));
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)));
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)));
280 if (forecast.getClouds() != null) {
281 final double cloudiness = (Double) forecast.getClouds();
283 new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness),
284 tempFormatter.format(cloudiness)));
287 listWeatherView.setAdapter(null);
288 adapter.addAll(entries);
289 listWeatherView.setAdapter(adapter);
293 public void onResume() {
296 final SharedPreferences sharedPreferences = PreferenceManager
297 .getDefaultSharedPreferences(this.getActivity());
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;
308 this.mIsFahrenheit = true;
312 // 2. Update weather data on display.
313 if (this.mChosenDay == 0) {
314 final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
315 .getCurrentWeatherData();
317 if (currentWeatherData != null) {
318 this.updateCurrentWeatherData(currentWeatherData);
321 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
322 .getForecastWeatherData();
323 if (forecastWeatherData != null) {
324 this.updateForecastWeatherData(forecastWeatherData, (int) this.mChosenDay);
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();
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));