1 package de.example.exampletdd.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;
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;
27 public class SpecificFragment extends Fragment {
28 private int mChosenDay;
31 public void onCreate(final Bundle savedInstanceState) {
32 super.onCreate(savedInstanceState);
34 final Bundle extras = this.getActivity().getIntent().getExtras();
38 this.mChosenDay = extras.getInt("CHOSEN_DAY", 0);
41 // Always 0 when tablet layout (by default shows the first day)
47 public View onCreateView(LayoutInflater inflater, ViewGroup container,
48 Bundle savedInstanceState) {
50 // Inflate the layout for this fragment
51 return inflater.inflate(R.layout.weather_specific_fragment, container, false);
55 public void onActivityCreated(final Bundle savedInstanceState) {
56 super.onActivityCreated(savedInstanceState);
58 if (savedInstanceState != null) {
60 final Forecast forecast = (Forecast) savedInstanceState.getSerializable("Forecast");
62 // TODO: Could it be better to store in global data forecast even if it is null value?
63 // So, perhaps do not check for null value and always store in global variable.
64 if (forecast != null) {
65 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
66 store.saveForecast(forecast);
69 this.mChosenDay = savedInstanceState.getInt("mChosenDay");
72 this.setHasOptionsMenu(false);
76 public void onSaveInstanceState(final Bundle savedInstanceState) {
79 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
80 final Forecast forecast = store.getForecast();
82 // TODO: Could it be better to save forecast data even if it is null value?
83 // So, perhaps do not check for null value.
84 if (forecast != null) {
85 savedInstanceState.putSerializable("Forecast", forecast);
88 savedInstanceState.putInt("mChosenDay", this.mChosenDay);
90 super.onSaveInstanceState(savedInstanceState);
94 * This method is used by tablet layout.
98 public void updateUIByChosenDay(final int chosenDay) {
99 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
100 final Forecast forecast = store.getForecast();
102 if (forecast != null) {
103 this.updateUI(forecast, chosenDay);
107 private interface UnitsConversor {
109 public double doConversion(final double value);
112 private void updateUI(final Forecast forecastWeatherData, final int chosenDay) {
114 final SharedPreferences sharedPreferences = PreferenceManager
115 .getDefaultSharedPreferences(this.getActivity());
117 // TODO: repeating the same code in Overview, Specific and Current!!!
118 // 1. Update units of measurement.
121 UnitsConversor tempUnitsConversor;
122 String keyPreference = this.getResources().getString(
123 R.string.weather_preferences_temperature_key);
124 String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
125 String unitsPreferenceValue = sharedPreferences.getString(
126 keyPreference, this.getString(R.string.weather_preferences_temperature_celsius));
127 if (unitsPreferenceValue.equals(values[0])) {
128 tempSymbol = values[0];
129 tempUnitsConversor = new UnitsConversor(){
132 public double doConversion(final double value) {
133 return value - 273.15;
137 } else if (unitsPreferenceValue.equals(values[1])) {
138 tempSymbol = values[1];
139 tempUnitsConversor = new UnitsConversor(){
142 public double doConversion(final double value) {
143 return (value * 1.8) - 459.67;
148 tempSymbol = values[2];
149 tempUnitsConversor = new UnitsConversor(){
152 public double doConversion(final double value) {
161 UnitsConversor windUnitsConversor;
162 keyPreference = this.getResources().getString(R.string.weather_preferences_wind_key);
163 values = this.getResources().getStringArray(R.array.weather_preferences_wind);
164 unitsPreferenceValue = sharedPreferences.getString(
165 keyPreference, this.getString(R.string.weather_preferences_wind_meters));
166 if (unitsPreferenceValue.equals(values[0])) {
167 windSymbol = values[0];
168 windUnitsConversor = new UnitsConversor(){
171 public double doConversion(double value) {
176 windSymbol = values[1];
177 windUnitsConversor = new UnitsConversor(){
180 public double doConversion(double value) {
181 return value * 2.237;
187 String pressureSymbol;
188 UnitsConversor pressureUnitsConversor;
189 keyPreference = this.getResources().getString(R.string.weather_preferences_pressure_key);
190 values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
191 unitsPreferenceValue = sharedPreferences.getString(
192 keyPreference, this.getString(R.string.weather_preferences_pressure_pascal));
193 if (unitsPreferenceValue.equals(values[0])) {
194 pressureSymbol = values[0];
195 pressureUnitsConversor = new UnitsConversor(){
198 public double doConversion(double value) {
203 pressureSymbol = values[1];
204 pressureUnitsConversor = new UnitsConversor(){
207 public double doConversion(double value) {
208 return value / 113.25d;
215 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
216 tempFormatter.applyPattern("#####.#####");
219 // 3. Prepare data for UI.
220 final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
221 .getList().get((chosenDay));
223 final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.US);
224 final Calendar calendar = Calendar.getInstance();
225 final Long forecastUNIXDate = (Long) forecast.getDt();
226 calendar.setTimeInMillis(forecastUNIXDate * 1000L);
227 final Date date = calendar.getTime();
230 if (forecast.getTemp().getMax() != null) {
231 double conversion = (Double) forecast.getTemp().getMax();
232 conversion = tempUnitsConversor.doConversion(conversion);
233 tempMax = tempFormatter.format(conversion) + tempSymbol;
236 if (forecast.getTemp().getMin() != null) {
237 double conversion = (Double) forecast.getTemp().getMin();
238 conversion = tempUnitsConversor.doConversion(conversion);
239 tempMin = tempFormatter.format(conversion) + tempSymbol;
242 if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null)
243 && (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) {
244 final String icon = forecast.getWeather().get(0).getIcon();
245 picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
246 .getResourceDrawable());
248 picture = BitmapFactory.decodeResource(this.getResources(),
249 R.drawable.weather_severe_alert);
252 String description = this.getString(R.string.test_field_description_when_error);
253 if (forecast.getWeather().size() > 0) {
254 description = forecast.getWeather().get(0).getDescription();
257 String humidityValue = "";
258 if (forecast.getHumidity() != null) {
259 final double conversion = (Double) forecast.getHumidity();
260 humidityValue = tempFormatter.format(conversion);
262 String pressureValue = "";
263 if (forecast.getPressure() != null) {
264 double conversion = (Double) forecast.getPressure();
265 conversion = pressureUnitsConversor.doConversion(conversion);
266 pressureValue = tempFormatter.format(conversion);
268 String windValue = "";
269 if (forecast.getSpeed() != null) {
270 double conversion = (Double) forecast.getSpeed();
271 conversion = windUnitsConversor.doConversion(conversion);
272 windValue = tempFormatter.format(conversion);
274 String rainValue = "";
275 if (forecast.getRain() != null) {
276 final double conversion = (Double) forecast.getRain();
277 rainValue = tempFormatter.format(conversion);
279 String cloudsValue = "";
280 if (forecast.getRain() != null) {
281 final double conversion = (Double) forecast.getClouds();
282 cloudsValue = tempFormatter.format(conversion);
286 if (forecast.getTemp().getDay() != null) {
287 double conversion = (Double) forecast.getTemp().getDay();
288 conversion = tempUnitsConversor.doConversion(conversion);
289 tempDay = tempFormatter.format(conversion) + tempSymbol;
291 String tempMorn = "";
292 if (forecast.getTemp().getMorn() != null) {
293 double conversion = (Double) forecast.getTemp().getMorn();
294 conversion = tempUnitsConversor.doConversion(conversion);
295 tempMorn = tempFormatter.format(conversion) + tempSymbol;
298 if (forecast.getTemp().getEve() != null) {
299 double conversion = (Double) forecast.getTemp().getEve();
300 conversion = tempUnitsConversor.doConversion(conversion);
301 tempEve = tempFormatter.format(conversion) + tempSymbol;
303 String tempNight = "";
304 if (forecast.getTemp().getNight() != null) {
305 double conversion = (Double) forecast.getTemp().getNight();
306 conversion = tempUnitsConversor.doConversion(conversion);
307 tempNight = tempFormatter.format(conversion) + tempSymbol;
312 this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
314 final TextView tempMaxView = (TextView) getActivity().findViewById(R.id.weather_specific_temp_max);
315 tempMaxView.setText(tempMax);
316 final TextView tempMinView = (TextView) getActivity().findViewById(R.id.weather_specific_temp_min);
317 tempMinView.setText(tempMin);
318 final ImageView pictureView = (ImageView) getActivity().findViewById(R.id.weather_specific_picture);
319 pictureView.setImageBitmap(picture);
321 final TextView descriptionView = (TextView) getActivity().findViewById(R.id.weather_specific_description);
322 descriptionView.setText(description);
324 final TextView humidityValueView = (TextView) getActivity().findViewById(R.id.weather_specific_humidity_value);
325 humidityValueView.setText(humidityValue);
326 ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_value)).setText(pressureValue);
327 ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_units)).setText(pressureSymbol);
328 ((TextView) getActivity().findViewById(R.id.weather_specific_wind_value)).setText(windValue);;
329 ((TextView) getActivity().findViewById(R.id.weather_specific_wind_units)).setText(windSymbol);
330 final TextView rainValueView = (TextView) getActivity().findViewById(R.id.weather_specific_rain_value);
331 rainValueView.setText(rainValue);
332 final TextView cloudsValueView = (TextView) getActivity().findViewById(R.id.weather_specific_clouds_value);
333 cloudsValueView.setText(cloudsValue);
335 final TextView tempDayView = (TextView) getActivity().findViewById(R.id.weather_specific_day_temperature);
336 tempDayView.setText(tempDay);
337 final TextView tempMornView = (TextView) getActivity().findViewById(R.id.weather_specific_morn_temperature);
338 tempMornView.setText(tempMorn);
339 final TextView tempEveView = (TextView) getActivity().findViewById(R.id.weather_specific_eve_temperature);
340 tempEveView.setText(tempEve);
341 final TextView tempNightView = (TextView) getActivity().findViewById(R.id.weather_specific_night_temperature);
342 tempNightView.setText(tempNight);
346 public void onResume() {
349 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
350 final Forecast forecast = store.getForecast();
352 if (forecast != null) {
353 this.updateUI(forecast, this.mChosenDay);