From: gu.martinm@gmail.com Date: Tue, 8 Apr 2014 20:07:13 +0000 (+0200) Subject: No time for comments X-Git-Tag: weatherinformation-1.0~184 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=4183c4fde279bc7a81c9a9e1defc8dcff79207a6;p=AndroidWeatherInformation No time for comments --- diff --git a/res/values/strings.xml b/res/values/strings.xml index ce95f6e..2307940 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -31,6 +31,7 @@ Connection error timeout Impossible to receive weather data. Impossible to save current location. + Impossible to receive city and country values. Icon weather City,country weather_preferences_units diff --git a/src/de/example/exampletdd/WeatherInformationMapActivity.java b/src/de/example/exampletdd/WeatherInformationMapActivity.java index 8d7e8bb..ad67b52 100644 --- a/src/de/example/exampletdd/WeatherInformationMapActivity.java +++ b/src/de/example/exampletdd/WeatherInformationMapActivity.java @@ -9,18 +9,13 @@ import java.io.OutputStream; import java.io.StreamCorruptedException; import java.util.List; import java.util.Locale; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import android.app.Activity; +import android.app.DialogFragment; import android.content.Context; import android.location.Address; import android.location.Geocoder; +import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.widget.TextView; @@ -33,6 +28,8 @@ import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; +import de.example.exampletdd.fragment.ErrorDialogFragment; +import de.example.exampletdd.fragment.ProgressDialogFragment; import de.example.exampletdd.model.GeocodingData; public class WeatherInformationMapActivity extends Activity { @@ -40,7 +37,6 @@ public class WeatherInformationMapActivity extends Activity { private static final String TAG = "WeatherInformationMapActivity"; private GoogleMap mMap; private Marker mMarker; - private ExecutorService mExec; @Override protected void onCreate(final Bundle savedInstanceState) { @@ -56,8 +52,6 @@ public class WeatherInformationMapActivity extends Activity { this.mMap.setOnMapLongClickListener(new LongClickListener()); this.mMap.setOnMarkerClickListener(new MarkerClickListener()); - - this.mExec = Executors.newSingleThreadExecutor(); } @Override @@ -95,14 +89,6 @@ public class WeatherInformationMapActivity extends Activity { } } - @Override - public void onDestroy() { - if (this.mExec != null) { - this.mExec.shutdownNow(); - } - super.onDestroy(); - } - private void storeGeocodingDataToFile(final GeocodingData geocodingData) throws FileNotFoundException, IOException { final OutputStream persistenceFile = this.openFileOutput( @@ -139,7 +125,6 @@ public class WeatherInformationMapActivity extends Activity { } private class LongClickListener implements OnMapLongClickListener { - private static final String TAG = "LongClickListener"; @Override public void onMapLongClick(final LatLng point) { @@ -151,56 +136,87 @@ public class WeatherInformationMapActivity extends Activity { WeatherInformationMapActivity.this.mMarker.setPosition(point); } - final Future task = WeatherInformationMapActivity.this.mExec - .submit(new GeocoderTask(point.latitude, point.longitude)); + final GeocoderAsyncTask geocoderAsyncTask = new GeocoderAsyncTask(); + + geocoderAsyncTask.execute(point.latitude, point.longitude); + } + } + + public class GeocoderAsyncTask extends AsyncTask { + private static final String TAG = "GeocoderAsyncTask"; + private final DialogFragment newFragment; + + public GeocoderAsyncTask() { + this.newFragment = ProgressDialogFragment + .newInstance(R.string.weather_progress_getdata); + } + + @Override + protected void onPreExecute() { + this.newFragment.show(WeatherInformationMapActivity.this.getFragmentManager(), "progressDialog"); + } + + @Override + protected GeocodingData doInBackground(final Object... params) { + final double latitude = (Double) params[0]; + final double longitude = (Double) params[1]; + + + GeocodingData geocodingData = null; try { - final GeocodingData geocodingData = task.get(5, - TimeUnit.SECONDS); - final TextView cityCountry = (TextView) WeatherInformationMapActivity.this - .findViewById(R.id.weather_map_citycountry_data); - - final String city = (geocodingData.getCity() == null) ? "city not found" - : geocodingData.getCity(); - final String country = (geocodingData.getCountry() == null) ? "country not found" - : geocodingData.getCountry(); - cityCountry.setText(city + "," + country); - - WeatherInformationMapActivity.this - .storeGeocodingDataToFile(geocodingData); - } catch (final InterruptedException e) { - Log.e(TAG, "LongClickListener exception: ", e); - Thread.currentThread().interrupt(); - } catch (final ExecutionException e) { - final Throwable cause = e.getCause(); - Log.e(TAG, "LongClickListener exception: ", cause); - } catch (final TimeoutException e) { - Log.e(TAG, "LongClickListener exception: ", e); - } catch (final FileNotFoundException e) { - Log.e(TAG, "LongClickListener exception: ", e); + geocodingData = this.getGeocodingData(latitude, longitude); } catch (final IOException e) { - Log.e(TAG, "LongClickListener exception: ", e); - } finally { - task.cancel(true); + Log.e(TAG, "doInBackground exception: ", e); } + return geocodingData; } - } - public class GeocoderTask implements Callable { - private final double latitude; - private final double longitude; + @Override + protected void onPostExecute(final GeocodingData geocodingData) { + this.newFragment.dismiss(); - public GeocoderTask(final double latitude, final double longitude) { - this.latitude = latitude; - this.longitude = longitude; + if (geocodingData == null) { + final DialogFragment newFragment = ErrorDialogFragment.newInstance(R.string.error_dialog_location_error); + newFragment.show(WeatherInformationMapActivity.this.getFragmentManager(), "errorDialog"); + + return; + } + + try { + this.onPostExecuteThrowable(geocodingData); + } catch (final FileNotFoundException e) { + Log.e(TAG, "GeocoderAsyncTask onPostExecute exception: ", e); + final DialogFragment newFragment = ErrorDialogFragment.newInstance(R.string.error_dialog_location_error); + newFragment.show(WeatherInformationMapActivity.this.getFragmentManager(), "errorDialog"); + } catch (final IOException e) { + Log.e(TAG, "GeocoderAsyncTask onPostExecute exception: ", e); + final DialogFragment newFragment = ErrorDialogFragment.newInstance(R.string.error_dialog_location_error); + newFragment.show(WeatherInformationMapActivity.this.getFragmentManager(), "errorDialog"); + } } - @Override - public GeocodingData call() throws Exception { + private void onPostExecuteThrowable(final GeocodingData geocodingData) + throws FileNotFoundException, IOException { + WeatherInformationMapActivity.this.storeGeocodingDataToFile(geocodingData); + + final String city = (geocodingData.getCity() == null) ? + WeatherInformationMapActivity.this.getString(R.string.city_not_found) + : geocodingData.getCity(); + final String country = (geocodingData.getCountry() == null) ? + WeatherInformationMapActivity.this.getString(R.string.country_not_found) + : geocodingData.getCountry(); + + final TextView cityCountry = (TextView) WeatherInformationMapActivity.this + .findViewById(R.id.weather_map_citycountry_data); + + cityCountry.setText(city + "," + country); + } + + private GeocodingData getGeocodingData(final double latitude, final double longitude) throws IOException { final Geocoder geocoder = new Geocoder( WeatherInformationMapActivity.this, Locale.getDefault()); - final List
addresses = geocoder.getFromLocation( - this.latitude, this.longitude, 1); + final List
addresses = geocoder.getFromLocation(latitude, longitude, 1); if (addresses == null) { return null; @@ -210,13 +226,13 @@ public class WeatherInformationMapActivity extends Activity { return null; } - return new GeocodingData.Builder() - .setLatitude(this.latitude) - .setLongitude(this.longitude) - .setCity(addresses.get(0).getLocality()) - .setCountry(addresses.get(0).getCountryName()) - .build(); + return new GeocodingData.Builder().setLatitude(latitude) + .setLongitude(longitude) + .setCity(addresses.get(0).getLocality()) + .setCountry(addresses.get(0).getCountryName()) + .build(); } + } private class MarkerClickListener implements OnMarkerClickListener { diff --git a/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java b/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java index daefa98..efc8657 100644 --- a/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java +++ b/src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java @@ -275,7 +275,7 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath } } - public class WeatherTask extends AsyncTask { + public class WeatherTask extends AsyncTask { private static final String TAG = "WeatherTask"; private final WeatherHTTPClient weatherHTTPClient; private final WeatherService weatherService; @@ -292,7 +292,7 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath @Override protected void onPreExecute() { this.newFragment.show(WeatherInformationDataFragment.this.getActivity() - .getFragmentManager(), "errorDialog"); + .getFragmentManager(), "progressDialog"); } @Override @@ -321,12 +321,15 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath @Override protected void onPostExecute(final WeatherData weatherData) { + this.weatherHTTPClient.close(); + this.newFragment.dismiss(); if (weatherData != null) { try { this.onPostExecuteThrowable(weatherData); } catch (final IOException e) { + Log.e(TAG, "WeatherTask onPostExecute exception: ", e); ((ErrorMessage) WeatherInformationDataFragment.this .getActivity()) .createErrorDialog(R.string.error_dialog_generic_error); @@ -336,21 +339,14 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath .getActivity()) .createErrorDialog(R.string.error_dialog_generic_error); } - - this.weatherHTTPClient.close(); } @Override protected void onCancelled(final WeatherData weatherData) { - this.onCancelled(); - ((ErrorMessage) WeatherInformationDataFragment.this.getActivity()) - .createErrorDialog(R.string.error_dialog_connection_tiemout); - this.weatherHTTPClient.close(); - } - @Override - protected void onProgressUpdate(final Integer... progress) { + ((ErrorMessage) WeatherInformationDataFragment.this.getActivity()) + .createErrorDialog(R.string.error_dialog_connection_tiemout); } private WeatherData doInBackgroundThrowable(final Object... params)