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