de396fd7dca59632a9be52931a47bd592d3e996a
[JavaForFun] /
1 package de.example.exampletdd.fragment.current;
2
3 import java.io.IOException;
4 import java.text.DecimalFormat;
5 import java.text.NumberFormat;
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
8 import java.util.Locale;
9
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;
28
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;
34
35     @Override
36     public void onCreate(final Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38
39         this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this.getActivity());
40
41         this.mReceiver = new BroadcastReceiver() {
42
43             @Override
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);
55                     }
56
57                 }
58             }
59         };
60
61         final IntentFilter filter = new IntentFilter();
62         filter.addAction("de.example.exampletdd.UPDATECURRENTWEATHER");
63         this.getActivity().registerReceiver(this.mReceiver, filter);
64     }
65
66     @Override
67     public void onActivityCreated(final Bundle savedInstanceState) {
68         super.onActivityCreated(savedInstanceState);
69
70         final ListView listWeatherView = this.getListView();
71         listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
72
73         if (savedInstanceState != null) {
74             // Restore state
75             final CurrentWeatherData currentWeatherData = (CurrentWeatherData) savedInstanceState
76                     .getSerializable("CurrentWeatherData");
77
78             if (currentWeatherData != null) {
79                 try {
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");
85                 }
86             }
87         }
88
89         this.setHasOptionsMenu(false);
90
91         this.setEmptyText("No data available");
92
93         this.setListShownNoAnimation(false);
94     }
95
96     @Override
97     public void onResume() {
98         super.onResume();
99
100         final SharedPreferences sharedPreferences = PreferenceManager
101                 .getDefaultSharedPreferences(this.getActivity());
102
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;
111         } else {
112             this.mIsFahrenheit = true;
113         }
114
115         // 2. Try to restore old information
116         final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
117                 .getCurrentWeatherData();
118         if (currentWeatherData != null) {
119             this.updateCurrentWeatherData(currentWeatherData);
120         }
121     }
122
123     @Override
124     public void onSaveInstanceState(final Bundle savedInstanceState) {
125
126         // Save state
127         final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
128                 .getCurrentWeatherData();
129
130         if (currentWeatherData != null) {
131             savedInstanceState.putSerializable("CurrentWeatherData", currentWeatherData);
132         }
133
134         super.onSaveInstanceState(savedInstanceState);
135     }
136
137     @Override
138     public void onDestroy() {
139         super.onDestroy();
140         this.getActivity().unregisterReceiver(this.mReceiver);
141     }
142
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);
147
148         final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
149         final String symbol = this.mIsFahrenheit ? "ºF" : "ºC";
150
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(),
156                 layouts);
157
158
159         String tempMax = "";
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;
164         }
165         String tempMin = "";
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;
170         }
171         Bitmap picture;
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());
178         } else {
179             picture = BitmapFactory.decodeResource(this.getResources(),
180                     R.drawable.weather_severe_alert);
181         }
182         final WeatherCurrentDataEntryFirst entryFirst = new WeatherCurrentDataEntryFirst(tempMax,
183                 tempMin, picture);
184         adapter.add(entryFirst);
185
186         String description = "no description available";
187         if (currentWeatherData.getWeather().size() > 0) {
188             description = currentWeatherData.getWeather().get(0).getDescription();
189         }
190         final WeatherCurrentDataEntrySecond entrySecond = new WeatherCurrentDataEntrySecond(
191                 description);
192         adapter.add(entrySecond);
193
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);
199         }
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);
205         }
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);
211         }
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);
217         }
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);
223         }
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);
229         }
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);
235         }
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);
241         }
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);
247         }
248         final WeatherCurrentDataEntryFifth entryFifth = new WeatherCurrentDataEntryFifth(
249                 sunRiseTime, sunSetTime, humidityValue, pressureValue, windValue, rainValue,
250                 feelsLike, symbol, snowValue, cloudsValue);
251         adapter.add(entryFifth);
252
253
254         this.setListAdapter(adapter);
255     }
256 }