No time for comments
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Tue, 8 Apr 2014 20:07:13 +0000 (22:07 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Tue, 8 Apr 2014 20:07:13 +0000 (22:07 +0200)
res/values/strings.xml
src/de/example/exampletdd/WeatherInformationMapActivity.java
src/de/example/exampletdd/fragment/WeatherInformationDataFragment.java

index ce95f6e..2307940 100644 (file)
@@ -31,6 +31,7 @@
     <string name="error_dialog_connection_tiemout">Connection error timeout</string>
     <string name="error_dialog_generic_error">Impossible to receive weather data.</string>
     <string name="error_dialog_store_geocoding_data">Impossible to save current location.</string>
+    <string name="error_dialog_location_error">Impossible to receive city and country values.</string>
     <string name="icon_weather_description">Icon weather</string>
     <string name="header_action_bar">City,country</string>
     <string name="weather_preferences_units_key">weather_preferences_units</string>
index 8d7e8bb..ad67b52 100644 (file)
@@ -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<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;
@@ -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 {
index daefa98..efc8657 100644 (file)
@@ -275,7 +275,7 @@ public class WeatherInformationDataFragment extends Fragment implements GetWeath
         }
     }
 
-    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;
@@ -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)