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