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;
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 {
private static final String TAG = "WeatherInformationMapActivity";
private GoogleMap mMap;
private Marker mMarker;
- private ExecutorService mExec;
@Override
protected void onCreate(final Bundle savedInstanceState) {
this.mMap.setOnMapLongClickListener(new LongClickListener());
this.mMap.setOnMarkerClickListener(new MarkerClickListener());
-
- this.mExec = Executors.newSingleThreadExecutor();
}
@Override
}
}
- @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(
}
private class LongClickListener implements OnMapLongClickListener {
- private static final String TAG = "LongClickListener";
@Override
public void onMapLongClick(final LatLng point) {
WeatherInformationMapActivity.this.mMarker.setPosition(point);
}
- final Future<GeocodingData> 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<Object, Void, GeocodingData> {
+ 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<GeocodingData> {
- 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<Address> addresses = geocoder.getFromLocation(
- this.latitude, this.longitude, 1);
+ final List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses == null) {
return null;
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 {
}
}
- public class WeatherTask extends AsyncTask<Object, Integer, WeatherData> {
+ public class WeatherTask extends AsyncTask<Object, Void, WeatherData> {
private static final String TAG = "WeatherTask";
private final WeatherHTTPClient weatherHTTPClient;
private final WeatherService weatherService;
@Override
protected void onPreExecute() {
this.newFragment.show(WeatherInformationDataFragment.this.getActivity()
- .getFragmentManager(), "errorDialog");
+ .getFragmentManager(), "progressDialog");
}
@Override
@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);
.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)