fce0356fedeb9bb3a5960b9f202102b8a283f889
[JavaForFun] /
1 package de.example.exampletdd.fragment.specific;
2
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.io.StreamCorruptedException;
6 import java.text.DecimalFormat;
7 import java.text.NumberFormat;
8 import java.text.SimpleDateFormat;
9 import java.util.ArrayList;
10 import java.util.Collection;
11 import java.util.Date;
12 import java.util.List;
13 import java.util.Locale;
14
15 import android.app.DialogFragment;
16 import android.app.Fragment;
17 import android.content.SharedPreferences;
18 import android.graphics.Bitmap;
19 import android.graphics.BitmapFactory;
20 import android.os.Bundle;
21 import android.preference.PreferenceManager;
22 import android.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.ImageView;
27 import android.widget.ListView;
28 import de.example.exampletdd.R;
29 import de.example.exampletdd.activityinterface.GetWeather;
30 import de.example.exampletdd.fragment.ErrorDialogFragment;
31 import de.example.exampletdd.model.WeatherData;
32 import de.example.exampletdd.service.WeatherServicePersistenceFile;
33
34 public class WeatherInformationSpecificDataFragment extends Fragment implements GetWeather {
35     private static final String TAG = "WeatherInformationDataFragment";
36     private boolean mIsFahrenheit;
37     private String mLanguage;
38     private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
39
40     @Override
41     public void onCreate(final Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43
44         this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
45                 this.getActivity());
46
47         final SharedPreferences sharedPreferences = PreferenceManager
48                 .getDefaultSharedPreferences(this.getActivity());
49         final String keyPreference = this.getResources().getString(
50                 R.string.weather_preferences_language_key);
51         this.mLanguage = sharedPreferences.getString(
52                 keyPreference, "");
53     }
54
55     @Override
56     public View onCreateView(final LayoutInflater inflater,
57             final ViewGroup container, final Bundle savedInstanceState) {
58         final View rootView = inflater.inflate(R.layout.weather_data_list,
59                 container, false);
60
61         return rootView;
62     }
63
64     @Override
65     public void onActivityCreated(final Bundle savedInstanceState) {
66         super.onActivityCreated(savedInstanceState);
67
68         final ListView listWeatherView = (ListView) this.getActivity().findViewById(
69                 R.id.weather_data_list_view);
70
71         final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
72                 R.layout.weather_data_entry_list);
73
74         final Collection<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
75
76         adapter.addAll(entries);
77         listWeatherView.setAdapter(adapter);
78
79         if (savedInstanceState != null) {
80             // Restore state
81             final WeatherData weatherData = (WeatherData) savedInstanceState
82                     .getSerializable("weatherData");
83             try {
84                 this.mWeatherServicePersistenceFile.storeWeatherData(weatherData);
85             } catch (final IOException e) {
86                 final DialogFragment newFragment = ErrorDialogFragment
87                         .newInstance(R.string.error_dialog_generic_error);
88                 newFragment.show(this.getFragmentManager(), "errorDialog");
89             }
90         }
91     }
92
93     @Override
94     public void onSaveInstanceState(final Bundle savedInstanceState) {
95
96         // Save state
97         WeatherData weatherData = null;
98         try {
99             weatherData = this.mWeatherServicePersistenceFile.getWeatherData();
100         } catch (final StreamCorruptedException e) {
101             Log.e(TAG, "onResume exception: ", e);
102         } catch (final FileNotFoundException e) {
103             Log.e(TAG, "onResume exception: ", e);
104         } catch (final IOException e) {
105             Log.e(TAG, "onResume exception: ", e);
106         } catch (final ClassNotFoundException e) {
107             Log.e(TAG, "onResume exception: ", e);
108         }
109
110         if (weatherData != null) {
111             savedInstanceState.putSerializable("weatherData", weatherData);
112         }
113
114         super.onSaveInstanceState(savedInstanceState);
115     }
116
117     @Override
118     public void getWeather() {
119         WeatherData weatherData = null;
120         try {
121             weatherData = this.mWeatherServicePersistenceFile.getWeatherData();
122         } catch (final StreamCorruptedException e) {
123             Log.e(TAG, "onResume exception: ", e);
124         } catch (final FileNotFoundException e) {
125             Log.e(TAG, "onResume exception: ", e);
126         } catch (final IOException e) {
127             Log.e(TAG, "onResume exception: ", e);
128         } catch (final ClassNotFoundException e) {
129             Log.e(TAG, "onResume exception: ", e);
130         }
131         if (weatherData != null) {
132             this.updateWeatherData(weatherData);
133         }
134     }
135
136     public void updateWeatherData(final WeatherData weatherData) {
137         final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.getDefault());
138         tempFormatter.applyPattern("#####.#####");
139         final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z", Locale.getDefault());
140
141         final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
142
143         final List<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
144
145         final ListView listWeatherView = (ListView) this.getActivity().findViewById(
146                 R.id.weather_data_list_view);
147
148         final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
149                 R.layout.weather_data_entry_list);
150
151         if (weatherData.getWeather() != null) {
152             entries.set(0, new WeatherSpecificDataEntry(this.getString(R.string.text_field_description), weatherData.getWeather()
153                     .getDescription()));
154             double conversion = weatherData.getMain().getTemp();
155             conversion = conversion - tempUnits;
156             entries.set(1, new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem), tempFormatter.format(conversion)));
157             conversion = weatherData.getMain().getMaxTemp();
158             conversion = conversion - tempUnits;
159             entries.set(2, new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_max), tempFormatter.format(conversion)));
160             conversion = weatherData.getMain().getMinTemp();
161             conversion = conversion - tempUnits;
162             entries.set(3, new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_min), tempFormatter.format(conversion)));
163         }
164
165         if (weatherData.getSystem() != null) {
166             long unixTime = weatherData.getSystem().getSunRiseTime();
167             Date unixDate = new Date(unixTime * 1000L);
168             String dateFormatUnix = dateFormat.format(unixDate);
169             entries.set(4, new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_rise), dateFormatUnix));
170
171             unixTime = weatherData.getSystem().getSunSetTime();
172             unixDate = new Date(unixTime * 1000L);
173             dateFormatUnix = dateFormat.format(unixDate);
174             entries.set(5, new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_set), dateFormatUnix));
175         }
176
177         if (weatherData.getClouds() != null) {
178             final double cloudiness = weatherData.getClouds().getCloudiness();
179             entries.set(6, new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness), tempFormatter.format(cloudiness)));
180         }
181
182         if (weatherData.getWeather().getIcon() != null) {
183             final Bitmap icon = BitmapFactory.decodeByteArray(
184                     weatherData.getIconData(), 0,
185                     weatherData.getIconData().length);
186             final ImageView imageIcon = (ImageView) this.getActivity()
187                     .findViewById(R.id.weather_picture);
188             imageIcon.setImageBitmap(icon);
189         }
190
191
192
193         listWeatherView.setAdapter(null);
194         adapter.addAll(entries);
195         listWeatherView.setAdapter(adapter);
196     }
197
198     @Override
199     public void onResume() {
200         super.onResume();
201
202         final SharedPreferences sharedPreferences = PreferenceManager
203                 .getDefaultSharedPreferences(this.getActivity());
204
205         // 1. Update units of measurement.
206         String keyPreference = this.getResources().getString(
207                 R.string.weather_preferences_units_key);
208         final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
209         final String celsius = this.getResources().getString(
210                 R.string.weather_preferences_units_celsius);
211         if (unitsPreferenceValue.equals(celsius)) {
212             this.mIsFahrenheit = false;
213         } else {
214             this.mIsFahrenheit = true;
215         }
216
217
218         // 2. Update current data on display.
219         WeatherData weatherData = null;
220         try {
221             weatherData = this.mWeatherServicePersistenceFile.getWeatherData();
222         } catch (final StreamCorruptedException e) {
223             Log.e(TAG, "onResume exception: ", e);
224         } catch (final FileNotFoundException e) {
225             Log.e(TAG, "onResume exception: ", e);
226         } catch (final IOException e) {
227             Log.e(TAG, "onResume exception: ", e);
228         } catch (final ClassNotFoundException e) {
229             Log.e(TAG, "onResume exception: ", e);
230         }
231         if (weatherData != null) {
232             this.updateWeatherData(weatherData);
233         }
234
235
236         // 3. If language changed, try to retrieve new data for new language
237         // (new strings with the chosen language)
238         keyPreference = this.getResources().getString(
239                 R.string.weather_preferences_language_key);
240         final String languagePreferenceValue = sharedPreferences.getString(
241                 keyPreference, "");
242         if (!languagePreferenceValue.equals(this.mLanguage)) {
243             this.mLanguage = languagePreferenceValue;
244             this.getWeather();
245         }
246     }
247
248     private List<WeatherSpecificDataEntry> createEmptyEntriesList() {
249         final List<WeatherSpecificDataEntry> entries = new ArrayList<WeatherSpecificDataEntry>();
250         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_description), null));
251         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem), null));
252         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_max), null));
253         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_tem_min), null));
254         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_rise), null));
255         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_sun_set), null));
256         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_cloudiness), null));
257         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_time), null));
258         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_rain_amount), null));
259         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_wind_speed), null));
260         entries.add(new WeatherSpecificDataEntry(this.getString(R.string.text_field_humidity), null));
261
262         return entries;
263     }
264 }