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 if (forecast != null) {
63 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
64 store.saveForecast(forecast);
67 this.mChosenDay = savedInstanceState.getInt("mChosenDay");
70 this.setHasOptionsMenu(false);
74 public void onSaveInstanceState(final Bundle savedInstanceState) {
77 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
78 final Forecast forecast = store.getForecast();
80 if (forecast != null) {
81 savedInstanceState.putSerializable("Forecast", forecast);
84 savedInstanceState.putInt("mChosenDay", this.mChosenDay);
86 super.onSaveInstanceState(savedInstanceState);
90 * This method is used by tablet layout.
94 public void updateUIByChosenDay(final int chosenDay) {
95 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
96 final Forecast forecast = store.getForecast();
98 if (forecast != null) {
99 this.updateUI(forecast, chosenDay);
103 private interface UnitsConversor {
105 public double doConversion(final double value);
108 private void updateUI(final Forecast forecastWeatherData, final int chosenDay) {
110 final SharedPreferences sharedPreferences = PreferenceManager
111 .getDefaultSharedPreferences(this.getActivity());
113 // TODO: repeating the same code in Overview, Specific and Current!!!
114 // 1. Update units of measurement.
117 UnitsConversor tempUnitsConversor;
118 String keyPreference = this.getResources().getString(
119 R.string.weather_preferences_temperature_key);
120 String[] values = this.getResources().getStringArray(R.array.weather_preferences_temperature);
121 String unitsPreferenceValue = sharedPreferences.getString(
122 keyPreference, this.getString(R.string.weather_preferences_temperature_celsius));
123 if (unitsPreferenceValue.equals(values[0])) {
124 tempSymbol = values[0];
125 tempUnitsConversor = new UnitsConversor(){
128 public double doConversion(final double value) {
129 return value - 273.15;
133 } else if (unitsPreferenceValue.equals(values[1])) {
134 tempSymbol = values[1];
135 tempUnitsConversor = new UnitsConversor(){
138 public double doConversion(final double value) {
139 return (value * 1.8) - 459.67;
144 tempSymbol = values[2];
145 tempUnitsConversor = new UnitsConversor(){
148 public double doConversion(final double value) {
157 UnitsConversor windUnitsConversor;
158 keyPreference = this.getResources().getString(R.string.weather_preferences_wind_key);
159 values = this.getResources().getStringArray(R.array.weather_preferences_wind);
160 unitsPreferenceValue = sharedPreferences.getString(
161 keyPreference, this.getString(R.string.weather_preferences_wind_meters));
162 if (unitsPreferenceValue.equals(values[0])) {
163 windSymbol = values[0];
164 windUnitsConversor = new UnitsConversor(){
167 public double doConversion(double value) {
172 windSymbol = values[1];
173 windUnitsConversor = new UnitsConversor(){
176 public double doConversion(double value) {
177 return value * 2.237;
183 String pressureSymbol;
184 UnitsConversor pressureUnitsConversor;
185 keyPreference = this.getResources().getString(R.string.weather_preferences_pressure_key);
186 values = this.getResources().getStringArray(R.array.weather_preferences_pressure);
187 unitsPreferenceValue = sharedPreferences.getString(
188 keyPreference, this.getString(R.string.weather_preferences_pressure_pascal));
189 if (unitsPreferenceValue.equals(values[0])) {
190 pressureSymbol = values[0];
191 pressureUnitsConversor = new UnitsConversor(){
194 public double doConversion(double value) {
199 pressureSymbol = values[1];
200 pressureUnitsConversor = new UnitsConversor(){
203 public double doConversion(double value) {
204 return value / 113.25d;
211 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
212 tempFormatter.applyPattern("#####.#####");
215 // 3. Prepare data for UI.
216 final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
217 .getList().get((chosenDay));
219 final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.US);
220 final Calendar calendar = Calendar.getInstance();
221 final Long forecastUNIXDate = (Long) forecast.getDt();
222 calendar.setTimeInMillis(forecastUNIXDate * 1000L);
223 final Date date = calendar.getTime();
226 if (forecast.getTemp().getMax() != null) {
227 double conversion = (Double) forecast.getTemp().getMax();
228 conversion = tempUnitsConversor.doConversion(conversion);
229 tempMax = tempFormatter.format(conversion) + tempSymbol;
232 if (forecast.getTemp().getMin() != null) {
233 double conversion = (Double) forecast.getTemp().getMin();
234 conversion = tempUnitsConversor.doConversion(conversion);
235 tempMin = tempFormatter.format(conversion) + tempSymbol;
238 if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null)
239 && (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) {
240 final String icon = forecast.getWeather().get(0).getIcon();
241 picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
242 .getResourceDrawable());
244 picture = BitmapFactory.decodeResource(this.getResources(),
245 R.drawable.weather_severe_alert);
248 String description = this.getString(R.string.text_field_description_when_error);
249 if (forecast.getWeather().size() > 0) {
250 description = forecast.getWeather().get(0).getDescription();
253 String humidityValue = "";
254 if (forecast.getHumidity() != null) {
255 final double conversion = (Double) forecast.getHumidity();
256 humidityValue = tempFormatter.format(conversion);
258 String pressureValue = "";
259 if (forecast.getPressure() != null) {
260 double conversion = (Double) forecast.getPressure();
261 conversion = pressureUnitsConversor.doConversion(conversion);
262 pressureValue = tempFormatter.format(conversion);
264 String windValue = "";
265 if (forecast.getSpeed() != null) {
266 double conversion = (Double) forecast.getSpeed();
267 conversion = windUnitsConversor.doConversion(conversion);
268 windValue = tempFormatter.format(conversion);
270 String rainValue = "";
271 if (forecast.getRain() != null) {
272 final double conversion = (Double) forecast.getRain();
273 rainValue = tempFormatter.format(conversion);
275 String cloudsValue = "";
276 if (forecast.getRain() != null) {
277 final double conversion = (Double) forecast.getClouds();
278 cloudsValue = tempFormatter.format(conversion);
282 if (forecast.getTemp().getDay() != null) {
283 double conversion = (Double) forecast.getTemp().getDay();
284 conversion = tempUnitsConversor.doConversion(conversion);
285 tempDay = tempFormatter.format(conversion) + tempSymbol;
287 String tempMorn = "";
288 if (forecast.getTemp().getMorn() != null) {
289 double conversion = (Double) forecast.getTemp().getMorn();
290 conversion = tempUnitsConversor.doConversion(conversion);
291 tempMorn = tempFormatter.format(conversion) + tempSymbol;
294 if (forecast.getTemp().getEve() != null) {
295 double conversion = (Double) forecast.getTemp().getEve();
296 conversion = tempUnitsConversor.doConversion(conversion);
297 tempEve = tempFormatter.format(conversion) + tempSymbol;
299 String tempNight = "";
300 if (forecast.getTemp().getNight() != null) {
301 double conversion = (Double) forecast.getTemp().getNight();
302 conversion = tempUnitsConversor.doConversion(conversion);
303 tempNight = tempFormatter.format(conversion) + tempSymbol;
308 this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
310 final TextView tempMaxView = (TextView) getActivity().findViewById(R.id.weather_specific_temp_max);
311 tempMaxView.setText(tempMax);
312 final TextView tempMinView = (TextView) getActivity().findViewById(R.id.weather_specific_temp_min);
313 tempMinView.setText(tempMin);
314 final ImageView pictureView = (ImageView) getActivity().findViewById(R.id.weather_specific_picture);
315 pictureView.setImageBitmap(picture);
317 final TextView descriptionView = (TextView) getActivity().findViewById(R.id.weather_specific_description);
318 descriptionView.setText(description);
320 final TextView humidityValueView = (TextView) getActivity().findViewById(R.id.weather_specific_humidity_value);
321 humidityValueView.setText(humidityValue);
322 ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_value)).setText(pressureValue);
323 ((TextView) getActivity().findViewById(R.id.weather_specific_pressure_units)).setText(pressureSymbol);
324 ((TextView) getActivity().findViewById(R.id.weather_specific_wind_value)).setText(windValue);;
325 ((TextView) getActivity().findViewById(R.id.weather_specific_wind_units)).setText(windSymbol);
326 final TextView rainValueView = (TextView) getActivity().findViewById(R.id.weather_specific_rain_value);
327 rainValueView.setText(rainValue);
328 final TextView cloudsValueView = (TextView) getActivity().findViewById(R.id.weather_specific_clouds_value);
329 cloudsValueView.setText(cloudsValue);
331 final TextView tempDayView = (TextView) getActivity().findViewById(R.id.weather_specific_day_temperature);
332 tempDayView.setText(tempDay);
333 final TextView tempMornView = (TextView) getActivity().findViewById(R.id.weather_specific_morn_temperature);
334 tempMornView.setText(tempMorn);
335 final TextView tempEveView = (TextView) getActivity().findViewById(R.id.weather_specific_eve_temperature);
336 tempEveView.setText(tempEve);
337 final TextView tempNightView = (TextView) getActivity().findViewById(R.id.weather_specific_night_temperature);
338 tempNightView.setText(tempNight);
342 public void onResume() {
345 final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext());
346 final Forecast forecast = store.getForecast();
348 if (forecast != null) {
349 this.updateUI(forecast, this.mChosenDay);