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