From 433f159d7651e853d3da1f14be8b36bb180733e4 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Sun, 28 Sep 2014 15:54:32 +0200 Subject: [PATCH] WeatherInformation: 1. Update widget from application 2. Avoid caches when updating widget 3. Using getApplicationContext as much as possible --- .../exampletdd/WeatherInformationBootReceiver.java | 14 ++++++++++---- src/de/example/exampletdd/WidgetIntentService.java | 15 ++++++--------- .../exampletdd/fragment/current/CurrentFragment.java | 11 ++++------- .../exampletdd/fragment/overview/OverviewFragment.java | 4 ++-- .../widget/WeatherInformationWidgetProvider.java | 4 ++-- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/de/example/exampletdd/WeatherInformationBootReceiver.java b/src/de/example/exampletdd/WeatherInformationBootReceiver.java index a9c939a..05b751d 100644 --- a/src/de/example/exampletdd/WeatherInformationBootReceiver.java +++ b/src/de/example/exampletdd/WeatherInformationBootReceiver.java @@ -40,10 +40,16 @@ public class WeatherInformationBootReceiver extends BroadcastReceiver { // TODO: better use some string instead of .class? In case I change the service class // this could be a problem (I guess) final Intent serviceIntent = new Intent(context, NotificationIntentService.class); - final PendingIntent alarmIntent = PendingIntent.getService(context, 0, serviceIntent, - PendingIntent.FLAG_UPDATE_CURRENT); - alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() - + chosenInterval, chosenInterval, alarmIntent); + final PendingIntent alarmIntent = PendingIntent.getService( + context, + 0, + serviceIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + alarmMgr.setInexactRepeating( + AlarmManager.ELAPSED_REALTIME, + SystemClock.elapsedRealtime() + chosenInterval, + chosenInterval, + alarmIntent); } } } diff --git a/src/de/example/exampletdd/WidgetIntentService.java b/src/de/example/exampletdd/WidgetIntentService.java index 9ee67ac..3f686b8 100644 --- a/src/de/example/exampletdd/WidgetIntentService.java +++ b/src/de/example/exampletdd/WidgetIntentService.java @@ -14,7 +14,6 @@ import org.apache.http.client.ClientProtocolException; import android.app.IntentService; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; -import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Intent; import android.content.SharedPreferences; @@ -36,6 +35,7 @@ import de.example.exampletdd.parser.JPOSWeatherParser; import de.example.exampletdd.service.IconsList; import de.example.exampletdd.service.PermanentStorage; import de.example.exampletdd.service.ServiceParser; +import de.example.exampletdd.widget.WeatherInformationWidgetProvider; public class WidgetIntentService extends IntentService { private static final String TAG = "WidgetIntentService"; @@ -127,7 +127,8 @@ public class WidgetIntentService extends IntentService { final String urlAPI = this.getResources().getString(R.string.uri_api_weather_today); final String url = weatherService.createURIAPICurrent(urlAPI, APIVersion, weatherLocation.getLatitude(), weatherLocation.getLongitude()); - final String jsonData = HTTPClient.retrieveDataAsString(new URL(url)); + final String urlWithoutCache = url.concat("&time=" + System.currentTimeMillis()); + final String jsonData = HTTPClient.retrieveDataAsString(new URL(urlWithoutCache)); final Current current = weatherService.retrieveCurrentFromJPOS(jsonData); // TODO: what is this for? I guess I could skip it :/ final Calendar now = Calendar.getInstance(); @@ -222,18 +223,14 @@ public class WidgetIntentService extends IntentService { private void updateWidget(final RemoteViews remoteView, final int appWidgetId) { - final AppWidgetManager manager = AppWidgetManager.getInstance(this); + final AppWidgetManager manager = AppWidgetManager.getInstance(this.getApplicationContext()); manager.updateAppWidget(appWidgetId, remoteView); - - Log.i(TAG, "updateWidget updated"); } private void updateWidgets(final RemoteViews remoteView) { - final ComponentName widgets = new ComponentName("de.example.exampletdd.widget", AppWidgetProvider.class.getCanonicalName()); - final AppWidgetManager manager = AppWidgetManager.getInstance(this); + final ComponentName widgets = new ComponentName(this.getApplicationContext(), WeatherInformationWidgetProvider.class); + final AppWidgetManager manager = AppWidgetManager.getInstance(this.getApplicationContext()); manager.updateAppWidget(widgets, remoteView); - - Log.i(TAG, "updateWidgets updated"); } } diff --git a/src/de/example/exampletdd/fragment/current/CurrentFragment.java b/src/de/example/exampletdd/fragment/current/CurrentFragment.java index 0c2a263..d3b60ae 100644 --- a/src/de/example/exampletdd/fragment/current/CurrentFragment.java +++ b/src/de/example/exampletdd/fragment/current/CurrentFragment.java @@ -104,9 +104,9 @@ public class CurrentFragment extends Fragment { if (currentRemote != null) { // 1. Check conditions. They must be the same as the ones that triggered the AsyncTask. - final DatabaseQueries query = new DatabaseQueries(CurrentFragment.this.getActivity().getApplicationContext()); + final DatabaseQueries query = new DatabaseQueries(context.getApplicationContext()); final WeatherLocation weatherLocation = query.queryDataBase(); - final PermanentStorage store = new PermanentStorage(CurrentFragment.this.getActivity().getApplicationContext()); + final PermanentStorage store = new PermanentStorage(context.getApplicationContext()); final Current current = store.getCurrent(); if (current == null || !CurrentFragment.this.isDataFresh(weatherLocation.getLastCurrentUIUpdate())) { @@ -119,12 +119,9 @@ public class CurrentFragment extends Fragment { query.updateDataBase(weatherLocation); // 4. Update Widget's UI. - // TODO: Unable to start service intent not found U=0 WHYYYYYYYY? ANDROID SUCKSSSSSSS - final Intent intentWidget = new Intent(); - intentWidget.setClassName("de.example.exampletdd", WidgetIntentService.class.getCanonicalName()); - intent.putExtra("appWidgetId", 0); + final Intent intentWidget = new Intent(context.getApplicationContext(), WidgetIntentService.class); intentWidget.putExtra("updateByApp", true); - CurrentFragment.this.getActivity().getApplicationContext().startService(intent); + context.getApplicationContext().startService(intentWidget); } } else { diff --git a/src/de/example/exampletdd/fragment/overview/OverviewFragment.java b/src/de/example/exampletdd/fragment/overview/OverviewFragment.java index 465279b..b3094ea 100644 --- a/src/de/example/exampletdd/fragment/overview/OverviewFragment.java +++ b/src/de/example/exampletdd/fragment/overview/OverviewFragment.java @@ -96,9 +96,9 @@ public class OverviewFragment extends ListFragment { if (forecastRemote != null) { // 1. Check conditions. They must be the same as the ones that triggered the AsyncTask. - final DatabaseQueries query = new DatabaseQueries(OverviewFragment.this.getActivity().getApplicationContext()); + final DatabaseQueries query = new DatabaseQueries(context.getApplicationContext()); final WeatherLocation weatherLocation = query.queryDataBase(); - final PermanentStorage store = new PermanentStorage(OverviewFragment.this.getActivity().getApplicationContext()); + final PermanentStorage store = new PermanentStorage(context.getApplicationContext()); final Forecast forecast = store.getForecast(); if (forecast == null || !OverviewFragment.this.isDataFresh(weatherLocation.getLastForecastUIUpdate())) { diff --git a/src/de/example/exampletdd/widget/WeatherInformationWidgetProvider.java b/src/de/example/exampletdd/widget/WeatherInformationWidgetProvider.java index 4c59fd5..9f9ef7d 100644 --- a/src/de/example/exampletdd/widget/WeatherInformationWidgetProvider.java +++ b/src/de/example/exampletdd/widget/WeatherInformationWidgetProvider.java @@ -21,7 +21,7 @@ public class WeatherInformationWidgetProvider extends AppWidgetProvider { for (int i=0; i