1 package com.weather.information.fragment.specific;
3 import java.text.DecimalFormat;
4 import java.text.NumberFormat;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
8 import java.util.Locale;
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;
22 import com.weather.information.R;
23 import com.weather.information.model.forecastweather.Forecast;
24 import com.weather.information.service.IconsList;
25 import com.weather.information.service.PermanentStorage;
28 public class SpecificFragment extends Fragment {
29 private int mChosenDay;
32 public void onCreate(final Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
35 final Bundle extras = this.getActivity().getIntent().getExtras();
39 this.mChosenDay = extras.getInt("CHOSEN_DAY", 0);
42 // Always 0 when tablet layout (by default shows the first day)
48 public View onCreateView(LayoutInflater inflater, ViewGroup container,
49 Bundle savedInstanceState) {
51 // Inflate the layout for this fragment
52 return inflater.inflate(R.layout.weather_specific_fragment, container, false);
56 public void onActivityCreated(final Bundle savedInstanceState) {
57 super.onActivityCreated(savedInstanceState);
59 if (savedInstanceState != null) {
61 final Forecast forecast = (Forecast) savedInstanceState.getSerializable("Forecast");
63 if (forecast != null) {
64 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
65 store.saveForecast(forecast);
68 this.mChosenDay = savedInstanceState.getInt("mChosenDay");
71 this.setHasOptionsMenu(false);
75 public void onSaveInstanceState(final Bundle savedInstanceState) {
78 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
79 final Forecast forecast = store.getForecast();
81 if (forecast != null) {
82 savedInstanceState.putSerializable("Forecast", forecast);
85 savedInstanceState.putInt("mChosenDay", this.mChosenDay);
87 super.onSaveInstanceState(savedInstanceState);
91 * This method is used by tablet layout.
95 public void updateUIByChosenDay(final int chosenDay) {
96 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
97 final Forecast forecast = store.getForecast();
99 if (forecast != null) {
100 this.updateUI(forecast, chosenDay);
104 private interface UnitsConversor {
106 public double doConversion(final double value);
109 private void updateUI(final Forecast forecastWeatherData, final int chosenDay) {
111 final SharedPreferences sharedPreferences = PreferenceManager
112 .getDefaultSharedPreferences(this.getActivity());
114 // TODO: repeating the same code in Overview, Specific and Current!!!
115 // 1. Update units of measurement.
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(){
129 public double doConversion(final double value) {
130 return value - 273.15;
134 } else if (unitsPreferenceValue.equals(values[1])) {
135 tempSymbol = values[1];
136 tempUnitsConversor = new UnitsConversor(){
139 public double doConversion(final double value) {
140 return (value * 1.8) - 459.67;
145 tempSymbol = values[2];
146 tempUnitsConversor = new UnitsConversor(){
149 public double doConversion(final double value) {
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(){
168 public double doConversion(double value) {
173 windSymbol = values[1];
174 windUnitsConversor = new UnitsConversor(){
177 public double doConversion(double value) {
178 return value * 2.237;
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(){
195 public double doConversion(double value) {
200 pressureSymbol = values[1];
201 pressureUnitsConversor = new UnitsConversor(){
204 public double doConversion(double value) {
205 return value / 113.25d;
212 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
213 tempFormatter.applyPattern("#####.#####");
216 // 3. Prepare data for UI.
217 final com.weather.information.model.forecastweather.List forecast = forecastWeatherData
218 .getList().get((chosenDay));
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();
227 if (forecast.getTemp().getMax() != null) {
228 double conversion = (Double) forecast.getTemp().getMax();
229 conversion = tempUnitsConversor.doConversion(conversion);
230 tempMax = tempFormatter.format(conversion) + tempSymbol;
233 if (forecast.getTemp().getMin() != null) {
234 double conversion = (Double) forecast.getTemp().getMin();
235 conversion = tempUnitsConversor.doConversion(conversion);
236 tempMin = tempFormatter.format(conversion) + tempSymbol;
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());
245 picture = BitmapFactory.decodeResource(this.getResources(),
246 R.drawable.weather_severe_alert);
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();
254 String humidityValue = "";
255 if (forecast.getHumidity() != null) {
256 final double conversion = (Double) forecast.getHumidity();
257 humidityValue = tempFormatter.format(conversion);
259 String pressureValue = "";
260 if (forecast.getPressure() != null) {
261 double conversion = (Double) forecast.getPressure();
262 conversion = pressureUnitsConversor.doConversion(conversion);
263 pressureValue = tempFormatter.format(conversion);
265 String windValue = "";
266 if (forecast.getSpeed() != null) {
267 double conversion = (Double) forecast.getSpeed();
268 conversion = windUnitsConversor.doConversion(conversion);
269 windValue = tempFormatter.format(conversion);
271 String rainValue = "";
272 if (forecast.getRain() != null) {
273 final double conversion = (Double) forecast.getRain();
274 rainValue = tempFormatter.format(conversion);
276 String cloudsValue = "";
277 if (forecast.getRain() != null) {
278 final double conversion = (Double) forecast.getClouds();
279 cloudsValue = tempFormatter.format(conversion);
283 if (forecast.getTemp().getDay() != null) {
284 double conversion = (Double) forecast.getTemp().getDay();
285 conversion = tempUnitsConversor.doConversion(conversion);
286 tempDay = tempFormatter.format(conversion) + tempSymbol;
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;
295 if (forecast.getTemp().getEve() != null) {
296 double conversion = (Double) forecast.getTemp().getEve();
297 conversion = tempUnitsConversor.doConversion(conversion);
298 tempEve = tempFormatter.format(conversion) + tempSymbol;
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;
309 this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
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);
318 final TextView descriptionView = (TextView) getActivity().findViewById(R.id.weather_specific_description);
319 descriptionView.setText(description);
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);
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);
343 public void onResume() {
346 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
347 final Forecast forecast = store.getForecast();
349 if (forecast != null) {
350 this.updateUI(forecast, this.mChosenDay);