comparison app/src/main/java/com/five_ten_sg/connectbot/service/TerminalBridge.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/com/five_ten_sg/connectbot/service/TerminalBridge.java@651aff5a46c7
children 105815cce146
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1 /*
2 * ConnectBot: simple, powerful, open-source SSH client for Android
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package com.five_ten_sg.connectbot.service;
19
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.nio.charset.Charset;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.LinkedHashSet;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Set;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34 import com.five_ten_sg.connectbot.R;
35 import com.five_ten_sg.connectbot.TerminalView;
36 import com.five_ten_sg.connectbot.bean.HostBean;
37 import com.five_ten_sg.connectbot.bean.PortForwardBean;
38 import com.five_ten_sg.connectbot.bean.SelectionArea;
39 import com.five_ten_sg.connectbot.transport.AbsTransport;
40 import com.five_ten_sg.connectbot.transport.TransportFactory;
41 import com.five_ten_sg.connectbot.util.HostDatabase;
42 import com.five_ten_sg.connectbot.util.PreferenceConstants;
43 import com.five_ten_sg.connectbot.util.StringPickerDialog;
44 import android.app.AlertDialog;
45 import android.content.Context;
46 import android.graphics.Bitmap;
47 import android.graphics.Bitmap.Config;
48 import android.graphics.Canvas;
49 import android.graphics.Color;
50 import android.graphics.Paint;
51 import android.graphics.Paint.FontMetrics;
52 import android.graphics.Typeface;
53 import android.os.Binder;
54 import android.os.Environment;
55 import android.text.ClipboardManager;
56 import android.text.Editable;
57 import android.text.method.CharacterPickerDialog;
58 import android.util.FloatMath;
59 import android.util.Log;
60 import android.view.KeyEvent;
61 import android.view.View;
62 import android.widget.AdapterView;
63 import android.widget.Button;
64 import de.mud.terminal.VDUBuffer;
65 import de.mud.terminal.VDUDisplay;
66 import de.mud.terminal.vt320;
67
68
69 /**
70 * Provides a bridge between a MUD terminal buffer and a possible TerminalView.
71 * This separation allows us to keep the TerminalBridge running in a background
72 * service. A TerminalView shares down a bitmap that we can use for rendering
73 * when available.
74 *
75 * This class also provides SSH hostkey verification prompting, and password
76 * prompting.
77 */
78 @SuppressWarnings("deprecation") // for ClipboardManager
79 public class TerminalBridge implements VDUDisplay {
80 public final static String TAG = "ConnectBot.TerminalBridge";
81
82 private final static float FONT_SIZE_FACTOR = 1.1f;
83
84 public Integer[] color;
85
86 public int defaultFg = HostDatabase.DEFAULT_FG_COLOR;
87 public int defaultBg = HostDatabase.DEFAULT_BG_COLOR;
88
89 protected final TerminalManager manager;
90 public final HostBean host;
91 public final String homeDirectory;
92
93 AbsTransport transport;
94
95 final Paint defaultPaint;
96
97 private Relay relay;
98
99 private String emulation; // aka answerback string, aka terminal type
100
101 public Bitmap bitmap = null;
102 public vt320 buffer = null;
103
104 public TerminalView parent = null;
105 private final Canvas canvas = new Canvas();
106
107 private boolean disconnected = false;
108 private boolean awaitingClose = false;
109
110 private boolean forcedSize = false;
111 private int columns;
112 private int rows;
113
114 public TerminalMonitor monitor = null;
115 private TerminalKeyListener keyListener = null;
116
117 private boolean selectingForCopy = false;
118 private final SelectionArea selectionArea;
119
120 // TODO add support for the new clipboard API
121 private ClipboardManager clipboard;
122
123 public int charWidth = -1;
124 public int charHeight = -1;
125 private int charTop = -1;
126 private float fontSize = -1;
127
128 private final List<FontSizeChangedListener> fontSizeChangedListeners;
129
130 private final List<String> localOutput;
131
132 /**
133 * Flag indicating if we should perform a full-screen redraw during our next
134 * rendering pass.
135 */
136 private boolean fullRedraw = false;
137
138 public PromptHelper promptHelper;
139
140 protected BridgeDisconnectedListener disconnectListener = null;
141
142 /**
143 * Create a new terminal bridge suitable for unit testing.
144 */
145 public TerminalBridge() {
146 buffer = new vt320() {
147 @Override
148 public void write(byte[] b) {}
149 @Override
150 public void write(int b) {}
151 @Override
152 public void sendTelnetCommand(byte cmd) {}
153 @Override
154 public void setWindowSize(int c, int r) {}
155 @Override
156 public void debug(String s) {}
157 };
158 emulation = null;
159 manager = null;
160 host = null;
161 homeDirectory = null;
162 defaultPaint = new Paint();
163 selectionArea = new SelectionArea();
164 localOutput = new LinkedList<String>();
165 fontSizeChangedListeners = new LinkedList<FontSizeChangedListener>();
166 transport = null;
167 keyListener = new TerminalKeyListener(manager, this, buffer, null);
168 monitor = null;
169 }
170
171 /**
172 * Create new terminal bridge with following parameters.
173 */
174 public TerminalBridge(final TerminalManager manager, final HostBean host, final String homeDirectory) throws IOException {
175 this.manager = manager;
176 this.host = host;
177 this.homeDirectory = homeDirectory;
178 emulation = host.getHostEmulation();
179
180 if ((emulation == null) || (emulation.length() == 0)) emulation = manager.getEmulation();
181
182 // create prompt helper to relay password and hostkey requests up to gui
183 promptHelper = new PromptHelper(this);
184 // create our default paint
185 defaultPaint = new Paint();
186 defaultPaint.setAntiAlias(true);
187 defaultPaint.setTypeface(Typeface.MONOSPACE);
188 defaultPaint.setFakeBoldText(true); // more readable?
189 localOutput = new LinkedList<String>();
190 fontSizeChangedListeners = new LinkedList<FontSizeChangedListener>();
191 setMyFontSize();
192 resetColors();
193 selectionArea = new SelectionArea();
194 }
195
196 public PromptHelper getPromptHelper() {
197 return promptHelper;
198 }
199
200 /**
201 * Spawn thread to open connection and start login process.
202 */
203 protected void startConnection() {
204 transport = TransportFactory.getTransport(host.getProtocol());
205 transport.setLinks(manager, this, homeDirectory, host, emulation);
206 buffer = transport.getTransportBuffer();
207 keyListener = transport.getTerminalKeyListener();
208 String monitor_init = host.getMonitor();
209
210 if ((monitor_init != null) && (monitor_init.length() > 0)) {
211 monitor = new TerminalMonitor(manager, buffer, parent, host, monitor_init);
212 }
213
214 transport.setCompression(host.getCompression());
215 transport.setHttpproxy(host.getHttpproxy());
216 transport.setUseAuthAgent(host.getUseAuthAgent());
217
218 if (transport.canForwardPorts()) {
219 for (PortForwardBean portForward : manager.hostdb.getPortForwardsForHost(host))
220 transport.addPortForward(portForward);
221 }
222
223 outputLine(manager.res.getString(R.string.terminal_connecting, host.getHostname(), host.getPort(), host.getProtocol()));
224 Thread connectionThread = new Thread(new Runnable() {
225 public void run() {
226 transport.connect();
227 }
228 });
229 connectionThread.setName("Connection");
230 connectionThread.setDaemon(true);
231 connectionThread.start();
232 }
233
234 /**
235 * Handle challenges from keyboard-interactive authentication mode.
236 */
237 public String[] replyToChallenge(String name, String instruction, int numPrompts, String[] prompt, boolean[] echo) {
238 String[] responses = new String[numPrompts];
239
240 for (int i = 0; i < numPrompts; i++) {
241 // request response from user for each prompt
242 responses[i] = promptHelper.requestPasswordPrompt(instruction, prompt[i]);
243 }
244
245 return responses;
246 }
247
248 /**
249 * @return charset in use by bridge
250 */
251 public Charset getCharset() {
252 if (relay != null) return relay.getCharset();
253
254 return keyListener.getCharset();
255 }
256
257 /**
258 * Sets the encoding used by the terminal. If the connection is live,
259 * then the character set is changed for the next read.
260 * @param encoding the canonical name of the character encoding
261 */
262 public void setCharset(String encoding) {
263 if (relay != null) relay.setCharset(encoding);
264
265 keyListener.setCharset(encoding);
266 }
267
268 /**
269 * Convenience method for writing a line into the underlying MUD buffer.
270 * Should never be called once the session is established.
271 */
272 public final void outputLine(String line) {
273 if (transport != null && transport.isSessionOpen())
274 Log.e(TAG, "Session established, cannot use outputLine!", new IOException("outputLine call traceback"));
275
276 synchronized (localOutput) {
277 final String s = line + "\r\n";
278 localOutput.add(s);
279 buffer.putString(s);
280 // For accessibility
281 final char[] charArray = s.toCharArray();
282 propagateConsoleText(charArray, charArray.length);
283 }
284 }
285
286 /**
287 * Inject a specific string into this terminal. Used for post-login strings
288 * and pasting clipboard.
289 */
290 public void injectString(final String string) {
291 if (string == null || string.length() == 0)
292 return;
293
294 Thread injectStringThread = new Thread(new Runnable() {
295 public void run() {
296 try {
297 transport.write(string.getBytes(host.getEncoding()));
298 }
299 catch (Exception e) {
300 Log.e(TAG, "Couldn't inject string to remote host: ", e);
301 }
302 }
303 });
304 injectStringThread.setName("InjectString");
305 injectStringThread.start();
306 }
307
308 /**
309 * Internal method to request actual PTY terminal once we've finished
310 * authentication. If called before authenticated, it will just fail.
311 */
312 public void onConnected() {
313 disconnected = false;
314 buffer.reset();
315 buffer.setAnswerBack(emulation);
316 localOutput.clear(); // We no longer need our local output.
317
318 if (HostDatabase.DELKEY_BACKSPACE.equals(host.getDelKey()))
319 buffer.setBackspace(vt320.DELETE_IS_BACKSPACE);
320 else
321 buffer.setBackspace(vt320.DELETE_IS_DEL);
322
323 // create thread to relay incoming connection data to buffer
324 // only if needed by the transport
325 if (transport.needsRelay()) {
326 relay = new Relay(this, transport, buffer, host.getEncoding());
327 Thread relayThread = new Thread(relay);
328 relayThread.setDaemon(true);
329 relayThread.setName("Relay");
330 relayThread.start();
331 }
332
333 // get proper font size
334 setMyFontSize();
335 // finally send any post-login string, if requested
336 injectString(host.getPostLogin());
337 }
338
339 private void setMyFontSize() {
340 if ((parent != null) && (host.getFixedSize())) {
341 resizeComputed(host.getFixedWidth(), host.getFixedHeight(), parent.getWidth(), parent.getHeight());
342 }
343 else {
344 setFontSize(host.getFontSize());
345 }
346 }
347
348 /**
349 * @return whether a session is open or not
350 */
351 public boolean isSessionOpen() {
352 if (transport != null) return transport.isSessionOpen();
353
354 return false;
355 }
356
357 public void setOnDisconnectedListener(BridgeDisconnectedListener disconnectListener) {
358 this.disconnectListener = disconnectListener;
359 }
360
361 /**
362 * Force disconnection of this terminal bridge.
363 */
364 public void dispatchDisconnect(boolean immediate) {
365 // We don't need to do this multiple times.
366 synchronized (this) {
367 if (disconnected && !immediate) return;
368
369 disconnected = true;
370 }
371
372 // Cancel any pending prompts.
373 promptHelper.cancelPrompt();
374 // disconnection request hangs if we havent really connected to a host yet
375 // temporary fix is to just spawn disconnection into a thread
376 Thread disconnectThread = new Thread(new Runnable() {
377 public void run() {
378 if (transport != null && transport.isConnected())
379 transport.close();
380 }
381 });
382 disconnectThread.setName("Disconnect");
383 disconnectThread.start();
384
385 if (immediate) {
386 awaitingClose = true;
387
388 if (disconnectListener != null)
389 disconnectListener.onDisconnected(TerminalBridge.this);
390 }
391 else {
392 final String line = manager.res.getString(R.string.alert_disconnect_msg);
393 buffer.putString("\r\n" + line + "\r\n");
394
395 if (host.getStayConnected()) {
396 manager.requestReconnect(this);
397 return;
398 }
399
400 Thread disconnectPromptThread = new Thread(new Runnable() {
401 public void run() {
402 Boolean result = promptHelper.requestBooleanPrompt(null,
403 manager.res.getString(R.string.prompt_host_disconnected));
404
405 if (result == null || result.booleanValue()) {
406 awaitingClose = true;
407
408 // Tell the TerminalManager that we can be destroyed now.
409 if (disconnectListener != null)
410 disconnectListener.onDisconnected(TerminalBridge.this);
411 }
412 }
413 });
414 disconnectPromptThread.setName("DisconnectPrompt");
415 disconnectPromptThread.setDaemon(true);
416 disconnectPromptThread.start();
417 }
418
419 // close the monitor
420 if (monitor != null) monitor.Disconnect();
421
422 monitor = null;
423 }
424
425 public void setSelectingForCopy(boolean selectingForCopy) {
426 this.selectingForCopy = selectingForCopy;
427 }
428
429 public boolean isSelectingForCopy() {
430 return selectingForCopy;
431 }
432
433 public SelectionArea getSelectionArea() {
434 return selectionArea;
435 }
436
437 public synchronized void tryKeyVibrate() {
438 manager.tryKeyVibrate();
439 }
440
441 /**
442 * Request a different font size. Will make call to parentChanged() to make
443 * sure we resize PTY if needed.
444 */
445 final void setFontSize(float size) {
446 if (size <= 0.0) size = 12.0f;
447
448 size = (float)(int)((size * 10.0f) + 0.5f) / 10.0f;
449 defaultPaint.setTextSize(size);
450 fontSize = size;
451 // read new metrics to get exact pixel dimensions
452 FontMetrics fm = defaultPaint.getFontMetrics();
453 charTop = (int)FloatMath.ceil(fm.top);
454 float[] widths = new float[1];
455 defaultPaint.getTextWidths("X", widths);
456 charWidth = (int)FloatMath.ceil(widths[0]);
457 charHeight = (int)FloatMath.ceil(fm.descent - fm.top);
458
459 // refresh any bitmap with new font size
460 if (parent != null) parentChanged(parent);
461
462 synchronized(fontSizeChangedListeners) {
463 for (FontSizeChangedListener ofscl : fontSizeChangedListeners)
464 ofscl.onFontSizeChanged(size);
465 }
466
467 host.setFontSize(size);
468 manager.hostdb.updateFontSize(host);
469 }
470
471 /**
472 * Add an {@link FontSizeChangedListener} to the list of listeners for this
473 * bridge.
474 *
475 * @param listener
476 * listener to add
477 */
478 public void addFontSizeChangedListener(FontSizeChangedListener listener) {
479 synchronized(fontSizeChangedListeners) {
480 fontSizeChangedListeners.add(listener);
481 }
482 }
483
484 /**
485 * Remove an {@link FontSizeChangedListener} from the list of listeners for
486 * this bridge.
487 *
488 * @param listener
489 */
490 public void removeFontSizeChangedListener(FontSizeChangedListener listener) {
491 synchronized(fontSizeChangedListeners) {
492 fontSizeChangedListeners.remove(listener);
493 }
494 }
495
496 /**
497 * Something changed in our parent {@link TerminalView}, maybe it's a new
498 * parent, or maybe it's an updated font size. We should recalculate
499 * terminal size information and request a PTY resize.
500 */
501
502 public final synchronized void parentChanged(TerminalView parent) {
503 if (manager != null && !manager.isResizeAllowed()) {
504 Log.d(TAG, "Resize is not allowed now");
505 return;
506 }
507
508 this.parent = parent;
509 final int width = parent.getWidth();
510 final int height = parent.getHeight();
511
512 // Something has gone wrong with our layout; we're 0 width or height!
513 if (width <= 0 || height <= 0)
514 return;
515
516 clipboard = (ClipboardManager) parent.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
517 keyListener.setClipboardManager(clipboard);
518
519 if (!forcedSize) {
520 // recalculate buffer size
521 int newColumns, newRows;
522 newColumns = width / charWidth;
523 newRows = height / charHeight;
524
525 // If nothing has changed in the terminal dimensions and not an intial
526 // draw then don't blow away scroll regions and such.
527 if (newColumns == columns && newRows == rows)
528 return;
529
530 columns = newColumns;
531 rows = newRows;
532 }
533
534 // reallocate new bitmap if needed
535 boolean newBitmap = (bitmap == null);
536
537 if (bitmap != null)
538 newBitmap = (bitmap.getWidth() != width || bitmap.getHeight() != height);
539
540 if (newBitmap) {
541 discardBitmap();
542 bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
543 canvas.setBitmap(bitmap);
544 }
545
546 // clear out any old buffer information
547 defaultPaint.setColor(Color.BLACK);
548 canvas.drawPaint(defaultPaint);
549
550 // Stroke the border of the terminal if the size is being forced;
551 if (forcedSize) {
552 int borderX = (columns * charWidth) + 1;
553 int borderY = (rows * charHeight) + 1;
554 defaultPaint.setColor(Color.GRAY);
555 defaultPaint.setStrokeWidth(0.0f);
556
557 if (width >= borderX)
558 canvas.drawLine(borderX, 0, borderX, borderY + 1, defaultPaint);
559
560 if (height >= borderY)
561 canvas.drawLine(0, borderY, borderX + 1, borderY, defaultPaint);
562 }
563
564 try {
565 // request a terminal pty resize
566 if (buffer != null) {
567 synchronized (buffer) {
568 buffer.setScreenSize(columns, rows, true);
569 }
570 }
571
572 if (transport != null)
573 transport.setDimensions(columns, rows, width, height);
574 }
575 catch (Exception e) {
576 Log.e(TAG, "Problem while trying to resize screen or PTY", e);
577 }
578
579 // redraw local output if we don't have a session to receive our resize request
580 if (transport == null) {
581 synchronized (localOutput) {
582 buffer.reset();
583
584 for (String line : localOutput)
585 buffer.putString(line);
586 }
587 }
588
589 // force full redraw with new buffer size
590 fullRedraw = true;
591 redraw();
592
593 // initial sequence from
594 // transport.connect()
595 // bridge.onConnected()
596 // bridge.setMyFontSize()
597 // bridge.resizeComputed()
598 // bridge.setFontSize()
599 // bridge.parentChanged() here is on the wrong thread
600 try {
601 parent.notifyUser(String.format("%d x %d", columns, rows));
602 }
603 catch (Exception e) {
604 Log.e(TAG, "Problem while trying to notify user", e);
605 }
606
607 Log.i(TAG, String.format("parentChanged() now width=%d, height=%d", columns, rows));
608 }
609
610 /**
611 * Somehow our parent {@link TerminalView} was destroyed. Now we don't need
612 * to redraw anywhere, and we can recycle our internal bitmap.
613 */
614
615 public synchronized void parentDestroyed() {
616 parent = null;
617 discardBitmap();
618 }
619
620 private void discardBitmap() {
621 if (bitmap != null)
622 bitmap.recycle();
623
624 bitmap = null;
625 }
626
627 public void propagateConsoleText(char[] rawText, int length) {
628 if (parent != null) {
629 parent.propagateConsoleText(rawText, length);
630 }
631 }
632
633 public void onDraw() {
634 int fg, bg;
635
636 synchronized (buffer) {
637 boolean entireDirty = buffer.update[0] || fullRedraw;
638 boolean isWideCharacter = false;
639
640 // walk through all lines in the buffer
641 for (int l = 0; l < buffer.height; l++) {
642 // check if this line is dirty and needs to be repainted
643 // also check for entire-buffer dirty flags
644 if (!entireDirty && !buffer.update[l + 1]) continue;
645
646 // reset dirty flag for this line
647 buffer.update[l + 1] = false;
648
649 // walk through all characters in this line
650 for (int c = 0; c < buffer.width; c++) {
651 int addr = 0;
652 int currAttr = buffer.charAttributes[buffer.windowBase + l][c];
653 {
654 int fgcolor = defaultFg;
655
656 // check if foreground color attribute is set
657 if ((currAttr & VDUBuffer.COLOR_FG) != 0)
658 fgcolor = ((currAttr & VDUBuffer.COLOR_FG) >> VDUBuffer.COLOR_FG_SHIFT) - 1;
659
660 if (fgcolor < 8 && (currAttr & VDUBuffer.BOLD) != 0)
661 fg = color[fgcolor + 8];
662 else
663 fg = color[fgcolor];
664 }
665
666 // check if background color attribute is set
667 if ((currAttr & VDUBuffer.COLOR_BG) != 0)
668 bg = color[((currAttr & VDUBuffer.COLOR_BG) >> VDUBuffer.COLOR_BG_SHIFT) - 1];
669 else
670 bg = color[defaultBg];
671
672 // support character inversion by swapping background and foreground color
673 if ((currAttr & VDUBuffer.INVERT) != 0) {
674 int swapc = bg;
675 bg = fg;
676 fg = swapc;
677 }
678
679 // set underlined attributes if requested
680 defaultPaint.setUnderlineText((currAttr & VDUBuffer.UNDERLINE) != 0);
681 isWideCharacter = (currAttr & VDUBuffer.FULLWIDTH) != 0;
682
683 if (isWideCharacter)
684 addr++;
685 else {
686 // determine the amount of continuous characters with the same settings and print them all at once
687 while (c + addr < buffer.width
688 && buffer.charAttributes[buffer.windowBase + l][c + addr] == currAttr) {
689 addr++;
690 }
691 }
692
693 // Save the current clip region
694 canvas.save(Canvas.CLIP_SAVE_FLAG);
695 // clear this dirty area with background color
696 defaultPaint.setColor(bg);
697
698 if (isWideCharacter) {
699 canvas.clipRect(c * charWidth,
700 l * charHeight,
701 (c + 2) * charWidth,
702 (l + 1) * charHeight);
703 }
704 else {
705 canvas.clipRect(c * charWidth,
706 l * charHeight,
707 (c + addr) * charWidth,
708 (l + 1) * charHeight);
709 }
710
711 canvas.drawPaint(defaultPaint);
712 // write the text string starting at 'c' for 'addr' number of characters
713 defaultPaint.setColor(fg);
714
715 if ((currAttr & VDUBuffer.INVISIBLE) == 0)
716 canvas.drawText(buffer.charArray[buffer.windowBase + l], c,
717 addr, c * charWidth, (l * charHeight) - charTop,
718 defaultPaint);
719
720 // Restore the previous clip region
721 canvas.restore();
722 // advance to the next text block with different characteristics
723 c += addr - 1;
724
725 if (isWideCharacter)
726 c++;
727 }
728 }
729
730 // reset entire-buffer flags
731 buffer.update[0] = false;
732 }
733
734 fullRedraw = false;
735 }
736
737 public void redraw() {
738 if (parent != null)
739 parent.postInvalidate();
740 }
741
742 // We don't have a scroll bar.
743 public void updateScrollBar() {
744 }
745
746 /**
747 * Resize terminal to fit [rows]x[cols] in screen of size [width]x[height]
748 * @param rows
749 * @param cols
750 * @param width
751 * @param height
752 */
753
754 public synchronized void resizeComputed(int cols, int rows, int width, int height) {
755 float size = 8.0f;
756 float step = 8.0f;
757 float limit = 0.125f;
758 int direction;
759 boolean fixed = true;
760
761 if (!fixed) {
762 while ((direction = fontSizeCompare(size, cols, rows, width, height)) < 0)
763 size += step;
764
765 if (direction == 0) {
766 Log.d("fontsize", String.format("Found match at %f", size));
767 return;
768 }
769
770 step /= 2.0f;
771 size -= step;
772
773 while ((direction = fontSizeCompare(size, cols, rows, width, height)) != 0
774 && step >= limit) {
775 step /= 2.0f;
776
777 if (direction > 0) {
778 size -= step;
779 }
780 else {
781 size += step;
782 }
783 }
784
785 if (direction > 0) size -= step;
786 }
787
788 this.columns = cols;
789 this.rows = rows;
790 forcedSize = true;
791
792 if (fixed) setFontSize(host.getFontSize());
793 else setFontSize(size);
794 }
795
796 private int fontSizeCompare(float size, int cols, int rows, int width, int height) {
797 // read new metrics to get exact pixel dimensions
798 defaultPaint.setTextSize(size);
799 FontMetrics fm = defaultPaint.getFontMetrics();
800 float[] widths = new float[1];
801 defaultPaint.getTextWidths("X", widths);
802 int termWidth = (int)widths[0] * cols;
803 int termHeight = (int)FloatMath.ceil(fm.descent - fm.top) * rows;
804 Log.d("fontsize", String.format("font size %f resulted in %d x %d", size, termWidth, termHeight));
805
806 // Check to see if it fits in resolution specified.
807 if (termWidth > width || termHeight > height)
808 return 1;
809
810 if (termWidth == width || termHeight == height)
811 return 0;
812
813 return -1;
814 }
815
816 /**
817 * @return whether underlying transport can forward ports
818 */
819 public boolean canFowardPorts() {
820 return transport.canForwardPorts();
821 }
822
823 /**
824 * Adds the {@link PortForwardBean} to the list.
825 * @param portForward the port forward bean to add
826 * @return true on successful addition
827 */
828 public boolean addPortForward(PortForwardBean portForward) {
829 return transport.addPortForward(portForward);
830 }
831
832 /**
833 * Removes the {@link PortForwardBean} from the list.
834 * @param portForward the port forward bean to remove
835 * @return true on successful removal
836 */
837 public boolean removePortForward(PortForwardBean portForward) {
838 return transport.removePortForward(portForward);
839 }
840
841 /**
842 * @return the list of port forwards
843 */
844 public List<PortForwardBean> getPortForwards() {
845 return transport.getPortForwards();
846 }
847
848 /**
849 * Enables a port forward member. After calling this method, the port forward should
850 * be operational.
851 * @param portForward member of our current port forwards list to enable
852 * @return true on successful port forward setup
853 */
854 public boolean enablePortForward(PortForwardBean portForward) {
855 if (!transport.isConnected()) {
856 Log.i(TAG, "Attempt to enable port forward while not connected");
857 return false;
858 }
859
860 return transport.enablePortForward(portForward);
861 }
862
863 /**
864 * Disables a port forward member. After calling this method, the port forward should
865 * be non-functioning.
866 * @param portForward member of our current port forwards list to enable
867 * @return true on successful port forward tear-down
868 */
869 public boolean disablePortForward(PortForwardBean portForward) {
870 if (!transport.isConnected()) {
871 Log.i(TAG, "Attempt to disable port forward while not connected");
872 return false;
873 }
874
875 return transport.disablePortForward(portForward);
876 }
877
878 /**
879 * @return whether underlying transport can transfer files
880 */
881 public boolean canTransferFiles() {
882 return transport.canTransferFiles();
883 }
884
885 /**
886 * Downloads the specified remote file to the local connectbot folder.
887 * @return true on success, false on failure
888 */
889 public boolean downloadFile(String remoteFile, String localFolder) {
890 return transport.downloadFile(remoteFile, localFolder);
891 }
892
893 /**
894 * Uploads the specified local file to the remote host's default directory.
895 * @return true on success, false on failure
896 */
897 public boolean uploadFile(String localFile, String remoteFolder, String remoteFile, String mode) {
898 if (mode == null)
899 mode = "0600";
900
901 return transport.uploadFile(localFile, remoteFolder, remoteFile, mode);
902 }
903
904 /**
905 * @return whether the TerminalBridge should close
906 */
907 public boolean isAwaitingClose() {
908 return awaitingClose;
909 }
910
911 /**
912 * @return whether this connection had started and subsequently disconnected
913 */
914 public boolean isDisconnected() {
915 return disconnected;
916 }
917
918 /* (non-Javadoc)
919 * @see de.mud.terminal.VDUDisplay#setColor(byte, byte, byte, byte)
920 */
921 public void setColor(int index, int red, int green, int blue) {
922 // Don't allow the system colors to be overwritten for now. May violate specs.
923 if (index < color.length && index >= 16)
924 color[index] = 0xff000000 | red << 16 | green << 8 | blue;
925 }
926
927 public final void resetColors() {
928 int[] defaults = manager.hostdb.getDefaultColorsForScheme(HostDatabase.DEFAULT_COLOR_SCHEME);
929 defaultFg = defaults[0];
930 defaultBg = defaults[1];
931 color = manager.hostdb.getColorsForScheme(HostDatabase.DEFAULT_COLOR_SCHEME);
932 }
933
934 private static Pattern urlPattern = null;
935
936 /**
937 * @return
938 */
939 public List<String> scanForURLs() {
940 Set<String> urls = new LinkedHashSet<String>();
941
942 if (urlPattern == null) {
943 // based on http://www.ietf.org/rfc/rfc2396.txt
944 String scheme = "[A-Za-z][-+.0-9A-Za-z]*";
945 String unreserved = "[-._~0-9A-Za-z]";
946 String pctEncoded = "%[0-9A-Fa-f]{2}";
947 String subDelims = "[!$&'()*+,;:=]";
948 String userinfo = "(?:" + unreserved + "|" + pctEncoded + "|" + subDelims + "|:)*";
949 String h16 = "[0-9A-Fa-f]{1,4}";
950 String decOctet = "(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])";
951 String ipv4address = decOctet + "\\." + decOctet + "\\." + decOctet + "\\." + decOctet;
952 String ls32 = "(?:" + h16 + ":" + h16 + "|" + ipv4address + ")";
953 String ipv6address = "(?:(?:" + h16 + "){6}" + ls32 + ")";
954 String ipvfuture = "v[0-9A-Fa-f]+.(?:" + unreserved + "|" + subDelims + "|:)+";
955 String ipLiteral = "\\[(?:" + ipv6address + "|" + ipvfuture + ")\\]";
956 String regName = "(?:" + unreserved + "|" + pctEncoded + "|" + subDelims + ")*";
957 String host = "(?:" + ipLiteral + "|" + ipv4address + "|" + regName + ")";
958 String port = "[0-9]*";
959 String authority = "(?:" + userinfo + "@)?" + host + "(?::" + port + ")?";
960 String pchar = "(?:" + unreserved + "|" + pctEncoded + "|" + subDelims + "|@)";
961 String segment = pchar + "*";
962 String pathAbempty = "(?:/" + segment + ")*";
963 String segmentNz = pchar + "+";
964 String pathAbsolute = "/(?:" + segmentNz + "(?:/" + segment + ")*)?";
965 String pathRootless = segmentNz + "(?:/" + segment + ")*";
966 String hierPart = "(?://" + authority + pathAbempty + "|" + pathAbsolute + "|" + pathRootless + ")";
967 String query = "(?:" + pchar + "|/|\\?)*";
968 String fragment = "(?:" + pchar + "|/|\\?)*";
969 String uriRegex = scheme + ":" + hierPart + "(?:" + query + ")?(?:#" + fragment + ")?";
970 urlPattern = Pattern.compile(uriRegex);
971 }
972
973 char[] visibleBuffer = new char[buffer.height * buffer.width];
974
975 for (int l = 0; l < buffer.height; l++)
976 System.arraycopy(buffer.charArray[buffer.windowBase + l], 0,
977 visibleBuffer, l * buffer.width, buffer.width);
978
979 Matcher urlMatcher = urlPattern.matcher(new String(visibleBuffer));
980
981 while (urlMatcher.find())
982 urls.add(urlMatcher.group());
983
984 return (new LinkedList<String> (urls));
985 }
986
987 /**
988 * @return
989 */
990 public boolean isUsingNetwork() {
991 return transport.usesNetwork();
992 }
993
994 /**
995 * @return
996 */
997 public TerminalKeyListener getKeyHandler() {
998 return keyListener;
999 }
1000
1001 /**
1002 *
1003 */
1004 public void resetScrollPosition() {
1005 // if we're in scrollback, scroll to bottom of window on input
1006 if (buffer.windowBase != buffer.screenBase)
1007 buffer.setWindowBase(buffer.screenBase);
1008 }
1009
1010 /**
1011 *
1012 */
1013 public void increaseFontSize() {
1014 setFontSize(fontSize * FONT_SIZE_FACTOR);
1015 }
1016
1017 /**
1018 *
1019 */
1020 public void decreaseFontSize() {
1021 setFontSize(fontSize / FONT_SIZE_FACTOR);
1022 }
1023
1024 /**
1025 * Auto-size window back to default
1026 */
1027 public void resetSize(TerminalView parent) {
1028 this.forcedSize = false;
1029 setMyFontSize();
1030 }
1031
1032 /**
1033 * Create a screenshot of the current view
1034 */
1035 public void captureScreen() {
1036 String msg;
1037 File dir, path;
1038 boolean success = true;
1039 Bitmap screenshot = this.bitmap;
1040
1041 if (manager == null || parent == null || screenshot == null)
1042 return;
1043
1044 SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd_HHmmss");
1045 String date = s.format(new Date());
1046 String pref_path = manager.prefs.getString(PreferenceConstants.SCREEN_CAPTURE_FOLDER, "");
1047 File default_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
1048
1049 if (pref_path.equals(""))
1050 dir = default_path;
1051 else
1052 dir = new File(pref_path);
1053
1054 path = new File(dir, "vx-" + date + ".png");
1055
1056 try {
1057 dir.mkdirs();
1058 FileOutputStream out = new FileOutputStream(path);
1059 screenshot.compress(Bitmap.CompressFormat.PNG, 90, out);
1060 out.close();
1061 }
1062 catch (Exception e) {
1063 e.printStackTrace();
1064 success = false;
1065 }
1066
1067 if (success) {
1068 msg = manager.getResources().getString(R.string.screenshot_saved_as) + " " + path;
1069
1070 if (manager.prefs.getBoolean(PreferenceConstants.SCREEN_CAPTURE_POPUP, true)) {
1071 new AlertDialog.Builder(parent.getContext())
1072 .setTitle(R.string.screenshot_success_title)
1073 .setMessage(msg)
1074 .setPositiveButton(R.string.button_close, null)
1075 .show();
1076 }
1077 }
1078 else {
1079 msg = manager.getResources().getString(R.string.screenshot_not_saved_as) + " " + path;
1080 new AlertDialog.Builder(parent.getContext())
1081 .setTitle(R.string.screenshot_error_title)
1082 .setMessage(msg)
1083 .setNegativeButton(R.string.button_close, null)
1084 .show();
1085 }
1086
1087 return;
1088 }
1089
1090 /**
1091 * Show change font size dialog
1092 */
1093 public boolean showFontSizeDialog() {
1094 final String pickerString = "+-";
1095 CharSequence str = "";
1096 Editable content = Editable.Factory.getInstance().newEditable(str);
1097
1098 if (parent == null)
1099 return false;
1100
1101 CharacterPickerDialog cpd = new CharacterPickerDialog(parent.getContext(),
1102 parent, content, pickerString, true) {
1103 private void changeFontSize(CharSequence result) {
1104 if (result.equals("+"))
1105 increaseFontSize();
1106 else if (result.equals("-"))
1107 decreaseFontSize();
1108 }
1109 @Override
1110 public void onItemClick(AdapterView p, View v, int pos, long id) {
1111 final String result = String.valueOf(pickerString.charAt(pos));
1112 changeFontSize(result);
1113 }
1114 @Override
1115 public void onClick(View v) {
1116 if (v instanceof Button) {
1117 final CharSequence result = ((Button) v).getText();
1118
1119 if (result.equals(""))
1120 dismiss();
1121 else
1122 changeFontSize(result);
1123 }
1124 }
1125 @Override
1126 public boolean dispatchKeyEvent(KeyEvent event) {
1127 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1128 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
1129 dismiss();
1130
1131 return keyListener.onKey(parent, event.getKeyCode(), event);
1132 }
1133
1134 return true;
1135 }
1136 };
1137 cpd.show();
1138 return true;
1139 }
1140
1141 /**
1142 * Show arrows dialog
1143 */
1144 public boolean showArrowsDialog() {
1145 final String []pickerStrings = {"←", "→", "↑", "↓", "tab", "ins", "del", "ret"};
1146 final HashMap<String, Integer> keymap = new HashMap<String, Integer>();
1147 keymap.put("←", vt320.KEY_LEFT);
1148 keymap.put("→", vt320.KEY_RIGHT);
1149 keymap.put("↑", vt320.KEY_UP);
1150 keymap.put("↓", vt320.KEY_DOWN);
1151 keymap.put("tab", vt320.KEY_TAB);
1152 keymap.put("ins", vt320.KEY_INSERT);
1153 keymap.put("del", vt320.KEY_DELETE);
1154 keymap.put("ret", vt320.KEY_ENTER);
1155 CharSequence str = "";
1156 Editable content = Editable.Factory.getInstance().newEditable(str);
1157
1158 if (parent == null) return false;
1159
1160 StringPickerDialog cpd = new StringPickerDialog(parent.getContext(),
1161 parent, content,
1162 pickerStrings, true) {
1163 private void buttonPressed(String s) {
1164 if (keymap.containsKey(s)) buffer.keyPressed(keymap.get(s), ' ', 0);
1165 }
1166 @Override
1167 public void onItemClick(AdapterView p, View v, int pos, long id) {
1168 buttonPressed(pickerStrings[pos]);
1169 }
1170 @Override
1171 public void onClick(View v) {
1172 if (v instanceof Button) {
1173 final String s = ((Button) v).getText().toString();
1174
1175 if (s.equals("")) dismiss();
1176 else buttonPressed(s);
1177 }
1178 }
1179 @Override
1180 public boolean dispatchKeyEvent(KeyEvent event) {
1181 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1182 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
1183 dismiss();
1184
1185 return keyListener.onKey(parent, event.getKeyCode(), event);
1186 }
1187
1188 return true;
1189 }
1190 };
1191 cpd.show();
1192 return true;
1193 }
1194
1195
1196 /**
1197 * CTRL dialog
1198 */
1199 private String getCtrlString() {
1200 final String defaultSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1201 String set = manager.prefs.getString(PreferenceConstants.CTRL_STRING, defaultSet);
1202
1203 if (set == null || set.equals("")) {
1204 set = defaultSet;
1205 }
1206
1207 return set;
1208 }
1209
1210 public boolean showCtrlDialog() {
1211 CharSequence str = "";
1212 Editable content = Editable.Factory.getInstance().newEditable(str);
1213
1214 if (parent == null)
1215 return false;
1216
1217 CharacterPickerDialog cpd = new CharacterPickerDialog(parent.getContext(),
1218 parent, content, getCtrlString(), true) {
1219 private void buttonPressed(CharSequence result) {
1220 int code = result.toString().toUpperCase().charAt(0) - 64;
1221
1222 if (code > 0 && code < 80) {
1223 try {
1224 transport.write(code);
1225 }
1226 catch (IOException e) {
1227 Log.d(TAG, "Error writing CTRL+" + result.toString().toUpperCase().charAt(0));
1228 }
1229 }
1230
1231 dismiss();
1232 }
1233 @Override
1234 public void onItemClick(AdapterView p, View v, int pos, long id) {
1235 final String result = String.valueOf(getCtrlString().charAt(pos));
1236 buttonPressed(result);
1237 }
1238 @Override
1239 public void onClick(View v) {
1240 if (v instanceof Button) {
1241 final CharSequence result = ((Button) v).getText();
1242
1243 if (result.equals(""))
1244 dismiss();
1245 else
1246 buttonPressed(result);
1247 }
1248 }
1249 @Override
1250 public boolean dispatchKeyEvent(KeyEvent event) {
1251 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1252 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
1253 dismiss();
1254
1255 return keyListener.onKey(parent, event.getKeyCode(), event);
1256 }
1257
1258 return true;
1259 }
1260 };
1261 cpd.show();
1262 return true;
1263 }
1264
1265 /**
1266 * Function keys dialog
1267 */
1268 public boolean showFKeysDialog() {
1269 final String []pickerStrings = {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24", "←", "→", "↑", "↓", "tab", "ins", "del", "ret"};
1270 final HashMap<String, Integer> keymap = new HashMap<String, Integer>();
1271 keymap.put("F1", vt320.KEY_F1);
1272 keymap.put("F2", vt320.KEY_F2);
1273 keymap.put("F3", vt320.KEY_F3);
1274 keymap.put("F4", vt320.KEY_F4);
1275 keymap.put("F5", vt320.KEY_F5);
1276 keymap.put("F6", vt320.KEY_F6);
1277 keymap.put("F7", vt320.KEY_F7);
1278 keymap.put("F8", vt320.KEY_F8);
1279 keymap.put("F9", vt320.KEY_F9);
1280 keymap.put("F10", vt320.KEY_F10);
1281 keymap.put("F11", vt320.KEY_F11);
1282 keymap.put("F12", vt320.KEY_F12);
1283 keymap.put("F13", vt320.KEY_F13);
1284 keymap.put("F14", vt320.KEY_F14);
1285 keymap.put("F15", vt320.KEY_F15);
1286 keymap.put("F16", vt320.KEY_F16);
1287 keymap.put("F17", vt320.KEY_F17);
1288 keymap.put("F18", vt320.KEY_F18);
1289 keymap.put("F19", vt320.KEY_F19);
1290 keymap.put("F20", vt320.KEY_F20);
1291 keymap.put("F21", vt320.KEY_F21);
1292 keymap.put("F22", vt320.KEY_F22);
1293 keymap.put("F23", vt320.KEY_F23);
1294 keymap.put("F24", vt320.KEY_F24);
1295 keymap.put("←", vt320.KEY_LEFT);
1296 keymap.put("→", vt320.KEY_RIGHT);
1297 keymap.put("↑", vt320.KEY_UP);
1298 keymap.put("↓", vt320.KEY_DOWN);
1299 keymap.put("tab", vt320.KEY_TAB);
1300 keymap.put("ins", vt320.KEY_INSERT);
1301 keymap.put("del", vt320.KEY_DELETE);
1302 keymap.put("ret", vt320.KEY_ENTER);
1303 CharSequence str = "";
1304 Editable content = Editable.Factory.getInstance().newEditable(str);
1305
1306 if (parent == null) return false;
1307
1308 StringPickerDialog cpd = new StringPickerDialog(parent.getContext(),
1309 parent, content,
1310 pickerStrings, true) {
1311 private void buttonPressed(String s) {
1312 if (keymap.containsKey(s)) buffer.keyPressed(keymap.get(s), ' ', 0);
1313
1314 dismiss();
1315 }
1316 @Override
1317 public void onItemClick(AdapterView p, View v, int pos, long id) {
1318 buttonPressed(pickerStrings[pos]);
1319 }
1320 @Override
1321 public void onClick(View v) {
1322 if (v instanceof Button) {
1323 final String s = ((Button) v).getText().toString();
1324
1325 if (s.equals("")) dismiss();
1326 else buttonPressed(s);
1327 }
1328 }
1329 @Override
1330 public boolean dispatchKeyEvent(KeyEvent event) {
1331 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1332 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
1333 dismiss();
1334
1335 return keyListener.onKey(parent, event.getKeyCode(), event);
1336 }
1337
1338 return true;
1339 }
1340 };
1341 cpd.show();
1342 return true;
1343 }
1344
1345 private String getPickerString() {
1346 final String defaultSet = "~\\^()[]{}<>|/:_;,.!@#$%&*?\"'-+=";
1347 String set = manager.prefs.getString(PreferenceConstants.PICKER_STRING, defaultSet);
1348
1349 if (set == null || set.equals("")) {
1350 set = defaultSet;
1351 }
1352
1353 return set;
1354 }
1355
1356 public boolean showCharPickerDialog() {
1357 CharSequence str = "";
1358 Editable content = Editable.Factory.getInstance().newEditable(str);
1359
1360 if (parent == null || !transport.isAuthenticated())
1361 return false;
1362
1363 CharacterPickerDialog cpd = new CharacterPickerDialog(parent.getContext(),
1364 parent, content, getPickerString(), true) {
1365 private void writeChar(CharSequence result) {
1366 try {
1367 if (transport.isAuthenticated())
1368 transport.write(result.toString().getBytes(getCharset().name()));
1369 }
1370 catch (IOException e) {
1371 Log.e(TAG, "Problem with the CharacterPickerDialog", e);
1372 }
1373
1374 if (!manager.prefs.getBoolean(PreferenceConstants.PICKER_KEEP_OPEN, false))
1375 dismiss();
1376 }
1377 @Override
1378 public void onItemClick(AdapterView p, View v, int pos, long id) {
1379 String result = String.valueOf(getPickerString().charAt(pos));
1380 writeChar(result);
1381 }
1382 @Override
1383 public void onClick(View v) {
1384 if (v instanceof Button) {
1385 CharSequence result = ((Button) v).getText();
1386
1387 if (result.equals(""))
1388 dismiss();
1389 else
1390 writeChar(result);
1391 }
1392 }
1393 @Override
1394 public boolean dispatchKeyEvent(KeyEvent event) {
1395 int keyCode = event.getKeyCode();
1396
1397 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1398 // close window if SYM or BACK keys are pressed
1399 if (keyListener.isSymKey(keyCode) ||
1400 keyCode == KeyEvent.KEYCODE_BACK) {
1401 dismiss();
1402 return true;
1403 }
1404 }
1405
1406 return super.dispatchKeyEvent(event);
1407 }
1408 };
1409 cpd.show();
1410 return true;
1411 }
1412 }