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.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.US);
114 tempFormatter.applyPattern("#####.#####");
115 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
116 final String symbol = this.mIsFahrenheit ? "ºF" : "ºC";
119 final int[] layouts = new int[4];
120 layouts[0] = R.layout.weather_current_data_entry_first;
121 layouts[1] = R.layout.weather_current_data_entry_second;
122 layouts[2] = R.layout.weather_current_data_entry_third;
123 layouts[3] = R.layout.weather_current_data_entry_fourth;
124 final WeatherCurrentDataAdapter adapter = new WeatherCurrentDataAdapter(this.getActivity(),
128 final de.example.exampletdd.model.forecastweather.List forecast = forecastWeatherData
129 .getList().get((chosenDay));
131 final SimpleDateFormat dayFormatter = new SimpleDateFormat("EEEE - MMM d", Locale.US);
132 final Calendar calendar = Calendar.getInstance();
133 final Long forecastUNIXDate = (Long) forecast.getDt();
134 calendar.setTimeInMillis(forecastUNIXDate * 1000L);
135 final Date date = calendar.getTime();
136 this.getActivity().getActionBar().setSubtitle(dayFormatter.format(date).toUpperCase());
140 if (forecast.getTemp().getMax() != null) {
141 double conversion = (Double) forecast.getTemp().getMax();
142 conversion = conversion - tempUnits;
143 tempMax = tempFormatter.format(conversion) + symbol;
146 if (forecast.getTemp().getMin() != null) {
147 double conversion = (Double) forecast.getTemp().getMin();
148 conversion = conversion - tempUnits;
149 tempMin = tempFormatter.format(conversion) + symbol;
152 if ((forecast.getWeather().size() > 0) && (forecast.getWeather().get(0).getIcon() != null)
153 && (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) {
154 final String icon = forecast.getWeather().get(0).getIcon();
155 picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
156 .getResourceDrawable());
158 picture = BitmapFactory.decodeResource(this.getResources(),
159 R.drawable.weather_severe_alert);
161 final WeatherCurrentDataEntryFirst entryFirst = new WeatherCurrentDataEntryFirst(tempMax,
163 adapter.add(entryFirst);
165 String description = "no description available";
166 if (forecast.getWeather().size() > 0) {
167 description = forecast.getWeather().get(0).getDescription();
169 final WeatherCurrentDataEntrySecond entrySecond = new WeatherCurrentDataEntrySecond(
171 adapter.add(entrySecond);
174 String humidityValue = "";
175 if (forecast.getHumidity() != null) {
176 final double conversion = (Double) forecast.getHumidity();
177 humidityValue = tempFormatter.format(conversion);
179 String pressureValue = "";
180 if (forecast.getPressure() != null) {
181 final double conversion = (Double) forecast.getPressure();
182 pressureValue = tempFormatter.format(conversion);
184 String windValue = "";
185 if (forecast.getSpeed() != null) {
186 final double conversion = (Double) forecast.getSpeed();
187 windValue = tempFormatter.format(conversion);
189 String rainValue = "";
190 if (forecast.getRain() != null) {
191 final double conversion = (Double) forecast.getRain();
192 rainValue = tempFormatter.format(conversion);
194 String cloudsValue = "";
195 if (forecast.getRain() != null) {
196 final double conversion = (Double) forecast.getClouds();
197 cloudsValue = tempFormatter.format(conversion);
199 final WeatherCurrentDataEntryThird entryThird = new WeatherCurrentDataEntryThird(
200 humidityValue, pressureValue, windValue, rainValue, cloudsValue);
201 adapter.add(entryThird);
204 if (forecast.getTemp().getDay() != null) {
205 double conversion = (Double) forecast.getTemp().getDay();
206 conversion = conversion - tempUnits;
207 tempDay = tempFormatter.format(conversion) + symbol;
209 String tempMorn = "";
210 if (forecast.getTemp().getMorn() != null) {
211 double conversion = (Double) forecast.getTemp().getMorn();
212 conversion = conversion - tempUnits;
213 tempMorn = tempFormatter.format(conversion) + symbol;
216 if (forecast.getTemp().getEve() != null) {
217 double conversion = (Double) forecast.getTemp().getEve();
218 conversion = conversion - tempUnits;
219 tempEve = tempFormatter.format(conversion) + symbol;
221 String tempNight = "";
222 if (forecast.getTemp().getNight() != null) {
223 double conversion = (Double) forecast.getTemp().getNight();
224 conversion = conversion - tempUnits;
225 tempNight = tempFormatter.format(conversion) + symbol;
227 final WeatherCurrentDataEntryFourth entryFourth = new WeatherCurrentDataEntryFourth(
228 tempMorn, tempDay, tempEve, tempNight);
229 adapter.add(entryFourth);
231 this.setListAdapter(adapter);
235 public void onResume() {
238 final SharedPreferences sharedPreferences = PreferenceManager
239 .getDefaultSharedPreferences(this.getActivity());
241 // 1. Update units of measurement.
242 final String keyPreference = this.getResources().getString(
243 R.string.weather_preferences_units_key);
244 final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
245 final String celsius = this.getResources().getString(
246 R.string.weather_preferences_units_celsius);
247 if (unitsPreferenceValue.equals(celsius)) {
248 this.mIsFahrenheit = false;
250 this.mIsFahrenheit = true;
254 // 2. Update weather data on display.
255 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
256 .getForecastWeatherData();
257 if (forecastWeatherData != null) {
258 this.updateForecastWeatherData(forecastWeatherData, this.mChosenDay);