<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
-
+
+ <uses-permission android:name="de.example.exampletdd.UPDATEOVERVIEWWEATHER" />
+ <uses-permission android:name="de.example.exampletdd.UPDATECURRENTWEATHER" />
+ <uses-permission android:name="de.example.exampletdd.UPDATETIMERATEWEATHERBATCH" />
+ <uses-permission android:name="de.example.exampletdd.UPDATEGEOCODINGWEATHERBATCH" />
+
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
</activity>
<activity
android:name="de.example.exampletdd.WeatherInformationSpecificDataActivity"
- android:parentActivityName="de.example.exampletdd.WeatherTabsActivity" >
+ android:parentActivityName="de.example.exampletdd.WeatherTabsActivity"
+ android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.WEATHERINFORMATIONSPECIFICDATA" />
</activity>
<activity
android:name="de.example.exampletdd.WeatherInformationCurrentDataActivity"
- android:parentActivityName="de.example.exampletdd.WeatherInformationActivity" >
+ android:parentActivityName="de.example.exampletdd.WeatherInformationActivity"
+ android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.WEATHERINFORMATIONCURRENTDATA" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
+
+ <service
+ android:name=".WeatherInformationBatch"
+ android:process=":weatherinformationbatch"
+ android:exported="false" />
+
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
# Project target.
target=android-18
-android.library.reference.1=../../../../../ALLGEMEINS/ALLGEMEIN/Android/android-sdk-linux/extras/google/google_play_services/libproject/google-play-services_lib
+android.library.reference.1=../../../../android/android-sdk-linux/extras/google/google_play_services/libproject/google-play-services_lib
android.library=false
<item
- android:id="@+id/weather_menu_get"
- android:menuCategory="system"
- android:title="@string/action_get_weather"
- android:titleCondensed="@string/action_get_weather"
- android:checked="false"
- android:visible="true"
- android:checkable="false"
- android:enabled="true"
- android:icon="@drawable/ic_action_refresh"
- android:showAsAction="ifRoom">
- </item>
- <item
android:id="@+id/weather_menu_settings"
android:menuCategory="system"
android:title="@string/action_settings"
--- /dev/null
+package de.example.exampletdd;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Calendar;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.http.client.ClientProtocolException;
+
+import android.app.Service;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.http.AndroidHttpClient;
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.os.IBinder;
+import android.util.Log;
+
+import com.fasterxml.jackson.core.JsonParseException;
+
+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 {
+ private static final String TAG = "WeatherInformationBatch";
+ private static final String resultsNumber = "14";
+ private WeatherServicePersistenceFile mWeatherServicePersistenceFile;
+ private ScheduledExecutorService mUpdateWeatherTask;
+ private int mUpdateTimeRate;
+
+ private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+
+ @Override
+ public void onReceive(final Context context, final Intent intent) {
+ // This method will be run in the main thread of this service.
+ final String action = intent.getAction();
+ if (action.equals("de.example.exampletdd.UPDATETIMERATEWEATHERBATCH")) {
+ final Bundle extras = intent.getExtras();
+ WeatherInformationBatch.this.mUpdateTimeRate = extras.getInt("UPDATE_RATE_TIME", 60);
+
+ WeatherInformationBatch.this.updateWeather();
+ }
+
+ if (action.equals("de.example.exampletdd.UPDATEGEOCODINGWEATHERBATCH")) {
+ WeatherInformationBatch.this.updateWeather();
+ }
+ }
+ };
+
+ @Override
+ public IBinder onBind(final Intent intent) {
+ return null;
+ }
+
+ @Override
+ public int onStartCommand(final Intent intent, final int flags, final int startId) {
+ final Bundle extras = intent.getExtras();
+ this.mUpdateTimeRate = extras.getInt("UPDATE_RATE_TIME", 60);
+
+ this.mWeatherServicePersistenceFile = new WeatherServicePersistenceFile(this);
+
+ this.updateWeather();
+
+
+ final IntentFilter filter = new IntentFilter();
+ filter.addAction("de.example.exampletdd.UPDATETIMERATEWEATHERBATCH");
+ filter.addAction("de.example.exampletdd.UPDATEGEOCODINGWEATHERBATCH");
+ this.registerReceiver(this.mReceiver, filter);
+
+ return Service.START_REDELIVER_INTENT;
+ }
+
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ this.unregisterReceiver(this.mReceiver);
+ if (this.mUpdateWeatherTask != null) {
+ this.mUpdateWeatherTask.shutdownNow();
+ }
+ }
+
+ private void updateWeather() {
+ if (this.mUpdateWeatherTask != null) {
+ this.mUpdateWeatherTask.shutdownNow();
+ }
+
+ this.mUpdateWeatherTask = Executors.newScheduledThreadPool(1);
+ this.mUpdateWeatherTask.scheduleAtFixedRate(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ WeatherInformationBatch.this.updateWeatherTask();
+ } catch (final Throwable e) {
+ Log.i(TAG, "updateWeather, unexpected exception: ", e);
+ }
+ }
+ }, 0, this.mUpdateTimeRate, TimeUnit.SECONDS);
+ }
+
+ private void updateWeatherTask() {
+ 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 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, "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;
+ }
+ }
+}
import java.util.Locale;
import android.app.ActionBar;
+import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
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
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)
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;
private static final int NUM_ITEMS = 2;
private MyAdapter mAdapter;
private ViewPager mPager;
- private GetWeather mGetWeather;
@Override
protected void onCreate(final Bundle savedInstanceState) {
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
"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",
}
-
- public void getWeather() {
- this.mGetWeather.getRemoteWeatherInformation();
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ this.stopService(new Intent(this, WeatherInformationBatch.class));
}
public class MyAdapter extends FragmentPagerAdapter {
if (position == 0) {
return new WeatherInformationCurrentDataFragment();
} else {
- final WeatherInformationOverviewFragment fragment = new WeatherInformationOverviewFragment();
- WeatherTabsActivity.this.mGetWeather = fragment;
+ final Fragment fragment = new WeatherInformationOverviewFragment();
return fragment;
}
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
.getCurrentWeatherData();
if (currentWeatherData != null) {
this.updateCurrentWeatherData(currentWeatherData);
- } else {
- // 3. Try to update weather data on display with remote
- this.getRemoteCurrentWeatherInformation();
}
}
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);
this.setListAdapter(adapter);
}
-
- public class CurrentWeatherTask extends AsyncTask<Object, Void, CurrentWeatherData> {
- 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);
- }
- }
}
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;
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;
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) {
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
}
@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<WeatherOverviewEntry> entries = new ArrayList<WeatherOverviewEntry>();
final WeatherOverviewAdapter adapter = new WeatherOverviewAdapter(this.getActivity(),
R.layout.weather_main_entry_list);
}
}
-
- public class ForecastWeatherTask extends AsyncTask<Object, Void, ForecastWeatherData> {
- 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);
- }
- }
}
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;
super.onSaveInstanceState(savedInstanceState);
}
- @Override
public void getWeatherByDay(final int chosenDay) {
final ForecastWeatherData forecastWeatherData = this.mWeatherServicePersistenceFile
}
- @Override
- public void getRemoteWeatherInformation() {
- // Nothing to do.
- }
-
public void updateForecastWeatherData(final ForecastWeatherData forecastWeatherData,
final int chosenDay) {
package de.example.exampletdd.service;
+import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
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) {
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 {
oos.close();
}
}
+
+ this.renameFile(WEATHER_GEOCODING_TEMPORARY_FILE, WEATHER_GEOCODING_FILE);
}
public GeocodingData getGeocodingData() {
oos.close();
}
}
+
+ this.renameFile(CURRENT_WEATHER_DATA_TEMPORARY_FILE, CURRENT_WEATHER_DATA_FILE);
}
public CurrentWeatherData getCurrentWeatherData() {
oos.close();
}
}
+
+ this.renameFile(FORECAST_WEATHER_DATA_TEMPORARY_FILE, FORECAST_WEATHER_DATA_FILE);
}
public ForecastWeatherData getForecastWeatherData() {
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);
+ }
}