c646255dcdd1a0f668908f5badb872c169ea5416
[JavaForFun] /
1 package de.example.exampletdd.fragment.overview;
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.Calendar;
9 import java.util.Date;
10 import java.util.List;
11 import java.util.Locale;
12
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;
34
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;
42
43     @Override
44     public void onCreate(final Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46
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, "");
52
53         this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this.getActivity());
54
55         this.mReceiver = new BroadcastReceiver() {
56
57             @Override
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);
69                     }
70
71                 }
72             }
73         };
74
75         final IntentFilter filter = new IntentFilter();
76         filter.addAction("de.example.exampletdd.UPDATEOVERVIEWWEATHER");
77         this.getActivity().registerReceiver(this.mReceiver, filter);
78     }
79
80     @Override
81     public void onActivityCreated(final Bundle savedInstanceState) {
82         super.onActivityCreated(savedInstanceState);
83
84         final ListView listWeatherView = this.getListView();
85         listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
86
87         if (savedInstanceState != null) {
88             // Restore state
89             final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
90                     .getSerializable("ForecastWeatherData");
91
92             if (forecastWeatherData != null) {
93                 try {
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");
100                 }
101             }
102
103             this.mListState = savedInstanceState.getParcelable("ListState");
104         }
105
106         this.setHasOptionsMenu(false);
107
108         final WeatherOverviewAdapter adapter = new WeatherOverviewAdapter(
109                 this.getActivity(), R.layout.weather_main_entry_list);
110
111
112         this.setEmptyText("Press download to receive weather information");
113
114         this.setListAdapter(adapter);
115         this.setListShown(true);
116         this.setListShownNoAnimation(true);
117     }
118
119     @Override
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) {
124             // handset layout
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);
130         } else {
131             // tablet layout
132             fragment.getWeatherByDay((int) id);
133         }
134     }
135
136     @Override
137     public void onSaveInstanceState(final Bundle savedInstanceState) {
138
139         // Save state
140         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
141                 .getForecastWeatherData();
142
143         if (forecastWeatherData != null) {
144             savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
145         }
146
147         this.mListState = this.getListView().onSaveInstanceState();
148         savedInstanceState.putParcelable("ListState", this.mListState);
149
150         super.onSaveInstanceState(savedInstanceState);
151     }
152
153     @Override
154     public void onDestroy() {
155         super.onDestroy();
156         this.getActivity().unregisterReceiver(this.mReceiver);
157     }
158
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);
163
164
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";
171
172
173         final Calendar calendar = Calendar.getInstance();
174         for (final de.example.exampletdd.model.forecastweather.List forecast : forecastWeatherData
175                 .getList()) {
176
177             Bitmap picture;
178
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());
185             } else {
186                 picture = BitmapFactory.decodeResource(this.getResources(),
187                         R.drawable.weather_severe_alert);
188             }
189
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);
195
196             Double maxTemp = null;
197             if (forecast.getTemp().getMax() != null) {
198                 maxTemp = (Double) forecast.getTemp().getMax();
199                 maxTemp = maxTemp - tempUnits;
200             }
201
202             Double minTemp = null;
203             if (forecast.getTemp().getMin() != null) {
204                 minTemp = (Double) forecast.getTemp().getMin();
205                 minTemp = minTemp - tempUnits;
206             }
207
208             if ((maxTemp != null) && (minTemp != null)) {
209                 entries.add(new WeatherOverviewEntry(dayTextName, monthAndDayNumberText,
210                         tempFormatter.format(maxTemp) + symbol, tempFormatter.format(minTemp) + symbol,
211                         picture));
212             }
213         }
214
215         this.setListAdapter(null);
216         adapter.addAll(entries);
217         this.setListAdapter(adapter);
218     }
219
220     @Override
221     public void onResume() {
222         super.onResume();
223
224         final SharedPreferences sharedPreferences = PreferenceManager
225                 .getDefaultSharedPreferences(this.getActivity());
226
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;
235         } else {
236             this.mIsFahrenheit = true;
237         }
238
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, "");
243
244
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);
253         }
254
255     }
256 }