When there is no swipe, use a different touch handler.

Fixes b/ 6291400 Multiple messages are selected by scrolling up and down in Inbox
Fixes b/6291013 java.lang.ClassCastException: com.android.mail.browse.ConversationListFooterView cannot be cast to com.android.mail.browse.ConversationItemView
Change-Id: I44e5a21326a620405cbdf769e736d37bbda5ceac
diff --git a/src/com/android/mail/browse/ConversationItemView.java b/src/com/android/mail/browse/ConversationItemView.java
index a66b5c8..af22776 100644
--- a/src/com/android/mail/browse/ConversationItemView.java
+++ b/src/com/android/mail/browse/ConversationItemView.java
@@ -158,6 +158,7 @@
     private boolean mCheckboxesEnabled;
     private CheckForTap mPendingCheckForTap;
     private CheckForLongPress mPendingCheckForLongPress;
+    private boolean mSwipeEnabled;
     private static Bitmap MORE_FOLDERS;
 
     static {
@@ -361,24 +362,26 @@
     }
 
     public void bind(Cursor cursor, ViewMode viewMode, ConversationSelectionSet set,
-            Folder folder, boolean checkboxesEnabled) {
+            Folder folder, boolean checkboxesEnabled, boolean swipeEnabled) {
         mViewMode = viewMode;
         mHeader = ConversationItemViewModel.forCursor(cursor);
         mSelectedConversationSet = set;
         mDisplayedFolder = folder;
         mCheckboxesEnabled = checkboxesEnabled;
+        mSwipeEnabled = swipeEnabled;
         setContentDescription(mHeader.getContentDescription(mContext));
         requestLayout();
     }
 
 
     public void bind(Conversation conversation, ViewMode viewMode, ConversationSelectionSet set,
-            Folder folder, boolean checkboxesEnabled) {
+            Folder folder, boolean checkboxesEnabled, boolean swipeEnabled) {
         mViewMode = viewMode;
         mHeader = ConversationItemViewModel.forConversation(conversation);
         mSelectedConversationSet = set;
         mDisplayedFolder = folder;
         mCheckboxesEnabled = checkboxesEnabled;
+        mSwipeEnabled = swipeEnabled;
         setContentDescription(mHeader.getContentDescription(mContext));
         requestLayout();
     }
@@ -1069,6 +1072,9 @@
      */
     @Override
     public boolean onTouchEvent(MotionEvent event) {
+        if (!mSwipeEnabled) {
+            return onTouchEventNoSwipe(event);
+        }
         boolean handled = true;
 
         int x = (int) event.getX();
@@ -1130,6 +1136,54 @@
         return handled;
     }
 
+    private boolean onTouchEventNoSwipe(MotionEvent event) {
+        boolean handled = true;
+
+        int x = (int) event.getX();
+        int y = (int) event.getY();
+        switch (event.getAction()) {
+            case MotionEvent.ACTION_DOWN:
+                mDownEvent = true;
+                // In order to allow the down event and subsequent move events
+                // to bubble to the swipe handler, we need to return that all
+                // down events are handled.
+                handled = isTouchInCheckmark(x, y) || isTouchInStar(x, y);
+                break;
+            case MotionEvent.ACTION_CANCEL:
+                mDownEvent = false;
+                break;
+            case MotionEvent.ACTION_UP:
+                if (mDownEvent) {
+                    // ConversationItemView gets the first chance to handle up
+                    // events if there was a down event and there was no move
+                    // event in between. In this case, ConversationItemView
+                    // received the down event, and then an up event in the
+                    // same location (+/- slop). Treat this as a click on the
+                    // view or on a specific part of the view.
+                    if (isTouchInCheckmark(x, y)) {
+                        // Touch on the check mark
+                        toggleCheckMark();
+                    } else if (isTouchInStar(x, y)) {
+                        // Touch on the star
+                        toggleStar();
+                    }
+                    handled = true;
+                } else {
+                    // There was no down event that this view was made aware of,
+                    // therefore it cannot handle it.
+                    handled = false;
+                }
+                break;
+        }
+
+        if (!handled) {
+            // Let View try to handle it as well.
+            handled = super.onTouchEvent(event);
+        }
+
+        return handled;
+    }
+
     /**
      * Return if this item should respond to long clicks.
      */
diff --git a/src/com/android/mail/ui/AnimatedAdapter.java b/src/com/android/mail/ui/AnimatedAdapter.java
index 57848e0..d0f7b17 100644
--- a/src/com/android/mail/ui/AnimatedAdapter.java
+++ b/src/com/android/mail/ui/AnimatedAdapter.java
@@ -58,6 +58,7 @@
     private Folder mFolder;
     private final ListView mListView;
     private Settings mCachedSettings;
+    private boolean mSwipeEnabled;
     /**
      * Used only for debugging.
      */
@@ -77,6 +78,7 @@
         mShowFooter = false;
         mListView = listView;
         mCachedSettings = settings;
+        mSwipeEnabled = account.supportsCapability(UIProvider.AccountCapabilities.ARCHIVE);
     }
 
     @Override
@@ -111,7 +113,8 @@
     public void bindView(View view, Context context, Cursor cursor) {
         if (!isPositionAnimating(view) && !isPositionFooter(view)) {
             ((ConversationItemView) view).bind(cursor, mViewMode, mBatchConversations, mFolder,
-                    mCachedSettings != null ? !mCachedSettings.hideCheckboxes : false);
+                    mCachedSettings != null ? !mCachedSettings.hideCheckboxes : false,
+                    mSwipeEnabled);
         }
     }
 
@@ -250,7 +253,8 @@
             ConversationItemView convView = (ConversationItemView) super.getView(position, null,
                     mListView);
             convView.bind(conversation, mViewMode, mBatchConversations, mFolder,
-                    mCachedSettings != null ? !mCachedSettings.hideCheckboxes : false);
+                    mCachedSettings != null ? !mCachedSettings.hideCheckboxes : false,
+                    mSwipeEnabled);
             convView.startUndoAnimation(mViewMode, this);
             return convView;
         } else {
diff --git a/src/com/android/mail/ui/SwipeHelper.java b/src/com/android/mail/ui/SwipeHelper.java
index 96715ad..8a50d49 100644
--- a/src/com/android/mail/ui/SwipeHelper.java
+++ b/src/com/android/mail/ui/SwipeHelper.java
@@ -189,7 +189,10 @@
             case MotionEvent.ACTION_DOWN:
                 mLastY = ev.getY();
                 mDragging = false;
-                mCurrView = (ConversationItemView)mCallback.getChildAtPosition(ev);
+                View view = mCallback.getChildAtPosition(ev);
+                if (view instanceof ConversationItemView) {
+                    mCurrView = (ConversationItemView) view;
+                }
                 mVelocityTracker.clear();
                 if (mCurrView != null) {
                     mCurrAnimView = mCallback.getChildContentView(mCurrView);
diff --git a/src/com/android/mail/ui/SwipeableListView.java b/src/com/android/mail/ui/SwipeableListView.java
index cfc6472..5f53656 100644
--- a/src/com/android/mail/ui/SwipeableListView.java
+++ b/src/com/android/mail/ui/SwipeableListView.java
@@ -72,6 +72,10 @@
         mEnableSwipe = enable;
     }
 
+    public boolean isSwipeEnabled() {
+        return mEnableSwipe;
+    }
+
     public void setSwipeCompleteListener(SwipeCompleteListener listener) {
         mSwipeCompleteListener = listener;
     }