85b4d1eff8e9316d452b73a8a37bffe5c94dde7e
[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.content.SharedPreferences;
12 import android.graphics.Bitmap;
13 import android.graphics.BitmapFactory;
14 import android.os.Bundle;
15 import android.preference.PreferenceManager;
16 import android.support.v4.app.DialogFragment;
17 import android.support.v4.app.ListFragment;
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.US);
114         tempFormatter.applyPattern("#####.#####");
115         final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
116         final String symbol = this.mIsFahrenheit ? "ºF" : "ºC";
117
118
119         final int[] layouts = new int[4];
120         layouts[0] = R.layout.weather_current_data_entry_first;
121         layouts[1] = R.layout.weather_current_data_entry_second;
122         layouts[2] = R.layout.weather_current_data_entry_third;
123         layouts[3] = R.layout.weather_current_data_entry_fourth;
124         final WeatherCurrentDataAdapter adapter = new WeatherCurrentDataAdapter(this.getActivity(),
125                 layouts);
126
127
128         final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
129                 .getList().get((chosenDay));
130
131         final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.US);
132         final Calendar calendar = Calendar.getInstance();
133         final Long forecastUNIXDate = (Long) forecast.getDt();
134         calendar.setTimeInMillis(forecastUNIXDate * 1000L);
135         final Date date = calendar.getTime();
136         this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
137
138
139         String tempMax = "";
140         if (forecast.getTemp().getMax() != null) {
141             double conversion = (Double) forecast.getTemp().getMax();
142             conversion = conversion - tempUnits;
143             tempMax = tempFormatter.format(conversion) + symbol;
144         }
145         String tempMin = "";
146         if (forecast.getTemp().getMin() != null) {
147             double conversion = (Double) forecast.getTemp().getMin();
148             conversion = conversion - tempUnits;
149             tempMin = tempFormatter.format(conversion) + symbol;
150         }
151         Bitmap picture;
152         if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null)
153                 && (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) {
154             final String icon = forecast.getWeather().get(0).getIcon();
155             picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
156                     .getResourceDrawable());
157         } else {
158             picture = BitmapFactory.decodeResource(this.getResources(),
159                     R.drawable.weather_severe_alert);
160         }
161         final WeatherCurrentDataEntryFirst entryFirst = new WeatherCurrentDataEntryFirst(tempMax,
162                 tempMin, picture);
163         adapter.add(entryFirst);
164
165         String description = "no description available";
166         if (forecast.getWeather().size() > 0) {
167             description = forecast.getWeather().get(0).getDescription();
168         }
169         final WeatherCurrentDataEntrySecond entrySecond = new WeatherCurrentDataEntrySecond(
170                 description);
171         adapter.add(entrySecond);
172
173
174         String humidityValue = "";
175         if (forecast.getHumidity() != null) {
176             final double conversion = (Double) forecast.getHumidity();
177             humidityValue = tempFormatter.format(conversion);
178         }
179         String pressureValue = "";
180         if (forecast.getPressure() != null) {
181             final double conversion = (Double) forecast.getPressure();
182             pressureValue = tempFormatter.format(conversion);
183         }
184         String windValue = "";
185         if (forecast.getSpeed() != null) {
186             final double conversion = (Double) forecast.getSpeed();
187             windValue = tempFormatter.format(conversion);
188         }
189         String rainValue = "";
190         if (forecast.getRain() != null) {
191             final double conversion = (Double) forecast.getRain();
192             rainValue = tempFormatter.format(conversion);
193         }
194         String cloudsValue = "";
195         if (forecast.getRain() != null) {
196             final double conversion = (Double) forecast.getClouds();
197             cloudsValue = tempFormatter.format(conversion);
198         }
199         final WeatherCurrentDataEntryThird entryThird = new WeatherCurrentDataEntryThird(
200                 humidityValue, pressureValue, windValue, rainValue, cloudsValue);
201         adapter.add(entryThird);
202
203         String tempDay = "";
204         if (forecast.getTemp().getDay() != null) {
205             double conversion = (Double) forecast.getTemp().getDay();
206             conversion = conversion - tempUnits;
207             tempDay = tempFormatter.format(conversion) + symbol;
208         }
209         String tempMorn = "";
210         if (forecast.getTemp().getMorn() != null) {
211             double conversion = (Double) forecast.getTemp().getMorn();
212             conversion = conversion - tempUnits;
213             tempMorn = tempFormatter.format(conversion) + symbol;
214         }
215         String tempEve = "";
216         if (forecast.getTemp().getEve() != null) {
217             double conversion = (Double) forecast.getTemp().getEve();
218             conversion = conversion - tempUnits;
219             tempEve = tempFormatter.format(conversion) + symbol;
220         }
221         String tempNight = "";
222         if (forecast.getTemp().getNight() != null) {
223             double conversion = (Double) forecast.getTemp().getNight();
224             conversion = conversion - tempUnits;
225             tempNight = tempFormatter.format(conversion) + symbol;
226         }
227         final WeatherCurrentDataEntryFourth entryFourth = new WeatherCurrentDataEntryFourth(
228                 tempMorn, tempDay, tempEve, tempNight);
229         adapter.add(entryFourth);
230
231         this.setListAdapter(adapter);
232     }
233
234     @Override
235     public void onResume() {
236         super.onResume();
237
238         final SharedPreferences sharedPreferences = PreferenceManager
239                 .getDefaultSharedPreferences(this.getActivity());
240
241         // 1. Update units of measurement.
242         final String keyPreference = this.getResources().getString(
243                 R.string.weather_preferences_units_key);
244         final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
245         final String celsius = this.getResources().getString(
246                 R.string.weather_preferences_units_celsius);
247         if (unitsPreferenceValue.equals(celsius)) {
248             this.mIsFahrenheit = false;
249         } else {
250             this.mIsFahrenheit = true;
251         }
252
253
254         // 2. Update weather data on display.
255         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
256                 .getForecastWeatherData();
257         if (forecastWeatherData != null) {
258             this.updateForecastWeatherData(forecastWeatherData, this.mChosenDay);
259         }
260     }
261 }