From: gu.martinm@gmail.com Date: Mon, 5 May 2014 19:51:47 +0000 (+0200) Subject: No time for comments X-Git-Tag: weatherinformation-1.0~162 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=cb0983b3d4a3be5ff3a97bcbe6ac5470181ef16e;p=AndroidWeatherInformation No time for comments --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 134f903..1fb26ff 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -95,7 +95,7 @@ + android:enabled="false"> @@ -103,6 +103,7 @@ diff --git a/libs/jackson-core-2.2.3.jar b/libs/jackson-core-2.2.3.jar new file mode 100644 index 0000000..24318a4 Binary files /dev/null and b/libs/jackson-core-2.2.3.jar differ diff --git a/project.properties b/project.properties index 7612c3e..770d261 100644 --- a/project.properties +++ b/project.properties @@ -12,5 +12,5 @@ # Project target. target=android-18 -android.library.reference.1=../../../../android/android-sdk-linux/extras/google/google_play_services/libproject/google-play-services_lib +android.library.reference.1=../../../../../ALLGEMEINS/ALLGEMEIN/Android/android-sdk-linux/extras/google/google_play_services/libproject/google-play-services_lib android.library=false diff --git a/src/de/example/exampletdd/WeatherInformationBatch.java b/src/de/example/exampletdd/WeatherInformationBatch.java index b7d8f58..3ec9f55 100644 --- a/src/de/example/exampletdd/WeatherInformationBatch.java +++ b/src/de/example/exampletdd/WeatherInformationBatch.java @@ -9,11 +9,9 @@ import java.util.Calendar; 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; @@ -23,167 +21,110 @@ import de.example.exampletdd.httpclient.CustomHTTPClient; 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 { - 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 { diff --git a/src/de/example/exampletdd/WeatherTabsActivity.java b/src/de/example/exampletdd/WeatherTabsActivity.java index 3bffec4..d8abcbb 100644 --- a/src/de/example/exampletdd/WeatherTabsActivity.java +++ b/src/de/example/exampletdd/WeatherTabsActivity.java @@ -2,22 +2,17 @@ package de.example.exampletdd; 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; @@ -31,7 +26,7 @@ public class WeatherTabsActivity extends FragmentActivity { private MyAdapter mAdapter; private ViewPager mPager; private GeocodingData mGeocodingData; - private int mUpdateTimeRate; + private WeatherServicePersistenceFile mWeatherServicePersistenceFile; @Override protected void onCreate(final Bundle savedInstanceState) { @@ -86,21 +81,35 @@ public class WeatherTabsActivity extends FragmentActivity { 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 @@ -143,7 +152,6 @@ public class WeatherTabsActivity extends FragmentActivity { if (savedInstanceState != null) { this.mGeocodingData = (GeocodingData) savedInstanceState .getSerializable("GEOCODINGDATA"); - this.mUpdateTimeRate = savedInstanceState.getInt("UPDATE_TIME_RATE"); } super.onRestoreInstanceState(savedInstanceState); @@ -170,7 +178,7 @@ public class WeatherTabsActivity extends FragmentActivity { 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")) { @@ -182,49 +190,42 @@ public class WeatherTabsActivity extends FragmentActivity { } 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);