From: Gustavo Martin Morcuende Date: Thu, 30 Oct 2014 02:58:02 +0000 (+0100) Subject: WeatherInformation: widget update and refresh, right behaviour X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=65c48643989d5d68aed158f9e0b83cd7b28075bc;p=JavaForFun WeatherInformation: widget update and refresh, right behaviour --- diff --git a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/WidgetIntentService.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/WidgetIntentService.java index 386f08e..0653c73 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/WidgetIntentService.java +++ b/Android/WeatherInformation/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/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java index 5245658..5a85320 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java +++ b/Android/WeatherInformation/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/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetConfigure.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetConfigure.java index 410a847..f061356 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetConfigure.java +++ b/Android/WeatherInformation/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/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java index 06970e7..3ad5b0b 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java +++ b/Android/WeatherInformation/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); } }