Merge "Handle missing settings"
diff --git a/src/com/android/mail/ui/AbstractActivityController.java b/src/com/android/mail/ui/AbstractActivityController.java
index 70dc30d..9da5e83 100644
--- a/src/com/android/mail/ui/AbstractActivityController.java
+++ b/src/com/android/mail/ui/AbstractActivityController.java
@@ -770,7 +770,7 @@
         if (savedState != null) {
             if (savedState.containsKey(SAVED_ACCOUNT)) {
                 mAccount = ((Account) savedState.getParcelable(SAVED_ACCOUNT));
-                onSettingsChanged(mAccount.settings);
+                mCachedSettings = mAccount.settings;
                 mActionBarView.setAccount(mAccount);
                 mActivity.invalidateOptionsMenu();
             }
@@ -795,7 +795,7 @@
                 }
                 if (mAccount != null) {
                     mActionBarView.setAccount(mAccount);
-                    onSettingsChanged(mAccount.settings);
+                    mCachedSettings = mAccount.settings;
                     mActivity.invalidateOptionsMenu();
                 }
 
@@ -839,7 +839,7 @@
 
                 mViewMode.enterSearchResultsListMode();
                 mAccount = ((Account) intent.getParcelableExtra(Utils.EXTRA_ACCOUNT));
-                onSettingsChanged(mAccount.settings);
+                mCachedSettings = mAccount.settings;
                 mActionBarView.setAccount(mAccount);
                 fetchSearchFolder(intent);
             }
diff --git a/src/com/android/mail/ui/FolderSelectionActivity.java b/src/com/android/mail/ui/FolderSelectionActivity.java
index 02d3862..4675eed 100644
--- a/src/com/android/mail/ui/FolderSelectionActivity.java
+++ b/src/com/android/mail/ui/FolderSelectionActivity.java
@@ -143,10 +143,10 @@
     /**
      * Create a widget for the specified account and folder
      */
-    private void createWidget() {
-        WidgetProvider.updateWidget(this, mAppWidgetId, mAccount, mSelectedFolder);
+    protected void createWidget(int id, Account account, Folder selectedFolder) {
+        WidgetProvider.updateWidget(this, id, account, selectedFolder);
         Intent result = new Intent();
-        result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
+        result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
         setResult(RESULT_OK, result);
         finish();
     }
@@ -155,7 +155,7 @@
     public void onClick(DialogInterface dialog, int which) {
         if (which == DialogInterface.BUTTON_POSITIVE) {
             // The only dialog that is
-            createWidget();
+            createWidget(mAppWidgetId, mAccount, mSelectedFolder);
         } else {
             doCancel();
         }
@@ -213,7 +213,7 @@
                     // Display a dialog offering to enable sync for this folder
                     showDialog(R.layout.folder_sync_for_widget_dialog);
                 } else {
-                    createWidget();
+                    createWidget(mAppWidgetId, mAccount, mSelectedFolder);
                 }
             }
         }
