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 // TODO: string resource
253 String description = "no description available";
254 if (forecast.getWeather().size() > 0) {
255 description = forecast.getWeather().get(0).getDescription();
259 String humidityValue = "";
260 if (forecast.getHumidity() != null) {
261 final double conversion = (Double) forecast.getHumidity();
262 humidityValue = tempFormatter.format(conversion);
264 String pressureValue = "";
265 if (forecast.getPressure() != null) {
266 double conversion = (Double) forecast.getPressure();
267 conversion = pressureUnitsConversor.doConversion(conversion);
268 pressureValue = tempFormatter.format(conversion);
270 String windValue = "";
271 if (forecast.getSpeed() != null) {
272 double conversion = (Double) forecast.getSpeed();
273 conversion = windUnitsConversor.doConversion(conversion);
274 windValue = tempFormatter.format(conversion);
276 String rainValue = "";
277 if (forecast.getRain() != null) {
278 final double conversion = (Double) forecast.getRain();
279 rainValue = tempFormatter.format(conversion);
281 String cloudsValue = "";
282 if (forecast.getRain() != null) {
283 final double conversion = (Double) forecast.getClouds();
284 cloudsValue = tempFormatter.format(conversion);
288 if (forecast.getTemp().getDay() != null) {
289 double conversion = (Double) forecast.getTemp().getDay();
290 conversion = tempUnitsConversor.doConversion(conversion);
291 tempDay = tempFormatter.format(conversion) + tempSymbol;
293 String tempMorn = "";
294 if (forecast.getTemp().getMorn() != null) {
295 double conversion = (Double) forecast.getTemp().getMorn();
296 conversion = tempUnitsConversor.doConversion(conversion);
297 tempMorn = tempFormatter.format(conversion) + tempSymbol;
300 if (forecast.getTemp().getEve() != null) {
301 double conversion = (Double) forecast.getTemp().getEve();
302 conversion = tempUnitsConversor.doConversion(conversion);
303 tempEve = tempFormatter.format(conversion) + tempSymbol;
305 String tempNight = "";
306 if (forecast.getTemp().getNight() != null) {
307 double conversion = (Double) forecast.getTemp().getNight();
308 conversion = tempUnitsConversor.doConversion(conversion);
309 tempNight = tempFormatter.format(conversion) + tempSymbol;
314 this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
316 final TextView tempMaxView = (TextView) getActivity().findViewById(R.id.weather_specific_temp_max);
317 tempMaxView.setText(tempMax);
318 final TextView tempMinView = (TextView) getActivity().findViewById(R.id.weather_specific_temp_min);
319 tempMinView.setText(tempMin);
320 final ImageView pictureView = (ImageView) getActivity().findViewById(R.id.weather_specific_picture);
321 pictureView.setImageBitmap(picture);
323 final TextView descriptionView = (TextView) getActivity().findViewById(R.id.weather_specific_description);
324 descriptionView.setText(description);
326 final TextView humidityValueView = (TextView) getActivity().findViewById(R.id.weather_specific_humidity_value);
327 humidityValueView.setText(humidityValue);
328 ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_value)).setText(pressureValue);
329 ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_units)).setText(pressureSymbol);
330 ((TextView) getActivity().findViewById(R.id.weather_specific_wind_value)).setText(windValue);;
331 ((TextView) getActivity().findViewById(R.id.weather_specific_wind_units)).setText(windSymbol);
332 final TextView rainValueView = (TextView) getActivity().findViewById(R.id.weather_specific_rain_value);
333 rainValueView.setText(rainValue);
334 final TextView cloudsValueView = (TextView) getActivity().findViewById(R.id.weather_specific_clouds_value);
335 cloudsValueView.setText(cloudsValue);
337 final TextView tempDayView = (TextView) getActivity().findViewById(R.id.weather_specific_day_temperature);
338 tempDayView.setText(tempDay);
339 final TextView tempMornView = (TextView) getActivity().findViewById(R.id.weather_specific_morn_temperature);
340 tempMornView.setText(tempMorn);
341 final TextView tempEveView = (TextView) getActivity().findViewById(R.id.weather_specific_eve_temperature);
342 tempEveView.setText(tempEve);
343 final TextView tempNightView = (TextView) getActivity().findViewById(R.id.weather_specific_night_temperature);
344 tempNightView.setText(tempNight);
348 public void onResume() {
351 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
352 final Forecast forecast = store.getForecast();
354 if (forecast != null) {
355 this.updateUI(forecast, this.mChosenDay);
358 // TODO: Overview is doing things with mListState... Why not here?