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.support.v4.content.LocalBroadcastManager;
22 import android.util.Log;
23 import android.widget.ListView;
24 import de.example.exampletdd.R;
25 import de.example.exampletdd.fragment.ErrorDialogFragment;
26 import de.example.exampletdd.fragment.overview.IconsList;
27 import de.example.exampletdd.model.currentweather.CurrentWeatherData;
28 import de.example.exampletdd.service.WeatherServicePersistenceFile;
30 public class WeatherInformationCurrentDataFragment extends ListFragment {
31 private static final String TAG = "WeatherInformationCurrentDataFragment";
32 private boolean mIsFahrenheit;
33 private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
34 private BroadcastReceiver mReceiver;
37 public void onCreate(final Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
40 this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this.getActivity());
42 this.mReceiver = new BroadcastReceiver() {
45 public void onReceive(final Context context, final Intent intent) {
46 // This method will be run in the main thread.
47 final String action = intent.getAction();
48 if (action.equals("de.example.exampletdd.UPDATECURRENTWEATHER")) {
49 Log.i(TAG, "WeatherInformationCurrentDataFragment Update Weather Data");
50 final CurrentWeatherData currentWeatherData =
51 WeatherInformationCurrentDataFragment.this.mWeatherServicePersistenceFile
52 .getCurrentWeatherData();
53 if (currentWeatherData != null) {
54 WeatherInformationCurrentDataFragment.this
55 .updateCurrentWeatherData(currentWeatherData);
64 public void onActivityCreated(final Bundle savedInstanceState) {
65 super.onActivityCreated(savedInstanceState);
67 final ListView listWeatherView = this.getListView();
68 listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
70 if (savedInstanceState != null) {
72 final CurrentWeatherData currentWeatherData = (CurrentWeatherData) savedInstanceState
73 .getSerializable("CurrentWeatherData");
75 if (currentWeatherData != null) {
77 this.mWeatherServicePersistenceFile.storeCurrentWeatherData(currentWeatherData);
78 } catch (final IOException e) {
79 final DialogFragment newFragment = ErrorDialogFragment
80 .newInstance(R.string.error_dialog_generic_error);
81 newFragment.show(this.getFragmentManager(), "errorDialog");
86 this.setHasOptionsMenu(false);
87 this.setEmptyText("No data available");
88 this.setListShown(true);
89 this.setListShownNoAnimation(true);
93 public void onResume() {
96 // 1. Register receiver
97 final IntentFilter filter = new IntentFilter();
98 filter.addAction("de.example.exampletdd.UPDATECURRENTWEATHER");
99 LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(this.mReceiver,
102 final SharedPreferences sharedPreferences = PreferenceManager
103 .getDefaultSharedPreferences(this.getActivity());
105 // 2. Update units of measurement.
106 final String keyPreference = this.getResources().getString(
107 R.string.weather_preferences_units_key);
108 final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
109 final String celsius = this.getResources().getString(
110 R.string.weather_preferences_units_celsius);
111 if (unitsPreferenceValue.equals(celsius)) {
112 this.mIsFahrenheit = false;
114 this.mIsFahrenheit = true;
117 // 3. Try to restore old information
118 final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
119 .getCurrentWeatherData();
120 if (currentWeatherData != null) {
121 this.updateCurrentWeatherData(currentWeatherData);
126 public void onPause() {
128 LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(this.mReceiver);
132 public void onSaveInstanceState(final Bundle savedInstanceState) {
135 final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
136 .getCurrentWeatherData();
138 if (currentWeatherData != null) {
139 savedInstanceState.putSerializable("CurrentWeatherData", currentWeatherData);
142 super.onSaveInstanceState(savedInstanceState);
145 public void updateCurrentWeatherData(final CurrentWeatherData currentWeatherData) {
146 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
147 tempFormatter.applyPattern("#####.#####");
148 final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.US);
150 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
151 final String symbol = this.mIsFahrenheit ? "ºF" : "ºC";
153 final int[] layouts = new int[3];
154 layouts[0] = R.layout.weather_current_data_entry_first;
155 layouts[1] = R.layout.weather_current_data_entry_second;
156 layouts[2] = R.layout.weather_current_data_entry_fifth;
157 final WeatherCurrentDataAdapter adapter = new WeatherCurrentDataAdapter(this.getActivity(),
162 if (currentWeatherData.getMain().getTemp_max() != null) {
163 double conversion = (Double) currentWeatherData.getMain().getTemp_max();
164 conversion = conversion - tempUnits;
165 tempMax = tempFormatter.format(conversion) + symbol;
168 if (currentWeatherData.getMain().getTemp_min() != null) {
169 double conversion = (Double) currentWeatherData.getMain().getTemp_min();
170 conversion = conversion - tempUnits;
171 tempMin = tempFormatter.format(conversion) + symbol;
174 if ((currentWeatherData.getWeather().size() > 0)
175 && (currentWeatherData.getWeather().get(0).getIcon() != null)
176 && (IconsList.getIcon(currentWeatherData.getWeather().get(0).getIcon()) != null)) {
177 final String icon = currentWeatherData.getWeather().get(0).getIcon();
178 picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
179 .getResourceDrawable());
181 picture = BitmapFactory.decodeResource(this.getResources(),
182 R.drawable.weather_severe_alert);
184 final WeatherCurrentDataEntryFirst entryFirst = new WeatherCurrentDataEntryFirst(tempMax,
186 adapter.add(entryFirst);
188 String description = "no description available";
189 if (currentWeatherData.getWeather().size() > 0) {
190 description = currentWeatherData.getWeather().get(0).getDescription();
192 final WeatherCurrentDataEntrySecond entrySecond = new WeatherCurrentDataEntrySecond(
194 adapter.add(entrySecond);
196 String humidityValue = "";
197 if ((currentWeatherData.getMain() != null)
198 && (currentWeatherData.getMain().getHumidity() != null)) {
199 final double conversion = (Double) currentWeatherData.getMain().getHumidity();
200 humidityValue = tempFormatter.format(conversion);
202 String pressureValue = "";
203 if ((currentWeatherData.getMain() != null)
204 && (currentWeatherData.getMain().getPressure() != null)) {
205 final double conversion = (Double) currentWeatherData.getMain().getPressure();
206 pressureValue = tempFormatter.format(conversion);
208 String windValue = "";
209 if ((currentWeatherData.getWind() != null)
210 && (currentWeatherData.getWind().getSpeed() != null)) {
211 final double conversion = (Double) currentWeatherData.getWind().getSpeed();
212 windValue = tempFormatter.format(conversion);
214 String rainValue = "";
215 if ((currentWeatherData.getRain() != null)
216 && (currentWeatherData.getRain().get3h() != null)) {
217 final double conversion = (Double) currentWeatherData.getRain().get3h();
218 rainValue = tempFormatter.format(conversion);
220 String cloudsValue = "";
221 if ((currentWeatherData.getClouds() != null)
222 && (currentWeatherData.getClouds().getAll() != null)) {
223 final double conversion = (Double) currentWeatherData.getClouds().getAll();
224 cloudsValue = tempFormatter.format(conversion);
226 String snowValue = "";
227 if ((currentWeatherData.getSnow() != null)
228 && (currentWeatherData.getSnow().get3h() != null)) {
229 final double conversion = (Double) currentWeatherData.getSnow().get3h();
230 snowValue = tempFormatter.format(conversion);
232 String feelsLike = "";
233 if (currentWeatherData.getMain().getTemp() != null) {
234 double conversion = (Double) currentWeatherData.getMain().getTemp();
235 conversion = conversion - tempUnits;
236 feelsLike = tempFormatter.format(conversion);
238 String sunRiseTime = "";
239 if (currentWeatherData.getSys().getSunrise() != null) {
240 final long unixTime = (Long) currentWeatherData.getSys().getSunrise();
241 final Date unixDate = new Date(unixTime * 1000L);
242 sunRiseTime = dateFormat.format(unixDate);
244 String sunSetTime = "";
245 if (currentWeatherData.getSys().getSunset() != null) {
246 final long unixTime = (Long) currentWeatherData.getSys().getSunset();
247 final Date unixDate = new Date(unixTime * 1000L);
248 sunSetTime = dateFormat.format(unixDate);
250 final WeatherCurrentDataEntryFifth entryFifth = new WeatherCurrentDataEntryFifth(
251 sunRiseTime, sunSetTime, humidityValue, pressureValue, windValue, rainValue,
252 feelsLike, symbol, snowValue, cloudsValue);
253 adapter.add(entryFifth);
256 this.setListAdapter(adapter);