From 533e21c548df59b282e8e9fbf0643d2114c3e859 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Thu, 30 Oct 2014 03:58:02 +0100 Subject: [PATCH] WeatherInformation: widget update and refresh, right behaviour --- .../de/example/exampletdd/WidgetIntentService.java | 4 +-- .../fragment/current/CurrentFragment.java | 20 ++++++------- .../example/exampletdd/widget/WidgetConfigure.java | 2 +- .../example/exampletdd/widget/WidgetProvider.java | 35 ++++++++++++---------- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/de/example/exampletdd/WidgetIntentService.java b/app/src/main/java/de/example/exampletdd/WidgetIntentService.java index 386f08e..0653c73 100644 --- a/app/src/main/java/de/example/exampletdd/WidgetIntentService.java +++ b/app/src/main/java/de/example/exampletdd/WidgetIntentService.java @@ -50,7 +50,7 @@ public class WidgetIntentService extends IntentService { protected void onHandleIntent(final Intent intent) { Log.i(TAG, "onHandleIntent"); final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); - final boolean isForceRefreshAppWidget = intent.getBooleanExtra("forceRefreshAppWidget", false); + final boolean isRefreshAppWidget = intent.getBooleanExtra("refreshAppWidget", false); if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { // Nothing to do. Something went wrong. Show error. @@ -69,7 +69,7 @@ public class WidgetIntentService extends IntentService { } // TODO: improve this code. Too tired right now... - if (!isForceRefreshAppWidget && this.isDataFresh(weatherLocation.getLastCurrentUIUpdate())) { + if (!isRefreshAppWidget) { RemoteViews view; final PermanentStorage store = new PermanentStorage(this.getApplicationContext()); diff --git a/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java b/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java index 5245658..5a85320 100644 --- a/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java +++ b/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java @@ -13,9 +13,7 @@ import java.util.Locale; import org.apache.http.client.ClientProtocolException; -import android.appwidget.AppWidgetManager; import android.content.BroadcastReceiver; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; @@ -39,7 +37,6 @@ import android.widget.TextView; import com.fasterxml.jackson.core.JsonParseException; import de.example.exampletdd.R; -import de.example.exampletdd.WidgetIntentService; import de.example.exampletdd.httpclient.CustomHTTPClient; import de.example.exampletdd.model.DatabaseQueries; import de.example.exampletdd.model.WeatherLocation; @@ -118,13 +115,7 @@ public class CurrentFragment extends Fragment { // 3. Update current data. store.saveCurrent(currentRemote); - // 4. If is new data (new location) update widgets. - if (weatherLocation.getIsNew()) { - WidgetProvider.updateAllAppWidgets(context); - } - - // 5. Update location data. - weatherLocation.setIsNew(false); + // 4. Update location data. weatherLocation.setLastCurrentUIUpdate(new Date()); query.updateDataBase(weatherLocation); } @@ -160,6 +151,15 @@ public class CurrentFragment extends Fragment { return; } + // If is new location update widgets. + if (weatherLocation.getIsNew()) { + WidgetProvider.refreshAllAppWidgets(this.getActivity().getApplicationContext()); + // Update location data. + weatherLocation.setIsNew(false); + query.updateDataBase(weatherLocation); + } + + final PermanentStorage store = new PermanentStorage(this.getActivity().getApplicationContext()); final Current current = store.getCurrent(); diff --git a/app/src/main/java/de/example/exampletdd/widget/WidgetConfigure.java b/app/src/main/java/de/example/exampletdd/widget/WidgetConfigure.java index 410a847..f061356 100644 --- a/app/src/main/java/de/example/exampletdd/widget/WidgetConfigure.java +++ b/app/src/main/java/de/example/exampletdd/widget/WidgetConfigure.java @@ -112,7 +112,7 @@ public class WidgetConfigure extends Activity { public void onClickRefresh(final View view) { // Push widget update to surface - WidgetProvider.forceRefreshAppWidget(this.getApplicationContext(), mAppWidgetId); + WidgetProvider.refreshAppWidget(this.getApplicationContext(), mAppWidgetId); } public void onClickOk(final View view) { diff --git a/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java b/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java index 06970e7..3ad5b0b 100644 --- a/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java +++ b/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java @@ -24,7 +24,7 @@ public class WidgetProvider extends AppWidgetProvider { // To prevent any ANR timeouts, we perform the update in a service final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); - intent.putExtra("forceRefreshAppWidget", true); + intent.putExtra("refreshAppWidget", true); context.startService(intent); } } @@ -38,15 +38,30 @@ public class WidgetProvider extends AppWidgetProvider { } } - static void updateAppWidget(final Context context, final int appWidgetId) { + public static void updateAppWidget(final Context context, final int appWidgetId) { final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext()); updateAppWidget(context, appWidgetManager, appWidgetId); } - static void forceRefreshAppWidget(final Context context, final int appWidgetId) { + public static void refreshAppWidget(final Context context, final int appWidgetId) { final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext()); + refreshAppWidget(context.getApplicationContext(), appWidgetManager, appWidgetId); + } + + public static void refreshAllAppWidgets(final Context context) { + final ComponentName widgets = new ComponentName(context.getApplicationContext(), WidgetProvider.class); + final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext()); + + final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(widgets); + for (final int appWidgetId : appWidgetIds) { + refreshAppWidget(context.getApplicationContext(), appWidgetManager, appWidgetId); + } + } + + private static void refreshAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) { + int widgetId; Bundle myOptions = appWidgetManager.getAppWidgetOptions(appWidgetId); @@ -67,20 +82,10 @@ public class WidgetProvider extends AppWidgetProvider { final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); - intent.putExtra("forceRefreshAppWidget", true); + intent.putExtra("refreshAppWidget", true); context.startService(intent); } - public static void updateAllAppWidgets(final Context context) { - final ComponentName widgets = new ComponentName(context.getApplicationContext(), WidgetProvider.class); - final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext()); - - final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(widgets); - for (final int appWidgetId : appWidgetIds) { - updateAppWidget(context.getApplicationContext(), appWidgetManager, appWidgetId); - } - } - private static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) { int widgetId; @@ -103,7 +108,7 @@ public class WidgetProvider extends AppWidgetProvider { final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); - intent.putExtra("forceRefreshAppWidget", false); + intent.putExtra("refreshAppWidget", false); context.startService(intent); } } -- 2.1.4