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;
8 import java.util.Locale;
10 import android.app.DialogFragment;
11 import android.app.ListFragment;
12 import android.content.SharedPreferences;
13 import android.os.Bundle;
14 import android.preference.PreferenceManager;
15 import android.widget.ListView;
16 import de.example.exampletdd.R;
17 import de.example.exampletdd.activityinterface.GetWeather;
18 import de.example.exampletdd.fragment.ErrorDialogFragment;
19 import de.example.exampletdd.model.forecastweather.ForecastWeatherData;
20 import de.example.exampletdd.service.WeatherServicePersistenceFile;
22 public class WeatherInformationSpecificDataFragment extends ListFragment implements GetWeather {
23 private boolean mIsFahrenheit;
24 private int mChosenDay;
25 private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
28 public void onCreate(final Bundle savedInstanceState) {
29 super.onCreate(savedInstanceState);
31 final Bundle extras = this.getActivity().getIntent().getExtras();
34 this.mChosenDay = extras.getInt("CHOSEN_DAY", 0);
39 this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
42 // final SharedPreferences sharedPreferences = PreferenceManager
43 // .getDefaultSharedPreferences(this.getActivity());
44 // final String keyPreference = this.getResources().getString(
45 // R.string.weather_preferences_language_key);
46 // this.mLanguage = sharedPreferences.getString(
47 // keyPreference, "");
51 public void onActivityCreated(final Bundle savedInstanceState) {
52 super.onActivityCreated(savedInstanceState);
54 final ListView listWeatherView = this.getListView();
55 listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
57 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
58 R.layout.weather_data_entry_list);
60 this.setEmptyText("No data available");
62 this.setListAdapter(adapter);
63 this.setListShown(true);
64 this.setListShownNoAnimation(true);
66 if (savedInstanceState != null) {
68 final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
69 .getSerializable("ForecastWeatherData");
71 if (forecastWeatherData != null) {
73 this.mWeatherServicePersistenceFile
74 .storeForecastWeatherData(forecastWeatherData);
75 } catch (final IOException e) {
76 final DialogFragment newFragment = ErrorDialogFragment
77 .newInstance(R.string.error_dialog_generic_error);
78 newFragment.show(this.getFragmentManager(), "errorDialog");
85 public void onSaveInstanceState(final Bundle savedInstanceState) {
88 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
89 .getForecastWeatherData();
91 if (forecastWeatherData != null) {
92 savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
95 super.onSaveInstanceState(savedInstanceState);
99 public void getWeatherByDay(final int chosenDay) {
101 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
102 .getForecastWeatherData();
103 if (forecastWeatherData != null) {
104 this.updateForecastWeatherData(forecastWeatherData, chosenDay);
110 public void getRemoteWeatherInformation() {
115 public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData,
116 final int chosenDay) {
117 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale
119 tempFormatter.applyPattern("#####.#####");
120 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
122 final List<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
124 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(
125 this.getActivity(), R.layout.weather_data_entry_list);
128 final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
129 .getList().get((chosenDay));
131 if (forecast.getWeather().size() > 0) {
133 new WeatherSpecificDataEntry(this.getString(R.string.text_field_description),
134 forecast.getWeather().get(0).getDescription()));
137 if (forecast.getTemp().getDay() != null) {
138 double conversion = (Double) forecast.getTemp().getDay();
139 conversion = conversion - tempUnits;
140 entries.set(1, new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem),
141 tempFormatter.format(conversion)));
144 if (forecast.getTemp().getMax() != null) {
145 double conversion = (Double) forecast.getTemp().getMax();
146 conversion = conversion - tempUnits;
147 entries.set(2, new WeatherSpecificDataEntry(
148 this.getString(R.string.text_field_tem_max), tempFormatter.format(conversion)));
151 if (forecast.getTemp().getMin() != null) {
152 double conversion = (Double) forecast.getTemp().getMin();
153 conversion = conversion - tempUnits;
154 entries.set(3, new WeatherSpecificDataEntry(
155 this.getString(R.string.text_field_tem_min), tempFormatter.format(conversion)));
159 if (forecast.getClouds() != null) {
160 final double cloudiness = (Double) forecast.getClouds();
162 new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness),
163 tempFormatter.format(cloudiness)));
166 adapter.addAll(entries);
167 this.setListAdapter(adapter);
171 public void onResume() {
174 final SharedPreferences sharedPreferences = PreferenceManager
175 .getDefaultSharedPreferences(this.getActivity());
177 // 1. Update units of measurement.
178 final String keyPreference = this.getResources().getString(
179 R.string.weather_preferences_units_key);
180 final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
181 final String celsius = this.getResources().getString(
182 R.string.weather_preferences_units_celsius);
183 if (unitsPreferenceValue.equals(celsius)) {
184 this.mIsFahrenheit = false;
186 this.mIsFahrenheit = true;
190 // 2. Update weather data on display.
192 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
193 .getForecastWeatherData();
194 if (forecastWeatherData != null) {
195 this.updateForecastWeatherData(forecastWeatherData, this.mChosenDay);
197 // 2.1 Empty list by default
198 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(
199 this.getActivity(), R.layout.weather_data_entry_list);
200 this.setListAdapter(adapter);
205 // 3. If language changed, try to retrieve new data for new language
206 // (new strings with the chosen language)
207 // keyPreference = this.getResources().getString(
208 // R.string.weather_preferences_language_key);
209 // final String languagePreferenceValue = sharedPreferences.getString(
210 // keyPreference, "");
211 // if (!languagePreferenceValue.equals(this.mLanguage)) {
212 // this.mLanguage = languagePreferenceValue;
213 // this.getWeather();
217 private List<WeatherSpecificDataEntry> createEmptyEntriesList() {
218 final List<WeatherSpecificDataEntry> entries = new ArrayList<WeatherSpecificDataEntry>();
219 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_description), null));
220 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem), null));
221 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_max), null));
222 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_min), null));
223 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_rise), null));
224 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_set), null));
225 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness), null));
226 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_time), null));
227 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_amount), null));
228 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_wind_speed), null));
229 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_humidity), null));