1 package de.example.exampletdd.fragment;
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URISyntaxException;
7 import java.text.DecimalFormat;
8 import java.text.SimpleDateFormat;
11 import org.apache.http.client.ClientProtocolException;
12 import org.json.JSONException;
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;
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;
49 public View onCreateView(final LayoutInflater inflater,
50 final ViewGroup container, final Bundle savedInstanceState) {
51 final View rootView = inflater.inflate(R.layout.fragment_main,
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);
67 public void onClickGetWeather() {
69 final IJPOSWeatherParser JPOSWeatherParser = new JPOSWeatherParser();
70 final WeatherService weatherService = new WeatherService(
72 final AndroidHttpClient httpClient = AndroidHttpClient
73 .newInstance("Android Weather Information Agent");
74 final WeatherHTTPClient HTTPweatherClient = new WeatherHTTPClient(
77 final WeatherTask weatherTask = new WeatherTask(HTTPweatherClient, weatherService);
79 final EditText cityCountry = (EditText) this.getActivity()
80 .findViewById(R.id.editTextCity);
82 weatherTask.execute(cityCountry.getText().toString());
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;
90 if (weatherData.getWeather() != null) {
91 this.weatherDescription.setText(weatherData.getWeather()
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));
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);
110 unixTime = weatherData.getSystem().getSunSetTime();
111 unixDate = new Date(unixTime * 1000L);
112 dateFormatUnix = dateFormat.format(unixDate);
113 this.sunSet.setText(dateFormatUnix);
116 if (weatherData.getIconData() != null) {
117 final Bitmap icon = BitmapFactory.decodeByteArray(
118 weatherData.getIconData(), 0,
119 weatherData.getIconData().length);
120 this.imageIcon.setImageBitmap(icon);
125 public void onResume() {
128 final SharedPreferences sharedPreferences = PreferenceManager
129 .getDefaultSharedPreferences(this.getActivity());
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;
139 this.isFahrenheit = true;
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;
148 public WeatherTask(final WeatherHTTPClient weatherHTTPClient,
149 final WeatherService weatherService) {
150 this.weatherHTTPClient = weatherHTTPClient;
151 this.weatherService = weatherService;
155 protected WeatherData doInBackground(final Object... params) {
156 WeatherData weatherData = null;
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);
171 this.weatherHTTPClient.close();
178 protected void onPostExecute(final WeatherData weatherData) {
179 if (weatherData != null) {
180 WeatherInformationDataFragmentDeprecated.this.updateWeatherData(weatherData);
182 ((ErrorMessage) WeatherInformationDataFragmentDeprecated.this.getActivity())
183 .createErrorDialog(R.string.error_dialog_generic_error);
186 this.weatherHTTPClient.close();
190 protected void onCancelled(final WeatherData weatherData) {
192 ((ErrorMessage) WeatherInformationDataFragmentDeprecated.this.getActivity())
193 .createErrorDialog(R.string.error_dialog_connection_tiemout);
195 this.weatherHTTPClient.close();
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);
210 final String jsonData = this.weatherHTTPClient.retrieveJSONDataFromAPI(new URL(url));
213 final WeatherData weatherData = this.weatherService.retrieveWeather(jsonData);
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);