From 1eb37c2031852fef449977604180656dc8bf719c Mon Sep 17 00:00:00 2001 From: cketti Date: Wed, 7 Jul 2010 22:14:45 +0000 Subject: [PATCH] Added menu option to select & copy text in the message view. The standard ScrollView causes problems when selecting text (and the message is long enough so scrolling is possible). The newly introduced ToggleScrollView can be set to disable scrolling via touch gestures thus making copying multi-line text sections possible. Fixes issue 662 --- res/layout-land/message_view.xml | 4 +- res/layout/message_view.xml | 4 +- res/menu/message_view_option.xml | 4 ++ res/values/strings.xml | 1 + src/com/fsck/k9/activity/MessageView.java | 49 ++++++++++++++++++- .../fsck/k9/activity/ToggleScrollView.java | 33 +++++++++++++ 6 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 src/com/fsck/k9/activity/ToggleScrollView.java diff --git a/res/layout-land/message_view.xml b/res/layout-land/message_view.xml index 26c9866d2..a7aa0977e 100644 --- a/res/layout-land/message_view.xml +++ b/res/layout-land/message_view.xml @@ -5,7 +5,7 @@ android:layout_width="fill_parent" android:layout_height="fill_parent" > - - + - - + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 8b7e911fb..d75520f66 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -92,6 +92,7 @@ Copy Show full header Hide full header + Select text Mark as unread Move to diff --git a/src/com/fsck/k9/activity/MessageView.java b/src/com/fsck/k9/activity/MessageView.java index 1eb6e0525..a2d5fa674 100644 --- a/src/com/fsck/k9/activity/MessageView.java +++ b/src/com/fsck/k9/activity/MessageView.java @@ -40,6 +40,7 @@ import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; +import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; @@ -52,7 +53,6 @@ import android.widget.Button; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.LinearLayout; -import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; @@ -111,6 +111,7 @@ public class MessageView extends K9Activity implements OnClickListener private View mArchiveScrolling; private View mMoveScrolling; private View mSpamScrolling; + private ToggleScrollView mToggleScrollView; private Account mAccount; private MessageReference mMessageReference; @@ -151,6 +152,17 @@ public class MessageView extends K9Activity implements OnClickListener } } + @Override + public boolean dispatchTouchEvent(MotionEvent ev) + { + if (ev.getAction() == MotionEvent.ACTION_UP) + { + // Text selection is finished. Allow scrolling again. + mToggleScrollView.setScrolling(true); + } + + return super.dispatchTouchEvent(ev); + } @Override public boolean dispatchKeyEvent(KeyEvent event) @@ -173,6 +185,16 @@ public class MessageView extends K9Activity implements OnClickListener { switch (keyCode) { + case KeyEvent.KEYCODE_SHIFT_LEFT: + case KeyEvent.KEYCODE_SHIFT_RIGHT: + { + /* + * Selecting text started via shift key. Disable scrolling as + * this causes problems when selecting text. + */ + mToggleScrollView.setScrolling(false); + break; + } case KeyEvent.KEYCODE_DEL: { onDelete(); @@ -644,7 +666,7 @@ public class MessageView extends K9Activity implements OnClickListener mDateView = (TextView)findViewById(R.id.date); mTimeView = (TextView)findViewById(R.id.time); - mTopView = (ScrollView)findViewById(R.id.top_view); + mTopView = mToggleScrollView = (ToggleScrollView)findViewById(R.id.top_view); mMessageContentView = (WebView)findViewById(R.id.message_content); mAttachments = (LinearLayout)findViewById(R.id.attachments); @@ -1536,6 +1558,9 @@ public class MessageView extends K9Activity implements OnClickListener case R.id.show_full_header: onShowAdditionalHeaders(); break; + case R.id.select_text: + emulateShiftHeld(mMessageContentView); + break; default: return super.onOptionsItemSelected(item); } @@ -2114,4 +2139,24 @@ public class MessageView extends K9Activity implements OnClickListener slide.setInterpolator(new AccelerateInterpolator()); return slide; } + + /** + * Emulate the shift key being pressed to trigger the text selection mode + * of a WebView. + */ + private void emulateShiftHeld(WebView view) + { + try + { + mToggleScrollView.setScrolling(false); + + KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, + KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0); + shiftPressEvent.dispatch(view); + } + catch (Exception e) + { + Log.e(K9.LOG_TAG, "Exception in emulateShiftHeld()", e); + } + } } diff --git a/src/com/fsck/k9/activity/ToggleScrollView.java b/src/com/fsck/k9/activity/ToggleScrollView.java new file mode 100644 index 000000000..46af9c77f --- /dev/null +++ b/src/com/fsck/k9/activity/ToggleScrollView.java @@ -0,0 +1,33 @@ +package com.fsck.k9.activity; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.widget.ScrollView; + +public class ToggleScrollView extends ScrollView +{ + private boolean mScrolling = true; + + public ToggleScrollView(Context context, AttributeSet attrs) + { + super(context, attrs); + } + + public void setScrolling(boolean enable) + { + mScrolling = enable; + } + + @Override + public boolean onTouchEvent(MotionEvent ev) + { + return (mScrolling) ? super.onTouchEvent(ev) : true; + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent ev) + { + return (mScrolling) ? super.onInterceptTouchEvent(ev) : false; + } +}