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
This commit is contained in:
m0viefreak 2012-05-01 01:56:06 +02:00
parent 9e1fa63139
commit 022940d4f5
3 changed files with 31 additions and 24 deletions

View File

@ -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
}

View File

@ -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);

View File

@ -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<MessageReference> 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() {