Store recent folder list in the AbstractActivityController

Change-Id: Ie557cf4e67b55c238c323063e3211117c6d101b3
diff --git a/src/com/android/mail/AccountSpinnerAdapter.java b/src/com/android/mail/AccountSpinnerAdapter.java
index bd03bdb..c46df39 100644
--- a/src/com/android/mail/AccountSpinnerAdapter.java
+++ b/src/com/android/mail/AccountSpinnerAdapter.java
@@ -116,9 +116,14 @@
         return TYPE_FOLDER;
     }
 
-    public AccountSpinnerAdapter(Context context) {
+    /**
+     * Create a spinner adapter with the context and the list of recent folders.
+     * @param context
+     * @param recentFolders
+     */
+    public AccountSpinnerAdapter(Context context, RecentFolderList recentFolders) {
         mInflater = LayoutInflater.from(context);
-        mRecentFolders = new RecentFolderList(null, context);
+        mRecentFolders = recentFolders;
     }
 
     /**
@@ -137,7 +142,7 @@
      */
     public void setCurrentFolder(Folder folder) {
         mCurrentFolder = folder;
-        mRecentFolderList = mRecentFolders.changeCurrentFolder(folder);
+        mRecentFolderList = mRecentFolders.getSortedArray(folder);
     }
 
     /**
@@ -146,7 +151,6 @@
      */
     public void setCurrentAccount(Account account) {
         mCurrentAccount = account;
-        mRecentFolders.changeCurrentAccount(account);
         mRecentFolderList = mRecentFolders.getSortedArray(mCurrentFolder);
         notifyDataSetChanged();
     }
diff --git a/src/com/android/mail/ui/AbstractActivityController.java b/src/com/android/mail/ui/AbstractActivityController.java
index 87e8e8f..31415e5 100644
--- a/src/com/android/mail/ui/AbstractActivityController.java
+++ b/src/com/android/mail/ui/AbstractActivityController.java
@@ -89,6 +89,7 @@
     protected ActionBarView mActionBarView;
     protected final RestrictedActivity mActivity;
     protected final Context mContext;
+    protected final RecentFolderList mRecentFolderList;
     protected ConversationListContext mConvListContext;
     private FetchAccountFolderTask mFetchAccountFolderTask;
     protected Conversation mCurrentConversation;
@@ -109,6 +110,8 @@
     private final Set<Uri> mCurrentAccountUris = Sets.newHashSet();
     protected Settings mCachedSettings;
     private FetchSearchFolderTask mFetchSearchFolderTask;
+    /** Whether we have recorded this folder as a recent folder yet? */
+    private boolean mFolderTouched = false;
 
     protected static final String LOG_TAG = new LogUtils().getLogTag();
     private static final int ACCOUNT_CURSOR_LOADER = 0;
@@ -120,6 +123,7 @@
         mViewMode = viewMode;
         mContext = activity.getApplicationContext();
         IS_TABLET_DEVICE = Utils.useTabletUI(mContext);
+        mRecentFolderList = new RecentFolderList(mContext);
     }
 
     @Override
@@ -196,13 +200,12 @@
         ActionBar actionBar = mActivity.getActionBar();
         mActionBarView = (ActionBarView) LayoutInflater.from(mContext).inflate(
                 R.layout.actionbar_view, null);
