diff --git a/res/layout/message_view.xml b/res/layout/message_view.xml index ce623229b..3fc9aff5d 100644 --- a/res/layout/message_view.xml +++ b/res/layout/message_view.xml @@ -39,7 +39,7 @@ android:paddingTop="2dip" android:background="#101010"> 0) { - mPrevNextCursor.moveToPrevious(); - mMessageId = mPrevNextCursor.getLong(0); + private boolean moveToNewer() { + // Guard with !isFirst() because Cursor.moveToPrev() returns false even as it moves + // from first to before-first. + if (mMessageListCursor != null + && !mMessageListCursor.isFirst() + && mMessageListCursor.moveToPrevious()) { + mMessageId = mMessageListCursor.getLong(0); messageChanged(); return true; } @@ -715,11 +732,11 @@ public class MessageView extends Activity implements OnClickListener { case R.id.delete: onDelete(); break; - case R.id.next: - onNext(); + case R.id.moveToOlder: + moveToOlder(); break; - case R.id.previous: - onPrevious(); + case R.id.moveToNewer: + moveToNewer(); break; case R.id.download: onDownloadAttachment((AttachmentInfo) view.getTag()); @@ -806,64 +823,44 @@ public class MessageView extends Activity implements OnClickListener { // Start an AsyncTask to make a new cursor and load the message mLoadMessageTask = new LoadMessageTask(mMessageId, true); mLoadMessageTask.execute(); - updatePrevNextArrows(mPrevNextCursor); + updateNavigationArrows(mMessageListCursor); } /** - * Reposition the next/prev cursor. Finish() the activity if we are no longer + * Reposition the older/newer cursor. Finish() the activity if we are no longer * in the list. Update the UI arrows as appropriate. */ - private void repositionPrevNextCursor() { + private void repositionMessageListCursor() { if (Email.DEBUG) { Email.log("MessageView: reposition to id=" + mMessageId); } // position the cursor on the current message - mPrevNextCursor.moveToPosition(-1); - while (mPrevNextCursor.moveToNext() && mPrevNextCursor.getLong(0) != mMessageId) { + mMessageListCursor.moveToPosition(-1); + while (mMessageListCursor.moveToNext() && mMessageListCursor.getLong(0) != mMessageId) { } - if (mPrevNextCursor.isAfterLast()) { + if (mMessageListCursor.isAfterLast()) { // overshoot - get out now, the list is no longer valid finish(); } - updatePrevNextArrows(mPrevNextCursor); + updateNavigationArrows(mMessageListCursor); } /** - * Based on the current position of the prev/next cursor, update the prev / next arrows. + * Update the arrows based on the current position of the older/newer cursor. */ - private void updatePrevNextArrows(Cursor cursor) { + private void updateNavigationArrows(Cursor cursor) { if (cursor != null) { - boolean hasPrev, hasNext; + boolean hasNewer, hasOlder; if (cursor.isAfterLast() || cursor.isBeforeFirst()) { // The cursor not being on a message means that the current message was not found. // While this should not happen, simply disable prev/next arrows in that case. - hasPrev = hasNext = false; + hasNewer = hasOlder = false; } else { - hasPrev = !cursor.isFirst(); - hasNext = !cursor.isLast(); - } - mPrevious.setVisibility(hasPrev ? View.VISIBLE : View.INVISIBLE); - mNext.setVisibility(hasNext ? View.VISIBLE : View.INVISIBLE); - } - } - - /** - * This observer is used to watch for external changes to the next/prev list - */ - private class NextPrevObserver extends ContentObserver { - - public NextPrevObserver(Handler handler) { - super(handler); - } - - @Override - public void onChange(boolean selfChange) { - // get a new next/prev cursor, but only if we already had one - // (otherwise it's "too soon" and other pathways will cause it to be loaded) - if (mLoadPrevNextTask == null && mPrevNextCursor != null) { - mLoadPrevNextTask = new LoadPrevNextTask(mMailboxId); - mLoadPrevNextTask.execute(); + hasNewer = !cursor.isFirst(); + hasOlder = !cursor.isLast(); } + mMoveToNewer.setVisibility(hasNewer ? View.VISIBLE : View.INVISIBLE); + mMoveToOlder.setVisibility(hasOlder ? View.VISIBLE : View.INVISIBLE); } } @@ -1064,10 +1061,10 @@ public class MessageView extends Activity implements OnClickListener { * It generates the same cursor as the one used in MessageList (but with an id-only projection), * scans through it until finds the current messageId, and takes the previous and next ids. */ - private class LoadPrevNextTask extends AsyncTask { + private class LoadMessageListTask extends AsyncTask { private long mLocalMailboxId; - public LoadPrevNextTask(long mailboxId) { + public LoadMessageListTask(long mailboxId) { mLocalMailboxId = mailboxId; } @@ -1088,16 +1085,16 @@ public class MessageView extends Activity implements OnClickListener { return; } // remove the reference to ourselves so another one can be launched - MessageView.this.mLoadPrevNextTask = null; + MessageView.this.mLoadMessageListTask = null; if (cursor.isClosed()) { return; } // replace the older cursor if there is one - closePrevNextCursor(); - mPrevNextCursor = cursor; - mPrevNextCursor.registerContentObserver(MessageView.this.mNextPrevObserver); - repositionPrevNextCursor(); + closeMessageListCursor(); + mMessageListCursor = cursor; + mMessageListCursor.registerContentObserver(MessageView.this.mCursorObserver); + repositionMessageListCursor(); } } @@ -1252,10 +1249,10 @@ public class MessageView extends Activity implements OnClickListener { if (mMailboxId == -1) { mMailboxId = message.mMailboxKey; } - // only start LoadPrevNextTask here if it's the first time - if (mPrevNextCursor == null) { - mLoadPrevNextTask = new LoadPrevNextTask(mMailboxId); - mLoadPrevNextTask.execute(); + // only start LoadMessageListTask here if it's the first time + if (mMessageListCursor == null) { + mLoadMessageListTask = new LoadMessageListTask(mMailboxId); + mLoadMessageListTask.execute(); } mSubjectView.setText(message.mSubject);