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.support.v4.content.LocalBroadcastManager;
27 import android.util.Log;
28 import android.view.View;
29 import android.widget.ListView;
30 import de.example.exampletdd.R;
31 import de.example.exampletdd.fragment.ErrorDialogFragment;
32 import de.example.exampletdd.fragment.specific.WeatherInformationSpecificDataFragment;
33 import de.example.exampletdd.model.forecastweather.ForecastWeatherData;
34 import de.example.exampletdd.service.WeatherServicePersistenceFile;
36 public class WeatherInformationOverviewFragment extends ListFragment {
37 private static final String TAG = "WeatherInformationOverviewFragment";
38 private boolean mIsFahrenheit;
39 private int mDayForecast;
40 private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
41 private Parcelable mListState;
42 private BroadcastReceiver mReceiver;
45 public void onCreate(final Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
48 final SharedPreferences sharedPreferences = PreferenceManager
49 .getDefaultSharedPreferences(this.getActivity());
50 final String keyPreference = this.getResources().getString(
51 R.string.weather_preferences_day_forecast_key);
52 final String dayForecast = sharedPreferences.getString(keyPreference, "");
53 this.mDayForecast = Integer.valueOf(dayForecast);
55 this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this.getActivity());
57 this.mReceiver = new BroadcastReceiver() {
60 public void onReceive(final Context context, final Intent intent) {
61 // This method will be run in the main thread.
62 final String action = intent.getAction();
63 if (action.equals("de.example.exampletdd.UPDATEOVERVIEWWEATHER")) {
64 Log.i(TAG, "WeatherInformationOverviewFragment Update Weather Data");
65 final ForecastWeatherData forecastWeatherData =
66 WeatherInformationOverviewFragment.this.mWeatherServicePersistenceFile
67 .getForecastWeatherData();
68 if (forecastWeatherData != null) {
69 WeatherInformationOverviewFragment.this
70 .updateForecastWeatherData(forecastWeatherData);
79 public void onActivityCreated(final Bundle savedInstanceState) {
80 super.onActivityCreated(savedInstanceState);
82 final ListView listWeatherView = this.getListView();
83 listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
85 if (savedInstanceState != null) {
87 final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
88 .getSerializable("ForecastWeatherData");
90 if (forecastWeatherData != null) {
92 this.mWeatherServicePersistenceFile
93 .storeForecastWeatherData(forecastWeatherData);
94 } catch (final IOException e) {
95 final DialogFragment newFragment = ErrorDialogFragment
96 .newInstance(R.string.error_dialog_generic_error);
97 newFragment.show(this.getFragmentManager(), "errorDialog");
101 this.mListState = savedInstanceState.getParcelable("ListState");
104 this.setHasOptionsMenu(false);
106 final WeatherOverviewAdapter adapter = new WeatherOverviewAdapter(
107 this.getActivity(), R.layout.weather_main_entry_list);
110 this.setEmptyText("No data available");
112 this.setListAdapter(adapter);
113 this.setListShown(true);
114 this.setListShownNoAnimation(true);
118 public void onResume() {
121 // 1. Register receiver.
122 final IntentFilter filter = new IntentFilter();
123 filter.addAction("de.example.exampletdd.UPDATEOVERVIEWWEATHER");
124 LocalBroadcastManager.getInstance(this.getActivity()).registerReceiver(this.mReceiver, filter);
126 final SharedPreferences sharedPreferences = PreferenceManager
127 .getDefaultSharedPreferences(this.getActivity());
129 // 2. Update units of measurement.
130 String keyPreference = this.getResources()
131 .getString(R.string.weather_preferences_units_key);
132 final String unitsPreferenceValue = sharedPreferences.getString(keyPreference, "");
133 final String celsius = this.getResources().getString(
134 R.string.weather_preferences_units_celsius);
135 if (unitsPreferenceValue.equals(celsius)) {
136 this.mIsFahrenheit = false;
138 this.mIsFahrenheit = true;
141 // 3. Update number day forecast.
142 keyPreference = this.getResources()
143 .getString(R.string.weather_preferences_day_forecast_key);
144 final String dayForecast = sharedPreferences.getString(keyPreference, "");
145 this.mDayForecast = Integer.valueOf(dayForecast);
147 // 4. Update forecast weather data on display.
148 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
149 .getForecastWeatherData();
150 if ((this.mListState != null) && (forecastWeatherData != null)) {
151 this.updateForecastWeatherData(forecastWeatherData);
152 this.getListView().onRestoreInstanceState(this.mListState);
153 } else if (forecastWeatherData != null) {
154 this.updateForecastWeatherData(forecastWeatherData);
160 public void onPause() {
162 LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(this.mReceiver);
166 public void onSaveInstanceState(final Bundle savedInstanceState) {
169 final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
170 .getForecastWeatherData();
172 if (forecastWeatherData != null) {
173 savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
176 this.mListState = this.getListView().onSaveInstanceState();
177 savedInstanceState.putParcelable("ListState", this.mListState);
179 super.onSaveInstanceState(savedInstanceState);
183 public void onListItemClick(final ListView l, final View v, final int position, final long id) {
184 final WeatherInformationSpecificDataFragment fragment = (WeatherInformationSpecificDataFragment) this
185 .getFragmentManager().findFragmentById(R.id.weather_specific_data__fragment);
186 if (fragment == null) {
188 final Intent intent = new Intent("de.example.exampletdd.WEATHERINFO")
189 .setComponent(new ComponentName("de.example.exampletdd",
190 "de.example.exampletdd.WeatherInformationSpecificDataActivity"));
191 intent.putExtra("CHOSEN_DAY", (int) id);
192 WeatherInformationOverviewFragment.this.getActivity().startActivity(intent);
195 fragment.getWeatherByDay((int) id);
199 private void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData) {
200 final List<WeatherOverviewEntry> entries = new ArrayList<WeatherOverviewEntry>();
201 final WeatherOverviewAdapter adapter = new WeatherOverviewAdapter(this.getActivity(),
202 R.layout.weather_main_entry_list);
205 final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
206 tempFormatter.applyPattern("#####.##");
207 final SimpleDateFormat dayNameFormatter = new SimpleDateFormat("EEE", Locale.US);
208 final SimpleDateFormat monthAndDayNumberormatter = new SimpleDateFormat("MMM d", Locale.US);
209 final double tempUnits = this.mIsFahrenheit ? 0 : 273.15;
210 final String symbol = this.mIsFahrenheit ? "ºF" : "ºC";
213 final Calendar calendar = Calendar.getInstance();
214 int count = this.mDayForecast;
215 for (final de.example.exampletdd.model.forecastweather.List forecast : forecastWeatherData
220 if ((forecast.getWeather().size() > 0) &&
221 (forecast.getWeather().get(0).getIcon() != null) &&
222 (IconsList.getIcon(forecast.getWeather().get(0).getIcon()) != null)) {
223 final String icon = forecast.getWeather().get(0).getIcon();
224 picture = BitmapFactory.decodeResource(this.getResources(), IconsList.getIcon(icon)
225 .getResourceDrawable());
227 picture = BitmapFactory.decodeResource(this.getResources(),
228 R.drawable.weather_severe_alert);
231 final Long forecastUNIXDate = (Long) forecast.getDt();
232 calendar.setTimeInMillis(forecastUNIXDate * 1000L);
233 final Date dayTime = calendar.getTime();
234 final String dayTextName = dayNameFormatter.format(dayTime);
235 final String monthAndDayNumberText = monthAndDayNumberormatter.format(dayTime);
237 Double maxTemp = null;
238 if (forecast.getTemp().getMax() != null) {
239 maxTemp = (Double) forecast.getTemp().getMax();
240 maxTemp = maxTemp - tempUnits;
243 Double minTemp = null;
244 if (forecast.getTemp().getMin() != null) {
245 minTemp = (Double) forecast.getTemp().getMin();
246 minTemp = minTemp - tempUnits;
249 if ((maxTemp != null) && (minTemp != null)) {
250 entries.add(new WeatherOverviewEntry(dayTextName, monthAndDayNumberText,
251 tempFormatter.format(maxTemp) + symbol, tempFormatter.format(minTemp) + symbol,
261 this.setListAdapter(null);
262 adapter.addAll(entries);
263 this.setListAdapter(adapter);