-
         if (actionBar != null && mActionBarView != null) {
             // Why have a different variable for the same thing? We should apply
             // the same actions
             // on mActionBarView instead.
-            mActionBarView.initialize(mActivity, this, mViewMode, actionBar);
-            actionBar.setCustomView((LinearLayout) mActionBarView, new ActionBar.LayoutParams(
+            mActionBarView.initialize(mActivity, this, mViewMode, actionBar, mRecentFolderList);
+            actionBar.setCustomView(mActionBarView, new ActionBar.LayoutParams(
                     LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
             actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
                     ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE);
@@ -222,6 +225,7 @@
     public void onAccountChanged(Account account) {
         if (!account.equals(mAccount)) {
             mAccount = account;
+            mRecentFolderList.changeCurrentAccount(account);
             onSettingsChanged(null);
             restartSettingsLoader();
             mActionBarView.setAccount(mAccount);
@@ -274,11 +278,13 @@
         }
     }
 
+    /** Set the current folder */
     private void setFolder(Folder folder) {
         // Start watching folder for sync status.
         if (folder != null && !folder.equals(mFolder)) {
             mActionBarView.setRefreshInProgress(false);
             mFolder = folder;
+            mFolderTouched = false;
             mActionBarView.setFolder(mFolder);
             mActivity.getLoaderManager().restartLoader(FOLDER_CURSOR_LOADER, null, this);
         } else if (folder == null) {
@@ -540,6 +546,20 @@
         // conversation view's subject text.
     }
 
+    /**
+     * Children can override this method, but they must call super.showConversation().
+     * {@inheritDoc}
+     */
+    @Override
+    public void showConversation(Conversation conversation) {
+        // Add the folder that we were viewing to the recent folders list.
+        // We don't want to do this on every conversation access, the first is enough.
+        if (!mFolderTouched) {
+            mRecentFolderList.touchFolder(mFolder);
+            mFolderTouched = true;
+        }
+    }
+
     @Override
     public void onConversationSelected(Conversation conversation) {
         mCurrentConversation = conversation;
diff --git a/src/com/android/mail/ui/ActionBarView.java b/src/com/android/mail/ui/ActionBarView.java
index d5eaffb..4a45509 100644
--- a/src/com/android/mail/ui/ActionBarView.java
+++ b/src/com/android/mail/ui/ActionBarView.java
@@ -141,12 +141,12 @@
     }
 
     public void initialize(RestrictedActivity activity, ActivityController callback,
-            ViewMode viewMode, ActionBar actionBar) {
+            ViewMode viewMode, ActionBar actionBar, RecentFolderList recentFolders) {
         mActionBar = actionBar;
         mController = callback;
         mActivity = activity;
 
-        mSpinner = new AccountSpinnerAdapter(getContext());
+        mSpinner = new AccountSpinnerAdapter(getContext(), recentFolders);
         // Set the mode to Navigation mode and listen on navigation changes.
         mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
         mActionBar.setListNavigationCallbacks(mSpinner, this);
diff --git a/src/com/android/mail/ui/OnePaneController.java b/src/com/android/mail/ui/OnePaneController.java
index f4aff4f..918150e 100644
--- a/src/com/android/mail/ui/OnePaneController.java
+++ b/src/com/android/mail/ui/OnePaneController.java
@@ -145,6 +145,7 @@
 
     @Override
     public void showConversation(Conversation conversation) {
+        super.showConversation(conversation);
         if (mConvListContext != null && mConvListContext.isSearchResult()) {
             mViewMode.enterSearchResultsConversationMode();
         } else {
diff --git a/src/com/android/mail/ui/RecentFolderList.java b/src/com/android/mail/ui/RecentFolderList.java
index ad59764..c1c6279 100644
--- a/src/com/android/mail/ui/RecentFolderList.java
+++ b/src/com/android/mail/ui/RecentFolderList.java
@@ -47,7 +47,7 @@
     /** The application context */
     private final Context mContext;
     /** The current account */
-    private Account mAccount;
+    private Account mAccount = null;
     /**
      * Compare based on alphanumeric name of the folder, ignoring case.
      */
@@ -69,13 +69,16 @@
      * retrieve the RecentFolderList from persistent storage (if any).
      * @param account
      */
-    public RecentFolderList(Account account, Context context) {
+    public RecentFolderList(Context context) {
         mContext = context;
-        mAccount = account;
         mFolderCache = new LruCache<String, Folder>(NUM_FOLDERS);
-        loadFromUiProvider();
     }
 
+    /**
+     * Change the current account. This causes the recent label list to be written out to the
+     * provider, and a new list to be read for the new account.
+     * @param account
+     */
     public void changeCurrentAccount(Account account) {
         saveToUiProvider();
         mAccount = account;
@@ -110,21 +113,21 @@
     }
 
     /**
-     * Changes the current folder and returns the updated list of recent folders, <b>not</b>
-     * including the current folder.
+     * Marks the given folder as 'accessed' by the user interface, and its entry is updated in the
+     * recent folder list.
      * @param folder the folder we have changed to.
      */
-    public Folder[] changeCurrentFolder(Folder folder) {
+    public void touchFolder(Folder folder) {
         mFolderCache.putElement(folder.id, folder);
         // Update the UiProvider with the current recent folder list.
+        // TODO(viki): Perhaps not do this on every touch. This is excessive.
         saveToUiProvider();
-        return getSortedArray(folder);
     }
 
     /**
      * Requests the UIProvider to save this RecentFolderList to persistent storage.
      */
-    public void saveToUiProvider() {
+    private void saveToUiProvider() {
         if (mAccount == null || mFolderCache.isEmpty() || mAccount.recentFolderListUri == null)
             return;
         // Write the current recent folder list into the account.
@@ -142,12 +145,12 @@
     }
 
     /**
-     * Generate a sorted array of recent folders, <b>not</b> including the current folder.
-     * @param currentFolder the current folder being displayed.
+     * Generate a sorted array of recent folders, excluding the specified folders.
+     * @param exclude the folders to be excluded.
      */
-    public Folder[] getSortedArray(Folder currentFolder) {
+    public Folder[] getSortedArray(Folder exclude) {
         final int spaceForCurrentFolder =
-                (currentFolder != null && mFolderCache.getElement(currentFolder.id) != null)
+                (exclude != null && mFolderCache.getElement(exclude.id) != null)
                         ? 1 : 0;
         final int numRecents = mFolderCache.size() - spaceForCurrentFolder;
         final Folder[] folders = new Folder[numRecents];
@@ -155,7 +158,7 @@
         final List<Folder> recent = new ArrayList<Folder>(mFolderCache.values());
         Collections.sort(recent, ALPHABET_IGNORECASE);
         for (Folder f : recent) {
-            if (currentFolder == null || !TextUtils.equals(f.id, currentFolder.id)) {
+            if (exclude == null || !TextUtils.equals(f.id, exclude.id)) {
                 folders[i++] = f;
             }
         }
diff --git a/src/com/android/mail/ui/TwoPaneController.java b/src/com/android/mail/ui/TwoPaneController.java
index 2c2650b..49e281d 100644
--- a/src/com/android/mail/ui/TwoPaneController.java
+++ b/src/com/android/mail/ui/TwoPaneController.java
@@ -153,6 +153,7 @@
 
     @Override
     public void showConversation(Conversation conversation) {
+        super.showConversation(conversation);
         int mode = mViewMode.getMode();
         if (mode == ViewMode.SEARCH_RESULTS_LIST || mode == ViewMode.SEARCH_RESULTS_CONVERSATION) {
             mViewMode.enterSearchResultsConversationMode();