From e62ecfa43aba3171994e03104a99e5a814eec8fd Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 16 Dec 2014 01:34:53 +0100 Subject: [PATCH] When RuntimeException in doInBackground method we must go to error state. Becareful, there could be multiple AsyncTasks (if user is fast enough). So always check the trigger AsyncTask conditions. --- .../fragment/current/CurrentFragment.java | 41 ++++++++++++---------- .../fragment/overview/OverviewFragment.java | 35 +++++++++--------- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java b/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java index 18b974f..7cdbeba 100644 --- a/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java +++ b/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java @@ -115,32 +115,31 @@ public class CurrentFragment extends Fragment { if (action.equals(BROADCAST_INTENT_ACTION)) { final Current currentRemote = (Current) intent.getSerializableExtra("current"); - if (currentRemote != null) { + // 1. Check conditions. They must be the same as the ones that triggered the AsyncTask. + final DatabaseQueries query = new DatabaseQueries(context.getApplicationContext()); + final WeatherLocation weatherLocation = query.queryDataBase(); + final PermanentStorage store = new PermanentStorage(context.getApplicationContext()); + final Current current = store.getCurrent(); - // 1. Check conditions. They must be the same as the ones that triggered the AsyncTask. - final DatabaseQueries query = new DatabaseQueries(context.getApplicationContext()); - final WeatherLocation weatherLocation = query.queryDataBase(); - final PermanentStorage store = new PermanentStorage(context.getApplicationContext()); - final Current current = store.getCurrent(); + if (current == null || !CurrentFragment.this.isDataFresh(weatherLocation.getLastCurrentUIUpdate())) { - if (current == null || !CurrentFragment.this.isDataFresh(weatherLocation.getLastCurrentUIUpdate())) { - // 2. Update UI. - CurrentFragment.this.updateUI(currentRemote); + if (currentRemote != null) { + // 2. Update UI. + CurrentFragment.this.updateUI(currentRemote); - // 3. Update current data. - store.saveCurrent(currentRemote); + // 3. Update current data. + store.saveCurrent(currentRemote); // 4. Update location data. weatherLocation.setLastCurrentUIUpdate(new Date()); query.updateDataBase(weatherLocation); - } - - } else { - // Empty UI and show error message - CurrentFragment.this.getActivity().findViewById(R.id.weather_current_data_container).setVisibility(View.GONE); - CurrentFragment.this.getActivity().findViewById(R.id.weather_current_progressbar).setVisibility(View.GONE); - CurrentFragment.this.getActivity().findViewById(R.id.weather_current_error_message).setVisibility(View.VISIBLE); - } + } else { + // Empty UI and show error message + CurrentFragment.this.getActivity().findViewById(R.id.weather_current_data_container).setVisibility(View.GONE); + CurrentFragment.this.getActivity().findViewById(R.id.weather_current_progressbar).setVisibility(View.GONE); + CurrentFragment.this.getActivity().findViewById(R.id.weather_current_error_message).setVisibility(View.VISIBLE); + } + } } } }; @@ -412,6 +411,10 @@ public class CurrentFragment extends Fragment { } catch (final IOException e) { // logger infrastructure swallows UnknownHostException :/ Timber.e(e, "CurrentTask doInBackground exception: " + e.getMessage()); + } catch (final Throwable e) { + // I loathe doing this but we must show some error to our dear user by means + // of returning Forecast null value. + Timber.e(e, "CurrentTask doInBackground exception: "); } finally { HTTPClient.close(); } diff --git a/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java b/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java index 8424aba..852a0be 100644 --- a/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java +++ b/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java @@ -107,17 +107,17 @@ public class OverviewFragment extends ListFragment { if (action.equals(BROADCAST_INTENT_ACTION)) { final Forecast forecastRemote = (Forecast) intent.getSerializableExtra("forecast"); - if (forecastRemote != null) { + // 1. Check conditions. They must be the same as the ones that triggered the AsyncTask. + final DatabaseQueries query = new DatabaseQueries(context.getApplicationContext()); + final WeatherLocation weatherLocation = query.queryDataBase(); + final PermanentStorage store = new PermanentStorage(context.getApplicationContext()); + final Forecast forecast = store.getForecast(); - // 1. Check conditions. They must be the same as the ones that triggered the AsyncTask. - final DatabaseQueries query = new DatabaseQueries(context.getApplicationContext()); - final WeatherLocation weatherLocation = query.queryDataBase(); - final PermanentStorage store = new PermanentStorage(context.getApplicationContext()); - final Forecast forecast = store.getForecast(); + if (forecast == null || !OverviewFragment.this.isDataFresh(weatherLocation.getLastForecastUIUpdate())) { - if (forecast == null || !OverviewFragment.this.isDataFresh(weatherLocation.getLastForecastUIUpdate())) { - // 2. Update UI. - OverviewFragment.this.updateUI(forecastRemote); + if (forecastRemote != null) { + // 2. Update UI. + OverviewFragment.this.updateUI(forecastRemote); // 3. Update Data. store.saveForecast(forecastRemote); @@ -126,13 +126,12 @@ public class OverviewFragment extends ListFragment { // 4. Show list. OverviewFragment.this.setListShownNoAnimation(true); - } - - } else { - // Empty list and show error message (see setEmptyText in onCreate) - OverviewFragment.this.setListAdapter(null); - OverviewFragment.this.setListShownNoAnimation(true); - } + } else { + // Empty list and show error message (see setEmptyText in onCreate) + OverviewFragment.this.setListAdapter(null); + OverviewFragment.this.setListShownNoAnimation(true); + } + } } } }; @@ -339,6 +338,10 @@ public class OverviewFragment extends ListFragment { } catch (final IOException e) { // logger infrastructure swallows UnknownHostException :/ Timber.e(e, "OverviewTask doInBackground exception: " + e.getMessage()); + } catch (final Throwable e) { + // I loathe doing this but we must show some error to our dear user by means + // of returning Forecast null value. + Timber.e(e, "OverviewTask doInBackground exception: "); } finally { HTTPClient.close(); } -- 2.1.4