diff --git a/src/com/android/mail/ui/MailboxSelectionActivity.java b/src/com/android/mail/ui/MailboxSelectionActivity.java
index 711eeb8..797b49c 100644
--- a/src/com/android/mail/ui/MailboxSelectionActivity.java
+++ b/src/com/android/mail/ui/MailboxSelectionActivity.java
@@ -243,7 +243,7 @@
     private void selectAccount(Account account) {
         if (mCreateShortcut || mConfigureWidget) {
             // Invoked for a shortcut creation
-            final Intent intent = new Intent(this, FolderSelectionActivity.class);
+            final Intent intent = new Intent(this, getFolderSelectionActivity());
             intent.setFlags(
                     Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_FORWARD_RESULT);
             intent.setAction(mCreateShortcut ?
@@ -260,6 +260,13 @@
         }
     }
 
+    /**
+     * Return the class responsible for launching the folder selection activity.
+     */
+    protected Class<?> getFolderSelectionActivity() {
+        return FolderSelectionActivity.class;
+    }
+
     @Override
     public void onClick(View v) {
         switch (v.getId()) {
diff --git a/src/com/android/mail/widget/BaseWidgetProvider.java b/src/com/android/mail/widget/BaseWidgetProvider.java
index 1704c6e..0927270 100644
--- a/src/com/android/mail/widget/BaseWidgetProvider.java
+++ b/src/com/android/mail/widget/BaseWidgetProvider.java
@@ -20,9 +20,9 @@
 import com.android.mail.persistence.Persistence;
 import com.android.mail.providers.Account;
 import com.android.mail.providers.Folder;
-import com.android.mail.providers.MailAppProvider;
 import com.android.mail.providers.UIProvider;
 import com.android.mail.ui.MailboxSelectionActivity;
+import com.android.mail.utils.AccountUtils;
 import com.android.mail.utils.Utils;
 import com.google.common.collect.Sets;
 import com.google.common.primitives.Ints;
@@ -200,26 +200,12 @@
         }
     }
 
-    private static boolean isAccountValid(Context context, Account account) {
+    protected static boolean isAccountValid(Context context, Account account) {
         if (account != null) {
-            // TODO(mindyp) accounts should be cached so that we can lookup
-            // cached versions and correct later.
-            Cursor accountCursor = null;
-            try {
-                accountCursor = context.getContentResolver().query(
-                        MailAppProvider.getAccountsUri(), UIProvider.ACCOUNTS_PROJECTION,
-                        null, null, null);
-                if (accountCursor.moveToFirst()) {
-                    do {
-                        String newAccount = accountCursor.getString(UIProvider.ACCOUNT_URI_COLUMN);
-                        if (account != null && newAccount != null
-                                && TextUtils.equals(account.uri.toString(), newAccount))
-                            return true;
-                    } while (accountCursor.moveToNext());
-                }
-            } finally {
-                if (accountCursor != null) {
-                    accountCursor.close();
+            Account[] accounts = AccountUtils.getSyncingAccounts(context);
+            for (Account existing : accounts) {
+                if (account != null && existing != null && account.uri.equals(existing.uri)) {
+                    return true;
                 }
             }
         }
@@ -232,7 +218,6 @@
     public static boolean isWidgetConfigured(Context context, int appWidgetId, Account account,
             Folder folder) {
         if (isAccountValid(context, account)) {
-            // TODO: (mindyp) get widget preferences.
             return Persistence.getPreferences(context).getString(
                     WIDGET_ACCOUNT_PREFIX + appWidgetId, null) != null;
         }
@@ -271,44 +256,42 @@
         AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews);
     }
 
-    private static boolean isFolderSynchronized(Context context, Account account, Folder folder) {
-        // TODO: (mindyp) check the folder settings to see if it is syncing.
-        return true;
+    /**
+     * Modifies the remoteView for the given account and folder.
+     */
+    static void configureValidAccountWidget(Context context, RemoteViews remoteViews,
+            int appWidgetId, Account account, Folder folder, String folderDisplayName) {
+        BaseWidgetProvider.configureValidAccountWidget(context, remoteViews, appWidgetId, account,
+                folder, folderDisplayName, WidgetService.class);
     }
 
     /**
      * Modifies the remoteView for the given account and folder.
      */
     static void configureValidAccountWidget(Context context, RemoteViews remoteViews,
-            int appWidgetId, Account account, Folder folder, String folderDisplayName) {
-        PendingIntent clickIntent;// Widget is configured, now display the
-                                  // chosen account.
-
-        final boolean isFolderSynchronized = isFolderSynchronized(context, account, folder);
-
+            int appWidgetId, Account account, Folder folder, String folderDisplayName,
+            Class<?> widgetService) {
         remoteViews.setViewVisibility(R.id.widget_folder, View.VISIBLE);
         remoteViews.setTextViewText(R.id.widget_folder, folderDisplayName);
         remoteViews.setViewVisibility(R.id.widget_account, View.VISIBLE);
         remoteViews.setTextViewText(R.id.widget_account, account.name);
         remoteViews.setViewVisibility(R.id.widget_unread_count, View.VISIBLE);
         remoteViews.setViewVisibility(R.id.widget_compose, View.VISIBLE);
-        if (isFolderSynchronized) {
-            remoteViews.setViewVisibility(R.id.conversation_list, View.VISIBLE);
-            remoteViews.setViewVisibility(R.id.widget_folder_not_synced, View.GONE);
-        } else {
-            remoteViews.setViewVisibility(R.id.conversation_list, View.GONE);
-            remoteViews.setViewVisibility(R.id.widget_folder_not_synced, View.VISIBLE);
+        remoteViews.setViewVisibility(R.id.conversation_list, View.VISIBLE);
+        remoteViews.setViewVisibility(R.id.widget_folder_not_synced, View.GONE);
 
-            // TODO: (mindyp) Configure the remote view so clicks will launch an
-            // activity that
-            // will allow the user to enable sync for this folder.
-            // remoteViews.setOnClickPendingIntent(R.id.widget_folder_not_synced,
-            // clickIntent);
-        }
+        BaseWidgetProvider.configureValidWidgetIntents(context, remoteViews, appWidgetId, account,
+                folder, folderDisplayName, widgetService);
+    }
+
+    public static void configureValidWidgetIntents(Context context, RemoteViews remoteViews,
+            int appWidgetId, Account account, Folder folder, String folderDisplayName,
+            Class<?> serviceClass) {
         remoteViews.setViewVisibility(R.id.widget_configuration, View.GONE);
 
+
         // Launch an intent to avoid ANRs
-        final Intent intent = new Intent(context, WidgetService.class);
+        final Intent intent = new Intent(context, serviceClass);
         intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
         intent.putExtra(EXTRA_ACCOUNT, account.serialize());
         intent.putExtra(EXTRA_FOLDER, folder.serialize());
@@ -316,7 +299,7 @@
         remoteViews.setRemoteAdapter(R.id.conversation_list, intent);
         // Open mail app when click on header
         final Intent mailIntent = Utils.createViewFolderIntent(folder, account, false);
-        clickIntent = PendingIntent.getActivity(context, 0, mailIntent,
+        PendingIntent clickIntent = PendingIntent.getActivity(context, 0, mailIntent,
                 PendingIntent.FLAG_UPDATE_CURRENT);
         remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);
 
diff --git a/src/com/android/mail/widget/WidgetService.java b/src/com/android/mail/widget/WidgetService.java
index ec1b82a..a34cf2b 100644
--- a/src/com/android/mail/widget/WidgetService.java
+++ b/src/com/android/mail/widget/WidgetService.java
@@ -50,7 +50,14 @@
 
     @Override
     public RemoteViewsFactory onGetViewFactory(Intent intent) {
-        return new MailFactory(getApplicationContext(), intent);
+        return new MailFactory(getApplicationContext(), intent, this);
+    }
+
+
+    protected void configureValidAccountWidget(Context context, RemoteViews remoteViews,
+            int appWidgetId, Account account, Folder folder, String folderName) {
+        BaseWidgetProvider.configureValidAccountWidget(context, remoteViews, appWidgetId, account,
+                folder, folderName);
     }
 
     /**
@@ -74,8 +81,8 @@
         private boolean mShouldShowViewMore;
         private boolean mFolderInformationShown = false;
         private ContentResolver mResolver;
-
-        public MailFactory(Context context, Intent intent) {
+        private WidgetService mService;
+        public MailFactory(Context context, Intent intent, WidgetService service) {
             mContext = context;
             mAppWidgetId = intent.getIntExtra(
                     AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
@@ -83,6 +90,7 @@
             mFolder = new Folder(intent.getStringExtra(WidgetProvider.EXTRA_FOLDER));
             mWidgetConversationViewBuilder = new WidgetConversationViewBuilder(mContext, mAccount);
             mResolver = context.getContentResolver();
+            mService = service;
         }
 
         @Override
@@ -253,7 +261,6 @@
             if (!data.moveToFirst()) {
                 return;
             }
-
             final int unreadCount = data.getInt(UIProvider.FOLDER_UNREAD_COUNT_COLUMN);
             final String folderName = data.getString(UIProvider.FOLDER_NAME_COLUMN);
             mFolderCount = data.getInt(UIProvider.FOLDER_TOTAL_COUNT_COLUMN);
@@ -266,7 +273,7 @@
                 // manager doesn't cache the state of the remote views when doing a partial
                 // widget update. This causes the folder name to be shown as blank if the state
                 // of the widget is restored.
-                BaseWidgetProvider.configureValidAccountWidget(
+                mService.configureValidAccountWidget(
                         mContext, remoteViews, mAppWidgetId, mAccount, mFolder, folderName);
                 appWidgetManager.updateAppWidget(mAppWidgetId, remoteViews);
                 mFolderInformationShown = true;