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