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");
77 public void onSaveInstanceState(final Bundle savedInstanceState) {
80 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
81 .getForecastWeatherData();
83 if (forecastWeatherData != null) {
84 savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
87 super.onSaveInstanceState(savedInstanceState);
91 public void getWeatherByDay(final int chosenDay) {
93 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
94 .getForecastWeatherData();
95 if (forecastWeatherData != null) {
96 this.updateForecastWeatherData(forecastWeatherData, chosenDay);
102 public void getRemoteWeatherInformation() {
107 public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData,
108 final int chosenDay) {
109 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale
111 tempFormatter.applyPattern("#####.#####");
112 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
113 final String symbol = this.mIsFahrenheit ? "ºF" : "ºC";
116 final int[] layouts = new int[4];
117 layouts[0] = R.layout.weather_current_data_entry_first;
118 layouts[1] = R.layout.weather_current_data_entry_second;
119 layouts[2] = R.layout.weather_current_data_entry_third;
120 layouts[3] = R.layout.weather_current_data_entry_fourth;
121 final WeatherCurrentDataAdapter adapter = new WeatherCurrentDataAdapter(this.getActivity(),
125 final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
126 .getList().get((chosenDay));
128 final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.getDefault());
129 final Calendar calendar = Calendar.getInstance();
130 final Long forecastUNIXDate = (Long) forecast.getDt();
131 calendar.setTimeInMillis(forecastUNIXDate * 1000L);
132 final Date date = calendar.getTime();
133 this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
137 if (forecast.getTemp().getMax() != null) {
138 double conversion = (Double) forecast.getTemp().getMax();
139 conversion = conversion - tempUnits;
140 tempMax = tempFormatter.format(conversion) + symbol;
143 if (forecast.getTemp().getMin() != null) {
144 double conversion = (Double) forecast.getTemp().getMin();
145 conversion = conversion - tempUnits;
146 tempMin = tempFormatter.format(conversion) + symbol;
149 if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null)
150 && (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) {
151 final String icon = forecast.getWeather().get(0).getIcon();
152 picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
153 .getResourceDrawable());
155 picture = BitmapFactory.decodeResource(this.getResources(),
156 R.drawable.weather_severe_alert);
158 final WeatherCurrentDataEntryFirst entryFirst = new WeatherCurrentDataEntryFirst(tempMax,
160 adapter.add(entryFirst);
162 String description = "no description available";
163 if (forecast.getWeather().size() > 0) {
164 description = forecast.getWeather().get(0).getDescription();
166 final WeatherCurrentDataEntrySecond entrySecond = new WeatherCurrentDataEntrySecond(
168 adapter.add(entrySecond);
171 String humidityValue = "";
172 if (forecast.getHumidity() != null) {
173 final double conversion = (Double) forecast.getHumidity();
174 humidityValue = tempFormatter.format(conversion);
176 String pressureValue = "";
177 if (forecast.getPressure() != null) {
178 final double conversion = (Double) forecast.getPressure();
179 pressureValue = tempFormatter.format(conversion);
181 String windValue = "";
182 if (forecast.getSpeed() != null) {
183 final double conversion = (Double) forecast.getSpeed();
184 windValue = tempFormatter.format(conversion);
186 String rainValue = "";
187 if (forecast.getRain() != null) {
188 final double conversion = (Double) forecast.getRain();
189 rainValue = tempFormatter.format(conversion);
191 String cloudsValue = "";
192 if (forecast.getRain() != null) {
193 final double conversion = (Double) forecast.getClouds();
194 cloudsValue = tempFormatter.format(conversion);
196 final WeatherCurrentDataEntryThird entryThird = new WeatherCurrentDataEntryThird(
197 humidityValue, pressureValue, windValue, rainValue, cloudsValue);
198 adapter.add(entryThird);
201 if (forecast.getTemp().getDay() != null) {
202 double conversion = (Double) forecast.getTemp().getDay();
203 conversion = conversion - tempUnits;
204 tempDay = tempFormatter.format(conversion) + symbol;
206 String tempMorn = "";
207 if (forecast.getTemp().getMorn() != null) {
208 double conversion = (Double) forecast.getTemp().getMorn();
209 conversion = conversion - tempUnits;
210 tempMorn = tempFormatter.format(conversion) + symbol;
213 if (forecast.getTemp().getEve() != null) {
214 double conversion = (Double) forecast.getTemp().getEve();
215 conversion = conversion - tempUnits;
216 tempEve = tempFormatter.format(conversion) + symbol;
218 String tempNight = "";
219 if (forecast.getTemp().getNight() != null) {
220 double conversion = (Double) forecast.getTemp().getNight();
221 conversion = conversion - tempUnits;
222 tempNight = tempFormatter.format(conversion) + symbol;
224 final WeatherCurrentDataEntryFourth entryFourth = new WeatherCurrentDataEntryFourth(
225 tempMorn, tempDay, tempEve, tempNight);
226 adapter.add(entryFourth);
228 this.setListAdapter(adapter);
232 public void onResume() {
235 final SharedPreferences sharedPreferences = PreferenceManager
236 .getDefaultSharedPreferences(this.getActivity());
238 // 1. Update units of measurement.
239 final String keyPreference = this.getResources().getString(
240 R.string.weather_preferences_units_key);
241 final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
242 final String celsius = this.getResources().getString(
243 R.string.weather_preferences_units_celsius);
244 if (unitsPreferenceValue.equals(celsius)) {
245 this.mIsFahrenheit = false;
247 this.mIsFahrenheit = true;
251 // 2. Update weather data on display.
252 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
253 .getForecastWeatherData();
254 if (forecastWeatherData != null) {
255 this.updateForecastWeatherData(forecastWeatherData, this.mChosenDay);