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 unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
125 String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
126 if (unitsPreferenceValue.equals(values[0])) {
127 tempSymbol = values[0];
128 tempUnitsConversor = new UnitsConversor(){
131 public double doConversion(final double value) {
132 return value - 273.15;
136 } else if (unitsPreferenceValue.equals(values[1])) {
137 tempSymbol = values[1];
138 tempUnitsConversor = new UnitsConversor(){
141 public double doConversion(final double value) {
142 return (value * 1.8) - 459.67;
147 tempSymbol = values[2];
148 tempUnitsConversor = new UnitsConversor(){
151 public double doConversion(final double value) {
160 UnitsConversor windUnitsConversor;
161 keyPreference = this.getResources().getString(R.string.weather_preferences_wind_key);
162 unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
163 values = this.getResources().getStringArray(R.array.weather_preferences_wind);
164 if (unitsPreferenceValue.equals(values[0])) {
165 windSymbol = values[0];
166 windUnitsConversor = new UnitsConversor(){
169 public double doConversion(double value) {
174 windSymbol = values[1];
175 windUnitsConversor = new UnitsConversor(){
178 public double doConversion(double value) {
179 return value * 2.237;
185 String pressureSymbol;
186 UnitsConversor pressureUnitsConversor;
187 keyPreference = this.getResources().getString(R.string.weather_preferences_pressure_key);
188 unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
189 values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
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 de.example.exampletdd.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 // TODO: string resource
250 String description = "no description available";
251 if (forecast.getWeather().size() > 0) {
252 description = forecast.getWeather().get(0).getDescription();
256 String humidityValue = "";
257 if (forecast.getHumidity() != null) {
258 final double conversion = (Double) forecast.getHumidity();
259 humidityValue = tempFormatter.format(conversion);
261 String pressureValue = "";
262 if (forecast.getPressure() != null) {
263 double conversion = (Double) forecast.getPressure();
264 conversion = pressureUnitsConversor.doConversion(conversion);
265 pressureValue = tempFormatter.format(conversion);
267 String windValue = "";
268 if (forecast.getSpeed() != null) {
269 double conversion = (Double) forecast.getSpeed();
270 conversion = windUnitsConversor.doConversion(conversion);
271 windValue = tempFormatter.format(conversion);
273 String rainValue = "";
274 if (forecast.getRain() != null) {
275 final double conversion = (Double) forecast.getRain();
276 rainValue = tempFormatter.format(conversion);
278 String cloudsValue = "";
279 if (forecast.getRain() != null) {
280 final double conversion = (Double) forecast.getClouds();
281 cloudsValue = tempFormatter.format(conversion);
285 if (forecast.getTemp().getDay() != null) {
286 double conversion = (Double) forecast.getTemp().getDay();
287 conversion = tempUnitsConversor.doConversion(conversion);
288 tempDay = tempFormatter.format(conversion) + tempSymbol;
290 String tempMorn = "";
291 if (forecast.getTemp().getMorn() != null) {
292 double conversion = (Double) forecast.getTemp().getMorn();
293 conversion = tempUnitsConversor.doConversion(conversion);
294 tempMorn = tempFormatter.format(conversion) + tempSymbol;
297 if (forecast.getTemp().getEve() != null) {
298 double conversion = (Double) forecast.getTemp().getEve();
299 conversion = tempUnitsConversor.doConversion(conversion);
300 tempEve = tempFormatter.format(conversion) + tempSymbol;
302 String tempNight = "";
303 if (forecast.getTemp().getNight() != null) {
304 double conversion = (Double) forecast.getTemp().getNight();
305 conversion = tempUnitsConversor.doConversion(conversion);
306 tempNight = tempFormatter.format(conversion) + tempSymbol;
311 this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
313 final TextView tempMaxView = (TextView) getActivity().findViewById(R.id.weather_specific_temp_max);
314 tempMaxView.setText(tempMax);
315 final TextView tempMinView = (TextView) getActivity().findViewById(R.id.weather_specific_temp_min);
316 tempMinView.setText(tempMin);
317 final ImageView pictureView = (ImageView) getActivity().findViewById(R.id.weather_specific_picture);
318 pictureView.setImageBitmap(picture);
320 final TextView descriptionView = (TextView) getActivity().findViewById(R.id.weather_specific_description);
321 descriptionView.setText(description);
323 final TextView humidityValueView = (TextView) getActivity().findViewById(R.id.weather_specific_humidity_value);
324 humidityValueView.setText(humidityValue);
325 ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_value)).setText(pressureValue);
326 ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_units)).setText(pressureSymbol);
327 ((TextView) getActivity().findViewById(R.id.weather_specific_wind_value)).setText(windValue);;
328 ((TextView) getActivity().findViewById(R.id.weather_specific_wind_units)).setText(windSymbol);
329 final TextView rainValueView = (TextView) getActivity().findViewById(R.id.weather_specific_rain_value);
330 rainValueView.setText(rainValue);
331 final TextView cloudsValueView = (TextView) getActivity().findViewById(R.id.weather_specific_clouds_value);
332 cloudsValueView.setText(cloudsValue);
334 final TextView tempDayView = (TextView) getActivity().findViewById(R.id.weather_specific_day_temperature);
335 tempDayView.setText(tempDay);
336 final TextView tempMornView = (TextView) getActivity().findViewById(R.id.weather_specific_morn_temperature);
337 tempMornView.setText(tempMorn);
338 final TextView tempEveView = (TextView) getActivity().findViewById(R.id.weather_specific_eve_temperature);
339 tempEveView.setText(tempEve);
340 final TextView tempNightView = (TextView) getActivity().findViewById(R.id.weather_specific_night_temperature);
341 tempNightView.setText(tempNight);
345 public void onResume() {
348 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
349 final Forecast forecast = store.getForecast();
351 if (forecast != null) {
352 this.updateUI(forecast, this.mChosenDay);
355 // TODO: Overview is doing things with mListState... Why not here?