aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/org/connectbot/util/TerminalTextViewOverlay.java
blob: 6ceb64d5d6a2e7a1ffa34e6c9aa697d261e40bc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * ConnectBot: simple, powerful, open-source SSH client for Android
 * Copyright 2015 Kenny Root, Jeffrey Sharkey
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.connectbot.util;

import org.connectbot.R;
import org.connectbot.TerminalView;
import org.connectbot.service.TerminalBridge;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v4.view.MotionEventCompat;
import android.text.ClipboardManager;
import android.view.ActionMode;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.TextView;
import de.mud.terminal.VDUBuffer;
import de.mud.terminal.vt320;

/**
 * Custom TextView {@link TextView} which is intended to (invisibly) be on top of the TerminalView
 * (@link TerminalView) in order to allow the user to select and copy the text of the bitmap below.
 *
 * @author rhansby
 */
@TargetApi(11)
public class TerminalTextViewOverlay extends TextView {
	public TerminalView terminalView; // ryan: this name sucks
	private String currentSelection = "";
	private ActionMode selectionActionMode;
	private ClipboardManager clipboard;

	private int oldBufferHeight = 0;
	private int oldScrollY = -1;

	public TerminalTextViewOverlay(Context context, TerminalView terminalView) {
		super(context);

		this.terminalView = terminalView;
		clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);

