8322661e9b2b1540937e9a91382e4e7f4ef49007
[JavaForFun] /
1 package de.example.exampletdd.fragment;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URISyntaxException;
6 import java.net.URL;
7 import java.text.DecimalFormat;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10
11 import org.apache.http.client.ClientProtocolException;
12 import org.json.JSONException;
13
14 import android.app.Fragment;
15 import android.content.SharedPreferences;
16 import android.graphics.Bitmap;
17 import android.graphics.BitmapFactory;
18 import android.net.http.AndroidHttpClient;
19 import android.os.AsyncTask;
20 import android.os.Bundle;
21 import android.preference.PreferenceManager;
22 import android.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.EditText;
27 import android.widget.ImageView;
28 import de.example.exampletdd.R;
29 import de.example.exampletdd.activityinterface.ErrorMessage;
30 import de.example.exampletdd.activityinterface.OnClickButtons;
31 import de.example.exampletdd.httpclient.WeatherHTTPClient;
32 import de.example.exampletdd.model.WeatherData;
33 import de.example.exampletdd.parser.IJPOSWeatherParser;
34 import de.example.exampletdd.parser.JPOSWeatherParser;
35 import de.example.exampletdd.service.WeatherService;
36
37 public class WeatherInformationDataFragmentDeprecated extends Fragment implements OnClickButtons {
38     private boolean isFahrenheit;
39     private EditText weatherDescription;
40     private EditText temperature;
41     private EditText maxTemperature;
42     private EditText minTemperature;
43     private EditText sunRise;
44     private EditText sunSet;
45     private ImageView imageIcon;
46
47
48     @Override
49     public View onCreateView(final LayoutInflater inflater,
50             final ViewGroup container, final Bundle savedInstanceState) {
51         final View rootView = inflater.inflate(R.layout.fragment_main,
52                 container, false);
53
54
55         this.weatherDescription = (EditText) rootView.findViewById(R.id.editTextWeatherDescription);
56         this.temperature = (EditText) rootView.findViewById(R.id.editTextTemperature);
57         this.maxTemperature = (EditText) rootView.findViewById(R.id.editTextMaxTemperature);
58         this.minTemperature = (EditText) rootView.findViewById(R.id.editTextMinTemperature);
59         this.sunRise = (EditText) rootView.findViewById(R.id.editTextSunRise);
60         this.sunSet = (EditText) rootView.findViewById(R.id.editTextSunSet);
61         this.imageIcon = (ImageView) rootView.findViewById(R.id.imageIcon);
62
63         return rootView;
64     }
65
66     @Override
67     public void onClickGetWeather() {
68
69         final IJPOSWeatherParser JPOSWeatherParser = new JPOSWeatherParser();
70         final WeatherService weatherService = new WeatherService(
71                 JPOSWeatherParser);
72         final AndroidHttpClient httpClient = AndroidHttpClient
73                 .newInstance("Android Weather Information Agent");
74         final WeatherHTTPClient HTTPweatherClient = new WeatherHTTPClient(
75                 httpClient);
76
77         final WeatherTask weatherTask = new WeatherTask(HTTPweatherClient, weatherService);
78
79         final EditText cityCountry = (EditText) this.getActivity()
80                 .findViewById(R.id.editTextCity);
81
82         weatherTask.execute(cityCountry.getText().toString());
83     }
84
85     public void updateWeatherData(final WeatherData weatherData) {
86         final DecimalFormat tempFormatter = new DecimalFormat("#####.#####");
87         final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z");
88         final double tempUnits = this.isFahrenheit ? 0 : 273.15;
89
90         if (weatherData.getWeather() != null) {
91             this.weatherDescription.setText(weatherData.getWeather()
92                     .getDescription());
93             double conversion = weatherData.getMain().getTemp();
94             conversion = conversion - tempUnits;
95             this.temperature.setText(tempFormatter.format(conversion));
96             conversion = weatherData.getMain().getMaxTemp();
97             conversion = conversion - tempUnits;
98             this.maxTemperature.setText(tempFormatter.format(conversion));
99             conversion = weatherData.getMain().getMinTemp();
100             conversion = conversion - tempUnits;
101             this.minTemperature.setText(tempFormatter.format(conversion));
102         }
103
104         if (weatherData.getSystem() != null) {
105             long unixTime = weatherData.getSystem().getSunRiseTime();
106             Date unixDate = new Date(unixTime * 1000L);
107             String dateFormatUnix = dateFormat.format(unixDate);
108             this.sunRise.setText(dateFormatUnix);
109
110             unixTime = weatherData.getSystem().getSunSetTime();
111             unixDate = new Date(unixTime * 1000L);
112             dateFormatUnix = dateFormat.format(unixDate);
113             this.sunSet.setText(dateFormatUnix);
114         }
115
116         if (weatherData.getIconData() != null) {
117             final Bitmap icon = BitmapFactory.decodeByteArray(
118                     weatherData.getIconData(), 0,
119                     weatherData.getIconData().length);
120             this.imageIcon.setImageBitmap(icon);
121         }
122     }
123
124     @Override
125     public void onResume() {
126         super.onResume();
127
128         final SharedPreferences sharedPreferences = PreferenceManager
129                 .getDefaultSharedPreferences(this.getActivity());
130
131         final String unitsKey = this.getResources().getString(
132                 R.string.weather_preferences_units_key);
133         final String units = sharedPreferences.getString(unitsKey, "");
134         final String celsius = this.getResources().getString(
135                 R.string.weather_preferences_units_celsius);
136         if (units.equals(celsius)) {
137             this.isFahrenheit = false;
138         } else {
139             this.isFahrenheit = true;
140         }
141     }
142
143     public class WeatherTask extends AsyncTask<Object, Void, WeatherData> {
144         private static final String TAG = "JSONWeatherTask";
145         private final WeatherHTTPClient weatherHTTPClient;
146         private final WeatherService weatherService;
147
148         public WeatherTask(final WeatherHTTPClient weatherHTTPClient,
149                 final WeatherService weatherService) {
150             this.weatherHTTPClient = weatherHTTPClient;
151             this.weatherService = weatherService;
152         }
153
154         @Override
155         protected WeatherData doInBackground(final Object... params) {
156             WeatherData weatherData = null;
157
158             try {
159                 weatherData = this.doInBackgroundThrowable(params);
160             } catch (final ClientProtocolException e) {
161                 Log.e(TAG, "WeatherHTTPClient exception: ", e);
162             } catch (final MalformedURLException e) {
163                 Log.e(TAG, "Syntax URL exception: ", e);
164             } catch (final URISyntaxException e) {
165                 Log.e(TAG, "WeatherHTTPClient exception: ", e);
166             } catch (final IOException e) {
167                 Log.e(TAG, "WeatherHTTPClient exception: ", e);
168             } catch (final JSONException e) {
169                 Log.e(TAG, "WeatherService exception: ", e);
170             } finally {
171                 this.weatherHTTPClient.close();
172             }
173
174             return weatherData;
175         }
176
177         @Override
178         protected void onPostExecute(final WeatherData weatherData) {
179             if (weatherData != null) {
180                 WeatherInformationDataFragmentDeprecated.this.updateWeatherData(weatherData);
181             } else {
182                 ((ErrorMessage) WeatherInformationDataFragmentDeprecated.this.getActivity())
183                 .createErrorDialog(R.string.error_dialog_generic_error);
184             }
185
186             this.weatherHTTPClient.close();
187         }
188
189         @Override
190         protected void onCancelled(final WeatherData weatherData) {
191             this.onCancelled();
192             ((ErrorMessage) WeatherInformationDataFragmentDeprecated.this.getActivity())
193             .createErrorDialog(R.string.error_dialog_connection_tiemout);
194
195             this.weatherHTTPClient.close();
196         }
197
198         private WeatherData doInBackgroundThrowable(final Object... params)
199                 throws ClientProtocolException, MalformedURLException,
200                 URISyntaxException, IOException, JSONException {
201             final String cityCountry = (String) params[0];
202             final String urlAPICity = WeatherInformationDataFragmentDeprecated.this.getResources()
203                     .getString(R.string.uri_api_city);
204             final String APIVersion = WeatherInformationDataFragmentDeprecated.this.getResources()
205                     .getString(R.string.api_version);
206             String url = this.weatherService.createURIAPICityCountry(
207                     cityCountry, urlAPICity, APIVersion);
208
209
210             final String jsonData = this.weatherHTTPClient.retrieveJSONDataFromAPI(new URL(url));
211
212
213             final WeatherData weatherData = this.weatherService.retrieveWeather(jsonData);
214
215
216             final String icon = weatherData.getWeather().getIcon();
217             final String urlAPIicon = WeatherInformationDataFragmentDeprecated.this
218                     .getResources().getString(R.string.uri_api_icon);
219             url = this.weatherService.createURIAPIicon(icon, urlAPIicon);
220             final byte[] iconData = this.weatherHTTPClient
221                     .retrieveDataFromAPI(new URL(url)).toByteArray();
222             weatherData.setIconData(iconData);
223
224
225             return weatherData;
226         }
227     }
228 }