Android weather information: background weather update.
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Sat, 3 May 2014 18:48:11 +0000 (20:48 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Sat, 3 May 2014 18:48:11 +0000 (20:48 +0200)
Android/WeatherInformation/AndroidManifest.xml
Android/WeatherInformation/project.properties
Android/WeatherInformation/res/menu/weather_main_menu.xml
Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationBatch.java [new file with mode: 0644]
Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationMapActivity.java
Android/WeatherInformation/src/de/example/exampletdd/WeatherTabsActivity.java
Android/WeatherInformation/src/de/example/exampletdd/fragment/current/WeatherInformationCurrentDataFragment.java
Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/WeatherInformationOverviewFragment.java
Android/WeatherInformation/src/de/example/exampletdd/fragment/specific/WeatherInformationSpecificDataFragment.java
Android/WeatherInformation/src/de/example/exampletdd/service/WeatherServicePersistenceFile.java

index 21c1fc1..9b37dd1 100644 (file)
     <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"
@@ -73,7 +78,8 @@
         </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"
index 770d261..7612c3e 100644 (file)
@@ -12,5 +12,5 @@
 
 # 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
index 9796222..e75d3cf 100644 (file)
@@ -3,18 +3,6 @@
     
     
     <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"
diff --git a/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationBatch.java b/Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationBatch.java
new file mode 100644 (file)
index 0000000..f0bc835
--- /dev/null
@@ -0,0 +1,255 @@
+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;
+        }
+    }
+}
index ae1bbd6..4290104 100644 (file)
@@ -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)
index 4c137bd..5c2126d 100644 (file)
@@ -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;
             }
 
index 0a04dc0..de396fd 100644 (file)
@@ -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<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);
-        }
-    }
 }
index 9256c61..c646255 100644 (file)
@@ -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<WeatherOverviewEntry> entries = new ArrayList<WeatherOverviewEntry>();
         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<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);
-        }
-    }
 }
index 85b4d1e..57d8589 100644 (file)
@@ -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) {
index fded6e7..7d9c8c3 100644 (file)
@@ -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);
+    }
 }