import org.apache.http.client.ClientProtocolException;
-import android.app.Service;
+import android.app.IntentService;
import android.content.Intent;
import android.net.http.AndroidHttpClient;
-import android.os.AsyncTask;
-import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import de.example.exampletdd.model.GeocodingData;
import de.example.exampletdd.model.currentweather.CurrentWeatherData;
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 WeatherInformationBatch extends Service {
+public class WeatherInformationBatch extends IntentService {
private static final String TAG = "WeatherInformationBatch";
private static final String resultsNumber = "14";
private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
- @Override
- public IBinder onBind(final Intent intent) {
- Log.i(TAG, "WeatherInformationBatch onBind");
- return null;
+
+ public WeatherInformationBatch() {
+ super("WeatherInformationBatch");
}
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
Log.i(TAG, "WeatherInformationBatch onStartCommand");
-
this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this);
- this.updateWeather();
-
return super.onStartCommand(intent, flags, startId);
}
@Override
- public void onDestroy() {
- super.onDestroy();
- Log.i(TAG, "WeatherInformationBatch onDestroy");
- }
+ protected void onHandleIntent(final Intent intent) {
- private void updateWeather() {
final GeocodingData geocodingData = this.mWeatherServicePersistenceFile.getGeocodingData();
-
- Log.i(TAG, "WeatherInformationBatch updateWeather");
+ Log.i(TAG, "WeatherInformationBatch onHandleIntent");
if (geocodingData != null) {
- Log.i(TAG, "WeatherInformationBatch updateWeather, geocodingData not 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 ServiceWeatherTask weatherTask = new ServiceWeatherTask(HTTPweatherClient,
- weatherService);
-
- weatherTask.execute(geocodingData);
- }
- }
-
- private class ServiceWeatherTask extends AsyncTask<Object, Void, WeatherData> {
- 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, "WeatherInformationBatch onHandleIntent, geocodingData not null");
+ final WeatherServiceParser weatherService = new WeatherServiceParser(new JPOSWeatherParser());
+ final CustomHTTPClient weatherHTTPClient = new CustomHTTPClient(
+ AndroidHttpClient.newInstance("Android Weather Information Agent"));
- Log.i(TAG, "ServiceWeatherTask Update Remote Data");
try {
- weatherData = this.doInBackgroundThrowable(params);
+ final WeatherData weatherData = this.doInBackgroundThrowable(geocodingData,
+ weatherHTTPClient, weatherService);
+ this.onPostExecuteThrowable(weatherData);
+ } catch (final JsonParseException e) {
+ Log.e(TAG, "doInBackground exception: ", e);
} 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");
+ weatherHTTPClient.close();
}
}
+ }
- @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 WeatherData doInBackgroundThrowable(final GeocodingData geocodingData,
+ final CustomHTTPClient weatherHTTPClient, final WeatherServiceParser weatherService)
+ throws ClientProtocolException, MalformedURLException, URISyntaxException,
+ JsonParseException, IOException {
+
+ final String APIVersion = WeatherInformationBatch.this.getResources().getString(
+ R.string.api_version);
+
+ // 1. Today
+ String urlAPI = WeatherInformationBatch.this.getResources().getString(
+ R.string.uri_api_weather_today);
+ String url = weatherService.createURIAPITodayWeather(urlAPI, APIVersion,
+ geocodingData.getLatitude(), geocodingData.getLongitude());
+ String jsonData = weatherHTTPClient.retrieveDataAsString(new URL(url));
+ final CurrentWeatherData currentWeatherData = weatherService
+ .retrieveCurrentWeatherDataFromJPOS(jsonData);
+ final Calendar now = Calendar.getInstance();
+ currentWeatherData.setDate(now.getTime());
+
+ // 2. Forecast
+ urlAPI = WeatherInformationBatch.this.getResources().getString(
+ R.string.uri_api_weather_forecast);
+ url = weatherService.createURIAPIForecastWeather(urlAPI, APIVersion,
+ geocodingData.getLatitude(), geocodingData.getLongitude(), resultsNumber);
+ jsonData = weatherHTTPClient.retrieveDataAsString(new URL(url));
+ final ForecastWeatherData forecastWeatherData = weatherService
+ .retrieveForecastWeatherDataFromJPOS(jsonData);
+
+ return new WeatherData(forecastWeatherData, currentWeatherData);
+ }
- private void onPostExecuteThrowable(final WeatherData weatherData)
- throws FileNotFoundException, IOException {
+ private void onPostExecuteThrowable(final WeatherData weatherData)
+ throws FileNotFoundException, IOException {
- WeatherInformationBatch.this.mWeatherServicePersistenceFile
- .storeCurrentWeatherData(weatherData.getCurrentWeatherData());
- WeatherInformationBatch.this.mWeatherServicePersistenceFile
- .storeForecastWeatherData(weatherData.getForecastWeatherData());
+ WeatherInformationBatch.this.mWeatherServicePersistenceFile
+ .storeCurrentWeatherData(weatherData.getCurrentWeatherData());
+ WeatherInformationBatch.this.mWeatherServicePersistenceFile
+ .storeForecastWeatherData(weatherData.getForecastWeatherData());
- // Update weather views.
- final Intent updateCurrentWeather = new Intent(
- "de.example.exampletdd.UPDATECURRENTWEATHER");
- LocalBroadcastManager.getInstance(WeatherInformationBatch.this).sendBroadcast(
- updateCurrentWeather);
- final Intent updateOverviewWeather = new Intent(
- "de.example.exampletdd.UPDATEOVERVIEWWEATHER");
- LocalBroadcastManager.getInstance(WeatherInformationBatch.this).sendBroadcast(
- updateOverviewWeather);
+ // Update weather views.
+ final Intent updateCurrentWeather = new Intent(
+ "de.example.exampletdd.UPDATECURRENTWEATHER");
+ LocalBroadcastManager.getInstance(WeatherInformationBatch.this).sendBroadcast(
+ updateCurrentWeather);
+ final Intent updateOverviewWeather = new Intent(
+ "de.example.exampletdd.UPDATEOVERVIEWWEATHER");
+ LocalBroadcastManager.getInstance(WeatherInformationBatch.this).sendBroadcast(
+ updateOverviewWeather);
- }
}
private class WeatherData {
import android.app.ActionBar;
import android.app.ActionBar.Tab;
-import android.app.AlarmManager;
import android.app.FragmentTransaction;
-import android.app.PendingIntent;
import android.content.ComponentName;
-import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
-import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
-import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import de.example.exampletdd.fragment.current.WeatherInformationCurrentDataFragment;
private MyAdapter mAdapter;
private ViewPager mPager;
private GeocodingData mGeocodingData;
- private int mUpdateTimeRate;
+ private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
@Override
protected void onCreate(final Bundle savedInstanceState) {
actionBar.addTab(actionBar.newTab().setText("CURRENTLY").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("FORECAST").setTabListener(tabListener));
+ // this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this);
+ // if (savedInstanceState != null) {
+ // this.mGeocodingData = (GeocodingData) savedInstanceState
+ // .getSerializable("GEOCODINGDATA");
+ // final GeocodingData lastValue = this.mWeatherServicePersistenceFile.getGeocodingData();
+ // } else {
+ // this.mGeocodingData = this.mWeatherServicePersistenceFile.getGeocodingData();
+ // final Intent intent = new Intent(this, WeatherInformationBatch.class);
+ // if (this.mGeocodingData != null) {
+ // this.startService(intent);
+ // }
+ // }
- final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
- final String keyPreference = this.getString(R.string.weather_preferences_update_time_rate_key);
- final String updateTimeRate = sharedPreferences.getString(keyPreference, "");
- final int timeRate = Integer.valueOf(updateTimeRate);
- Log.i(TAG, "WeatherTabsActivity onCreate, timeRate: " + timeRate);
- final AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
+
+ // final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
+ // final String keyPreference = this.getString(R.string.weather_preferences_update_time_rate_key);
+ // final String updateTimeRate = sharedPreferences.getString(keyPreference, "");
+ // final int timeRate = Integer.valueOf(updateTimeRate);
+ // Log.i(TAG, "WeatherTabsActivity onCreate, timeRate: " + timeRate);
+
+ // final AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
// TODO: better use some string instead of .class? In case I change the service class
// this could be a problem (I guess)
- final Intent intent = new Intent(this, WeatherInformationBatch.class);
- final PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent,
- PendingIntent.FLAG_UPDATE_CURRENT);
- alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()
- + (timeRate * 1000), (timeRate * 1000), alarmIntent);
+ // final Intent intent = new Intent(this, WeatherInformationBatch.class);
+ // final PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent,
+ // PendingIntent.FLAG_UPDATE_CURRENT);
+ // alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()
+ // + (timeRate * 1000), (timeRate * 1000), alarmIntent);
}
@Override
if (savedInstanceState != null) {
this.mGeocodingData = (GeocodingData) savedInstanceState
.getSerializable("GEOCODINGDATA");
- this.mUpdateTimeRate = savedInstanceState.getInt("UPDATE_TIME_RATE");
}
super.onRestoreInstanceState(savedInstanceState);
final SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
- String keyPreference = this.getString(R.string.weather_preferences_day_forecast_key);
+ final String keyPreference = this.getString(R.string.weather_preferences_day_forecast_key);
final String value = sharedPreferences.getString(keyPreference, "");
String humanValue = "";
if (value.equals("5")) {
}
actionBar.getTabAt(1).setText(humanValue);
-
-
- if (geocodingData != null) {
- if ((this.mGeocodingData == null) || (!this.mGeocodingData.equals(geocodingData))) {
- Log.i(TAG, "WeatherTabsActivity onResume, startService");
- this.mGeocodingData = geocodingData;
- final Intent intent = new Intent(this, WeatherInformationBatch.class);
- this.startService(intent);
- }
- }
-
- keyPreference = this.getString(R.string.weather_preferences_update_time_rate_key);
- final String updateTimeRate = sharedPreferences.getString(keyPreference, "");
- final int timeRate = Integer.valueOf(updateTimeRate);
- if (this.mUpdateTimeRate != timeRate) {
- Log.i(TAG, "WeatherTabsActivity onResume, updateTimeRate: " + timeRate);
- this.mUpdateTimeRate = timeRate;
- final AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
- // TODO: better use some string instead of .class? In case I change the service class
- // this could be a problem (I guess)
- final Intent intent = new Intent(this, WeatherInformationBatch.class);
- final PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent,
- PendingIntent.FLAG_UPDATE_CURRENT);
- alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()
- + (timeRate * 1000), (timeRate * 1000), alarmIntent);
- }
+ final Intent intent = new Intent(this, WeatherInformationBatch.class);
+ this.startService(intent);
+
+ // if (geocodingData != null) {
+ // if ((this.mGeocodingData == null) || (!this.mGeocodingData.equals(geocodingData))) {
+ // Log.i(TAG, "WeatherTabsActivity onResume, startService");
+ // this.mGeocodingData = geocodingData;
+ // final Intent intent = new Intent(this, WeatherInformationBatch.class);
+ // this.startService(intent);
+ // }
+ // }
+ //
+ // keyPreference = this.getString(R.string.weather_preferences_update_time_rate_key);
+ // final String updateTimeRate = sharedPreferences.getString(keyPreference, "");
+ // final int timeRate = Integer.valueOf(updateTimeRate);
+ // if (this.mUpdateTimeRate != timeRate) {
+ // Log.i(TAG, "WeatherTabsActivity onResume, updateTimeRate: " + timeRate);
+ // this.mUpdateTimeRate = timeRate;
+ // final AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
+ // // TODO: better use some string instead of .class? In case I change the service class
+ // // this could be a problem (I guess)
+ // final Intent intent = new Intent(this, WeatherInformationBatch.class);
+ // final PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent,
+ // PendingIntent.FLAG_UPDATE_CURRENT);
+ // alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()
+ // + (timeRate * 1000), (timeRate * 1000), alarmIntent);
+ // }
}
@Override
public void onSaveInstanceState(final Bundle savedInstanceState) {
savedInstanceState.putSerializable("GEOCODINGDATA", this.mGeocodingData);
- savedInstanceState.putInt("UPDATE_TIME_RATE", this.mUpdateTimeRate);
super.onSaveInstanceState(savedInstanceState);
}
- @Override
- public void onStop() {
- super.onStop();
- Log.i(TAG, "WeatherTabsActivity onStop");
- this.stopService(new Intent(this, WeatherInformationBatch.class));
- }
-
private class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(final FragmentManager fm) {
super(fm);