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