Make constructor public to handle resume/rotate

Change-Id: I4953ae6ed753792eb8a7aced99474fe09976f140
diff --git a/src/com/android/mail/providers/UIProvider.java b/src/com/android/mail/providers/UIProvider.java
index 6767686..2331ddc 100644
--- a/src/com/android/mail/providers/UIProvider.java
+++ b/src/com/android/mail/providers/UIProvider.java
@@ -29,7 +29,6 @@
 import java.lang.String;
 import java.util.ArrayList;
 
-
 public class UIProvider {
     public static final String EMAIL_SEPARATOR = "\n";
     public static final long INVALID_CONVERSATION_ID = -1;
diff --git a/src/com/android/mail/ui/AbstractActivityController.java b/src/com/android/mail/ui/AbstractActivityController.java
index c142844..28f43a4 100644
--- a/src/com/android/mail/ui/AbstractActivityController.java
+++ b/src/com/android/mail/ui/AbstractActivityController.java
@@ -76,6 +76,7 @@
     private static final String SAVED_CONVERSATION_POSITION = "saved-conv-pos";
     // Keys for serialization of various information in Bundles.
     private static final String SAVED_LIST_CONTEXT = "saved-list-context";
+    private static final String SAVED_ACCOUNT = "saved-account";
 
     /**
      * Are we on a tablet device or not.
@@ -467,6 +468,10 @@
 
     @Override
     public void onSaveInstanceState(Bundle outState) {
+        if (mAccount != null) {
+            LogUtils.d(LOG_TAG, "Saving the account now");
+            outState.putParcelable(SAVED_ACCOUNT, mAccount);
+        }
         if (mConvListContext != null) {
             outState.putBundle(SAVED_LIST_CONTEXT, mConvListContext.toBundle());
         }
@@ -542,7 +547,7 @@
      * @param savedState
      */
     protected void restoreListContext(Bundle savedState) {
-        // TODO(viki): Auto-generated method stub
+        // TODO(viki): Restore the account, the folder, and the conversation, if any.
         Bundle listContextBundle = savedState.getBundle(SAVED_LIST_CONTEXT);
         if (listContextBundle != null) {
             mConvListContext = ConversationListContext.forBundle(listContextBundle);
@@ -559,10 +564,8 @@
     protected void restoreState(Bundle savedState) {
         if (savedState != null) {
             restoreListContext(savedState);
-            // Attach the menu handler here.
-
-            // Restore the view mode
             mViewMode.handleRestore(savedState);
+            mAccount = savedState.getParcelable(SAVED_ACCOUNT);
         } else {
             final Intent intent = mActivity.getIntent();
             if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
@@ -641,13 +644,14 @@
      * @return true if the account exists in the account cursor, false
      *         otherwise.
      */
-    private boolean existsInCursor(Cursor accountCursor, Account account) {
-        accountCursor.moveToFirst();
+    private boolean missingFromCursor(Cursor accountCursor, Account account) {
+        if (account == null || !accountCursor.moveToFirst())
+            return true;
         do {
             if (account.equals(new Account(accountCursor)))
-                return true;
+                return false;
         } while (accountCursor.moveToNext());
-        return false;
+        return true;
     }
 
     /**
@@ -655,16 +659,19 @@
      * in the list.
      *
      * @param loader
-     * @param data
+     * @param accounts cursor into the AccountCache
+     * @param currentAccountMissing whether the current account is missing from the set of accounts
      * @return true if the update was successful, false otherwise
      */
-    private boolean updateAccounts(Loader<Cursor> loader, Cursor accounts) {
-        // Load the first account in the absence of any other information.
+    private boolean updateAccounts(Loader<Cursor> loader, Cursor accounts,
+            boolean currentAccountMissing) {
         if (accounts == null || !accounts.moveToFirst()) {
             return false;
         }
-        Account newAccount = mAccount == null ? new Account(accounts) : mAccount;
+        Account newAccount = (mAccount == null || currentAccountMissing) ?
+                new Account(accounts) : mAccount;
         onAccountChanged(newAccount);
+        fetchAccountFolderInfo();
         final Account[] allAccounts = Account.getAllAccounts(accounts);
         mActionBarView.setAccounts(allAccounts);
         return (allAccounts.length > 0);
@@ -679,8 +686,9 @@
         // if the current account has vanished.
         final int id = loader.getId();
         if (id == ACCOUNT_CURSOR_LOADER) {
-            if (!isLoaderInitialized || !existsInCursor(data, mAccount)) {
-                isLoaderInitialized = updateAccounts(loader, data);
+            final boolean currentAccountMissing = missingFromCursor(data, mAccount);
+            if (!isLoaderInitialized || currentAccountMissing) {
+                isLoaderInitialized = updateAccounts(loader, data, currentAccountMissing);
             }
         } else if (id == FOLDER_CURSOR_LOADER) {
             // Check status of the cursor.
diff --git a/src/com/android/mail/ui/ControllableActivity.java b/src/com/android/mail/ui/ControllableActivity.java
index 3f37726..051b571 100644
--- a/src/com/android/mail/ui/ControllableActivity.java
+++ b/src/com/android/mail/ui/ControllableActivity.java
@@ -70,4 +70,10 @@
     void attachFolderList(FolderListFragment folderListFragment);
 
     void attachConversationView(ConversationViewFragment conversationViewFragment);
+
+    /**
+     * Return the folder change listener for this activity
+     * @return
+     */
+    FolderChangeListener getFolderChangeListener();
 }
diff --git a/src/com/android/mail/ui/ConversationListFragment.java b/src/com/android/mail/ui/ConversationListFragment.java
index 8ada1f2..9fe8fec 100644
--- a/src/com/android/mail/ui/ConversationListFragment.java
+++ b/src/com/android/mail/ui/ConversationListFragment.java
@@ -127,6 +127,9 @@
     private ConversationSelectionSet mSelectedSet = new ConversationSelectionSet();
     private SelectedConversationsActionMenu mSelectedConversationsActionMenu;
 
+    /**
+     * Constructor needs to be public to handle orientation changes and activity lifecycle events.
+     */
     public ConversationListFragment() {
         super();
         // Allow the fragment to observe changes to its own selection set. No other object is
@@ -227,11 +230,10 @@
 
     @Override
     public void onCreate(Bundle savedState) {
-        LogUtils.v(LOG_TAG, "onCreate in ConversationListFragment(this=%s)", this);
         super.onCreate(savedState);
 
         // Initialize fragment constants from resources
-        Resources res = getResources();
+        final Resources res = getResources();
         TIMESTAMP_UPDATE_INTERVAL = res.getInteger(R.integer.timestamp_update_interval);
         mUpdateTimestampsRunnable = new Runnable(){
             @Override
@@ -242,7 +244,7 @@
         };
 
         // Get the context from the arguments
-        Bundle args = getArguments();
+        final Bundle args = getArguments();
         mViewContext = ConversationListContext.forBundle(args.getBundle(CONVERSATION_LIST_KEY));
         mAccount = mViewContext.mAccount;
 
@@ -275,11 +277,8 @@
     @Override
     public View onCreateView(LayoutInflater inflater,
             ViewGroup container, Bundle savedInstanceState) {
-        LogUtils.v(LOG_TAG, "onCreateView in ConversationListFragment(this=%s)", this);
         View rootView = inflater.inflate(R.layout.conversation_list, null);
-
         mEmptyView = rootView.findViewById(R.id.empty_view);
-
         mListView = (ListView) rootView.findViewById(android.R.id.list);
         mListView.setHeaderDividersEnabled(false);
         mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
diff --git a/src/com/android/mail/ui/ConversationViewFragment.java b/src/com/android/mail/ui/ConversationViewFragment.java
index d3b19bc..e885a7c 100644
--- a/src/com/android/mail/ui/ConversationViewFragment.java
+++ b/src/com/android/mail/ui/ConversationViewFragment.java
@@ -86,7 +86,11 @@
     private static final String ARG_ACCOUNT = "account";
     private static final String ARG_CONVERSATION = "conversation";
 
+    /**
+     * Constructor needs to be public to handle orientation changes and activity lifecycle events.
+     */
     public ConversationViewFragment() {
+        super();
     }
 
     /**
diff --git a/src/com/android/mail/ui/FolderListFragment.java b/src/com/android/mail/ui/FolderListFragment.java
index 5a56ae1..1a5d6dc 100644
--- a/src/com/android/mail/ui/FolderListFragment.java
+++ b/src/com/android/mail/ui/FolderListFragment.java
@@ -59,16 +59,15 @@
     private FolderChangeListener mListener;
 
     private static final int FOLDER_LOADER_ID = 0;
-
     public static final int MODE_DEFAULT = 0;
     public static final int MODE_PICK = 1;
+    private static final String ARG_FOLDER_URI = "arg-folder-list-uri";
+
     /**
-     * Hidden constructor.
+     * Constructor needs to be public to handle orientation changes and activity lifecycle events.
      */
-    private FolderListFragment(FolderChangeListener listener, Uri uri) {
+    public FolderListFragment() {
         super();
-        mListener = listener;
-        mFolderListUri = uri;
     }
 
     /**
@@ -76,22 +75,16 @@
      * to display conversation list context.
      */
     public static FolderListFragment newInstance(FolderChangeListener listener, Uri uri) {
-        return newInstance(listener, uri, MODE_DEFAULT);
-    }
-
-    /**
-     * Creates a new instance of {@link ConversationListFragment}, initialized
-     * to display conversation list context.
-     */
-    public static FolderListFragment newInstance(FolderChangeListener listener, Uri uri,
-            int mode) {
-        FolderListFragment fragment = new FolderListFragment(listener, uri);
+        FolderListFragment fragment = new FolderListFragment();
+        Bundle args = new Bundle();
+        args.putString(ARG_FOLDER_URI, uri.toString());
+        fragment.setArguments(args);
         return fragment;
     }
 
     @Override
-    public void onActivityCreated(Bundle savedInstanceState) {
-        super.onActivityCreated(savedInstanceState);
+    public void onActivityCreated(Bundle savedState) {
+        super.onActivityCreated(savedState);
         // Strictly speaking, we get back an android.app.Activity from getActivity. However, the
         // only activity creating a ConversationListContext is a MailActivity which is of type
         // ControllableActivity, so this cast should be safe. If this cast fails, some other
@@ -109,20 +102,21 @@
             // Activity is finishing, just bail.
             return;
         }
+        mListener = mActivity.getFolderChangeListener();
+        final Bundle args = getArguments();
+        mFolderListUri = Uri.parse(args.getString(ARG_FOLDER_URI));
         mListView.setEmptyView(null);
         getLoaderManager().initLoader(FOLDER_LOADER_ID, Bundle.EMPTY, this);
     }
 
     @Override
     public void onCreate(Bundle savedState) {
-        LogUtils.v(LOG_TAG, "onCreate in FolderListFragment(this=%s)", this);
         super.onCreate(savedState);
     }
 
     @Override
     public View onCreateView(LayoutInflater inflater,
             ViewGroup container, Bundle savedInstanceState) {
-        LogUtils.v(LOG_TAG, "onCreateView in FolderListFragment(this=%s)", this);
         View rootView = inflater.inflate(R.layout.folder_list, null);
         mListView = (ListView) rootView.findViewById(android.R.id.list);
         mListView.setHeaderDividersEnabled(false);
@@ -226,4 +220,4 @@
     public void onViewModeChanged(int newMode) {
         // Listen on mode changes, when we move to Label list mode, change accordingly.
     }
-}
+}
\ No newline at end of file
diff --git a/src/com/android/mail/ui/FolderSelectionActivity.java b/src/com/android/mail/ui/FolderSelectionActivity.java
index 3358629..d57d8cb 100644
--- a/src/com/android/mail/ui/FolderSelectionActivity.java
+++ b/src/com/android/mail/ui/FolderSelectionActivity.java
@@ -93,8 +93,7 @@
         cancelButton.setOnClickListener(this);
 
         FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
-        Fragment fragment = FolderListFragment.newInstance(this, mAccount.folderListUri,
-                FolderListFragment.MODE_PICK);
+        Fragment fragment = FolderListFragment.newInstance(this, mAccount.folderListUri);
         fragmentTransaction.replace(R.id.folder_list_pane, fragment);
         fragmentTransaction.commitAllowingStateLoss();
     }
@@ -257,4 +256,9 @@
     @Override
     public void attachConversationView(ConversationViewFragment conversationViewFragment) {
     }
+
+    @Override
+    public FolderChangeListener getFolderChangeListener() {
+        return this;
+    }
 }
diff --git a/src/com/android/mail/ui/MailActivity.java b/src/com/android/mail/ui/MailActivity.java
index 724c681..720e522 100644
--- a/src/com/android/mail/ui/MailActivity.java
+++ b/src/com/android/mail/ui/MailActivity.java
@@ -246,4 +246,9 @@
     public StarHandler getStarHandler() {
         return mController;
     }
+
+    @Override
+    public FolderChangeListener getFolderChangeListener() {
+        return mController;
+    }
 }