		setTextColor(Color.TRANSPARENT);
		setTypeface(Typeface.MONOSPACE);
		setTextIsSelectable(true);
		setCustomSelectionActionModeCallback(new TextSelectionActionModeCallback());
	}

	public void refreshTextFromBuffer() {
		VDUBuffer vb = terminalView.bridge.getVDUBuffer();
		int numRows = vb.getBufferSize();
		int numCols = vb.getColumns() - 1;
		oldBufferHeight = numRows;

		String line = "";
		String buffer = "";

		for (int r = 0; r < numRows && vb.charArray[r] != null; r++) {
			for (int c = 0; c < numCols; c++) {
				line += vb.charArray[r][c];
			}
			buffer += line.replaceAll("\\s+$", "") + "\n";
			line = "";
		}

		oldScrollY = vb.getWindowBase() * getLineHeight();

		setText(buffer);
	}

	/**
	 * If there is a new line in the buffer, add an empty line
	 * in this TextView, so that selection seems to move up with the
	 * rest of the buffer.
	 */
	public void onBufferChanged() {
		VDUBuffer vb = terminalView.bridge.getVDUBuffer();
		int numRows = vb.getBufferSize();
		int numNewRows = numRows - oldBufferHeight;

		if (numNewRows <= 0) {
			return;
		}

		String newLines = "";
		for (int i = 0; i < numNewRows; i++) {
			newLines += "\n";
		}

		oldScrollY = (vb.getWindowBase() + numNewRows) * getLineHeight();
		oldBufferHeight = numRows;

		append(newLines);
	}

	@Override
	public boolean onPreDraw() {
		boolean superResult = super.onPreDraw();

		if (oldScrollY >= 0) {
			scrollTo(0, oldScrollY);
			oldScrollY = -1;
		}

		return superResult;
	}

	private void closeSelectionActionMode() {
		if (selectionActionMode != null) {
			selectionActionMode.finish();
			selectionActionMode = null;
		}
	}

	public void copyCurrentSelectionToClipboard() {
		if (currentSelection.length() != 0) {
			clipboard.setText(currentSelection);
		}
		closeSelectionActionMode();
	}

	private void pasteClipboard() {
		String clip = "";
		if (clipboard.hasText()) {
			clip = clipboard.getText().toString();
		}
		terminalView.bridge.injectString(clip);
	}

	@Override
	protected void onSelectionChanged(int selStart, int selEnd) {
		if (selStart <= selEnd) {
			currentSelection = getText().toString().substring(selStart, selEnd);
		}
		super.onSelectionChanged(selStart, selEnd);
	}

	@Override
	public void scrollTo(int x, int y) {
		int lineMultiple = y / getLineHeight();

		TerminalBridge bridge = terminalView.bridge;
		bridge.buffer.setWindowBase(lineMultiple);

		super.scrollTo(0, lineMultiple * getLineHeight());
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			// Selection may be beginning. Sync the TextView with the buffer.
			refreshTextFromBuffer();
		}

		// Mouse input is treated differently:
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
				MotionEventCompat.getSource(event) == InputDevice.SOURCE_MOUSE) {
			if (onMouseEvent(event, terminalView.bridge)) {
				return true;
			}
			terminalView.viewPager.setPagingEnabled(true);
		} else {
			terminalView.onTouchEvent(event);
		}

		super.onTouchEvent(event);

		return true;
	}

	@Override
	@TargetApi(12)
	public boolean onGenericMotionEvent(MotionEvent event) {
		if ((MotionEventCompat.getSource(event) & InputDevice.SOURCE_CLASS_POINTER) != 0) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_SCROLL:
				// Process scroll wheel movement:
				float yDistance = MotionEventCompat.getAxisValue(event, MotionEvent.AXIS_VSCROLL);

				vt320 vtBuffer = (vt320) terminalView.bridge.buffer;
				boolean mouseReport = vtBuffer.isMouseReportEnabled();
				if (mouseReport) {
					int row = (int) Math.floor(event.getY() / terminalView.bridge.charHeight);
					int col = (int) Math.floor(event.getX() / terminalView.bridge.charWidth);

					vtBuffer.mouseWheel(
							yDistance > 0,
							col,
							row,
							(event.getMetaState() & KeyEvent.META_CTRL_ON) != 0,
							(event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0,
							(event.getMetaState() & KeyEvent.META_META_ON) != 0);
					return true;
				}
			}
		}

		return super.onGenericMotionEvent(event);
	}

	/**
	 * @param event
	 * @param bridge
	 * @return True if the event is handled.
	 */
	@TargetApi(14)
	private boolean onMouseEvent(MotionEvent event, TerminalBridge bridge) {
		int row = (int) Math.floor(event.getY() / bridge.charHeight);
		int col = (int) Math.floor(event.getX() / bridge.charWidth);
		int meta = event.getMetaState();
		boolean shiftOn = (meta & KeyEvent.META_SHIFT_ON) != 0;
		vt320 vtBuffer = (vt320) bridge.buffer;
		boolean mouseReport = vtBuffer.isMouseReportEnabled();

		// MouseReport can be "defeated" using the shift key.
		if (!mouseReport || shiftOn) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				if (event.getButtonState() == MotionEvent.BUTTON_TERTIARY) {
					// Middle click pastes.
					pasteClipboard();
					return true;
				}

				// Begin "selection mode"

				if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
					closeSelectionActionMode();
				}
			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				// In the middle of selection.

				if (selectionActionMode == null) {
					selectionActionMode = startActionMode(new TextSelectionActionModeCallback());
				}

				int selectionStart = getSelectionStart();
				int selectionEnd = getSelectionEnd();

				if (selectionStart > selectionEnd) {
					int tempStart = selectionStart;
					selectionStart = selectionEnd;
					selectionEnd = tempStart;
				}

				currentSelection = getText().toString().substring(selectionStart, selectionEnd);
			}
		} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
			terminalView.viewPager.setPagingEnabled(false);
			vtBuffer.mousePressed(
					col, row, mouseEventToJavaModifiers(event));
			return true;
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			terminalView.viewPager.setPagingEnabled(true);
			vtBuffer.mouseReleased(col, row);
			return true;
		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			int buttonState = event.getButtonState();
			int button = (buttonState & MotionEvent.BUTTON_PRIMARY) != 0 ? 0 :
					(buttonState & MotionEvent.BUTTON_SECONDARY) != 0 ? 1 :
							(buttonState & MotionEvent.BUTTON_TERTIARY) != 0 ? 2 : 3;
			vtBuffer.mouseMoved(
					button,
					col,
					row,
					(meta & KeyEvent.META_CTRL_ON) != 0,
					(meta & KeyEvent.META_SHIFT_ON) != 0,
					(meta & KeyEvent.META_META_ON) != 0);
			return true;
		}

		return false;
	}

	/**
	 * Takes an android mouse event and produces a Java InputEvent modifiers int which can be
	 * passed to vt320.
	 * @param mouseEvent The {@link MotionEvent} which should be a mouse click or release.
	 * @return A Java InputEvent modifier int. See
	 * http://docs.oracle.com/javase/7/docs/api/java/awt/event/InputEvent.html
	 */
	@TargetApi(14)
	private static int mouseEventToJavaModifiers(MotionEvent mouseEvent) {
		if (MotionEventCompat.getSource(mouseEvent) != InputDevice.SOURCE_MOUSE) return 0;

		int mods = 0;

		// See http://docs.oracle.com/javase/7/docs/api/constant-values.html
		int buttonState = mouseEvent.getButtonState();
		if ((buttonState & MotionEvent.BUTTON_PRIMARY) != 0)
			mods |= 16;
		if ((buttonState & MotionEvent.BUTTON_SECONDARY) != 0)
			mods |= 8;
		if ((buttonState & MotionEvent.BUTTON_TERTIARY) != 0)
			mods |= 4;

		// Note: Meta and Ctrl are intentionally swapped here to keep logic in vt320 simple.
		int meta = mouseEvent.getMetaState();
		if ((meta & KeyEvent.META_META_ON) != 0)
			mods |= 2;
		if ((meta & KeyEvent.META_SHIFT_ON) != 0)
			mods |= 1;
		if ((meta & KeyEvent.META_CTRL_ON) != 0)
			mods |= 4;

		return mods;
	}

	@Override
	public boolean onCheckIsTextEditor() {
		// This prevents a cursor being displayed within the text.
		return false;
	}

	@Override
	public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
		return terminalView.onCreateInputConnection(outAttrs);
	}

	private class TextSelectionActionModeCallback implements ActionMode.Callback {
		private static final int COPY = 0;
		private static final int PASTE = 1;

		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			return false;
		}

		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			TerminalTextViewOverlay.this.selectionActionMode = mode;

			menu.clear();

			menu.add(0, COPY, 0, R.string.console_menu_copy)
					.setIcon(R.drawable.ic_action_copy)
					.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT | MenuItem.SHOW_AS_ACTION_IF_ROOM);
			menu.add(0, PASTE, 1, R.string.console_menu_paste)
					.setIcon(R.drawable.ic_action_paste)
					.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT | MenuItem.SHOW_AS_ACTION_IF_ROOM);

			return true;
		}

		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			switch (item.getItemId()) {
			case COPY:
				copyCurrentSelectionToClipboard();
				return true;
			case PASTE:
				pasteClipboard();
				mode.finish();
				return true;
			}

			return false;
		}

		@Override
		public void onDestroyActionMode(ActionMode mode) {
		}
	}
}