1 package de.example.exampletdd.fragment.specific;
3 import java.io.IOException;
4 import java.text.DecimalFormat;
5 import java.text.NumberFormat;
6 import java.text.SimpleDateFormat;
7 import java.util.Calendar;
9 import java.util.Locale;
11 import android.content.SharedPreferences;
12 import android.graphics.Bitmap;
13 import android.graphics.BitmapFactory;
14 import android.os.Bundle;
15 import android.preference.PreferenceManager;
16 import android.support.v4.app.DialogFragment;
17 import android.support.v4.app.ListFragment;
18 import android.widget.ListView;
19 import de.example.exampletdd.R;
20 import de.example.exampletdd.fragment.ErrorDialogFragment;
21 import de.example.exampletdd.fragment.overview.IconsList;
22 import de.example.exampletdd.model.forecastweather.ForecastWeatherData;
23 import de.example.exampletdd.service.WeatherServicePersistenceFile;
25 public class WeatherInformationSpecificDataFragment extends ListFragment {
26 private boolean mIsFahrenheit;
27 private int mChosenDay;
28 private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
31 public void onCreate(final Bundle savedInstanceState) {
32 super.onCreate(savedInstanceState);
34 final Bundle extras = this.getActivity().getIntent().getExtras();
37 this.mChosenDay = extras.getInt("CHOSEN_DAY", 0);
42 this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
47 public void onActivityCreated(final Bundle savedInstanceState) {
48 super.onActivityCreated(savedInstanceState);
50 final ListView listWeatherView = this.getListView();
51 listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
53 this.setEmptyText("No data available");
55 this.setListShownNoAnimation(false);
57 if (savedInstanceState != null) {
59 final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
60 .getSerializable("ForecastWeatherData");
62 if (forecastWeatherData != null) {
64 this.mWeatherServicePersistenceFile
65 .storeForecastWeatherData(forecastWeatherData);
66 } catch (final IOException e) {
67 final DialogFragment newFragment = ErrorDialogFragment
68 .newInstance(R.string.error_dialog_generic_error);
69 newFragment.show(this.getFragmentManager(), "errorDialog");
73 this.mChosenDay = savedInstanceState.getInt("Chosen day");
78 public void onSaveInstanceState(final Bundle savedInstanceState) {
81 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
82 .getForecastWeatherData();
84 if (forecastWeatherData != null) {
85 savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
88 savedInstanceState.putInt("Chosend day", this.mChosenDay);
90 super.onSaveInstanceState(savedInstanceState);
93 public void getWeatherByDay(final int chosenDay) {
95 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
96 .getForecastWeatherData();
97 if (forecastWeatherData != null) {
98 this.updateForecastWeatherData(forecastWeatherData, chosenDay);
104 public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData,
105 final int chosenDay) {
106 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
107 tempFormatter.applyPattern("#####.#####");
108 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
109 final String symbol = this.mIsFahrenheit ? "ºF" : "ºC";
112 final int[] layouts = new int[4];
113 layouts[0] = R.layout.weather_current_data_entry_first;
114 layouts[1] = R.layout.weather_current_data_entry_second;
115 layouts[2] = R.layout.weather_current_data_entry_third;
116 layouts[3] = R.layout.weather_current_data_entry_fourth;
117 final WeatherCurrentDataAdapter adapter = new WeatherCurrentDataAdapter(this.getActivity(),
121 final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
122 .getList().get((chosenDay));
124 final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.US);
125 final Calendar calendar = Calendar.getInstance();
126 final Long forecastUNIXDate = (Long) forecast.getDt();
127 calendar.setTimeInMillis(forecastUNIXDate * 1000L);
128 final Date date = calendar.getTime();
129 this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
133 if (forecast.getTemp().getMax() != null) {
134 double conversion = (Double) forecast.getTemp().getMax();
135 conversion = conversion - tempUnits;
136 tempMax = tempFormatter.format(conversion) + symbol;
139 if (forecast.getTemp().getMin() != null) {
140 double conversion = (Double) forecast.getTemp().getMin();
141 conversion = conversion - tempUnits;
142 tempMin = tempFormatter.format(conversion) + symbol;
145 if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null)
146 && (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) {
147 final String icon = forecast.getWeather().get(0).getIcon();
148 picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
149 .getResourceDrawable());
151 picture = BitmapFactory.decodeResource(this.getResources(),
152 R.drawable.weather_severe_alert);
154 final WeatherCurrentDataEntryFirst entryFirst = new WeatherCurrentDataEntryFirst(tempMax,
156 adapter.add(entryFirst);
158 String description = "no description available";
159 if (forecast.getWeather().size() > 0) {
160 description = forecast.getWeather().get(0).getDescription();
162 final WeatherCurrentDataEntrySecond entrySecond = new WeatherCurrentDataEntrySecond(
164 adapter.add(entrySecond);
167 String humidityValue = "";
168 if (forecast.getHumidity() != null) {
169 final double conversion = (Double) forecast.getHumidity();
170 humidityValue = tempFormatter.format(conversion);
172 String pressureValue = "";
173 if (forecast.getPressure() != null) {
174 final double conversion = (Double) forecast.getPressure();
175 pressureValue = tempFormatter.format(conversion);
177 String windValue = "";
178 if (forecast.getSpeed() != null) {
179 final double conversion = (Double) forecast.getSpeed();
180 windValue = tempFormatter.format(conversion);
182 String rainValue = "";
183 if (forecast.getRain() != null) {
184 final double conversion = (Double) forecast.getRain();
185 rainValue = tempFormatter.format(conversion);
187 String cloudsValue = "";
188 if (forecast.getRain() != null) {
189 final double conversion = (Double) forecast.getClouds();
190 cloudsValue = tempFormatter.format(conversion);
192 final WeatherCurrentDataEntryThird entryThird = new WeatherCurrentDataEntryThird(
193 humidityValue, pressureValue, windValue, rainValue, cloudsValue);
194 adapter.add(entryThird);
197 if (forecast.getTemp().getDay() != null) {
198 double conversion = (Double) forecast.getTemp().getDay();
199 conversion = conversion - tempUnits;
200 tempDay = tempFormatter.format(conversion) + symbol;
202 String tempMorn = "";
203 if (forecast.getTemp().getMorn() != null) {
204 double conversion = (Double) forecast.getTemp().getMorn();
205 conversion = conversion - tempUnits;
206 tempMorn = tempFormatter.format(conversion) + symbol;
209 if (forecast.getTemp().getEve() != null) {
210 double conversion = (Double) forecast.getTemp().getEve();
211 conversion = conversion - tempUnits;
212 tempEve = tempFormatter.format(conversion) + symbol;
214 String tempNight = "";
215 if (forecast.getTemp().getNight() != null) {
216 double conversion = (Double) forecast.getTemp().getNight();
217 conversion = conversion - tempUnits;
218 tempNight = tempFormatter.format(conversion) + symbol;
220 final WeatherCurrentDataEntryFourth entryFourth = new WeatherCurrentDataEntryFourth(
221 tempMorn, tempDay, tempEve, tempNight);
222 adapter.add(entryFourth);
224 this.setListAdapter(adapter);
228 public void onResume() {
231 final SharedPreferences sharedPreferences = PreferenceManager
232 .getDefaultSharedPreferences(this.getActivity());
234 // 1. Update units of measurement.
235 final String keyPreference = this.getResources().getString(
236 R.string.weather_preferences_units_key);
237 final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
238 final String celsius = this.getResources().getString(
239 R.string.weather_preferences_units_celsius);
240 if (unitsPreferenceValue.equals(celsius)) {
241 this.mIsFahrenheit = false;
243 this.mIsFahrenheit = true;
247 // 2. Update weather data on display.
248 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
249 .getForecastWeatherData();
250 if (forecastWeatherData != null) {
251 this.updateForecastWeatherData(forecastWeatherData, this.mChosenDay);