From ed5680536e73e6b58e1ef7e973f603757286c7fe Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Sat, 3 May 2014 20:48:11 +0200 Subject: [PATCH] Android weather information: background weather update. --- Android/WeatherInformation/AndroidManifest.xml | 19 +- Android/WeatherInformation/project.properties | 2 +- .../res/menu/weather_main_menu.xml | 12 - .../exampletdd/WeatherInformationBatch.java | 255 +++++++++++++++++++++ .../exampletdd/WeatherInformationMapActivity.java | 13 +- .../de/example/exampletdd/WeatherTabsActivity.java | 19 +- .../WeatherInformationCurrentDataFragment.java | 193 +++------------- .../WeatherInformationOverviewFragment.java | 187 +++------------ .../WeatherInformationSpecificDataFragment.java | 9 +- .../service/WeatherServicePersistenceFile.java | 23 +- 10 files changed, 384 insertions(+), 348 deletions(-) create mode 100644 Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationBatch.java diff --git a/Android/WeatherInformation/AndroidManifest.xml b/Android/WeatherInformation/AndroidManifest.xml index 21c1fc1..9b37dd1 100644 --- a/Android/WeatherInformation/AndroidManifest.xml +++ b/Android/WeatherInformation/AndroidManifest.xml @@ -24,7 +24,12 @@ - + + + + + + + android:parentActivityName="de.example.exampletdd.WeatherTabsActivity" + android:exported="false" > @@ -82,13 +88,20 @@ + android:parentActivityName="de.example.exampletdd.WeatherInformationActivity" + android:exported="false" > + + + - - { + private static final String TAG = "ServiceWeatherTask"; + private final CustomHTTPClient weatherHTTPClient; + private final WeatherServiceParser weatherService; + + private ServiceWeatherTask(final CustomHTTPClient weatherHTTPClient, + final WeatherServiceParser weatherService) { + this.weatherHTTPClient = weatherHTTPClient; + this.weatherService = weatherService; + } + + @Override + protected WeatherData doInBackground(final Object... params) { + WeatherData weatherData = null; + + Log.i(TAG, "ServiceWeatherTask Update Remote Data"); + try { + weatherData = this.doInBackgroundThrowable(params); + } catch (final ClientProtocolException e) { + Log.e(TAG, "doInBackground exception: ", e); + } catch (final MalformedURLException e) { + Log.e(TAG, "doInBackground exception: ", e); + } catch (final URISyntaxException e) { + Log.e(TAG, "doInBackground exception: ", e); + } catch (final JsonParseException e) { + Log.e(TAG, "doInBackground exception: ", e); + } catch (final IOException e) { + // logger infrastructure swallows UnknownHostException :/ + Log.e(TAG, "doInBackground exception: " + e.getMessage(), e); + } finally { + this.weatherHTTPClient.close(); + } + + return weatherData; + } + + @Override + protected void onPostExecute(final WeatherData weatherData) { + this.weatherHTTPClient.close(); + + if (weatherData != null) { + try { + this.onPostExecuteThrowable(weatherData); + } catch (final IOException e) { + Log.e(TAG, "onPostExecute exception: ", e); + } + } else { + Log.e(TAG, "onPostExecute WeatherData null value"); + } + } + + @Override + protected void onCancelled(final WeatherData weatherData) { + this.weatherHTTPClient.close(); + } + + private WeatherData doInBackgroundThrowable(final Object... params) + throws ClientProtocolException, MalformedURLException, URISyntaxException, + JsonParseException, IOException { + + // 1. Coordinates + final GeocodingData geocodingData = (GeocodingData) params[0]; + + final String APIVersion = WeatherInformationBatch.this.getResources().getString( + R.string.api_version); + + // 2. Today + String urlAPI = WeatherInformationBatch.this.getResources().getString( + R.string.uri_api_weather_today); + String url = this.weatherService.createURIAPITodayWeather(urlAPI, APIVersion, + geocodingData.getLatitude(), geocodingData.getLongitude()); + String jsonData = this.weatherHTTPClient.retrieveDataAsString(new URL(url)); + final CurrentWeatherData currentWeatherData = this.weatherService + .retrieveCurrentWeatherDataFromJPOS(jsonData); + final Calendar now = Calendar.getInstance(); + currentWeatherData.setDate(now.getTime()); + + // 3. Forecast + urlAPI = WeatherInformationBatch.this.getResources().getString( + R.string.uri_api_weather_forecast); + url = this.weatherService.createURIAPIForecastWeather(urlAPI, APIVersion, + geocodingData.getLatitude(), geocodingData.getLongitude(), resultsNumber); + jsonData = this.weatherHTTPClient.retrieveDataAsString(new URL(url)); + final ForecastWeatherData forecastWeatherData = this.weatherService + .retrieveForecastWeatherDataFromJPOS(jsonData); + + return new WeatherData(forecastWeatherData, currentWeatherData); + } + + private void onPostExecuteThrowable(final WeatherData weatherData) + throws FileNotFoundException, IOException { + + WeatherInformationBatch.this.mWeatherServicePersistenceFile + .storeCurrentWeatherData(weatherData.getCurrentWeatherData()); + WeatherInformationBatch.this.mWeatherServicePersistenceFile + .storeForecastWeatherData(weatherData.getForecastWeatherData()); + + // Update weather views. + final Intent updateCurrentWeather = new Intent( + "de.example.exampletdd.UPDATECURRENTWEATHER"); + WeatherInformationBatch.this.sendBroadcast(updateCurrentWeather); + final Intent updateOverviewWeather = new Intent( + "de.example.exampletdd.UPDATEOVERVIEWWEATHER"); + WeatherInformationBatch.this.sendBroadcast(updateOverviewWeather); + + } + } + + private class WeatherData { + private final ForecastWeatherData mForecastWeatherData; + private final CurrentWeatherData mCurrentWeatherData; + + public WeatherData(final ForecastWeatherData mForecastWeatherData, + final CurrentWeatherData mCurrentWeatherData) { + this.mForecastWeatherData = mForecastWeatherData; + this.mCurrentWeatherData = mCurrentWeatherData; + } + + public ForecastWeatherData getForecastWeatherData() { + return this.mForecastWeatherData; + } + + public CurrentWeatherData getCurrentWeatherData() { + return this.mCurrentWeatherData; + } + } +} diff --git a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationMapActivity.java b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationMapActivity.java index ae1bbd6..4290104 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationMapActivity.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationMapActivity.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Locale; import android.app.ActionBar; +import android.content.Intent; import android.location.Address; import android.location.Geocoder; import android.os.AsyncTask; @@ -79,6 +80,15 @@ public class WeatherInformationMapActivity extends FragmentActivity { actionBar.setTitle("Mark your location"); } + @Override + public void onPause() { + super.onPause(); + + final Intent updateOverviewWeather = new Intent( + "de.example.exampletdd.UPDATEGEOCODINGWEATHERBATCH"); + this.sendBroadcast(updateOverviewWeather); + } + private class LongClickListener implements OnMapLongClickListener { @Override @@ -150,8 +160,7 @@ public class WeatherInformationMapActivity extends FragmentActivity { private void onPostExecuteThrowable(final GeocodingData geocodingData) throws FileNotFoundException, IOException { - WeatherInformationMapActivity.this.mWeatherServicePersistenceFile - .storeGeocodingData(geocodingData); + WeatherInformationMapActivity.this.mWeatherServicePersistenceFile.storeGeocodingData(geocodingData); final String city = (geocodingData.getCity() == null) ? WeatherInformationMapActivity.this.getString(R.string.city_not_found) diff --git a/Android/WeatherInformation/src/de/example/exampletdd/WeatherTabsActivity.java b/Android/WeatherInformation/src/de/example/exampletdd/WeatherTabsActivity.java index 4c137bd..5c2126d 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/WeatherTabsActivity.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/WeatherTabsActivity.java @@ -15,7 +15,6 @@ import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.Menu; import android.view.MenuItem; -import de.example.exampletdd.activityinterface.GetWeather; import de.example.exampletdd.fragment.current.WeatherInformationCurrentDataFragment; import de.example.exampletdd.fragment.overview.WeatherInformationOverviewFragment; import de.example.exampletdd.model.GeocodingData; @@ -25,7 +24,6 @@ public class WeatherTabsActivity extends FragmentActivity { private static final int NUM_ITEMS = 2; private MyAdapter mAdapter; private ViewPager mPager; - private GetWeather mGetWeather; @Override protected void onCreate(final Bundle savedInstanceState) { @@ -79,6 +77,10 @@ public class WeatherTabsActivity extends FragmentActivity { actionBar.addTab(actionBar.newTab().setText("CURRENTLY").setTabListener(tabListener)); actionBar.addTab(actionBar.newTab().setText("FORECAST").setTabListener(tabListener)); + + final Intent intent = new Intent(this, WeatherInformationBatch.class); + intent.putExtra("UPDATE_RATE_TIME", 60); + this.startService(intent); } @Override @@ -104,9 +106,6 @@ public class WeatherTabsActivity extends FragmentActivity { "de.example.exampletdd.WeatherInformationPreferencesActivity")); this.startActivity(intent); return true; - } else if (itemId == R.id.weather_menu_get) { - this.getWeather(); - return true; } else if (itemId == R.id.weather_menu_map) { intent = new Intent("de.example.exampletdd.WEATHERINFO") .setComponent(new ComponentName("de.example.exampletdd", @@ -155,9 +154,10 @@ public class WeatherTabsActivity extends FragmentActivity { } - - public void getWeather() { - this.mGetWeather.getRemoteWeatherInformation(); + @Override + public void onDestroy() { + super.onDestroy(); + this.stopService(new Intent(this, WeatherInformationBatch.class)); } public class MyAdapter extends FragmentPagerAdapter { @@ -175,8 +175,7 @@ public class WeatherTabsActivity extends FragmentActivity { if (position == 0) { return new WeatherInformationCurrentDataFragment(); } else { - final WeatherInformationOverviewFragment fragment = new WeatherInformationOverviewFragment(); - WeatherTabsActivity.this.mGetWeather = fragment; + final Fragment fragment = new WeatherInformationOverviewFragment(); return fragment; } diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherInformationCurrentDataFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherInformationCurrentDataFragment.java index 0a04dc0..de396fd 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherInformationCurrentDataFragment.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherInformationCurrentDataFragment.java @@ -1,55 +1,66 @@ package de.example.exampletdd.fragment.current; -import java.io.FileNotFoundException; import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; -import java.util.Calendar; import java.util.Date; import java.util.Locale; -import org.apache.http.client.ClientProtocolException; - +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.net.http.AndroidHttpClient; -import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.DialogFragment; import android.support.v4.app.ListFragment; import android.util.Log; import android.widget.ListView; - -import com.fasterxml.jackson.core.JsonParseException; - import de.example.exampletdd.R; import de.example.exampletdd.fragment.ErrorDialogFragment; -import de.example.exampletdd.fragment.ProgressDialogFragment; import de.example.exampletdd.fragment.overview.IconsList; -import de.example.exampletdd.httpclient.CustomHTTPClient; -import de.example.exampletdd.model.GeocodingData; import de.example.exampletdd.model.currentweather.CurrentWeatherData; -import de.example.exampletdd.parser.IJPOSWeatherParser; -import de.example.exampletdd.parser.JPOSWeatherParser; -import de.example.exampletdd.service.WeatherServiceParser; import de.example.exampletdd.service.WeatherServicePersistenceFile; public class WeatherInformationCurrentDataFragment extends ListFragment { + private static final String TAG = "WeatherInformationCurrentDataFragment"; private boolean mIsFahrenheit; private WeatherServicePersistenceFile mWeatherServicePersistenceFile; + private BroadcastReceiver mReceiver; @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this.getActivity()); - this.mWeatherServicePersistenceFile.removeCurrentWeatherData(); + + this.mReceiver = new BroadcastReceiver() { + + @Override + public void onReceive(final Context context, final Intent intent) { + // This method will be run in the main thread. + final String action = intent.getAction(); + if (action.equals("de.example.exampletdd.UPDATECURRENTWEATHER")) { + Log.i(TAG, "WeatherInformationCurrentDataFragment Update Weather Data"); + final CurrentWeatherData currentWeatherData = + WeatherInformationCurrentDataFragment.this.mWeatherServicePersistenceFile + .getCurrentWeatherData(); + if (currentWeatherData != null) { + WeatherInformationCurrentDataFragment.this + .updateCurrentWeatherData(currentWeatherData); + } + + } + } + }; + + final IntentFilter filter = new IntentFilter(); + filter.addAction("de.example.exampletdd.UPDATECURRENTWEATHER"); + this.getActivity().registerReceiver(this.mReceiver, filter); } @Override @@ -106,9 +117,6 @@ public class WeatherInformationCurrentDataFragment extends ListFragment { .getCurrentWeatherData(); if (currentWeatherData != null) { this.updateCurrentWeatherData(currentWeatherData); - } else { - // 3. Try to update weather data on display with remote - this.getRemoteCurrentWeatherInformation(); } } @@ -126,6 +134,11 @@ public class WeatherInformationCurrentDataFragment extends ListFragment { super.onSaveInstanceState(savedInstanceState); } + @Override + public void onDestroy() { + super.onDestroy(); + this.getActivity().unregisterReceiver(this.mReceiver); + } public void updateCurrentWeatherData(final CurrentWeatherData currentWeatherData) { final DecimalFormat tempFormatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US); @@ -240,140 +253,4 @@ public class WeatherInformationCurrentDataFragment extends ListFragment { this.setListAdapter(adapter); } - - public class CurrentWeatherTask extends AsyncTask { - private static final String TAG = "WeatherTask"; - private final CustomHTTPClient weatherHTTPClient; - private final WeatherServiceParser weatherService; - private final DialogFragment newFragment; - - public CurrentWeatherTask(final CustomHTTPClient weatherHTTPClient, - final WeatherServiceParser weatherService) { - this.weatherHTTPClient = weatherHTTPClient; - this.weatherService = weatherService; - this.newFragment = ProgressDialogFragment.newInstance( - R.string.progress_dialog_get_remote_data, - WeatherInformationCurrentDataFragment.this - .getString(R.string.progress_dialog_generic_message)); - } - - @Override - protected void onPreExecute() { - this.newFragment.show(WeatherInformationCurrentDataFragment.this.getFragmentManager(), - "progressDialog"); - } - - @Override - protected CurrentWeatherData doInBackground(final Object... params) { - CurrentWeatherData currentWeatherData = null; - - try { - currentWeatherData = this.doInBackgroundThrowable(params); - } catch (final ClientProtocolException e) { - Log.e(TAG, "doInBackground exception: ", e); - } catch (final MalformedURLException e) { - Log.e(TAG, "doInBackground exception: ", e); - } catch (final URISyntaxException e) { - Log.e(TAG, "doInBackground exception: ", e); - } catch (final JsonParseException e) { - Log.e(TAG, "doInBackground exception: ", e); - } catch (final IOException e) { - // logger infrastructure swallows UnknownHostException :/ - Log.e(TAG, "doInBackground exception: " + e.getMessage(), e); - } finally { - this.weatherHTTPClient.close(); - } - - return currentWeatherData; - } - - @Override - protected void onPostExecute(final CurrentWeatherData currentWeatherData) { - this.weatherHTTPClient.close(); - - this.newFragment.dismiss(); - - if (currentWeatherData != null) { - try { - this.onPostExecuteThrowable(currentWeatherData); - } catch (final IOException e) { - WeatherInformationCurrentDataFragment.this.setListShown(true); - Log.e(TAG, "WeatherTask onPostExecute exception: ", e); - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_generic_error); - newFragment.show( - WeatherInformationCurrentDataFragment.this.getFragmentManager(), - "errorDialog"); - } - } else { - WeatherInformationCurrentDataFragment.this.setListShown(true); - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_generic_error); - newFragment.show(WeatherInformationCurrentDataFragment.this.getFragmentManager(), - "errorDialog"); - } - } - - @Override - protected void onCancelled(final CurrentWeatherData currentWeatherData) { - this.weatherHTTPClient.close(); - - final DialogFragment newFragment = ErrorDialogFragment - .newInstance(R.string.error_dialog_connection_tiemout); - newFragment.show(WeatherInformationCurrentDataFragment.this.getFragmentManager(), - "errorDialog"); - } - - private CurrentWeatherData doInBackgroundThrowable(final Object... params) - throws ClientProtocolException, MalformedURLException, URISyntaxException, - JsonParseException, IOException { - - // 1. Coordinates - final GeocodingData geocodingData = (GeocodingData) params[0]; - - final String APIVersion = WeatherInformationCurrentDataFragment.this.getResources() - .getString(R.string.api_version); - - // 2. Today - final String urlAPI = WeatherInformationCurrentDataFragment.this.getResources() - .getString(R.string.uri_api_weather_today); - final String url = this.weatherService.createURIAPITodayWeather(urlAPI, APIVersion, - geocodingData.getLatitude(), geocodingData.getLongitude()); - final String jsonData = this.weatherHTTPClient.retrieveDataAsString(new URL(url)); - final CurrentWeatherData currentWeatherData = this.weatherService - .retrieveCurrentWeatherDataFromJPOS(jsonData); - final Calendar now = Calendar.getInstance(); - currentWeatherData.setDate(now.getTime()); - - - return currentWeatherData; - } - - private void onPostExecuteThrowable(final CurrentWeatherData currentWeatherData) - throws FileNotFoundException, IOException { - - WeatherInformationCurrentDataFragment.this.mWeatherServicePersistenceFile - .storeCurrentWeatherData(currentWeatherData); - - WeatherInformationCurrentDataFragment.this.updateCurrentWeatherData(currentWeatherData); - } - } - - private void getRemoteCurrentWeatherInformation() { - - final GeocodingData geocodingData = this.mWeatherServicePersistenceFile.getGeocodingData(); - - if (geocodingData != null) { - final IJPOSWeatherParser JPOSWeatherParser = new JPOSWeatherParser(); - final WeatherServiceParser weatherService = new WeatherServiceParser(JPOSWeatherParser); - final AndroidHttpClient httpClient = AndroidHttpClient - .newInstance("Android Weather Information Agent"); - final CustomHTTPClient HTTPweatherClient = new CustomHTTPClient(httpClient); - - final CurrentWeatherTask weatherTask = new CurrentWeatherTask(HTTPweatherClient, - weatherService); - - weatherTask.execute(geocodingData); - } - } } diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java index 9256c61..c646255 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java @@ -1,10 +1,6 @@ package de.example.exampletdd.fragment.overview; -import java.io.FileNotFoundException; import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.SimpleDateFormat; @@ -14,15 +10,14 @@ import java.util.Date; import java.util.List; import java.util.Locale; -import org.apache.http.client.ClientProtocolException; - +import android.content.BroadcastReceiver; import android.content.ComponentName; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.net.http.AndroidHttpClient; -import android.os.AsyncTask; import android.os.Bundle; import android.os.Parcelable; import android.preference.PreferenceManager; @@ -31,27 +26,19 @@ import android.support.v4.app.ListFragment; import android.util.Log; import android.view.View; import android.widget.ListView; - -import com.fasterxml.jackson.core.JsonParseException; - import de.example.exampletdd.R; -import de.example.exampletdd.activityinterface.GetWeather; import de.example.exampletdd.fragment.ErrorDialogFragment; -import de.example.exampletdd.fragment.ProgressDialogFragment; import de.example.exampletdd.fragment.specific.WeatherInformationSpecificDataFragment; -import de.example.exampletdd.httpclient.CustomHTTPClient; -import de.example.exampletdd.model.GeocodingData; import de.example.exampletdd.model.forecastweather.ForecastWeatherData; -import de.example.exampletdd.parser.IJPOSWeatherParser; -import de.example.exampletdd.parser.JPOSWeatherParser; -import de.example.exampletdd.service.WeatherServiceParser; import de.example.exampletdd.service.WeatherServicePersistenceFile; -public class WeatherInformationOverviewFragment extends ListFragment implements GetWeather { +public class WeatherInformationOverviewFragment extends ListFragment { + private static final String TAG = "WeatherInformationOverviewFragment"; private boolean mIsFahrenheit; private String mDayForecast; private WeatherServicePersistenceFile mWeatherServicePersistenceFile; private Parcelable mListState; + private BroadcastReceiver mReceiver; @Override public void onCreate(final Bundle savedInstanceState) { @@ -64,7 +51,30 @@ public class WeatherInformationOverviewFragment extends ListFragment implements this.mDayForecast = sharedPreferences.getString(keyPreference, ""); this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this.getActivity()); - this.mWeatherServicePersistenceFile.removeForecastWeatherData(); + + this.mReceiver = new BroadcastReceiver() { + + @Override + public void onReceive(final Context context, final Intent intent) { + // This method will be run in the main thread. + final String action = intent.getAction(); + if (action.equals("de.example.exampletdd.UPDATEOVERVIEWWEATHER")) { + Log.i(TAG, "WeatherInformationOverviewFragment Update Weather Data"); + final ForecastWeatherData forecastWeatherData = + WeatherInformationOverviewFragment.this.mWeatherServicePersistenceFile + .getForecastWeatherData(); + if (forecastWeatherData != null) { + WeatherInformationOverviewFragment.this + .updateForecastWeatherData(forecastWeatherData); + } + + } + } + }; + + final IntentFilter filter = new IntentFilter(); + filter.addAction("de.example.exampletdd.UPDATEOVERVIEWWEATHER"); + this.getActivity().registerReceiver(this.mReceiver, filter); } @Override @@ -141,33 +151,12 @@ public class WeatherInformationOverviewFragment extends ListFragment implements } @Override - public void getRemoteWeatherInformation() { - - final GeocodingData geocodingData = this.mWeatherServicePersistenceFile.getGeocodingData(); - - if (geocodingData != null) { - final IJPOSWeatherParser JPOSWeatherParser = new JPOSWeatherParser(); - final WeatherServiceParser weatherService = new WeatherServiceParser( - JPOSWeatherParser); - final AndroidHttpClient httpClient = AndroidHttpClient - .newInstance("Android Weather Information Agent"); - final CustomHTTPClient HTTPweatherClient = new CustomHTTPClient( - httpClient); - - final ForecastWeatherTask weatherTask = new ForecastWeatherTask(HTTPweatherClient, - weatherService); - - - weatherTask.execute(geocodingData); - } - } - - @Override - public void getWeatherByDay(final int chosenDay) { - // Nothing to do. + public void onDestroy() { + super.onDestroy(); + this.getActivity().unregisterReceiver(this.mReceiver); } - public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData) { + private void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData) { final List entries = new ArrayList(); final WeatherOverviewAdapter adapter = new WeatherOverviewAdapter(this.getActivity(), R.layout.weather_main_entry_list); @@ -264,112 +253,4 @@ public class WeatherInformationOverviewFragment extends ListFragment implements } } - - public class ForecastWeatherTask extends AsyncTask { - private static final String TAG = "ForecastWeatherTask"; - private final CustomHTTPClient weatherHTTPClient; - private final WeatherServiceParser weatherService; - private final DialogFragment newFragment; - - public ForecastWeatherTask(final CustomHTTPClient weatherHTTPClient, - final WeatherServiceParser weatherService) { - this.weatherHTTPClient = weatherHTTPClient; - this.weatherService = weatherService; - this.newFragment = ProgressDialogFragment.newInstance( - R.string.progress_dialog_get_remote_data, - WeatherInformationOverviewFragment.this - .getString(R.string.progress_dialog_generic_message)); - } - - @Override - protected void onPreExecute() { - this.newFragment.show(WeatherInformationOverviewFragment.this.getFragmentManager(), - "progressDialog"); - } - - @Override - protected ForecastWeatherData doInBackground(final Object... params) { - ForecastWeatherData forecastWeatherData = null; - - try { - forecastWeatherData = this.doInBackgroundThrowable(params); - } catch (final ClientProtocolException e) { - Log.e(TAG, "doInBackground exception: ", e); - } catch (final MalformedURLException e) { - Log.e(TAG, "doInBackground exception: ", e); - } catch (final URISyntaxException e) { - Log.e(TAG, "doInBackground exception: ", e); - } catch (final JsonParseException e) { - Log.e(TAG, "doInBackground exception: ", e); - } catch (final IOException e) { - // logger infrastructure swallows UnknownHostException :/ - Log.e(TAG, "doInBackground exception: " + e.getMessage(), e); - } finally { - this.weatherHTTPClient.close(); - } - - return forecastWeatherData; - } - - @Override - protected void onPostExecute(final ForecastWeatherData 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); - // final DialogFragment newFragment = ErrorDialogFragment - // .newInstance(R.string.error_dialog_generic_error); - // newFragment.show(WeatherInformationOverviewFragment.this.getFragmentManager(), "errorDialog"); - } - } else { - // final DialogFragment newFragment = ErrorDialogFragment - // .newInstance(R.string.error_dialog_generic_error); - // newFragment.show(WeatherInformationOverviewFragment.this.getFragmentManager(), "errorDialog"); - } - } - - @Override - protected void onCancelled(final ForecastWeatherData weatherData) { - this.weatherHTTPClient.close(); - - // final DialogFragment newFragment = ErrorDialogFragment - // .newInstance(R.string.error_dialog_connection_tiemout); - // newFragment.show(WeatherInformationOverviewFragment.this.getFragmentManager(), "errorDialog"); - } - - private ForecastWeatherData doInBackgroundThrowable(final Object... params) - throws ClientProtocolException, MalformedURLException, - URISyntaxException, JsonParseException, IOException { - - // 1. Coordinates - final GeocodingData geocodingData = (GeocodingData) params[0]; - - - final String APIVersion = WeatherInformationOverviewFragment.this.getResources() - .getString(R.string.api_version); - // 2. Forecast - final String urlAPI = WeatherInformationOverviewFragment.this.getResources() - .getString(R.string.uri_api_weather_forecast); - final String url = this.weatherService.createURIAPIForecastWeather(urlAPI, APIVersion, - geocodingData.getLatitude(), geocodingData.getLongitude(), WeatherInformationOverviewFragment.this.mDayForecast); - final String jsonData = this.weatherHTTPClient.retrieveDataAsString(new URL(url)); - final ForecastWeatherData forecastWeatherData = this.weatherService - .retrieveForecastWeatherDataFromJPOS(jsonData); - - return forecastWeatherData; - } - - private void onPostExecuteThrowable(final ForecastWeatherData forecastWeatherData) - throws FileNotFoundException, IOException { - WeatherInformationOverviewFragment.this.mWeatherServicePersistenceFile - .storeForecastWeatherData(forecastWeatherData); - - WeatherInformationOverviewFragment.this.updateForecastWeatherData(forecastWeatherData); - } - } } diff --git a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java index 85b4d1e..57d8589 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java @@ -17,13 +17,12 @@ import android.support.v4.app.DialogFragment; import android.support.v4.app.ListFragment; import android.widget.ListView; import de.example.exampletdd.R; -import de.example.exampletdd.activityinterface.GetWeather; import de.example.exampletdd.fragment.ErrorDialogFragment; import de.example.exampletdd.fragment.overview.IconsList; import de.example.exampletdd.model.forecastweather.ForecastWeatherData; import de.example.exampletdd.service.WeatherServicePersistenceFile; -public class WeatherInformationSpecificDataFragment extends ListFragment implements GetWeather { +public class WeatherInformationSpecificDataFragment extends ListFragment { private boolean mIsFahrenheit; private int mChosenDay; private WeatherServicePersistenceFile mWeatherServicePersistenceFile; @@ -91,7 +90,6 @@ public class WeatherInformationSpecificDataFragment extends ListFragment impleme super.onSaveInstanceState(savedInstanceState); } - @Override public void getWeatherByDay(final int chosenDay) { final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile @@ -102,11 +100,6 @@ public class WeatherInformationSpecificDataFragment extends ListFragment impleme } - @Override - public void getRemoteWeatherInformation() { - // Nothing to do. - } - public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData, final int chosenDay) { diff --git a/Android/WeatherInformation/src/de/example/exampletdd/service/WeatherServicePersistenceFile.java b/Android/WeatherInformation/src/de/example/exampletdd/service/WeatherServicePersistenceFile.java index fded6e7..7d9c8c3 100644 --- a/Android/WeatherInformation/src/de/example/exampletdd/service/WeatherServicePersistenceFile.java +++ b/Android/WeatherInformation/src/de/example/exampletdd/service/WeatherServicePersistenceFile.java @@ -1,5 +1,6 @@ package de.example.exampletdd.service; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -14,11 +15,18 @@ import de.example.exampletdd.model.GeocodingData; import de.example.exampletdd.model.currentweather.CurrentWeatherData; import de.example.exampletdd.model.forecastweather.ForecastWeatherData; +/** + * TODO: show some error message when there is no enough space for saving files. :/ + * + */ public class WeatherServicePersistenceFile { private static final String TAG = "WeatherServicePersistenceFile"; private static final String CURRENT_WEATHER_DATA_FILE = "current_weatherdata.file"; + private static final String CURRENT_WEATHER_DATA_TEMPORARY_FILE = "current_weatherdata.tmp.file"; private static final String FORECAST_WEATHER_DATA_FILE = "forecast_weatherdata.file"; + private static final String FORECAST_WEATHER_DATA_TEMPORARY_FILE = "forecast_weatherdata.tmp.file"; private static final String WEATHER_GEOCODING_FILE = "weathergeocoding.file"; + private static final String WEATHER_GEOCODING_TEMPORARY_FILE = "weathergeocoding.tmp.file"; private final Context context; public WeatherServicePersistenceFile(final Context context) { @@ -28,7 +36,7 @@ public class WeatherServicePersistenceFile { public void storeGeocodingData(final GeocodingData geocodingData) throws FileNotFoundException, IOException { final OutputStream persistenceFile = this.context.openFileOutput( - WEATHER_GEOCODING_FILE, Context.MODE_PRIVATE); + WEATHER_GEOCODING_TEMPORARY_FILE, Context.MODE_PRIVATE); ObjectOutputStream oos = null; try { @@ -40,6 +48,8 @@ public class WeatherServicePersistenceFile { oos.close(); } } + + this.renameFile(WEATHER_GEOCODING_TEMPORARY_FILE, WEATHER_GEOCODING_FILE); } public GeocodingData getGeocodingData() { @@ -97,6 +107,8 @@ public class WeatherServicePersistenceFile { oos.close(); } } + + this.renameFile(CURRENT_WEATHER_DATA_TEMPORARY_FILE, CURRENT_WEATHER_DATA_FILE); } public CurrentWeatherData getCurrentWeatherData() { @@ -154,6 +166,8 @@ public class WeatherServicePersistenceFile { oos.close(); } } + + this.renameFile(FORECAST_WEATHER_DATA_TEMPORARY_FILE, FORECAST_WEATHER_DATA_FILE); } public ForecastWeatherData getForecastWeatherData() { @@ -193,4 +207,11 @@ public class WeatherServicePersistenceFile { public void removeForecastWeatherData() { this.context.deleteFile(FORECAST_WEATHER_DATA_FILE); } + + private void renameFile(final String temporaryName, final String finalName) { + final File filesDir = this.context.getFilesDir(); + final File temporaryFile = new File(filesDir, temporaryName); + final File endFile = new File(filesDir, finalName); + temporaryFile.renameTo(endFile); + } } -- 2.1.4