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