a44a462ca2bc0e10f0be2cb73b97f01d0bf5fcc4
[JavaForFun] /
1 package de.example.exampletdd.fragment.specific;
2
3 import java.text.DecimalFormat;
4 import java.text.NumberFormat;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
7 import java.util.Date;
8 import java.util.Locale;
9
10 import android.content.SharedPreferences;
11 import android.graphics.Bitmap;
12 import android.graphics.BitmapFactory;
13 import android.os.Bundle;
14 import android.preference.PreferenceManager;
15 import android.support.v4.app.Fragment;
16 import android.view.LayoutInflater;
17 import android.view.View;
18 import android.view.ViewGroup;
19 import android.widget.ImageView;
20 import android.widget.TextView;
21 import de.example.exampletdd.R;
22 import de.example.exampletdd.model.forecastweather.Forecast;
23 import de.example.exampletdd.service.IconsList;
24 import de.example.exampletdd.service.PermanentStorage;
25
26
27 public class SpecificFragment extends Fragment {
28     private int mChosenDay;
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                 // handset layout
38             this.mChosenDay = extras.getInt("CHOSEN_DAY", 0);
39         } else {
40                 // tablet layout
41                 // Always 0 when tablet layout (by default shows the first day)
42             this.mChosenDay = 0;
43         }
44     }
45     
46     @Override
47     public View onCreateView(LayoutInflater inflater, ViewGroup container,
48                              Bundle savedInstanceState) {
49     
50         // Inflate the layout for this fragment
51         return inflater.inflate(R.layout.weather_specific_fragment, container, false);
52     }
53     
54     @Override
55     public void onActivityCreated(final Bundle savedInstanceState) {
56         super.onActivityCreated(savedInstanceState);
57
58         if (savedInstanceState != null) {
59                 // Restore UI state
60             final Forecast forecast = (Forecast) savedInstanceState.getSerializable("Forecast");
61
62             if (forecast != null) {
63                 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
64                 store.saveForecast(forecast);
65             }
66
67             this.mChosenDay = savedInstanceState.getInt("mChosenDay");
68         }
69
70         this.setHasOptionsMenu(false);
71     }
72
73     @Override
74     public void onSaveInstanceState(final Bundle savedInstanceState) {
75
76         // Save UI state
77         final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
78         final Forecast forecast = store.getForecast();
79
80         if (forecast != null) {
81             savedInstanceState.putSerializable("Forecast", forecast);
82         }
83
84         savedInstanceState.putInt("mChosenDay", this.mChosenDay);
85
86         super.onSaveInstanceState(savedInstanceState);
87     }
88
89     /**
90      * This method is used by tablet layout.
91      * 
92      * @param chosenDay
93      */
94     public void updateUIByChosenDay(final int chosenDay) {
95         final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
96         final Forecast forecast = store.getForecast();
97
98         if (forecast != null) {
99             this.updateUI(forecast, chosenDay);
100         }
101     }
102
103     private interface UnitsConversor {
104         
105         public double doConversion(final double value);
106     }
107
108     private void updateUI(final Forecast forecastWeatherData, final int chosenDay) {
109
110         final SharedPreferences sharedPreferences = PreferenceManager
111                 .getDefaultSharedPreferences(this.getActivity());
112
113         // TODO: repeating the same code in Overview, Specific and Current!!!
114         // 1. Update units of measurement.
115         // 1.1 Temperature
116         String tempSymbol;
117         UnitsConversor tempUnitsConversor;
118         String keyPreference = this.getResources().getString(
119                 R.string.weather_preferences_temperature_key);
120         String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
121         String unitsPreferenceValue = sharedPreferences.getString(
122                 keyPreference, this.getString(R.string.weather_preferences_temperature_celsius));
123         if (unitsPreferenceValue.equals(values[0])) {
124                 tempSymbol = values[0];
125                 tempUnitsConversor = new UnitsConversor(){
126
127                                 @Override
128                                 public double doConversion(final double value) {
129                                         return value - 273.15;
130                                 }
131                         
132                 };
133         } else if (unitsPreferenceValue.equals(values[1])) {
134                 tempSymbol = values[1];
135                 tempUnitsConversor = new UnitsConversor(){
136
137                                 @Override
138                                 public double doConversion(final double value) {
139                                         return (value * 1.8) - 459.67;
140                                 }
141                         
142                 };
143         } else {
144                 tempSymbol = values[2];
145                 tempUnitsConversor = new UnitsConversor(){
146
147                                 @Override
148                                 public double doConversion(final double value) {
149                                         return value;
150                                 }
151                         
152                 };
153         }
154
155         // 1.2 Wind
156         String windSymbol;
157         UnitsConversor windUnitsConversor;
158         keyPreference = this.getResources().getString(R.string.weather_preferences_wind_key);
159         values = this.getResources().getStringArray(R.array.weather_preferences_wind);
160         unitsPreferenceValue = sharedPreferences.getString(
161                 keyPreference, this.getString(R.string.weather_preferences_wind_meters));
162         if (unitsPreferenceValue.equals(values[0])) {
163                 windSymbol = values[0];
164                 windUnitsConversor = new UnitsConversor(){
165
166                         @Override
167                         public double doConversion(double value) {
168                                 return value;
169                         }       
170                 };
171         } else {
172                 windSymbol = values[1];
173                 windUnitsConversor = new UnitsConversor(){
174
175                         @Override
176                         public double doConversion(double value) {
177                                 return value * 2.237;
178                         }       
179                 };
180         }
181
182         // 1.3 Pressure
183         String pressureSymbol;
184         UnitsConversor pressureUnitsConversor;
185         keyPreference = this.getResources().getString(R.string.weather_preferences_pressure_key);
186         values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
187         unitsPreferenceValue = sharedPreferences.getString(
188                 keyPreference, this.getString(R.string.weather_preferences_pressure_pascal));
189         if (unitsPreferenceValue.equals(values[0])) {
190                 pressureSymbol = values[0];
191                 pressureUnitsConversor = new UnitsConversor(){
192
193                         @Override
194                         public double doConversion(double value) {
195                                 return value;
196                         }       
197                 };
198         } else {
199                 pressureSymbol = values[1];
200                 pressureUnitsConversor = new UnitsConversor(){
201
202                         @Override
203                         public double doConversion(double value) {
204                                 return value / 113.25d;
205                         }       
206                 };
207         }
208
209
210         // 2. Formatters
211         final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
212         tempFormatter.applyPattern("#####.#####");
213         
214
215         // 3. Prepare data for UI.
216         final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
217                 .getList().get((chosenDay));
218
219         final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.US);
220         final Calendar calendar = Calendar.getInstance();
221         final Long forecastUNIXDate = (Long) forecast.getDt();
222         calendar.setTimeInMillis(forecastUNIXDate * 1000L);
223         final Date date = calendar.getTime();     
224
225         String tempMax = "";
226         if (forecast.getTemp().getMax() != null) {
227             double conversion = (Double) forecast.getTemp().getMax();
228             conversion = tempUnitsConversor.doConversion(conversion);
229             tempMax = tempFormatter.format(conversion) + tempSymbol;
230         }        
231         String tempMin = "";
232         if (forecast.getTemp().getMin() != null) {
233             double conversion = (Double) forecast.getTemp().getMin();
234             conversion = tempUnitsConversor.doConversion(conversion);
235             tempMin = tempFormatter.format(conversion) + tempSymbol;
236         }
237         Bitmap picture;
238         if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null)
239                 && (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) {
240             final String icon = forecast.getWeather().get(0).getIcon();
241             picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
242                     .getResourceDrawable());
243         } else {
244             picture = BitmapFactory.decodeResource(this.getResources(),
245                     R.drawable.weather_severe_alert);
246         }
247
248         String description = this.getString(R.string.text_field_description_when_error);
249         if (forecast.getWeather().size() > 0) {
250             description = forecast.getWeather().get(0).getDescription();
251         }
252
253         String humidityValue = "";
254         if (forecast.getHumidity() != null) {
255             final double conversion = (Double) forecast.getHumidity();
256             humidityValue = tempFormatter.format(conversion);
257         }        
258         String pressureValue = "";
259         if (forecast.getPressure() != null) {
260             double conversion = (Double) forecast.getPressure();
261             conversion = pressureUnitsConversor.doConversion(conversion);
262             pressureValue = tempFormatter.format(conversion);
263         }
264         String windValue = "";
265         if (forecast.getSpeed() != null) {
266             double conversion = (Double) forecast.getSpeed();
267             conversion = windUnitsConversor.doConversion(conversion);
268             windValue = tempFormatter.format(conversion);
269         }
270         String rainValue = "";
271         if (forecast.getRain() != null) {
272             final double conversion = (Double) forecast.getRain();
273             rainValue = tempFormatter.format(conversion);
274         }
275         String cloudsValue = "";
276         if (forecast.getRain() != null) {
277             final double conversion = (Double) forecast.getClouds();
278             cloudsValue = tempFormatter.format(conversion);
279         }
280
281         String tempDay = "";
282         if (forecast.getTemp().getDay() != null) {
283             double conversion = (Double) forecast.getTemp().getDay();
284             conversion = tempUnitsConversor.doConversion(conversion);
285             tempDay = tempFormatter.format(conversion) + tempSymbol;
286         }
287         String tempMorn = "";
288         if (forecast.getTemp().getMorn() != null) {
289             double conversion = (Double) forecast.getTemp().getMorn();
290             conversion = tempUnitsConversor.doConversion(conversion);
291             tempMorn = tempFormatter.format(conversion) + tempSymbol;
292         }
293         String tempEve = "";
294         if (forecast.getTemp().getEve() != null) {
295             double conversion = (Double) forecast.getTemp().getEve();
296             conversion = tempUnitsConversor.doConversion(conversion);
297             tempEve = tempFormatter.format(conversion) + tempSymbol;
298         }   
299         String tempNight = "";
300         if (forecast.getTemp().getNight() != null) {
301             double conversion = (Double) forecast.getTemp().getNight();
302             conversion = tempUnitsConversor.doConversion(conversion);
303             tempNight = tempFormatter.format(conversion) + tempSymbol;
304         }   
305
306
307         // 4. Update UI.
308         this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
309         
310         final TextView tempMaxView = (TextView) getActivity().findViewById(R.id.weather_specific_temp_max);
311         tempMaxView.setText(tempMax);
312         final TextView tempMinView = (TextView) getActivity().findViewById(R.id.weather_specific_temp_min);
313         tempMinView.setText(tempMin);
314         final ImageView pictureView = (ImageView) getActivity().findViewById(R.id.weather_specific_picture);
315         pictureView.setImageBitmap(picture);    
316         
317         final TextView descriptionView = (TextView) getActivity().findViewById(R.id.weather_specific_description);
318         descriptionView.setText(description);
319         
320         final TextView humidityValueView = (TextView) getActivity().findViewById(R.id.weather_specific_humidity_value);
321         humidityValueView.setText(humidityValue);
322         ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_value)).setText(pressureValue);
323         ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_units)).setText(pressureSymbol);
324         ((TextView) getActivity().findViewById(R.id.weather_specific_wind_value)).setText(windValue);;
325         ((TextView) getActivity().findViewById(R.id.weather_specific_wind_units)).setText(windSymbol);
326         final TextView rainValueView = (TextView) getActivity().findViewById(R.id.weather_specific_rain_value);
327         rainValueView.setText(rainValue);
328         final TextView cloudsValueView = (TextView) getActivity().findViewById(R.id.weather_specific_clouds_value);
329         cloudsValueView.setText(cloudsValue); 
330         
331         final TextView tempDayView = (TextView) getActivity().findViewById(R.id.weather_specific_day_temperature);
332         tempDayView.setText(tempDay);
333         final TextView tempMornView = (TextView) getActivity().findViewById(R.id.weather_specific_morn_temperature);
334         tempMornView.setText(tempMorn);
335         final TextView tempEveView = (TextView) getActivity().findViewById(R.id.weather_specific_eve_temperature);
336         tempEveView.setText(tempEve);
337         final TextView tempNightView = (TextView) getActivity().findViewById(R.id.weather_specific_night_temperature);
338         tempNightView.setText(tempNight);
339     }
340
341     @Override
342     public void onResume() {
343         super.onResume();
344
345         final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
346         final Forecast forecast = store.getForecast();
347
348         if (forecast != null) {
349             this.updateUI(forecast, this.mChosenDay);
350         }
351     }
352 }