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