From 561004883da8d1c5507c163eab7237262e4abbaf Mon Sep 17 00:00:00 2001 From: Ben Komalo Date: Fri, 14 Oct 2011 14:33:27 -0700 Subject: [PATCH] Fix widget update to properly refresh. Bug: 5413224 Change-Id: Id66c7dc8aac8b5e24c7d5c5b1633c9c3ab981b15 --- .../email/provider/WidgetProvider.java | 12 +++---- src/com/android/email/widget/EmailWidget.java | 34 ++++++++++++------- .../android/email/widget/WidgetManager.java | 22 +++++++++--- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/com/android/email/provider/WidgetProvider.java b/src/com/android/email/provider/WidgetProvider.java index 18fe33fbf..9f7fc8512 100644 --- a/src/com/android/email/provider/WidgetProvider.java +++ b/src/com/android/email/provider/WidgetProvider.java @@ -16,11 +16,6 @@ package com.android.email.provider; -import com.android.email.Email; -import com.android.email.widget.EmailWidget; -import com.android.email.widget.WidgetManager; -import com.android.emailcommon.Logging; - import android.app.Service; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; @@ -29,6 +24,11 @@ import android.content.Intent; import android.util.Log; import android.widget.RemoteViewsService; +import com.android.email.Email; +import com.android.email.widget.EmailWidget; +import com.android.email.widget.WidgetManager; +import com.android.emailcommon.Logging; + import java.io.FileDescriptor; import java.io.PrintWriter; @@ -56,7 +56,7 @@ public class WidgetProvider extends AppWidgetProvider { Log.d(EmailWidget.TAG, "onUpdate"); } super.onUpdate(context, appWidgetManager, appWidgetIds); - WidgetManager.getInstance().createWidgets(context, appWidgetIds); + WidgetManager.getInstance().updateWidgets(context, appWidgetIds); } @Override diff --git a/src/com/android/email/widget/EmailWidget.java b/src/com/android/email/widget/EmailWidget.java index 26455643c..f44903aac 100644 --- a/src/com/android/email/widget/EmailWidget.java +++ b/src/com/android/email/widget/EmailWidget.java @@ -16,19 +16,6 @@ package com.android.email.widget; -import com.android.email.Email; -import com.android.email.R; -import com.android.email.ResourceHelper; -import com.android.email.activity.MessageCompose; -import com.android.email.activity.UiUtilities; -import com.android.email.activity.Welcome; -import com.android.email.provider.WidgetProvider.WidgetService; -import com.android.emailcommon.Logging; -import com.android.emailcommon.provider.Account; -import com.android.emailcommon.provider.EmailContent.Message; -import com.android.emailcommon.provider.Mailbox; -import com.android.emailcommon.utility.EmailAsyncTask; - import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.content.Context; @@ -53,6 +40,19 @@ import android.view.View; import android.widget.RemoteViews; import android.widget.RemoteViewsService; +import com.android.email.Email; +import com.android.email.R; +import com.android.email.ResourceHelper; +import com.android.email.activity.MessageCompose; +import com.android.email.activity.UiUtilities; +import com.android.email.activity.Welcome; +import com.android.email.provider.WidgetProvider.WidgetService; +import com.android.emailcommon.Logging; +import com.android.emailcommon.provider.Account; +import com.android.emailcommon.provider.EmailContent.Message; +import com.android.emailcommon.provider.Mailbox; +import com.android.emailcommon.utility.EmailAsyncTask; + import java.util.List; /** @@ -171,6 +171,14 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory, mLoader.load(mAccountId, mailboxId); } + /** + * Resets the data in the widget and forces a reload. + */ + public void reset() { + mLoader.reset(); + start(); + } + private boolean isCursorValid() { return mCursor != null && !mCursor.isClosed(); } diff --git a/src/com/android/email/widget/WidgetManager.java b/src/com/android/email/widget/WidgetManager.java index 87d025941..d2cb17133 100644 --- a/src/com/android/email/widget/WidgetManager.java +++ b/src/com/android/email/widget/WidgetManager.java @@ -16,15 +16,15 @@ package com.android.email.widget; +import android.content.Context; +import android.content.SharedPreferences; +import android.util.Log; + import com.android.email.Email; import com.android.emailcommon.Logging; import com.android.emailcommon.provider.Account; import com.android.emailcommon.provider.Mailbox; -import android.content.Context; -import android.content.SharedPreferences; -import android.util.Log; - import java.io.FileDescriptor; import java.io.PrintWriter; import java.util.Map; @@ -69,6 +69,18 @@ public class WidgetManager { } } + public synchronized void updateWidgets(Context context, int[] widgetIds) { + for (int widgetId : widgetIds) { + // Find the widget in the map + final EmailWidget widget = WidgetManager.getInstance().get(widgetId); + if (widget != null) { + widget.reset(); + } else { + getOrCreateWidget(context, widgetId); + } + } + } + public synchronized EmailWidget getOrCreateWidget(Context context, int widgetId) { EmailWidget widget = WidgetManager.getInstance().get(widgetId); if (widget == null) { @@ -76,7 +88,7 @@ public class WidgetManager { Log.d(EmailWidget.TAG, "Create email widget; ID: " + widgetId); } widget = new EmailWidget(context, widgetId); - WidgetManager.getInstance().put(widgetId, widget); + put(widgetId, widget); widget.start(); } return widget;