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