aecda8a97a422d83b1d4a3d864bbe1e3e707191b
[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.Calendar;
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.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;
24
25 public class WeatherInformationSpecificDataFragment extends ListFragment implements GetWeather {
26     private boolean mIsFahrenheit;
27     private int mChosenDay;
28     private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
29
30     @Override
31     public void onCreate(final Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33
34         final Bundle extras = this.getActivity().getIntent().getExtras();
35
36         if (extras != null) {
37             this.mChosenDay = extras.getInt("CHOSEN_DAY", 0);
38         } else {
39             this.mChosenDay = 0;
40         }
41
42         this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
43                 this.getActivity());
44
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, "");
51     }
52
53     @Override
54     public void onActivityCreated(final Bundle savedInstanceState) {
55         super.onActivityCreated(savedInstanceState);
56
57         final ListView listWeatherView = this.getListView();
58         listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
59
60         final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
61                 R.layout.weather_data_entry_list);
62
63         this.setEmptyText("No data available");
64
65         this.setListAdapter(adapter);
66         this.setListShown(true);
67         this.setListShownNoAnimation(true);
68
69         if (savedInstanceState != null) {
70             // Restore state
71             final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
72                     .getSerializable("ForecastWeatherData");
73
74             if (forecastWeatherData != null) {
75                 try {
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");
82                 }
83             }
84         }
85     }
86
87     @Override
88     public void onSaveInstanceState(final Bundle savedInstanceState) {
89
90         // Save state
91         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
92                 .getForecastWeatherData();
93
94         if (forecastWeatherData != null) {
95             savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
96         }
97
98         super.onSaveInstanceState(savedInstanceState);
99     }
100
101     @Override
102     public void getWeatherByDay(final int chosenDay) {
103
104         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
105                 .getForecastWeatherData();
106         if (forecastWeatherData != null) {
107             this.updateForecastWeatherData(forecastWeatherData, chosenDay);
108         }
109
110     }
111
112     @Override
113     public void getRemoteWeatherInformation() {
114         // Nothing to do.
115     }
116
117
118     public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData,
119             final int chosenDay) {
120         final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale
121                 .getDefault());
122         tempFormatter.applyPattern("#####.#####");
123         final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
124
125         final List<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
126
127         final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(
128                 this.getActivity(), R.layout.weather_data_entry_list);
129
130
131         final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
132                 .getList().get((chosenDay));
133
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());
140
141         if (forecast.getWeather().size() > 0) {
142             entries.set(0,
143                     new WeatherSpecificDataEntry(this.getString(R.string.text_field_description),
144                             forecast.getWeather().get(0).getDescription()));
145         }
146
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)));
152         }
153
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)));
159         }
160
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)));
166         }
167
168
169         if (forecast.getClouds() != null) {
170             final double cloudiness = (Double) forecast.getClouds();
171             entries.set(6,
172                     new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness),
173                             tempFormatter.format(cloudiness)));
174         }
175
176         adapter.addAll(entries);
177         this.setListAdapter(adapter);
178     }
179
180     @Override
181     public void onResume() {
182         super.onResume();
183
184         final SharedPreferences sharedPreferences = PreferenceManager
185                 .getDefaultSharedPreferences(this.getActivity());
186
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;
195         } else {
196             this.mIsFahrenheit = true;
197         }
198
199
200         // 2. Update weather data on display.
201
202         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
203                 .getForecastWeatherData();
204         if (forecastWeatherData != null) {
205             this.updateForecastWeatherData(forecastWeatherData, this.mChosenDay);
206         } else {
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);
211         }
212
213
214
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();
224         // }
225     }
226
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));
240
241         return entries;
242     }
243 }