3b1ed4b84515a76ae121a66b0e9551614b39ac78
[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.Calendar;
8 import java.util.Date;
9 import java.util.Locale;
10
11 import android.app.DialogFragment;
12 import android.app.ListFragment;
13 import android.content.SharedPreferences;
14 import android.graphics.Bitmap;
15 import android.graphics.BitmapFactory;
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.fragment.overview.IconsList;
23 import de.example.exampletdd.model.forecastweather.ForecastWeatherData;
24 import de.example.exampletdd.service.WeatherServicePersistenceFile;
25
26 public class WeatherInformationSpecificDataFragment extends ListFragment implements GetWeather {
27     private boolean mIsFahrenheit;
28     private int mChosenDay;
29     private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
30
31     @Override
32     public void onCreate(final Bundle savedInstanceState) {
33         super.onCreate(savedInstanceState);
34
35         final Bundle extras = this.getActivity().getIntent().getExtras();
36
37         if (extras != null) {
38             this.mChosenDay = extras.getInt("CHOSEN_DAY", 0);
39         } else {
40             this.mChosenDay = 0;
41         }
42
43         this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
44                 this.getActivity());
45     }
46
47     @Override
48     public void onActivityCreated(final Bundle savedInstanceState) {
49         super.onActivityCreated(savedInstanceState);
50
51         final ListView listWeatherView = this.getListView();
52         listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
53
54         this.setEmptyText("No data available");
55
56         this.setListShownNoAnimation(false);
57
58         if (savedInstanceState != null) {
59             // Restore state
60             final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
61                     .getSerializable("ForecastWeatherData");
62
63             if (forecastWeatherData != null) {
64                 try {
65                     this.mWeatherServicePersistenceFile
66                     .storeForecastWeatherData(forecastWeatherData);
67                 } catch (final IOException e) {
68                     final DialogFragment newFragment = ErrorDialogFragment
69                             .newInstance(R.string.error_dialog_generic_error);
70                     newFragment.show(this.getFragmentManager(), "errorDialog");
71                 }
72             }
73
74             this.mChosenDay = savedInstanceState.getInt("Chosen day");
75         }
76     }
77
78     @Override
79     public void onSaveInstanceState(final Bundle savedInstanceState) {
80
81         // Save state
82         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
83                 .getForecastWeatherData();
84
85         if (forecastWeatherData != null) {
86             savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
87         }
88
89         savedInstanceState.putInt("Chosend day", this.mChosenDay);
90
91         super.onSaveInstanceState(savedInstanceState);
92     }
93
94     @Override
95     public void getWeatherByDay(final int chosenDay) {
96
97         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
98                 .getForecastWeatherData();
99         if (forecastWeatherData != null) {
100             this.updateForecastWeatherData(forecastWeatherData, chosenDay);
101         }
102
103     }
104
105     @Override
106     public void getRemoteWeatherInformation() {
107         // Nothing to do.
108     }
109
110
111     public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData,
112             final int chosenDay) {
113         final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale
114                 .getDefault());
115         tempFormatter.applyPattern("#####.#####");
116         final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
117         final String symbol = this.mIsFahrenheit ? "ºF" : "ºC";
118
119
120         final int[] layouts = new int[4];
121         layouts[0] = R.layout.weather_current_data_entry_first;
122         layouts[1] = R.layout.weather_current_data_entry_second;
123         layouts[2] = R.layout.weather_current_data_entry_third;
124         layouts[3] = R.layout.weather_current_data_entry_fourth;
125         final WeatherCurrentDataAdapter adapter = new WeatherCurrentDataAdapter(this.getActivity(),
126                 layouts);
127
128
129         final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
130                 .getList().get((chosenDay));
131
132         final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.getDefault());
133         final Calendar calendar = Calendar.getInstance();
134         final Long forecastUNIXDate = (Long) forecast.getDt();
135         calendar.setTimeInMillis(forecastUNIXDate * 1000L);
136         final Date date = calendar.getTime();
137         this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
138
139
140         String tempMax = "";
141         if (forecast.getTemp().getMax() != null) {
142             double conversion = (Double) forecast.getTemp().getMax();
143             conversion = conversion - tempUnits;
144             tempMax = tempFormatter.format(conversion) + symbol;
145         }
146         String tempMin = "";
147         if (forecast.getTemp().getMin() != null) {
148             double conversion = (Double) forecast.getTemp().getMin();
149             conversion = conversion - tempUnits;
150             tempMin = tempFormatter.format(conversion) + symbol;
151         }
152         Bitmap picture;
153         if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null)
154                 && (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) {
155             final String icon = forecast.getWeather().get(0).getIcon();
156             picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
157                     .getResourceDrawable());
158         } else {
159             picture = BitmapFactory.decodeResource(this.getResources(),
160                     R.drawable.weather_severe_alert);
161         }
162         final WeatherCurrentDataEntryFirst entryFirst = new WeatherCurrentDataEntryFirst(tempMax,
163                 tempMin, picture);
164         adapter.add(entryFirst);
165
166         String description = "no description available";
167         if (forecast.getWeather().size() > 0) {
168             description = forecast.getWeather().get(0).getDescription();
169         }
170         final WeatherCurrentDataEntrySecond entrySecond = new WeatherCurrentDataEntrySecond(
171                 description);
172         adapter.add(entrySecond);
173
174
175         String humidityValue = "";
176         if (forecast.getHumidity() != null) {
177             final double conversion = (Double) forecast.getHumidity();
178             humidityValue = tempFormatter.format(conversion);
179         }
180         String pressureValue = "";
181         if (forecast.getPressure() != null) {
182             final double conversion = (Double) forecast.getPressure();
183             pressureValue = tempFormatter.format(conversion);
184         }
185         String windValue = "";
186         if (forecast.getSpeed() != null) {
187             final double conversion = (Double) forecast.getSpeed();
188             windValue = tempFormatter.format(conversion);
189         }
190         String rainValue = "";
191         if (forecast.getRain() != null) {
192             final double conversion = (Double) forecast.getRain();
193             rainValue = tempFormatter.format(conversion);
194         }
195         String cloudsValue = "";
196         if (forecast.getRain() != null) {
197             final double conversion = (Double) forecast.getClouds();
198             cloudsValue = tempFormatter.format(conversion);
199         }
200         final WeatherCurrentDataEntryThird entryThird = new WeatherCurrentDataEntryThird(
201                 humidityValue, pressureValue, windValue, rainValue, cloudsValue);
202         adapter.add(entryThird);
203
204         String tempDay = "";
205         if (forecast.getTemp().getDay() != null) {
206             double conversion = (Double) forecast.getTemp().getDay();
207             conversion = conversion - tempUnits;
208             tempDay = tempFormatter.format(conversion) + symbol;
209         }
210         String tempMorn = "";
211         if (forecast.getTemp().getMorn() != null) {
212             double conversion = (Double) forecast.getTemp().getMorn();
213             conversion = conversion - tempUnits;
214             tempMorn = tempFormatter.format(conversion) + symbol;
215         }
216         String tempEve = "";
217         if (forecast.getTemp().getEve() != null) {
218             double conversion = (Double) forecast.getTemp().getEve();
219             conversion = conversion - tempUnits;
220             tempEve = tempFormatter.format(conversion) + symbol;
221         }
222         String tempNight = "";
223         if (forecast.getTemp().getNight() != null) {
224             double conversion = (Double) forecast.getTemp().getNight();
225             conversion = conversion - tempUnits;
226             tempNight = tempFormatter.format(conversion) + symbol;
227         }
228         final WeatherCurrentDataEntryFourth entryFourth = new WeatherCurrentDataEntryFourth(
229                 tempMorn, tempDay, tempEve, tempNight);
230         adapter.add(entryFourth);
231
232         this.setListAdapter(adapter);
233     }
234
235     @Override
236     public void onResume() {
237         super.onResume();
238
239         final SharedPreferences sharedPreferences = PreferenceManager
240                 .getDefaultSharedPreferences(this.getActivity());
241
242         // 1. Update units of measurement.
243         final String keyPreference = this.getResources().getString(
244                 R.string.weather_preferences_units_key);
245         final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
246         final String celsius = this.getResources().getString(
247                 R.string.weather_preferences_units_celsius);
248         if (unitsPreferenceValue.equals(celsius)) {
249             this.mIsFahrenheit = false;
250         } else {
251             this.mIsFahrenheit = true;
252         }
253
254
255         // 2. Update weather data on display.
256         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
257                 .getForecastWeatherData();
258         if (forecastWeatherData != null) {
259             this.updateForecastWeatherData(forecastWeatherData, this.mChosenDay);
260         }
261     }
262 }