1 package de.example.exampletdd.fragment.specific;
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;
10 import java.util.List;
11 import java.util.Locale;
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;
31 public class WeatherInformationSpecificDataFragment extends Fragment implements GetWeather {
32 private boolean mIsFahrenheit;
33 private String mLanguage;
34 private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
37 public void onCreate(final Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
40 this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(
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(
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,
61 public void onActivityCreated(final Bundle savedInstanceState) {
62 super.onActivityCreated(savedInstanceState);
64 final ListView listWeatherView = (ListView) this.getActivity().findViewById(
65 R.id.weather_data_list_view);
67 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
68 R.layout.weather_data_entry_list);
70 final Collection<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
72 adapter.addAll(entries);
73 listWeatherView.setAdapter(adapter);
75 if (savedInstanceState != null) {
77 final WeatherData weatherData = (WeatherData) savedInstanceState
78 .getSerializable("weatherData");
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");
90 public void onSaveInstanceState(final Bundle savedInstanceState) {
93 final WeatherData weatherData = this.mWeatherServicePersistenceFile.getWeatherData();
96 if (weatherData != null) {
97 savedInstanceState.putSerializable("weatherData", weatherData);
100 super.onSaveInstanceState(savedInstanceState);
104 public void getWeather() {
105 final WeatherData weatherData = this.mWeatherServicePersistenceFile.getWeatherData();
107 if (weatherData != null) {
108 this.updateWeatherData(weatherData);
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());
117 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
119 final List<WeatherSpecificDataEntry> entries = this.createEmptyEntriesList();
121 final ListView listWeatherView = (ListView) this.getActivity().findViewById(
122 R.id.weather_data_list_view);
124 final WeatherSpecificDataAdapter adapter = new WeatherSpecificDataAdapter(this.getActivity(),
125 R.layout.weather_data_entry_list);
127 if (weatherData.getWeather() != null) {
128 entries.set(0, new WeatherSpecificDataEntry(this.getString(R.string.text_field_description), weatherData.getWeather()
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)));
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));
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));
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)));
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);
169 listWeatherView.setAdapter(null);
170 adapter.addAll(entries);
171 listWeatherView.setAdapter(adapter);
175 public void onResume() {
178 final SharedPreferences sharedPreferences = PreferenceManager
179 .getDefaultSharedPreferences(this.getActivity());
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;
190 this.mIsFahrenheit = true;
194 // 2. Update current data on display.
195 final WeatherData weatherData = this.mWeatherServicePersistenceFile.getWeatherData();
197 if (weatherData != null) {
198 this.updateWeatherData(weatherData);
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(
208 if (!languagePreferenceValue.equals(this.mLanguage)) {
209 this.mLanguage = languagePreferenceValue;
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));