WeatherInformation:
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 28 Sep 2014 13:54:32 +0000 (15:54 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 28 Sep 2014 13:54:32 +0000 (15:54 +0200)
1. Update widget from application
2. Avoid caches when updating widget
3. Using getApplicationContext as much as possible

Android/WeatherInformation/src/de/example/exampletdd/WeatherInformationBootReceiver.java
Android/WeatherInformation/src/de/example/exampletdd/WidgetIntentService.java
Android/WeatherInformation/src/de/example/exampletdd/fragment/current/CurrentFragment.java
Android/WeatherInformation/src/de/example/exampletdd/fragment/overview/OverviewFragment.java
Android/WeatherInformation/src/de/example/exampletdd/widget/WeatherInformationWidgetProvider.java

index a9c939a..05b751d 100644 (file)
@@ -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);
             }
         }
     }
index 9ee67ac..3f686b8 100644 (file)
@@ -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");
        }
 }
index 0c2a263..d3b60ae 100644 (file)
@@ -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 {
index 465279b..b3094ea 100644 (file)
@@ -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())) {
index 4c59fd5..9f9ef7d 100644 (file)
@@ -21,7 +21,7 @@ public class WeatherInformationWidgetProvider extends AppWidgetProvider {
         for (int i=0; i<N; i++) {
             int appWidgetId = appWidgetIds[i];
             // To prevent any ANR timeouts, we perform the update in a service
-               final Intent intent = new Intent(context, WidgetIntentService.class);
+               final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
                intent.putExtra("appWidgetId", appWidgetId);
                intent.putExtra("updateByApp", false);
             context.startService(intent);
@@ -57,7 +57,7 @@ public class WeatherInformationWidgetProvider extends AppWidgetProvider {
         // the layout from our package).
         //final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
         
-        final Intent intent = new Intent(context, WidgetIntentService.class);
+        final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
        intent.putExtra("appWidgetId", appWidgetId);
         context.startService(intent);
     }