Conversation Position Tracker used for destructive actions

Change-Id: Id860fa76adbf28f013354506741c61a809a0ade7
diff --git a/src/com/android/mail/ui/AbstractActivityController.java b/src/com/android/mail/ui/AbstractActivityController.java
index c0908e9..6f9b90f 100644
--- a/src/com/android/mail/ui/AbstractActivityController.java
+++ b/src/com/android/mail/ui/AbstractActivityController.java
@@ -142,7 +142,9 @@
     private final int mFolderItemUpdateDelayMs;
 
     /** Keeps track of selected and unselected conversations */
-    private final ConversationPositionTracker mTracker = new ConversationPositionTracker();
+    private final ConversationPositionTracker mTracker =
+            new ConversationPositionTracker(mSelectedSet);
+
     /**
      * Action menu associated with the selected set.
      */
@@ -269,6 +271,7 @@
                                         .addListener(AbstractActivityController.this);
                                 mConversationListenerAdded = true;
                             }
+                            mTracker.updateCursor(mConversationListCursor);
                         }
                         if (shouldShowFirstConversation()) {
                             if (mConversationListCursor.getCount() > 0) {
@@ -971,10 +974,7 @@
      */
     protected void setCurrentConversation(Conversation conversation) {
         mCurrentConversation = conversation;
-        // TODO(viki); What position are we viewing?
         mTracker.initialize(mCurrentConversation);
-        mTracker.updateAdapterAndCursor(mConversationListFragment.getAnimatedAdapter(),
-                mConversationListCursor);
     }
 
     /**
@@ -1317,25 +1317,17 @@
             }
         }
 
+        /**
+         * Get the next conversation according to the AutoAdvance settings and the list of
+         * conversations available in the folder.
+         * @return
+         */
         public Conversation getNextConversation() {
-            Conversation next = null;
-            int pref = getAutoAdvanceSetting(mActivity);
-            Cursor c = mConversationListCursor;
-            if (c != null) {
-                c.moveToPosition(mCurrentConversation.position);
-            }
-            switch (pref) {
-                case AutoAdvance.NEWER:
-                    if (c.moveToPrevious()) {
-                        next = new Conversation(c);
-                    }
-                    break;
-                case AutoAdvance.OLDER:
-                    if (c.moveToNext()) {
-                        next = new Conversation(c);
-                    }
-                    break;
-            }
+            final int pref = getAutoAdvanceSetting(mActivity);
+            final boolean getNewer = (pref == AutoAdvance.NEWER && mTracker.hasNewer());
+            final boolean getOlder = (pref == AutoAdvance.OLDER && mTracker.hasOlder());
+            final Conversation next = getNewer ? mTracker.getNewer() :
+                (getOlder ? mTracker.getOlder() : null);
             return next;
         }
 
@@ -1387,6 +1379,7 @@
     public void onRefreshRequired() {
         // Refresh the query in the background
         getConversationListCursor().refresh();
+        mTracker.updateCursor(mConversationListCursor);
     }
 
     @Override