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.Calendar;
10 import java.util.List;
11 import java.util.Locale;
13 import android.app.DialogFragment;
14 import android.app.ListFragment;
15 import android.content.SharedPreferences;
16 import android.os.Bundle;
17 import android.preference.PreferenceManager;
18 import android.widget.ListView;
19 import de.example.exampletdd.R;
20 import de.example.exampletdd.activityinterface.GetWeather;
21 import de.example.exampletdd.fragment.ErrorDialogFragment;
22 import de.example.exampletdd.model.forecastweather.ForecastWeatherData;
23 import de.example.exampletdd.service.WeatherServicePersistenceFile;
25 public class WeatherInformationSpecificDataFragment extends ListFragment implements GetWeather {
26 private boolean mIsFahrenheit;
27 private int mChosenDay;
28 private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
31 public void onCreate(final Bundle savedInstanceState) {
32 super.onCreate(savedInstanceState);
34 final Bundle extras = this.getActivity().getIntent().getExtras();
37 this.mChosenDay = extras.getInt("CHOSEN_DAY", 0);
42 this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
45 // final SharedPreferences sharedPreferences = PreferenceManager
46 // .getDefaultSharedPreferences(this.getActivity());
47 // final String keyPreference = this.getResources().getString(
48 // R.string.weather_preferences_language_key);
49 // this.mLanguage = sharedPreferences.getString(
50 // keyPreference, "");
54 public void onActivityCreated(final Bundle savedInstanceState) {
55 super.onActivityCreated(savedInstanceState);
57 final ListView listWeatherView = this.getListView();
58 listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
60 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
61 R.layout.weather_data_entry_list);
63 this.setEmptyText("No data available");
65 this.setListAdapter(adapter);
66 this.setListShown(true);
67 this.setListShownNoAnimation(true);
69 if (savedInstanceState != null) {
71 final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
72 .getSerializable("ForecastWeatherData");
74 if (forecastWeatherData != null) {
76 this.mWeatherServicePersistenceFile
77 .storeForecastWeatherData(forecastWeatherData);
78 } catch (final IOException e) {
79 final DialogFragment newFragment = ErrorDialogFragment
80 .newInstance(R.string.error_dialog_generic_error);
81 newFragment.show(this.getFragmentManager(), "errorDialog");
88 public void onSaveInstanceState(final Bundle savedInstanceState) {
91 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
92 .getForecastWeatherData();
94 if (forecastWeatherData != null) {
95 savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
98 super.onSaveInstanceState(savedInstanceState);
102 public void getWeatherByDay(final int chosenDay) {
104 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
105 .getForecastWeatherData();
106 if (forecastWeatherData != null) {
107 this.updateForecastWeatherData(forecastWeatherData, chosenDay);
113 public void getRemoteWeatherInformation() {
118 public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData,
119 final int chosenDay) {
120 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale
122 tempFormatter.applyPattern("#####.#####");
123 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
125 final List<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
127 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(
128 this.getActivity(), R.layout.weather_data_entry_list);
131 final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
132 .getList().get((chosenDay));
134 final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.getDefault());
135 final Calendar calendar = Calendar.getInstance();
136 final Long forecastUNIXDate = (Long) forecast.getDt();
137 calendar.setTimeInMillis(forecastUNIXDate * 1000L);
138 final Date date = calendar.getTime();
139 this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
141 if (forecast.getWeather().size() > 0) {
143 new WeatherSpecificDataEntry(this.getString(R.string.text_field_description),
144 forecast.getWeather().get(0).getDescription()));
147 if (forecast.getTemp().getDay() != null) {
148 double conversion = (Double) forecast.getTemp().getDay();
149 conversion = conversion - tempUnits;
150 entries.set(1, new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem),
151 tempFormatter.format(conversion)));
154 if (forecast.getTemp().getMax() != null) {
155 double conversion = (Double) forecast.getTemp().getMax();
156 conversion = conversion - tempUnits;
157 entries.set(2, new WeatherSpecificDataEntry(
158 this.getString(R.string.text_field_tem_max), tempFormatter.format(conversion)));
161 if (forecast.getTemp().getMin() != null) {
162 double conversion = (Double) forecast.getTemp().getMin();
163 conversion = conversion - tempUnits;
164 entries.set(3, new WeatherSpecificDataEntry(
165 this.getString(R.string.text_field_tem_min), tempFormatter.format(conversion)));
169 if (forecast.getClouds() != null) {
170 final double cloudiness = (Double) forecast.getClouds();
172 new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness),
173 tempFormatter.format(cloudiness)));
176 adapter.addAll(entries);
177 this.setListAdapter(adapter);
181 public void onResume() {
184 final SharedPreferences sharedPreferences = PreferenceManager
185 .getDefaultSharedPreferences(this.getActivity());
187 // 1. Update units of measurement.
188 final String keyPreference = this.getResources().getString(
189 R.string.weather_preferences_units_key);
190 final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
191 final String celsius = this.getResources().getString(
192 R.string.weather_preferences_units_celsius);
193 if (unitsPreferenceValue.equals(celsius)) {
194 this.mIsFahrenheit = false;
196 this.mIsFahrenheit = true;
200 // 2. Update weather data on display.
202 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
203 .getForecastWeatherData();
204 if (forecastWeatherData != null) {
205 this.updateForecastWeatherData(forecastWeatherData, this.mChosenDay);
207 // 2.1 Empty list by default
208 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(
209 this.getActivity(), R.layout.weather_data_entry_list);
210 this.setListAdapter(adapter);
215 // 3. If language changed, try to retrieve new data for new language
216 // (new strings with the chosen language)
217 // keyPreference = this.getResources().getString(
218 // R.string.weather_preferences_language_key);
219 // final String languagePreferenceValue = sharedPreferences.getString(
220 // keyPreference, "");
221 // if (!languagePreferenceValue.equals(this.mLanguage)) {
222 // this.mLanguage = languagePreferenceValue;
223 // this.getWeather();
227 private List<WeatherSpecificDataEntry> createEmptyEntriesList() {
228 final List<WeatherSpecificDataEntry> entries = new ArrayList<WeatherSpecificDataEntry>();
229 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_description), null));
230 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem), null));
231 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_max), null));
232 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_min), null));
233 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_rise), null));
234 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_set), null));
235 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness), null));
236 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_time), null));
237 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_amount), null));
238 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_wind_speed), null));
239 entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_humidity), null));