1 package de.example.exampletdd.fragment.current;
3 import java.io.IOException;
4 import java.text.DecimalFormat;
5 import java.text.NumberFormat;
6 import java.text.SimpleDateFormat;
8 import java.util.Locale;
10 import android.content.BroadcastReceiver;
11 import android.content.Context;
12 import android.content.Intent;
13 import android.content.IntentFilter;
14 import android.content.SharedPreferences;
15 import android.graphics.Bitmap;
16 import android.graphics.BitmapFactory;
17 import android.os.Bundle;
18 import android.preference.PreferenceManager;
19 import android.support.v4.app.DialogFragment;
20 import android.support.v4.app.ListFragment;
21 import android.util.Log;
22 import android.widget.ListView;
23 import de.example.exampletdd.R;
24 import de.example.exampletdd.fragment.ErrorDialogFragment;
25 import de.example.exampletdd.fragment.overview.IconsList;
26 import de.example.exampletdd.model.currentweather.CurrentWeatherData;
27 import de.example.exampletdd.service.WeatherServicePersistenceFile;
29 public class WeatherInformationCurrentDataFragment extends ListFragment {
30 private static final String TAG = "WeatherInformationCurrentDataFragment";
31 private boolean mIsFahrenheit;
32 private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
33 private BroadcastReceiver mReceiver;
36 public void onCreate(final Bundle savedInstanceState) {
37 super.onCreate(savedInstanceState);
39 this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this.getActivity());
41 this.mReceiver = new BroadcastReceiver() {
44 public void onReceive(final Context context, final Intent intent) {
45 // This method will be run in the main thread.
46 final String action = intent.getAction();
47 if (action.equals("de.example.exampletdd.UPDATECURRENTWEATHER")) {
48 Log.i(TAG, "WeatherInformationCurrentDataFragment Update Weather Data");
49 final CurrentWeatherData currentWeatherData =
50 WeatherInformationCurrentDataFragment.this.mWeatherServicePersistenceFile
51 .getCurrentWeatherData();
52 if (currentWeatherData != null) {
53 WeatherInformationCurrentDataFragment.this
54 .updateCurrentWeatherData(currentWeatherData);
61 final IntentFilter filter = new IntentFilter();
62 filter.addAction("de.example.exampletdd.UPDATECURRENTWEATHER");
63 this.getActivity().registerReceiver(this.mReceiver, filter);
67 public void onActivityCreated(final Bundle savedInstanceState) {
68 super.onActivityCreated(savedInstanceState);
70 final ListView listWeatherView = this.getListView();
71 listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
73 if (savedInstanceState != null) {
75 final CurrentWeatherData currentWeatherData = (CurrentWeatherData) savedInstanceState
76 .getSerializable("CurrentWeatherData");
78 if (currentWeatherData != null) {
80 this.mWeatherServicePersistenceFile.storeCurrentWeatherData(currentWeatherData);
81 } catch (final IOException e) {
82 final DialogFragment newFragment = ErrorDialogFragment
83 .newInstance(R.string.error_dialog_generic_error);
84 newFragment.show(this.getFragmentManager(), "errorDialog");
89 this.setHasOptionsMenu(false);
91 this.setEmptyText("No data available");
93 this.setListShownNoAnimation(false);
97 public void onResume() {
100 final SharedPreferences sharedPreferences = PreferenceManager
101 .getDefaultSharedPreferences(this.getActivity());
103 // 1. Update units of measurement.
104 final String keyPreference = this.getResources().getString(
105 R.string.weather_preferences_units_key);
106 final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
107 final String celsius = this.getResources().getString(
108 R.string.weather_preferences_units_celsius);
109 if (unitsPreferenceValue.equals(celsius)) {
110 this.mIsFahrenheit = false;
112 this.mIsFahrenheit = true;
115 // 2. Try to restore old information
116 final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
117 .getCurrentWeatherData();
118 if (currentWeatherData != null) {
119 this.updateCurrentWeatherData(currentWeatherData);
124 public void onSaveInstanceState(final Bundle savedInstanceState) {
127 final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
128 .getCurrentWeatherData();
130 if (currentWeatherData != null) {
131 savedInstanceState.putSerializable("CurrentWeatherData", currentWeatherData);
134 super.onSaveInstanceState(savedInstanceState);
138 public void onDestroy() {
140 this.getActivity().unregisterReceiver(this.mReceiver);
143 public void updateCurrentWeatherData(final CurrentWeatherData currentWeatherData) {
144 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
145 tempFormatter.applyPattern("#####.#####");
146 final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z", Locale.US);
148 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
149 final String symbol = this.mIsFahrenheit ? "ºF" : "ºC";
151 final int[] layouts = new int[3];
152 layouts[0] = R.layout.weather_current_data_entry_first;
153 layouts[1] = R.layout.weather_current_data_entry_second;
154 layouts[2] = R.layout.weather_current_data_entry_fifth;
155 final WeatherCurrentDataAdapter adapter = new WeatherCurrentDataAdapter(this.getActivity(),
160 if (currentWeatherData.getMain().getTemp_max() != null) {
161 double conversion = (Double) currentWeatherData.getMain().getTemp_max();
162 conversion = conversion - tempUnits;
163 tempMax = tempFormatter.format(conversion) + symbol;
166 if (currentWeatherData.getMain().getTemp_min() != null) {
167 double conversion = (Double) currentWeatherData.getMain().getTemp_min();
168 conversion = conversion - tempUnits;
169 tempMin = tempFormatter.format(conversion) + symbol;
172 if ((currentWeatherData.getWeather().size() > 0)
173 && (currentWeatherData.getWeather().get(0).getIcon() != null)
174 && (IconsList.getIcon(currentWeatherData.getWeather().get(0).getIcon()) != null)) {
175 final String icon = currentWeatherData.getWeather().get(0).getIcon();
176 picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
177 .getResourceDrawable());
179 picture = BitmapFactory.decodeResource(this.getResources(),
180 R.drawable.weather_severe_alert);
182 final WeatherCurrentDataEntryFirst entryFirst = new WeatherCurrentDataEntryFirst(tempMax,
184 adapter.add(entryFirst);
186 String description = "no description available";
187 if (currentWeatherData.getWeather().size() > 0) {
188 description = currentWeatherData.getWeather().get(0).getDescription();
190 final WeatherCurrentDataEntrySecond entrySecond = new WeatherCurrentDataEntrySecond(
192 adapter.add(entrySecond);
194 String humidityValue = "";
195 if ((currentWeatherData.getMain() != null)
196 && (currentWeatherData.getMain().getHumidity() != null)) {
197 final double conversion = (Double) currentWeatherData.getMain().getHumidity();
198 humidityValue = tempFormatter.format(conversion);
200 String pressureValue = "";
201 if ((currentWeatherData.getMain() != null)
202 && (currentWeatherData.getMain().getPressure() != null)) {
203 final double conversion = (Double) currentWeatherData.getMain().getPressure();
204 pressureValue = tempFormatter.format(conversion);
206 String windValue = "";
207 if ((currentWeatherData.getWind() != null)
208 && (currentWeatherData.getWind().getSpeed() != null)) {
209 final double conversion = (Double) currentWeatherData.getWind().getSpeed();
210 windValue = tempFormatter.format(conversion);
212 String rainValue = "";
213 if ((currentWeatherData.getRain() != null)
214 && (currentWeatherData.getRain().get3h() != null)) {
215 final double conversion = (Double) currentWeatherData.getRain().get3h();
216 rainValue = tempFormatter.format(conversion);
218 String cloudsValue = "";
219 if ((currentWeatherData.getClouds() != null)
220 && (currentWeatherData.getClouds().getAll() != null)) {
221 final double conversion = (Double) currentWeatherData.getClouds().getAll();
222 cloudsValue = tempFormatter.format(conversion);
224 String snowValue = "";
225 if ((currentWeatherData.getSnow() != null)
226 && (currentWeatherData.getSnow().get3h() != null)) {
227 final double conversion = (Double) currentWeatherData.getSnow().get3h();
228 snowValue = tempFormatter.format(conversion);
230 String feelsLike = "";
231 if (currentWeatherData.getMain().getTemp() != null) {
232 double conversion = (Double) currentWeatherData.getMain().getTemp();
233 conversion = conversion - tempUnits;
234 feelsLike = tempFormatter.format(conversion);
236 String sunRiseTime = "";
237 if (currentWeatherData.getSys().getSunrise() != null) {
238 final long unixTime = (Long) currentWeatherData.getSys().getSunrise();
239 final Date unixDate = new Date(unixTime * 1000L);
240 sunRiseTime = dateFormat.format(unixDate);
242 String sunSetTime = "";
243 if (currentWeatherData.getSys().getSunset() != null) {
244 final long unixTime = (Long) currentWeatherData.getSys().getSunset();
245 final Date unixDate = new Date(unixTime * 1000L);
246 sunSetTime = dateFormat.format(unixDate);
248 final WeatherCurrentDataEntryFifth entryFifth = new WeatherCurrentDataEntryFifth(
249 sunRiseTime, sunSetTime, humidityValue, pressureValue, windValue, rainValue,
250 feelsLike, symbol, snowValue, cloudsValue);
251 adapter.add(entryFifth);
254 this.setListAdapter(adapter);