aaf140e07220b18a630d9861ee8c280dc9d6f653
[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.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;
35
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;
43
44     @Override
45     public void onCreate(final Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47
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);
54
55         this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this.getActivity());
56
57         this.mReceiver = new BroadcastReceiver() {
58
59             @Override
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);
71                     }
72
73                 }
74             }
75         };
76     }
77
78     @Override
79     public void onActivityCreated(final Bundle savedInstanceState) {
80         super.onActivityCreated(savedInstanceState);
81
82         final ListView listWeatherView = this.getListView();
83         listWeatherView.setChoiceMode(ListView.CHOICE_MODE_NONE);
84
85         if (savedInstanceState != null) {
86             // Restore state
87             final ForecastWeatherData forecastWeatherData = (ForecastWeatherData) savedInstanceState
88                     .getSerializable("ForecastWeatherData");
89
90             if (forecastWeatherData != null) {
91                 try {
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");
98                 }
99             }
100
101             this.mListState = savedInstanceState.getParcelable("ListState");
102         }
103
104         this.setHasOptionsMenu(false);
105
106         final WeatherOverviewAdapter adapter = new WeatherOverviewAdapter(
107                 this.getActivity(), R.layout.weather_main_entry_list);
108
109
110         this.setEmptyText("No data available");
111
112         this.setListAdapter(adapter);
113         this.setListShown(true);
114         this.setListShownNoAnimation(true);
115     }
116
117     @Override
118     public void onResume() {
119         super.onResume();
120
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);
125
126         final SharedPreferences sharedPreferences = PreferenceManager
127                 .getDefaultSharedPreferences(this.getActivity());
128
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;
137         } else {
138             this.mIsFahrenheit = true;
139         }
140
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);
146
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);
155         }
156
157     }
158
159     @Override
160     public void onPause() {
161         super.onPause();
162         LocalBroadcastManager.getInstance(this.getActivity()).unregisterReceiver(this.mReceiver);
163     }
164
165     @Override
166     public void onSaveInstanceState(final Bundle savedInstanceState) {
167
168         // Save state
169         final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
170                 .getForecastWeatherData();
171
172         if (forecastWeatherData != null) {
173             savedInstanceState.putSerializable("ForecastWeatherData", forecastWeatherData);
174         }
175
176         this.mListState = this.getListView().onSaveInstanceState();
177         savedInstanceState.putParcelable("ListState", this.mListState);
178
179         super.onSaveInstanceState(savedInstanceState);
180     }
181
182     @Override
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) {
187             // handset layout
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);
193         } else {
194             // tablet layout
195             fragment.getWeatherByDay((int) id);
196         }
197     }
198
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);
203
204
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";
211
212
213         final Calendar calendar = Calendar.getInstance();
214         int count = this.mDayForecast;
215         for (final de.example.exampletdd.model.forecastweather.List forecast : forecastWeatherData
216                 .getList()) {
217
218             Bitmap picture;
219
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());
226             } else {
227                 picture = BitmapFactory.decodeResource(this.getResources(),
228                         R.drawable.weather_severe_alert);
229             }
230
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);
236
237             Double maxTemp = null;
238             if (forecast.getTemp().getMax() != null) {
239                 maxTemp = (Double) forecast.getTemp().getMax();
240                 maxTemp = maxTemp - tempUnits;
241             }
242
243             Double minTemp = null;
244             if (forecast.getTemp().getMin() != null) {
245                 minTemp = (Double) forecast.getTemp().getMin();
246                 minTemp = minTemp - tempUnits;
247             }
248
249             if ((maxTemp != null) && (minTemp != null)) {
250                 entries.add(new WeatherOverviewEntry(dayTextName, monthAndDayNumberText,
251                         tempFormatter.format(maxTemp) + symbol, tempFormatter.format(minTemp) + symbol,
252                         picture));
253             }
254
255             count = count - 1;
256             if (count == 0) {
257                 break;
258             }
259         }
260
261         this.setListAdapter(null);
262         adapter.addAll(entries);
263         this.setListAdapter(adapter);
264     }
265 }