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.util.ArrayList;
7 import java.util.Collection;
9 import java.util.Locale;
11 import android.app.DialogFragment;
12 import android.app.Fragment;
13 import android.content.SharedPreferences;
14 import android.os.Bundle;
15 import android.preference.PreferenceManager;
16 import android.view.LayoutInflater;
17 import android.view.View;
18 import android.view.ViewGroup;
19 import android.widget.ListView;
20 import de.example.exampletdd.R;
21 import de.example.exampletdd.activityinterface.GetWeather;
22 import de.example.exampletdd.fragment.ErrorDialogFragment;
23 import de.example.exampletdd.model.forecastweather.ForecastWeatherData;
24 import de.example.exampletdd.service.WeatherServicePersistenceFile;
26 public class WeatherInformationSpecificDataFragment extends Fragment implements GetWeather {
27 private boolean mIsFahrenheit;
28 private int mChosenDay;
29 private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
32 public void onCreate(final Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
35 final Bundle extras = this.getActivity().getIntent().getExtras();
38 this.mChosenDay = extras.getInt("CHOSEN_DAY", 0);
43 this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
46 // final SharedPreferences sharedPreferences = PreferenceManager
47 // .getDefaultSharedPreferences(this.getActivity());
48 // final String keyPreference = this.getResources().getString(
49 // R.string.weather_preferences_language_key);
50 // this.mLanguage = sharedPreferences.getString(
51 // keyPreference, "");
55 public View onCreateView(final LayoutInflater inflater,
56 final ViewGroup container, final Bundle savedInstanceState) {
57 final View rootView = inflater.inflate(R.layout.weather_data_list,
64 public void onActivityCreated(final Bundle savedInstanceState) {
65 super.onActivityCreated(savedInstanceState);
67 final ListView listWeatherView = (ListView) this.getActivity().findViewById(
68 R.id.weather_data_list_view);
70 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
71 R.layout.weather_data_entry_list);
73 final Collection<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
75 adapter.addAll(entries);
76 listWeatherView.setAdapter(adapter);
78 if (savedInstanceState != null) {
80 final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
81 .getSerializable("ForecastWeatherData");
83 if (forecastWeatherData != null) {
85 this.mWeatherServicePersistenceFile
86 .storeForecastWeatherData(forecastWeatherData);
87 } catch (final IOException e) {
88 final DialogFragment newFragment = ErrorDialogFragment
89 .newInstance(R.string.error_dialog_generic_error);
90 newFragment.show(this.getFragmentManager(), "errorDialog");
97 public void onSaveInstanceState(final Bundle savedInstanceState) {
100 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
101 .getForecastWeatherData();
103 if (forecastWeatherData != null) {
104 savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
107 super.onSaveInstanceState(savedInstanceState);
111 public void getWeatherByDay(final int chosenDay) {
113 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
114 .getForecastWeatherData();
115 if (forecastWeatherData != null) {
116 this.updateForecastWeatherData(forecastWeatherData, chosenDay);
122 public void getRemoteWeatherInformation() {
127 public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData,
128 final int chosenDay) {
129 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale
131 tempFormatter.applyPattern("#####.#####");
132 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
134 final List<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
135 final ListView listWeatherView = (ListView) this.getActivity().findViewById(
136 R.id.weather_data_list_view);
137 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(
138 this.getActivity(), R.layout.weather_data_entry_list);
141 final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
142 .getList().get((chosenDay));
144 if (forecast.getWeather().size() > 0) {
146 new WeatherSpecificDataEntry(this.getString(R.string.text_field_description),
147 forecast.getWeather().get(0).getDescription()));
150 if (forecast.getTemp().getDay() != null) {
151 double conversion = (Double) forecast.getTemp().getDay();
152 conversion = conversion - tempUnits;
153 entries.set(1, new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem),
154 tempFormatter.format(conversion)));
157 if (forecast.getTemp().getMax() != null) {
158 double conversion = (Double) forecast.getTemp().getMax();
159 conversion = conversion - tempUnits;
160 entries.set(2, new WeatherSpecificDataEntry(
161 this.getString(R.string.text_field_tem_max), tempFormatter.format(conversion)));
164 if (forecast.getTemp().getMin() != null) {
165 double conversion = (Double) forecast.getTemp().getMin();
166 conversion = conversion - tempUnits;
167 entries.set(3, new WeatherSpecificDataEntry(
168 this.getString(R.string.text_field_tem_min), tempFormatter.format(conversion)));
172 if (forecast.getClouds() != null) {
173 final double cloudiness = (Double) forecast.getClouds();
175 new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness),
176 tempFormatter.format(cloudiness)));
179 listWeatherView.setAdapter(null);
180 adapter.addAll(entries);
181 listWeatherView.setAdapter(adapter);
185 public void onResume() {
188 final SharedPreferences sharedPreferences = PreferenceManager
189 .getDefaultSharedPreferences(this.getActivity());
191 // 1. Update units of measurement.
192 final String keyPreference = this.getResources().getString(
193 R.string.weather_preferences_units_key);
194 final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
195 final String celsius = this.getResources().getString(
196 R.string.weather_preferences_units_celsius);
197 if (unitsPreferenceValue.equals(celsius)) {
198 this.mIsFahrenheit = false;
200 this.mIsFahrenheit = true;
204 // 2. Update weather data on display.
206 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
207 .getForecastWeatherData();
208 if (forecastWeatherData != null) {
209 this.updateForecastWeatherData(forecastWeatherData, this.mChosenDay);
214 // 3. If language changed, try to retrieve new data for new language
215 // (new strings with the chosen language)
216 // keyPreference = this.getResources().getString(
217 // R.string.weather_preferences_language_key);
218 // final String languagePreferenceValue = sharedPreferences.getString(
219 // keyPreference, "");
220 // if (!languagePreferenceValue.equals(this.mLanguage)) {
221 // this.mLanguage = languagePreferenceValue;
222 // this.getWeather();
226 private List<WeatherSpecificDataEntry> createEmptyEntriesList() {
227 final List<WeatherSpecificDataEntry> entries = new ArrayList<WeatherSpecificDataEntry>();
228 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_description), null));
229 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem), null));
230 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_max), null));
231 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_min), null));
232 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_rise), null));
233 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_set), null));
234 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness), null));
235 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_time), null));
236 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_amount), null));
237 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_wind_speed), null));
238 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_humidity), null));