aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/org/connectbot/TerminalView.java
blob: 767a4a008313d84175d868244404d564af881c5e (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
/*
 * ConnectBot: simple, powerful, open-source SSH client for Android
 * Copyright 2007 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;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.connectbot.bean.SelectionArea;
import org.connectbot.service.FontSizeChangedListener;
import org.connectbot.service.TerminalBridge;
import org.connectbot.service.TerminalKeyListener;
import org.connectbot.util.PreferenceConstants;
import org.connectbot.util.TerminalViewPager;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelXorXfermode;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.view.MotionEventCompat;
import android.text.ClipboardManager;
import android.view.ActionMode;
import android.view.GestureDetector;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.ViewGroup.LayoutParams;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.TextView;
import android.widget.Toast;
import de.mud.terminal.VDUBuffer;
import de.mud.terminal.vt320;

/**
 * User interface {@link TextView} for showing a TerminalBridge in an
 * {@link android.app.Activity}. Handles drawing bitmap updates and passing keystrokes down
 * to terminal.
 *
 * On Honeycomb devices and above (>= APIv11), a TextView with transparent text (which is identical
 * to the bitmap) is drawn above the bitmap. This TextView exists to allow the user to
 * select and copy text.
 *
 * @author jsharkey
 */
public class TerminalView extends TextView implements FontSizeChangedListener {

	private final Context context;
	public final TerminalBridge bridge;

	private final TerminalViewPager viewPager;
	private GestureDetector gestureDetector;
	private SharedPreferences prefs;

	private ClipboardManager clipboard;
	private ActionMode selectionActionMode = null;
	private String currentSelection = "";

	// These are only used for pre-Honeycomb copying.
	private int lastTouchedRow, lastTouchedCol;

	private final Paint paint;
	private final Paint cursorPaint;
	private final Paint cursorStrokePaint;

	// Cursor paints to distinguish modes
	private Path ctrlCursor, altCursor, shiftCursor;
	private RectF tempSrc, tempDst;
	private Matrix scaleMatrix;
	private static final Matrix.ScaleToFit scaleType = Matrix.ScaleToFit.FILL;

	private Toast notification = null;
	private String lastNotification = null;
	private volatile boolean notifications = true;

	// Related to Accessibility Features
	private boolean mAccessibilityInitialized = false;
	private boolean mAccessibilityActive = true;
	private Object[] mAccessibilityLock = new Object[0];
	private StringBuffer mAccessibilityBuffer;
	private Pattern mControlCodes = null;
	private Matcher mCodeMatcher = null;
	private AccessibilityEventSender mEventSender = null;

	private static final String BACKSPACE_CODE = "\\x08\\x1b\\[K";
	private static final String CONTROL_CODE_PATTERN = "\\x1b\\[K[^m]+[m|:]";

	private static final int ACCESSIBILITY_EVENT_THRESHOLD = 1000;
	private static final String SCREENREADER_INTENT_ACTION = "android.accessibilityservice.AccessibilityService";
	private static final String SCREENREADER_INTENT_CATEGORY = "android.accessibilityservice.category.FEEDBACK_SPOKEN";

	public TerminalView(Context context, TerminalBridge bridge, TerminalViewPager pager) {
		super(context);

		this.context = context;
		this.bridge = bridge;
		this.viewPager = pager;

		setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		setFocusable(true);
		setFocusableInTouchMode(true);

		paint = new Paint();

		cursorPaint = new Paint();
		cursorPaint.setColor(bridge.color[bridge.defaultFg]);
		cursorPaint.setXfermode(new PixelXorXfermode(bridge.color[bridge.defaultBg]));
		cursorPaint.setAntiAlias(true);

		cursorStrokePaint = new Paint(cursorPaint);
		cursorStrokePaint.setStrokeWidth(0.1f);
		cursorStrokePaint.setStyle(Paint.Style.STROKE);

		/*
		 * Set up our cursor indicators on a 1x1 Path object which we can later
		 * transform to our character width and height
		 */
		// TODO make this into a resource somehow
		shiftCursor = new Path();
		shiftCursor.lineTo(0.5f, 0.33f);
		shiftCursor.lineTo(1.0f, 0.0f);

		altCursor = new Path();
		altCursor.moveTo(0.0f, 1.0f);
		altCursor.lineTo(0.5f, 0.66f);
		altCursor.lineTo(1.0f, 1.0f);

		ctrlCursor = new Path();
		ctrlCursor.moveTo(0.0f, 0.25f);
		ctrlCursor.lineTo(1.0f, 0.5f);
		ctrlCursor.lineTo(0.0f, 0.75f);

		// For creating the transform when the terminal resizes
		tempSrc = new RectF();
		tempSrc.set(0.0f, 0.0f, 1.0f, 1.0f);
		tempDst = new RectF();
		scaleMatrix = new Matrix();

		bridge.addFontSizeChangedListener(this);
		bridge.parentChanged(this);

		// connect our view up to the bridge
		setOnKeyListener(bridge.getKeyHandler());

		mAccessibilityBuffer = new StringBuffer();

		// Enable accessibility features if a screen reader is active.
		new AccessibilityStateTester().execute((Void) null);

		clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
		prefs = PreferenceManager.getDefaultSharedPreferences(context);

		setTextColor(Color.TRANSPARENT);
		setTypeface(Typeface.MONOSPACE);
		onFontSizeChanged(bridge.getFontSize());

		// Allow selection of and copying text for Honeycomb and above devices.
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			setTextIsSelectable(true);
			setCustomSelectionActionModeCallback(new TextSelectionActionModeCallback());
		}

		gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
			private TerminalBridge bridge = TerminalView.this.bridge;
			private float totalY = 0;

			@Override
			public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
				// if releasing then reset total scroll
				if (e2.getAction() == MotionEvent.ACTION_UP) {
					totalY = 0;
				}

				// activate consider if within x tolerance
				int touchSlop =
						ViewConfiguration.get(TerminalView.this.context).getScaledTouchSlop();
				if (Math.abs(e1.getX() - e2.getX()) < touchSlop * 4) {
					// estimate how many rows we have scrolled through
					// accumulate distance that doesn't trigger immediate scroll
					totalY += distanceY;
					final int moved = (int) (totalY / bridge.charHeight);

					// Consume as pg up/dn only if towards left third of screen with the gesture
					// enabled.
					boolean pgUpDnGestureEnabled =
							prefs.getBoolean(PreferenceConstants.PG_UPDN_GESTURE, false);
					if (e2.getX() <= getWidth() / 3 && pgUpDnGestureEnabled) {
						// otherwise consume as pgup/pgdown for every 5 lines
						if (moved > 5) {
							((vt320) bridge.buffer).keyPressed(vt320.KEY_PAGE_DOWN, ' ', 0);
							bridge.tryKeyVibrate();
							totalY = 0;
						} else if (moved < -5) {
							((vt320) bridge.buffer).keyPressed(vt320.KEY_PAGE_UP, ' ', 0);
							bridge.tryKeyVibrate();
							totalY = 0;
						}
					} else if (moved != 0) {
						int base = bridge.buffer.getWindowBase();
						bridge.buffer.setWindowBase(base + moved);
						totalY = 0;
					}
				}

				return true;
			}

			@Override
			public boolean onSingleTapConfirmed(MotionEvent e) {
				viewPager.performClick();
				return super.onSingleTapConfirmed(e);
			}
		});
	}

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

	public void copyCurrentSelectionToClipboard() {
		ClipboardManager clipboard =
				(ClipboardManager) TerminalView.this.context.getSystemService(Context.CLIPBOARD_SERVICE);
		if (currentSelection.length() != 0) {
			clipboard.setText(currentSelection);
		}

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			closeSelectionActionMode();
		}
	}

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

	@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, bridge)) {
				return true;
			}
			viewPager.setPagingEnabled(true);
		} else if (gestureDetector != null) {
			// The gesture detector should not be called if touch event was from mouse.
			gestureDetector.onTouchEvent(event);
		}

		// Old version of copying, only for pre-Honeycomb.
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
			// when copying, highlight the area
			if (bridge.isSelectingForCopy()) {
				SelectionArea area = bridge.getSelectionArea();
				int row = (int) Math.floor(event.getY() / bridge.charHeight);
				int col = (int) Math.floor(event.getX() / bridge.charWidth);

				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					// recording starting area
					viewPager.setPagingEnabled(false);
					if (area.isSelectingOrigin()) {
						area.setRow(row);
						area.setColumn(col);
						lastTouchedRow = row;
						lastTouchedCol = col;
						bridge.redraw();
					}
					return true;
				case MotionEvent.ACTION_MOVE:
							/* ignore when user hasn't moved since last time so
							 * we can fine-tune with directional pad
							 */
					if (row == lastTouchedRow && col == lastTouchedCol)
						return true;

					// if the user moves, start the selection for other corner
					area.finishSelectingOrigin();

					// update selected area
					area.setRow(row);
					area.setColumn(col);
					lastTouchedRow = row;
					lastTouchedCol = col;
					bridge.redraw();
					return true;
				case MotionEvent.ACTION_UP:
							/* If they didn't move their finger, maybe they meant to
							 * select the rest of the text with the directional pad.
							 */
					if (area.getLeft() == area.getRight() &&
							area.getTop() == area.getBottom()) {
						return true;
					}

					// copy selected area to clipboard
					String copiedText = area.copyFrom(bridge.buffer);

					clipboard.setText(copiedText);
					Toast.makeText(
						context,
						context.getString(R.string.console_copy_done, copiedText.length()),
						Toast.LENGTH_LONG).show();

					// fall through to clear state

				case MotionEvent.ACTION_CANCEL:
					// make sure we clear any highlighted area
					area.reset();
					bridge.setSelectingForCopy(false);
					bridge.redraw();
					viewPager.setPagingEnabled(true);
					return true;
				}
			}

			return true;
		}

		super.onTouchEvent(event);

		return true;
	}

	@TargetApi(11)
	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) {
			TerminalView.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:
				String clip = clipboard.getText().toString();
				TerminalView.this.bridge.injectString(clip);
				mode.finish();
				return true;
			}

			return false;
		}

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

	/**
	 * @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.
					String clip = clipboard.getText().toString();
					bridge.injectString(clip);
					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) {
			viewPager.setPagingEnabled(false);
			vtBuffer.mousePressed(
					col, row, mouseEventToJavaModifiers(event));
			return true;
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			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
	@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) bridge.buffer;
				boolean mouseReport = vtBuffer.isMouseReportEnabled();
				if (mouseReport) {
					int row = (int) Math.floor(event.getY() / bridge.charHeight);
					int col = (int) Math.floor(event.getX() / 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;
				} else if (yDistance != 0) {
					int base = bridge.buffer.getWindowBase();
					bridge.buffer.setWindowBase(base - Math.round(yDistance));
					return true;
				}
			}
		}
		return super.onGenericMotionEvent(event);
	}

	// TODO: cleanup and possibly optimize
	private void refreshTextFromBuffer() {
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
			// Do not run this function because the textView is not selectable pre-Honeycomb.
			return;
		}

		VDUBuffer vb = bridge.getVDUBuffer();

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

		int windowBase = vb.getWindowBase();
		int rowBegin = vb.getTopMargin();
		int rowEnd = vb.getBottomMargin();
		int numCols = vb.getColumns() - 1;

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

		setText(buffer);
	}

	/**
	 * Only intended for pre-Honeycomb devices.
	 */
	public void startPreHoneycombCopyMode() {
		// mark as copying and reset any previous bounds
		SelectionArea area = bridge.getSelectionArea();
		area.reset();
		area.setBounds(bridge.buffer.getColumns(), bridge.buffer.getRows());

		bridge.setSelectingForCopy(true);

		// Make sure we show the initial selection
		bridge.redraw();
	}

	public void destroy() {
		// tell bridge to destroy its bitmap
		bridge.parentDestroyed();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);

		bridge.parentChanged(this);

		scaleCursors();
	}

	public void onFontSizeChanged(final float size) {
		scaleCursors();

		((Activity) context).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				setTextSize(size);

				// For the TextView to line up with the bitmap text, lineHeight must be equal to
				// the bridge's charHeight. See TextView.getLineHeight(), which has been reversed to
				// derive lineSpacingMultiplier.
				float lineSpacingMultiplier = (float) bridge.charHeight / getPaint().getFontMetricsInt(null);
				setLineSpacing(0.0f, lineSpacingMultiplier);
			}
		});
	}

	private void scaleCursors() {
		// Create a scale matrix to scale our 1x1 representation of the cursor
		tempDst.set(0.0f, 0.0f, bridge.charWidth, bridge.charHeight);
		scaleMatrix.setRectToRect(tempSrc, tempDst, scaleType);
	}

	@Override
	public void onDraw(Canvas canvas) {
		if (bridge.bitmap != null) {
			// draw the bitmap
			bridge.onDraw();

			// draw the bridge bitmap if it exists
			canvas.drawBitmap(bridge.bitmap, 0, 0, paint);

			// also draw cursor if visible
			if (bridge.buffer.isCursorVisible()) {
				int cursorColumn = bridge.buffer.getCursorColumn();
				final int cursorRow = bridge.buffer.getCursorRow();

				final int columns = bridge.buffer.getColumns();

				if (cursorColumn == columns)
					cursorColumn = columns - 1;

				if (cursorColumn < 0 || cursorRow < 0)
					return;

				int currentAttribute = bridge.buffer.getAttributes(
						cursorColumn, cursorRow);
				boolean onWideCharacter = (currentAttribute & VDUBuffer.FULLWIDTH) != 0;

				int x = cursorColumn * bridge.charWidth;
				int y = (bridge.buffer.getCursorRow()
						+ bridge.buffer.screenBase - bridge.buffer.windowBase)
						* bridge.charHeight;

				// Save the current clip and translation
				canvas.save();

				canvas.translate(x, y);
				canvas.clipRect(0, 0,
						bridge.charWidth * (onWideCharacter ? 2 : 1),
						bridge.charHeight);
				canvas.drawPaint(cursorPaint);

				final int deadKey = bridge.getKeyHandler().getDeadKey();
				if (deadKey != 0) {
					canvas.drawText(new char[] { (char) deadKey }, 0, 1, 0, 0, cursorStrokePaint);
				}

				// Make sure we scale our decorations to the correct size.
				canvas.concat(scaleMatrix);

				int metaState = bridge.getKeyHandler().getMetaState();

				if ((metaState & TerminalKeyListener.OUR_SHIFT_ON) != 0)
					canvas.drawPath(shiftCursor, cursorStrokePaint);
				else if ((metaState & TerminalKeyListener.OUR_SHIFT_LOCK) != 0)
					canvas.drawPath(shiftCursor, cursorPaint);

				if ((metaState & TerminalKeyListener.OUR_ALT_ON) != 0)
					canvas.drawPath(altCursor, cursorStrokePaint);
				else if ((metaState & TerminalKeyListener.OUR_ALT_LOCK) != 0)
					canvas.drawPath(altCursor, cursorPaint);

				if ((metaState & TerminalKeyListener.OUR_CTRL_ON) != 0)
					canvas.drawPath(ctrlCursor, cursorStrokePaint);
				else if ((metaState & TerminalKeyListener.OUR_CTRL_LOCK) != 0)
					canvas.drawPath(ctrlCursor, cursorPaint);

				// Restore previous clip region
				canvas.restore();
			}

			// draw any highlighted area
			if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB &&
				bridge.isSelectingForCopy()) {
				SelectionArea area = bridge.getSelectionArea();
				canvas.save(Canvas.CLIP_SAVE_FLAG);
				canvas.clipRect(
					area.getLeft() * bridge.charWidth,
					area.getTop() * bridge.charHeight,
					(area.getRight() + 1) * bridge.charWidth,
					(area.getBottom() + 1) * bridge.charHeight
				);
				canvas.drawPaint(cursorPaint);
				canvas.restore();
			}
		}

		super.onDraw(canvas);
	}

	public void notifyUser(String message) {
		if (!notifications)
			return;

		if (notification != null) {
			// Don't keep telling the user the same thing.
			if (lastNotification != null && lastNotification.equals(message))
				return;

			notification.setText(message);
			notification.show();
		} else {
			notification = Toast.makeText(context, message, Toast.LENGTH_SHORT);
			notification.show();
		}

		lastNotification = message;
	}

	/**
	 * Ask the {@link TerminalBridge} we're connected to to resize to a specific size.
	 * @param width
	 * @param height
	 */
	public void forceSize(int width, int height) {
		bridge.resizeComputed(width, height, getWidth(), getHeight());
	}

	/**
	 * Sets the ability for the TerminalView to display Toast notifications to the user.
	 * @param value whether to enable notifications or not
	 */
	public void setNotifications(boolean value) {
		notifications = value;
	}

	@Override
	public boolean onCheckIsTextEditor() {
		return true;
	}

	@Override
	public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
		outAttrs.imeOptions |=
			EditorInfo.IME_FLAG_NO_EXTRACT_UI |
			EditorInfo.IME_FLAG_NO_ENTER_ACTION |
			EditorInfo.IME_ACTION_NONE;
		outAttrs.inputType = EditorInfo.TYPE_NULL;
		return new BaseInputConnection(this, false) {
			@Override
			public boolean deleteSurroundingText (int leftLength, int rightLength) {
				if (rightLength == 0 && leftLength == 0) {
					return this.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
				}
				for (int i = 0; i < leftLength; i++) {
					this.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
				}
				// TODO: forward delete
				return true;
			}
		};
	}

	public void propagateConsoleText(char[] rawText, int length) {
		if (mAccessibilityActive) {
			synchronized (mAccessibilityLock) {
				mAccessibilityBuffer.append(rawText, 0, length);
			}

			if (mAccessibilityInitialized) {
				if (mEventSender != null) {
					removeCallbacks(mEventSender);
				} else {
					mEventSender = new AccessibilityEventSender();
				}

				postDelayed(mEventSender, ACCESSIBILITY_EVENT_THRESHOLD);
			}
		}
	}

	private class AccessibilityEventSender implements Runnable {
		public void run() {
			synchronized (mAccessibilityLock) {
				if (mCodeMatcher == null) {
					mCodeMatcher = mControlCodes.matcher(mAccessibilityBuffer);
				} else {
					mCodeMatcher.reset(mAccessibilityBuffer);
				}

				// Strip all control codes out.
				mAccessibilityBuffer = new StringBuffer(mCodeMatcher.replaceAll(" "));

				// Apply Backspaces using backspace character sequence
				int i = mAccessibilityBuffer.indexOf(BACKSPACE_CODE);
				while (i != -1) {
					mAccessibilityBuffer = mAccessibilityBuffer.replace(i == 0 ? 0 : i - 1, i
							+ BACKSPACE_CODE.length(), "");
					i = mAccessibilityBuffer.indexOf(BACKSPACE_CODE);
				}

				if (mAccessibilityBuffer.length() > 0) {
					AccessibilityEvent event = AccessibilityEvent.obtain(
							AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
					event.setFromIndex(0);
					event.setAddedCount(mAccessibilityBuffer.length());
					event.getText().add(mAccessibilityBuffer);

					sendAccessibilityEventUnchecked(event);
					mAccessibilityBuffer.setLength(0);
				}
			}
		}
	}

	private class AccessibilityStateTester extends AsyncTask<Void, Void, Boolean> {
		@Override
		protected Boolean doInBackground(Void... params) {
			/*
			 * Presumably if the accessibility manager is not enabled, we don't
			 * need to send accessibility events.
			 */
			final AccessibilityManager accessibility = (AccessibilityManager) context
					.getSystemService(Context.ACCESSIBILITY_SERVICE);
			if (!accessibility.isEnabled()) {
				return false;
			}

			/*
			 * Restrict the set of intents to only accessibility services that
			 * have the category FEEDBACK_SPOKEN (aka, screen readers).
			 */
			final Intent screenReaderIntent = new Intent(SCREENREADER_INTENT_ACTION);
			screenReaderIntent.addCategory(SCREENREADER_INTENT_CATEGORY);

			final ContentResolver cr = context.getContentResolver();

			final List<ResolveInfo> screenReaders = context.getPackageManager().queryIntentServices(
					screenReaderIntent, 0);

			boolean foundScreenReader = false;

			final int N = screenReaders.size();
			for (int i = 0; i < N; i++) {
				final ResolveInfo screenReader = screenReaders.get(i);

				/*
				 * All screen readers are expected to implement a content
				 * provider that responds to:
				 * content://<nameofpackage>.providers.StatusProvider
				 */
				final Cursor cursor = cr.query(
						Uri.parse("content://" + screenReader.serviceInfo.packageName
								+ ".providers.StatusProvider"), null, null, null, null);
				if (cursor != null && cursor.moveToFirst()) {
					/*
					 * These content providers use a special cursor that only has
					 * one element, an integer that is 1 if the screen reader is
					 * running.
					 */
					final int status = cursor.getInt(0);

					cursor.close();

					if (status == 1) {
						foundScreenReader = true;
						break;
					}
				}
			}

			if (foundScreenReader) {
				mControlCodes = Pattern.compile(CONTROL_CODE_PATTERN);
			}

			return foundScreenReader;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			mAccessibilityActive = result;

			mAccessibilityInitialized = true;

			if (result) {
				mEventSender = new AccessibilityEventSender();
				postDelayed(mEventSender, ACCESSIBILITY_EVENT_THRESHOLD);
			} else {
				mAccessibilityBuffer = null;
			}
		}
	}
}