Repository: connectbot/connectbot
Issue: Swipe to send page up/down does not work

Even though I have checked "Page up/down gesture" in the settings, swiping on the left side of the screen does not send page up/down keys. It only scrolls the terminal, like the rest of the screen.

This is with the current newest master (c9682d3). The error was introduced by #277, as stated by @rhansby in #270.


Changed files:
app/src/main/java/org/connectbot/util/TerminalTextViewOverlay.java, app/src/main/java/org/connectbot/TerminalView.java, app/src/main/java/org/connectbot/service/TerminalBridge.java

diff --git a/app/src/main/java/org/connectbot/TerminalView.java b/app/src/main/java/org/connectbot/TerminalView.java
index 11e71ac3..bdeba26a 100644
--- a/app/src/main/java/org/connectbot/TerminalView.java
+++ b/app/src/main/java/org/connectbot/TerminalView.java
@@ -200,14 +200,12 @@ public class TerminalView extends FrameLayout implements FontSizeChangedListener
 			private TerminalBridge bridge = TerminalView.this.bridge;
 			private float totalY = 0;
 
+			/**
+			 * This should only handle scrolling when terminalTextViewOverlay is {@code null}, but
+			 * we need to handle the page up/down gesture if it's enabled.
+			 */
 			@Override
 			public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
-				// The terminalTextViewOverlay handles scrolling. Only handle scrolling if it
-				// is not available (i.e. on pre-Honeycomb devices).
-				if (terminalTextViewOverlay != null) {
-					return false;
-				}
-
 				// activate consider if within x tolerance
 				int touchSlop =
 						ViewConfiguration.get(TerminalView.this.context).getScaledTouchSlop();
@@ -221,7 +219,7 @@ public class TerminalView extends FrameLayout implements FontSizeChangedListener
 					// enabled.
 					boolean pgUpDnGestureEnabled =
 							prefs.getBoolean(PreferenceConstants.PG_UPDN_GESTURE, false);
-					if (e2.getX() <= getWidth() / 3 && pgUpDnGestureEnabled) {
+					if (pgUpDnGestureEnabled && e2.getX() <= getWidth() / 3) {
 						// otherwise consume as pgup/pgdown for every 5 lines
 						if (moved > 5) {
 							((vt320) bridge.buffer).keyPressed(vt320.KEY_PAGE_DOWN, ' ', 0);
@@ -232,14 +230,16 @@ public class TerminalView extends FrameLayout implements FontSizeChangedListener
 							bridge.tryKeyVibrate();
 							totalY = 0;
 						}
-					} else if (moved != 0) {
+						return true;
+					} else if (terminalTextViewOverlay == null && moved != 0) {
 						int base = bridge.buffer.getWindowBase();
 						bridge.buffer.setWindowBase(base + moved);
 						totalY = 0;
+						return false;
 					}
 				}
 
-				return true;
+				return false;
 			}
 
 			@Override
@@ -266,8 +266,8 @@ public class TerminalView extends FrameLayout implements FontSizeChangedListener
 
 	@Override
 	public boolean onTouchEvent(MotionEvent event) {
-		if (gestureDetector != null) {
-			gestureDetector.onTouchEvent(event);
+		if (gestureDetector != null && gestureDetector.onTouchEvent(event)) {
+			return true;
 		}
 
 		// Old version of copying, only for pre-Honeycomb.
@@ -341,9 +341,7 @@ public class TerminalView extends FrameLayout implements FontSizeChangedListener
 			return true;
 		}
 
-		super.onTouchEvent(event);
-
-		return true;
+		return super.onTouchEvent(event);
 	}
 
 	/**
diff --git a/app/src/main/java/org/connectbot/service/TerminalBridge.java b/app/src/main/java/org/connectbot/service/TerminalBridge.java
index d15585ce..798db16b 100644
--- a/app/src/main/java/org/connectbot/service/TerminalBridge.java
+++ b/app/src/main/java/org/connectbot/service/TerminalBridge.java
@@ -986,15 +986,8 @@ public class TerminalBridge implements VDUDisplay {
 		color = manager.colordb.getColorsForScheme(HostDatabase.DEFAULT_COLOR_SCHEME);
 	}
 
-	private static Pattern urlPattern = null;
-
-	/**
-	 * @return
-	 */
-	public List<String> scanForURLs() {
-		List<String> urls = new LinkedList<String>();
-
-		if (urlPattern == null) {
+	private static class PatternHolder {
+		static {
 			// based on http://www.ietf.org/rfc/rfc2396.txt
 			String scheme = "[A-Za-z][-+.0-9A-Za-z]*";
 			String unreserved = "[-._~0-9A-Za-z]";
@@ -1024,13 +1017,21 @@ public class TerminalBridge implements VDUDisplay {
 			String uriRegex = scheme + ":" + hierPart + "(?:" + query + ")?(?:#" + fragment + ")?";
 			urlPattern = Pattern.compile(uriRegex);
 		}
+		private static final Pattern urlPattern;
+	}
+
+	/**
+	 * @return
+	 */
+	public List<String> scanForURLs() {
+		List<String> urls = new LinkedList<String>();
 
 		char[] visibleBuffer = new char[buffer.height * buffer.width];
 		for (int l = 0; l < buffer.height; l++)
 			System.arraycopy(buffer.charArray[buffer.windowBase + l], 0,
 					visibleBuffer, l * buffer.width, buffer.width);
 
-		Matcher urlMatcher = urlPattern.matcher(new String(visibleBuffer));
+		Matcher urlMatcher = PatternHolder.urlPattern.matcher(new String(visibleBuffer));
 		while (urlMatcher.find())
 			urls.add(urlMatcher.group());
 
diff --git a/app/src/main/java/org/connectbot/util/TerminalTextViewOverlay.java b/app/src/main/java/org/connectbot/util/TerminalTextViewOverlay.java
index a3e5388c..c219ce11 100644
--- a/app/src/main/java/org/connectbot/util/TerminalTextViewOverlay.java
+++ b/app/src/main/java/org/connectbot/util/TerminalTextViewOverlay.java
@@ -75,16 +75,23 @@ public class TerminalTextViewOverlay extends TextView {
 		oldBufferHeight = numRows;
 
 		StringBuilder buffer = new StringBuilder();
+		int previousTotalLength = 0;
 
 		for (int r = 0; r < numRows && vb.charArray[r] != null; r++) {
 			for (int c = 0; c < numCols; c++) {
 				buffer.append(vb.charArray[r][c]);
 			}
-			while (buffer.length() > 0 &&
+
+			// Truncate all the new whitespace without removing the old data.
+			while (buffer.length() > previousTotalLength &&
 					Character.isWhitespace(buffer.charAt(buffer.length() - 1))) {
 				buffer.setLength(buffer.length() - 1);
 			}
+
+			// Make sure each line ends with a carriage return and then remember the buffer
+			// at that length.
 			buffer.append('\\n');
+			previousTotalLength = buffer.length();
 		}
 
 		oldScrollY = vb.getWindowBase() * getLineHeight();
@@ -184,12 +191,12 @@ public class TerminalTextViewOverlay extends TextView {
 			}
 			terminalView.viewPager.setPagingEnabled(true);
 		} else {
-			terminalView.onTouchEvent(event);
+			if (terminalView.onTouchEvent(event)) {
+				return true;
+			}
 		}
 
-		super.onTouchEvent(event);
-
-		return true;
+		return super.onTouchEvent(event);
 	}
 
 	@Override