7ffd441f29a3e976e224299100a2d51500711e5a
[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.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;
29
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;
35
36     @Override
37     public void onCreate(final Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39
40         this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this.getActivity());
41
42         this.mReceiver = new BroadcastReceiver() {
43
44             @Override
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);
56                     }
57
58                 }
59             }
60         };
61     }
62
63     @Override
64     public void onActivityCreated(final Bundle savedInstanceState) {
65         super.onActivityCreated(savedInstanceState);
66
67         final ListView listWeatherView = this.getListView();
68         listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
69
70         if (savedInstanceState != null) {
71             // Restore state
72             final CurrentWeatherData currentWeatherData = (CurrentWeatherData) savedInstanceState
73                     .getSerializable("CurrentWeatherData");
74
75             if (currentWeatherData != null) {
76                 try {
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");
82                 }
83             }
84         }
85
86         this.setHasOptionsMenu(false);
87         this.setEmptyText("No data available");
88         this.setListShown(true);
89         this.setListShownNoAnimation(true);
90     }
91
92     @Override
93     public void onResume() {
94         super.onResume();
95
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,
100                 filter);
101
102         final SharedPreferences sharedPreferences = PreferenceManager
103                 .getDefaultSharedPreferences(this.getActivity());
104
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;
113         } else {
114             this.mIsFahrenheit = true;
115         }
116
117         // 3. Try to restore old information
118         final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
119                 .getCurrentWeatherData();
120         if (currentWeatherData != null) {
121             this.updateCurrentWeatherData(currentWeatherData);
122         }
123     }
124
125     @Override
126     public void onPause() {
127         super.onPause();
128         LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(this.mReceiver);
129     }
130
131     @Override
132     public void onSaveInstanceState(final Bundle savedInstanceState) {
133
134         // Save state
135         final CurrentWeatherData currentWeatherData = this.mWeatherServicePersistenceFile
136                 .getCurrentWeatherData();
137
138         if (currentWeatherData != null) {
139             savedInstanceState.putSerializable("CurrentWeatherData", currentWeatherData);
140         }
141
142         super.onSaveInstanceState(savedInstanceState);
143     }
144
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);
149
150         final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
151         final String symbol = this.mIsFahrenheit ? "ºF" : "ºC";
152
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(),
158                 layouts);
159
160
161         String tempMax = "";
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;
166         }
167         String tempMin = "";
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;
172         }
173         Bitmap picture;
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());
180         } else {
181             picture = BitmapFactory.decodeResource(this.getResources(),
182                     R.drawable.weather_severe_alert);
183         }
184         final WeatherCurrentDataEntryFirst entryFirst = new WeatherCurrentDataEntryFirst(tempMax,
185                 tempMin, picture);
186         adapter.add(entryFirst);
187
188         String description = "no description available";
189         if (currentWeatherData.getWeather().size() > 0) {
190             description = currentWeatherData.getWeather().get(0).getDescription();
191         }
192         final WeatherCurrentDataEntrySecond entrySecond = new WeatherCurrentDataEntrySecond(
193                 description);
194         adapter.add(entrySecond);
195
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);
201         }
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);
207         }
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);
213         }
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);
219         }
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);
225         }
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);
231         }
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);
237         }
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);
243         }
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);
249         }
250         final WeatherCurrentDataEntryFifth entryFifth = new WeatherCurrentDataEntryFifth(
251                 sunRiseTime, sunSetTime, humidityValue, pressureValue, windValue, rainValue,
252                 feelsLike, symbol, snowValue, cloudsValue);
253         adapter.add(entryFifth);
254
255
256         this.setListAdapter(adapter);
257     }
258 }