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