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