1 package de.example.exampletdd.fragment.overview;
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.Calendar;
10 import java.util.List;
11 import java.util.Locale;
13 import android.content.BroadcastReceiver;
14 import android.content.ComponentName;
15 import android.content.Context;
16 import android.content.Intent;
17 import android.content.IntentFilter;
18 import android.content.SharedPreferences;
19 import android.graphics.Bitmap;
20 import android.graphics.BitmapFactory;
21 import android.os.Bundle;
22 import android.os.Parcelable;
23 import android.preference.PreferenceManager;
24 import android.support.v4.app.DialogFragment;
25 import android.support.v4.app.ListFragment;
26 import android.util.Log;
27 import android.view.View;
28 import android.widget.ListView;
29 import de.example.exampletdd.R;
30 import de.example.exampletdd.fragment.ErrorDialogFragment;
31 import de.example.exampletdd.fragment.specific.WeatherInformationSpecificDataFragment;
32 import de.example.exampletdd.model.forecastweather.ForecastWeatherData;
33 import de.example.exampletdd.service.WeatherServicePersistenceFile;
35 public class WeatherInformationOverviewFragment extends ListFragment {
36 private static final String TAG = "WeatherInformationOverviewFragment";
37 private boolean mIsFahrenheit;
38 private String mDayForecast;
39 private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
40 private Parcelable mListState;
41 private BroadcastReceiver mReceiver;
44 public void onCreate(final Bundle savedInstanceState) {
45 super.onCreate(savedInstanceState);
47 final SharedPreferences sharedPreferences = PreferenceManager
48 .getDefaultSharedPreferences(this.getActivity());
49 final String keyPreference = this.getResources().getString(
50 R.string.weather_preferences_day_forecast_key);
51 this.mDayForecast = sharedPreferences.getString(keyPreference, "");
53 this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this.getActivity());
55 this.mReceiver = new BroadcastReceiver() {
58 public void onReceive(final Context context, final Intent intent) {
59 // This method will be run in the main thread.
60 final String action = intent.getAction();
61 if (action.equals("de.example.exampletdd.UPDATEOVERVIEWWEATHER")) {
62 Log.i(TAG, "WeatherInformationOverviewFragment Update Weather Data");
63 final ForecastWeatherData forecastWeatherData =
64 WeatherInformationOverviewFragment.this.mWeatherServicePersistenceFile
65 .getForecastWeatherData();
66 if (forecastWeatherData != null) {
67 WeatherInformationOverviewFragment.this
68 .updateForecastWeatherData(forecastWeatherData);
75 final IntentFilter filter = new IntentFilter();
76 filter.addAction("de.example.exampletdd.UPDATEOVERVIEWWEATHER");
77 this.getActivity().registerReceiver(this.mReceiver, filter);
81 public void onActivityCreated(final Bundle savedInstanceState) {
82 super.onActivityCreated(savedInstanceState);
84 final ListView listWeatherView = this.getListView();
85 listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
87 if (savedInstanceState != null) {
89 final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
90 .getSerializable("ForecastWeatherData");
92 if (forecastWeatherData != null) {
94 this.mWeatherServicePersistenceFile
95 .storeForecastWeatherData(forecastWeatherData);
96 } catch (final IOException e) {
97 final DialogFragment newFragment = ErrorDialogFragment
98 .newInstance(R.string.error_dialog_generic_error);
99 newFragment.show(this.getFragmentManager(), "errorDialog");
103 this.mListState = savedInstanceState.getParcelable("ListState");
106 this.setHasOptionsMenu(false);
108 final WeatherOverviewAdapter adapter = new WeatherOverviewAdapter(
109 this.getActivity(), R.layout.weather_main_entry_list);
112 this.setEmptyText("Press download to receive weather information");
114 this.setListAdapter(adapter);
115 this.setListShown(true);
116 this.setListShownNoAnimation(true);
120 public void onListItemClick(final ListView l, final View v, final int position, final long id) {
121 final WeatherInformationSpecificDataFragment fragment = (WeatherInformationSpecificDataFragment) this
122 .getFragmentManager().findFragmentById(R.id.weather_specific_data__fragment);
123 if (fragment == null) {
125 final Intent intent = new Intent("de.example.exampletdd.WEATHERINFO")
126 .setComponent(new ComponentName("de.example.exampletdd",
127 "de.example.exampletdd.WeatherInformationSpecificDataActivity"));
128 intent.putExtra("CHOSEN_DAY", (int) id);
129 WeatherInformationOverviewFragment.this.getActivity().startActivity(intent);
132 fragment.getWeatherByDay((int) id);
137 public void onSaveInstanceState(final Bundle savedInstanceState) {
140 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
141 .getForecastWeatherData();
143 if (forecastWeatherData != null) {
144 savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
147 this.mListState = this.getListView().onSaveInstanceState();
148 savedInstanceState.putParcelable("ListState", this.mListState);
150 super.onSaveInstanceState(savedInstanceState);
154 public void onDestroy() {
156 this.getActivity().unregisterReceiver(this.mReceiver);
159 private void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData) {
160 final List<WeatherOverviewEntry> entries = new ArrayList<WeatherOverviewEntry>();
161 final WeatherOverviewAdapter adapter = new WeatherOverviewAdapter(this.getActivity(),
162 R.layout.weather_main_entry_list);
165 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
166 tempFormatter.applyPattern("#####.##");
167 final SimpleDateFormat dayNameFormatter = new SimpleDateFormat("EEE", Locale.US);
168 final SimpleDateFormat monthAndDayNumberormatter = new SimpleDateFormat("MMM d", Locale.US);
169 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
170 final String symbol = this.mIsFahrenheit ? "ºF" : "ºC";
173 final Calendar calendar = Calendar.getInstance();
174 for (final de.example.exampletdd.model.forecastweather.List forecast : forecastWeatherData
179 if ((forecast.getWeather().size() > 0) &&
180 (forecast.getWeather().get(0).getIcon() != null) &&
181 (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) {
182 final String icon = forecast.getWeather().get(0).getIcon();
183 picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
184 .getResourceDrawable());
186 picture = BitmapFactory.decodeResource(this.getResources(),
187 R.drawable.weather_severe_alert);
190 final Long forecastUNIXDate = (Long) forecast.getDt();
191 calendar.setTimeInMillis(forecastUNIXDate * 1000L);
192 final Date dayTime = calendar.getTime();
193 final String dayTextName = dayNameFormatter.format(dayTime);
194 final String monthAndDayNumberText = monthAndDayNumberormatter.format(dayTime);
196 Double maxTemp = null;
197 if (forecast.getTemp().getMax() != null) {
198 maxTemp = (Double) forecast.getTemp().getMax();
199 maxTemp = maxTemp - tempUnits;
202 Double minTemp = null;
203 if (forecast.getTemp().getMin() != null) {
204 minTemp = (Double) forecast.getTemp().getMin();
205 minTemp = minTemp - tempUnits;
208 if ((maxTemp != null) && (minTemp != null)) {
209 entries.add(new WeatherOverviewEntry(dayTextName, monthAndDayNumberText,
210 tempFormatter.format(maxTemp) + symbol, tempFormatter.format(minTemp) + symbol,
215 this.setListAdapter(null);
216 adapter.addAll(entries);
217 this.setListAdapter(adapter);
221 public void onResume() {
224 final SharedPreferences sharedPreferences = PreferenceManager
225 .getDefaultSharedPreferences(this.getActivity());
227 // 1. Update units of measurement.
228 String keyPreference = this.getResources().getString(
229 R.string.weather_preferences_units_key);
230 final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
231 final String celsius = this.getResources().getString(
232 R.string.weather_preferences_units_celsius);
233 if (unitsPreferenceValue.equals(celsius)) {
234 this.mIsFahrenheit = false;
236 this.mIsFahrenheit = true;
239 // 2. Update number day forecast.
240 keyPreference = this.getResources().getString(
241 R.string.weather_preferences_day_forecast_key);
242 this.mDayForecast = sharedPreferences.getString(keyPreference, "");
245 // 3. Update forecast weather data on display.
246 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
247 .getForecastWeatherData();
248 if ((this.mListState != null) && (forecastWeatherData != null)) {
249 this.updateForecastWeatherData(forecastWeatherData);
250 this.getListView().onRestoreInstanceState(this.mListState);
251 } else if (forecastWeatherData != null) {
252 this.updateForecastWeatherData(forecastWeatherData);