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.app.DialogFragment;
12 import android.app.ListFragment;
13 import android.content.SharedPreferences;
14 import android.graphics.Bitmap;
15 import android.graphics.BitmapFactory;
16 import android.os.Bundle;
17 import android.preference.PreferenceManager;
18 import android.widget.ListView;
19 import de.example.exampletdd.R;
20 import de.example.exampletdd.activityinterface.GetWeather;
21 import de.example.exampletdd.fragment.ErrorDialogFragment;
22 import de.example.exampletdd.fragment.overview.IconsList;
23 import de.example.exampletdd.model.forecastweather.ForecastWeatherData;
24 import de.example.exampletdd.service.WeatherServicePersistenceFile;
26 public class WeatherInformationSpecificDataFragment extends ListFragment implements GetWeather {
27 private boolean mIsFahrenheit;
28 private int mChosenDay;
29 private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
32 public void onCreate(final Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
35 final Bundle extras = this.getActivity().getIntent().getExtras();
38 this.mChosenDay = extras.getInt("CHOSEN_DAY", 0);
43 this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
48 public void onActivityCreated(final Bundle savedInstanceState) {
49 super.onActivityCreated(savedInstanceState);
51 final ListView listWeatherView = this.getListView();
52 listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
54 this.setEmptyText("No data available");
56 this.setListShownNoAnimation(false);
58 if (savedInstanceState != null) {
60 final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
61 .getSerializable("ForecastWeatherData");
63 if (forecastWeatherData != null) {
65 this.mWeatherServicePersistenceFile
66 .storeForecastWeatherData(forecastWeatherData);
67 } catch (final IOException e) {
68 final DialogFragment newFragment = ErrorDialogFragment
69 .newInstance(R.string.error_dialog_generic_error);
70 newFragment.show(this.getFragmentManager(), "errorDialog");
74 this.mChosenDay = savedInstanceState.getInt("Chosen day");
79 public void onSaveInstanceState(final Bundle savedInstanceState) {
82 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
83 .getForecastWeatherData();
85 if (forecastWeatherData != null) {
86 savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
89 savedInstanceState.putInt("Chosend day", this.mChosenDay);
91 super.onSaveInstanceState(savedInstanceState);
95 public void getWeatherByDay(final int chosenDay) {
97 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
98 .getForecastWeatherData();
99 if (forecastWeatherData != null) {
100 this.updateForecastWeatherData(forecastWeatherData, chosenDay);
106 public void getRemoteWeatherInformation() {
111 public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData,
112 final int chosenDay) {
113 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale
115 tempFormatter.applyPattern("#####.#####");
116 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
117 final String symbol = this.mIsFahrenheit ? "ºF" : "ºC";
120 final int[] layouts = new int[4];
121 layouts[0] = R.layout.weather_current_data_entry_first;
122 layouts[1] = R.layout.weather_current_data_entry_second;
123 layouts[2] = R.layout.weather_current_data_entry_third;
124 layouts[3] = R.layout.weather_current_data_entry_fourth;
125 final WeatherCurrentDataAdapter adapter = new WeatherCurrentDataAdapter(this.getActivity(),
129 final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
130 .getList().get((chosenDay));
132 final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.getDefault());
133 final Calendar calendar = Calendar.getInstance();
134 final Long forecastUNIXDate = (Long) forecast.getDt();
135 calendar.setTimeInMillis(forecastUNIXDate * 1000L);
136 final Date date = calendar.getTime();
137 this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
141 if (forecast.getTemp().getMax() != null) {
142 double conversion = (Double) forecast.getTemp().getMax();
143 conversion = conversion - tempUnits;
144 tempMax = tempFormatter.format(conversion) + symbol;
147 if (forecast.getTemp().getMin() != null) {
148 double conversion = (Double) forecast.getTemp().getMin();
149 conversion = conversion - tempUnits;
150 tempMin = tempFormatter.format(conversion) + symbol;
153 if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null)
154 && (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) {
155 final String icon = forecast.getWeather().get(0).getIcon();
156 picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
157 .getResourceDrawable());
159 picture = BitmapFactory.decodeResource(this.getResources(),
160 R.drawable.weather_severe_alert);
162 final WeatherCurrentDataEntryFirst entryFirst = new WeatherCurrentDataEntryFirst(tempMax,
164 adapter.add(entryFirst);
166 String description = "no description available";
167 if (forecast.getWeather().size() > 0) {
168 description = forecast.getWeather().get(0).getDescription();
170 final WeatherCurrentDataEntrySecond entrySecond = new WeatherCurrentDataEntrySecond(
172 adapter.add(entrySecond);
175 String humidityValue = "";
176 if (forecast.getHumidity() != null) {
177 final double conversion = (Double) forecast.getHumidity();
178 humidityValue = tempFormatter.format(conversion);
180 String pressureValue = "";
181 if (forecast.getPressure() != null) {
182 final double conversion = (Double) forecast.getPressure();
183 pressureValue = tempFormatter.format(conversion);
185 String windValue = "";
186 if (forecast.getSpeed() != null) {
187 final double conversion = (Double) forecast.getSpeed();
188 windValue = tempFormatter.format(conversion);
190 String rainValue = "";
191 if (forecast.getRain() != null) {
192 final double conversion = (Double) forecast.getRain();
193 rainValue = tempFormatter.format(conversion);
195 String cloudsValue = "";
196 if (forecast.getRain() != null) {
197 final double conversion = (Double) forecast.getClouds();
198 cloudsValue = tempFormatter.format(conversion);
200 final WeatherCurrentDataEntryThird entryThird = new WeatherCurrentDataEntryThird(
201 humidityValue, pressureValue, windValue, rainValue, cloudsValue);
202 adapter.add(entryThird);
205 if (forecast.getTemp().getDay() != null) {
206 double conversion = (Double) forecast.getTemp().getDay();
207 conversion = conversion - tempUnits;
208 tempDay = tempFormatter.format(conversion) + symbol;
210 String tempMorn = "";
211 if (forecast.getTemp().getMorn() != null) {
212 double conversion = (Double) forecast.getTemp().getMorn();
213 conversion = conversion - tempUnits;
214 tempMorn = tempFormatter.format(conversion) + symbol;
217 if (forecast.getTemp().getEve() != null) {
218 double conversion = (Double) forecast.getTemp().getEve();
219 conversion = conversion - tempUnits;
220 tempEve = tempFormatter.format(conversion) + symbol;
222 String tempNight = "";
223 if (forecast.getTemp().getNight() != null) {
224 double conversion = (Double) forecast.getTemp().getNight();
225 conversion = conversion - tempUnits;
226 tempNight = tempFormatter.format(conversion) + symbol;
228 final WeatherCurrentDataEntryFourth entryFourth = new WeatherCurrentDataEntryFourth(
229 tempMorn, tempDay, tempEve, tempNight);
230 adapter.add(entryFourth);
232 this.setListAdapter(adapter);
236 public void onResume() {
239 final SharedPreferences sharedPreferences = PreferenceManager
240 .getDefaultSharedPreferences(this.getActivity());
242 // 1. Update units of measurement.
243 final String keyPreference = this.getResources().getString(
244 R.string.weather_preferences_units_key);
245 final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
246 final String celsius = this.getResources().getString(
247 R.string.weather_preferences_units_celsius);
248 if (unitsPreferenceValue.equals(celsius)) {
249 this.mIsFahrenheit = false;
251 this.mIsFahrenheit = true;
255 // 2. Update weather data on display.
256 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
257 .getForecastWeatherData();
258 if (forecastWeatherData != null) {
259 this.updateForecastWeatherData(forecastWeatherData, this.mChosenDay);