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