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.
}
// 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());
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;
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;
// 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);
}
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();
// 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);
}
}
}
}
- 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);
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;
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);
}
}