From 022940d4f5f58db7526a5469908f0258ce850bf3 Mon Sep 17 00:00:00 2001 From: m0viefreak Date: Tue, 1 May 2012 01:56:06 +0200 Subject: [PATCH] Fix gesture detection This commit addresses 2 issues: 1) Before, a general GestureDetector was registered on the highest level in K9Activity This resulted in EVERY inherited activity to have a useless, unused gesture detector. But more than that, in MessageList, a second GestureDetector was assigned to the ListView. On every fling gesture, both detectors called the onSwipe() methods, which technically did the following: - The one directly assigned to the ListView would work corectly by mapping the (local) event coordinates to the right entry in the ListView - The global one worked on screen coordinates, so the onSwipe() method would likely select the wrong ListView entry (system menu bar offset). - For some reason this "worked" fine, and only the correct entry was selected, despite two detectors used. 2) The gesture detection for the MessageView caused problems when the message itself was scrollable, i.e. wide HTML mails. A fling gesture inside the WebView would scroll the message, but also switch the message. This commit fixes all those by doing the following: - Don't register the GestureDetector in K9Activity, instead make the member variable accessible by subclasses. - In the subclasses that need a detector register it - In K9Activity.dispatchTouchEvent() check for mGestureDetector being null - For MessageList: * Remove the duplicate gesture detector assigned to the ListView * in the handleSwipe() methods: calclulate pixel offset of the ListView to make it work using the global screen coordinates - For MessageView: Limit sensitive area to the message header, to prevent interference with the WebView scrolling - Respect current behavior: * Force-enable gestures for the MessageList * Respect user setting in MessageView - Make sure that after a successful swipe gesture, any pending action is cancelled, to prevent unwanted things to happen (such as expanding the header after changing the message, or a context menu popping up in the MessageList). See http://code.google.com/p/android/issues/detail?id=8497 --- src/com/fsck/k9/activity/K9Activity.java | 16 +++++++++------- src/com/fsck/k9/activity/MessageList.java | 21 ++++++--------------- src/com/fsck/k9/activity/MessageView.java | 18 ++++++++++++++++-- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/com/fsck/k9/activity/K9Activity.java b/src/com/fsck/k9/activity/K9Activity.java index c346ab71c..d7a2f2438 100644 --- a/src/com/fsck/k9/activity/K9Activity.java +++ b/src/com/fsck/k9/activity/K9Activity.java @@ -19,7 +19,7 @@ import com.fsck.k9.K9; public class K9Activity extends Activity { - private GestureDetector gestureDetector; + protected GestureDetector mGestureDetector; @Override public void onCreate(Bundle icicle) { @@ -27,10 +27,6 @@ public class K9Activity extends Activity { setTheme(K9.getK9ThemeResourceId()); super.onCreate(icicle); setupFormats(); - - // Gesture detection - gestureDetector = new GestureDetector(new MyGestureDetector()); - } public static void setLanguage(Context context, String language) { @@ -51,8 +47,10 @@ public class K9Activity extends Activity { @Override public boolean dispatchTouchEvent(MotionEvent ev) { - super.dispatchTouchEvent(ev); - return gestureDetector.onTouchEvent(ev); + if (mGestureDetector != null) { + mGestureDetector.onTouchEvent(ev); + } + return super.dispatchTouchEvent(ev); } @Override @@ -184,6 +182,10 @@ public class K9Activity extends Activity { Log.d(K9.LOG_TAG, "New swipe algorithm: Swipe did not meet minimum distance requirements."); return false; } + + // successful fling, cancel the 2nd event to prevent any other action from happening + // see http://code.google.com/p/android/issues/detail?id=8497 + e2.setAction(MotionEvent.ACTION_CANCEL); } catch (Exception e) { // nothing } diff --git a/src/com/fsck/k9/activity/MessageList.java b/src/com/fsck/k9/activity/MessageList.java index 535ddee04..fb2714269 100644 --- a/src/com/fsck/k9/activity/MessageList.java +++ b/src/com/fsck/k9/activity/MessageList.java @@ -690,6 +690,9 @@ public class MessageList mPreviewLines = K9.messageListPreviewLines(); initializeMessageList(getIntent(), true); + + // Enable gesture detection for MessageLists + mGestureDetector = new GestureDetector(new MyGestureDetector(true)); } @Override @@ -924,20 +927,6 @@ public class MessageList mBatchMoveButton.setVisibility(K9.batchButtonsMove() ? View.VISIBLE : View.GONE); mBatchFlagButton.setVisibility(K9.batchButtonsFlag() ? View.VISIBLE : View.GONE); mBatchDoneButton.setVisibility(K9.batchButtonsUnselect() ? View.VISIBLE : View.GONE); - - // Gesture detection - gestureDetector = new GestureDetector(new MyGestureDetector(true)); - gestureListener = new View.OnTouchListener() { - @Override - public boolean onTouch(View v, MotionEvent event) { - if (gestureDetector.onTouchEvent(event)) { - return true; - } - return false; - } - }; - - mListView.setOnTouchListener(gestureListener); } /** @@ -1807,7 +1796,9 @@ public class MessageList * @param selected true if this was an attempt to select (i.e. left to right). */ private void handleSwipe(final MotionEvent downMotion, final boolean selected) { - int position = mListView.pointToPosition((int) downMotion.getX(), (int) downMotion.getY()); + int[] listPosition = new int[2]; + mListView.getLocationOnScreen(listPosition); + int position = mListView.pointToPosition((int) downMotion.getRawX() - listPosition[0], (int) downMotion.getRawY() - listPosition[1]); if (position != AdapterView.INVALID_POSITION) { MessageInfoHolder msgInfoHolder = (MessageInfoHolder) mAdapter.getItem(position); diff --git a/src/com/fsck/k9/activity/MessageView.java b/src/com/fsck/k9/activity/MessageView.java index 5dcfcc6d1..5465f3fbb 100644 --- a/src/com/fsck/k9/activity/MessageView.java +++ b/src/com/fsck/k9/activity/MessageView.java @@ -4,6 +4,7 @@ import android.app.Dialog; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; +import android.graphics.Rect; import android.net.Uri; import android.os.Bundle; import android.os.Handler; @@ -47,6 +48,7 @@ public class MessageView extends K9Activity implements OnClickListener { private View mArchive; private View mMove; private View mSpam; + private View mHeader; private Account mAccount; private MessageReference mMessageReference; private ArrayList mMessageReferences; @@ -293,6 +295,7 @@ public class MessageView extends K9Activity implements OnClickListener { setContentView(R.layout.message_view); mMessageView = (SingleMessageView) findViewById(R.id.message_view); + mHeader = findViewById(R.id.header_container); //set a callback for the attachment view. With this callback the attachmentview //request the start of a filebrowser activity. @@ -379,6 +382,9 @@ public class MessageView extends K9Activity implements OnClickListener { mNext.requestFocus(); } + // Enable gesture detection for MessageViews + mGestureDetector = new GestureDetector(new MyGestureDetector(false)); + setupButtonViews(); displayMessage(mMessageReference); } @@ -715,7 +721,8 @@ public class MessageView extends K9Activity implements OnClickListener { */ @Override protected void onSwipeRightToLeft(MotionEvent e1, MotionEvent e2) { - onNext(); + if (isEventInsideHeader(e1)) + onNext(); } /** @@ -723,7 +730,14 @@ public class MessageView extends K9Activity implements OnClickListener { */ @Override protected void onSwipeLeftToRight(MotionEvent e1, MotionEvent e2) { - onPrevious(); + if (isEventInsideHeader(e1)) + onPrevious(); + } + + private boolean isEventInsideHeader(MotionEvent e) { + Rect headerRect = new Rect(); + mHeader.getGlobalVisibleRect(headerRect); + return headerRect.contains((int)e.getRawX(), (int)e.getRawY()); } protected void onNext() {