Mercurial > 510Connectbot
annotate src/com/five_ten_sg/connectbot/service/TerminalBridge.java @ 230:bc40032ad1da
use StringPickerDialog for soft cursor keypad
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 09 Jul 2014 14:27:51 -0700 |
parents | 61ed3984fc1d |
children | 502ba7668993 |
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; | |
223
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
43 import com.five_ten_sg.connectbot.util.StringPickerDialog; |
0 | 44 import android.app.AlertDialog; |
45 import android.content.Context; | |
46 import android.graphics.Bitmap; | |
47 import android.graphics.Bitmap.Config; | |
48 import android.graphics.Canvas; | |
49 import android.graphics.Color; | |
50 import android.graphics.Paint; | |
51 import android.graphics.Paint.FontMetrics; | |
52 import android.graphics.Typeface; | |
53 import android.os.Binder; | |
54 import android.os.Environment; | |
55 import android.text.ClipboardManager; | |
56 import android.text.Editable; | |
57 import android.text.method.CharacterPickerDialog; | |
58 import android.util.FloatMath; | |
59 import android.util.Log; | |
60 import android.view.KeyEvent; | |
61 import android.view.View; | |
62 import android.widget.AdapterView; | |
63 import android.widget.Button; | |
64 import de.mud.terminal.VDUBuffer; | |
65 import de.mud.terminal.VDUDisplay; | |
66 import de.mud.terminal.vt320; | |
67 | |
68 | |
69 /** | |
70 * Provides a bridge between a MUD terminal buffer and a possible TerminalView. | |
71 * This separation allows us to keep the TerminalBridge running in a background | |
72 * service. A TerminalView shares down a bitmap that we can use for rendering | |
73 * when available. | |
74 * | |
75 * This class also provides SSH hostkey verification prompting, and password | |
76 * prompting. | |
77 */ | |
78 @SuppressWarnings("deprecation") // for ClipboardManager | |
79 public class TerminalBridge implements VDUDisplay { | |
80 public final static String TAG = "ConnectBot.TerminalBridge"; | |
81 | |
189
ab6f64d1a24a
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
188
diff
changeset
|
82 public final static float DEFAULT_FONT_SIZE = 10.0f; |
ab6f64d1a24a
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
188
diff
changeset
|
83 private final static float FONT_SIZE_FACTOR = 1.1f; |
0 | 84 |
85 public Integer[] color; | |
86 | |
87 public int defaultFg = HostDatabase.DEFAULT_FG_COLOR; | |
88 public int defaultBg = HostDatabase.DEFAULT_BG_COLOR; | |
89 | |
90 protected final TerminalManager manager; | |
95
e1c43d50f9d8
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
94
diff
changeset
|
91 public final HostBean host; |
e1c43d50f9d8
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
94
diff
changeset
|
92 public final String homeDirectory; |
0 | 93 |
94 AbsTransport transport; | |
95 | |
96 final Paint defaultPaint; | |
97 | |
98 private Relay relay; | |
99 | |
67
99d5b02ad90c
allow host override terminal type
Carl Byington <carl@five-ten-sg.com>
parents:
66
diff
changeset
|
100 private String emulation; // aka answerback string, aka terminal type |
0 | 101 |
102 public Bitmap bitmap = null; | |
103 public vt320 buffer = null; | |
104 | |
47
a3fd10a8c0de
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
45
diff
changeset
|
105 public TerminalView parent = null; |
0 | 106 private final Canvas canvas = new Canvas(); |
107 | |
108 private boolean disconnected = false; | |
109 private boolean awaitingClose = false; | |
110 | |
111 private boolean forcedSize = false; | |
112 private int columns; | |
113 private int rows; | |
114 | |
115 public TerminalMonitor monitor = null; | |
116 private TerminalKeyListener keyListener = null; | |
117 | |
118 private boolean selectingForCopy = false; | |
119 private final SelectionArea selectionArea; | |
120 | |
121 // TODO add support for the new clipboard API | |
122 private ClipboardManager clipboard; | |
123 | |
42
7ac846a07ed4
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
40
diff
changeset
|
124 public int charWidth = -1; |
7ac846a07ed4
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
40
diff
changeset
|
125 public int charHeight = -1; |
7ac846a07ed4
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
40
diff
changeset
|
126 private int charTop = -1; |
7ac846a07ed4
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
40
diff
changeset
|
127 private float fontSize = -1; |
0 | 128 |
129 private final List<FontSizeChangedListener> fontSizeChangedListeners; | |
130 | |
131 private final List<String> localOutput; | |
132 | |
133 /** | |
134 * Flag indicating if we should perform a full-screen redraw during our next | |
135 * rendering pass. | |
136 */ | |
137 private boolean fullRedraw = false; | |
138 | |
139 public PromptHelper promptHelper; | |
140 | |
141 protected BridgeDisconnectedListener disconnectListener = null; | |
142 | |
143 /** | |
144 * Create a new terminal bridge suitable for unit testing. | |
145 */ | |
146 public TerminalBridge() { | |
147 buffer = new vt320() { | |
148 @Override | |
149 public void write(byte[] b) {} | |
150 @Override | |
151 public void write(int b) {} | |
152 @Override | |
153 public void sendTelnetCommand(byte cmd) {} | |
154 @Override | |
155 public void setWindowSize(int c, int r) {} | |
156 @Override | |
157 public void debug(String s) {} | |
158 }; | |
95
e1c43d50f9d8
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
94
diff
changeset
|
159 emulation = null; |
e1c43d50f9d8
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
94
diff
changeset
|
160 manager = null; |
e1c43d50f9d8
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
94
diff
changeset
|
161 host = null; |
e1c43d50f9d8
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
94
diff
changeset
|
162 homeDirectory = null; |
e1c43d50f9d8
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
94
diff
changeset
|
163 defaultPaint = new Paint(); |
0 | 164 selectionArea = new SelectionArea(); |
95
e1c43d50f9d8
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
94
diff
changeset
|
165 localOutput = new LinkedList<String>(); |
0 | 166 fontSizeChangedListeners = new LinkedList<FontSizeChangedListener>(); |
167 transport = null; | |
168 keyListener = new TerminalKeyListener(manager, this, buffer, null); | |
169 monitor = null; | |
170 } | |
171 | |
172 /** | |
42
7ac846a07ed4
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
40
diff
changeset
|
173 * Create new terminal bridge with following parameters. |
0 | 174 */ |
94
e3b83c4f02f1
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
93
diff
changeset
|
175 public TerminalBridge(final TerminalManager manager, final HostBean host, final String homeDirectory) throws IOException { |
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>(); | |
188
cf677a6f586d
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
182
diff
changeset
|
192 float defaultFontSize = Float.parseFloat(manager.prefs.getString(PreferenceConstants.DEFAULT_FONT_SIZE, "-1")); |
0 | 193 Log.i(TAG, "fontSize: " + this.fontSize + ", defaultFontSize: " + defaultFontSize); |
194 | |
188
cf677a6f586d
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
182
diff
changeset
|
195 float hostFontSize; |
cf677a6f586d
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
182
diff
changeset
|
196 if (fontSize <= 0) { |
cf677a6f586d
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
182
diff
changeset
|
197 if ((defaultFontSize > 0) && (host.getFontSize() <= 0)) |
0 | 198 hostFontSize = defaultFontSize; |
199 else | |
200 hostFontSize = host.getFontSize(); | |
201 } | |
202 else | |
42
7ac846a07ed4
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
40
diff
changeset
|
203 hostFontSize = fontSize; |
0 | 204 |
42
7ac846a07ed4
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
40
diff
changeset
|
205 if (hostFontSize <= 0) hostFontSize = DEFAULT_FONT_SIZE; |
0 | 206 |
207 setFontSize(hostFontSize); | |
208 resetColors(); | |
209 selectionArea = new SelectionArea(); | |
210 } | |
211 | |
212 public PromptHelper getPromptHelper() { | |
213 return promptHelper; | |
214 } | |
215 | |
216 /** | |
217 * Spawn thread to open connection and start login process. | |
218 */ | |
219 protected void startConnection() { | |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
220 transport = TransportFactory.getTransport(host.getProtocol()); |
94
e3b83c4f02f1
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
93
diff
changeset
|
221 transport.setLinks(manager, this, homeDirectory, host, emulation); |
42
7ac846a07ed4
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
40
diff
changeset
|
222 buffer = transport.getTransportBuffer(); |
31
139394237973
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
29
diff
changeset
|
223 keyListener = transport.getTerminalKeyListener(); |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
95
diff
changeset
|
224 String monitor_init = host.getMonitor(); |
29
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
225 |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
226 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
|
227 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
|
228 } |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
229 |
0 | 230 transport.setCompression(host.getCompression()); |
231 transport.setHttpproxy(host.getHttpproxy()); | |
232 transport.setUseAuthAgent(host.getUseAuthAgent()); | |
233 | |
234 if (transport.canForwardPorts()) { | |
235 for (PortForwardBean portForward : manager.hostdb.getPortForwardsForHost(host)) | |
236 transport.addPortForward(portForward); | |
237 } | |
238 | |
239 outputLine(manager.res.getString(R.string.terminal_connecting, host.getHostname(), host.getPort(), host.getProtocol())); | |
240 Thread connectionThread = new Thread(new Runnable() { | |
241 public void run() { | |
242 transport.connect(); | |
243 } | |
244 }); | |
245 connectionThread.setName("Connection"); | |
246 connectionThread.setDaemon(true); | |
247 connectionThread.start(); | |
248 } | |
249 | |
250 /** | |
251 * Handle challenges from keyboard-interactive authentication mode. | |
252 */ | |
253 public String[] replyToChallenge(String name, String instruction, int numPrompts, String[] prompt, boolean[] echo) { | |
254 String[] responses = new String[numPrompts]; | |
255 | |
256 for (int i = 0; i < numPrompts; i++) { | |
257 // request response from user for each prompt | |
258 responses[i] = promptHelper.requestPasswordPrompt(instruction, prompt[i]); | |
259 } | |
260 | |
261 return responses; | |
262 } | |
263 | |
264 /** | |
265 * @return charset in use by bridge | |
266 */ | |
267 public Charset getCharset() { | |
42
7ac846a07ed4
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
40
diff
changeset
|
268 if (relay != null) return relay.getCharset(); |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
95
diff
changeset
|
269 |
45
80dcebe51af2
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
42
diff
changeset
|
270 return keyListener.getCharset(); |
0 | 271 } |
272 | |
273 /** | |
274 * Sets the encoding used by the terminal. If the connection is live, | |
275 * then the character set is changed for the next read. | |
276 * @param encoding the canonical name of the character encoding | |
277 */ | |
278 public void setCharset(String encoding) { | |
42
7ac846a07ed4
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
40
diff
changeset
|
279 if (relay != null) relay.setCharset(encoding); |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
95
diff
changeset
|
280 |
0 | 281 keyListener.setCharset(encoding); |
282 } | |
283 | |
284 /** | |
285 * Convenience method for writing a line into the underlying MUD buffer. | |
286 * Should never be called once the session is established. | |
287 */ | |
288 public final void outputLine(String line) { | |
289 if (transport != null && transport.isSessionOpen()) | |
290 Log.e(TAG, "Session established, cannot use outputLine!", new IOException("outputLine call traceback")); | |
291 | |
292 synchronized (localOutput) { | |
293 final String s = line + "\r\n"; | |
294 localOutput.add(s); | |
295 buffer.putString(s); | |
296 // For accessibility | |
297 final char[] charArray = s.toCharArray(); | |
298 propagateConsoleText(charArray, charArray.length); | |
299 } | |
300 } | |
301 | |
302 /** | |
303 * Inject a specific string into this terminal. Used for post-login strings | |
304 * and pasting clipboard. | |
305 */ | |
306 public void injectString(final String string) { | |
307 if (string == null || string.length() == 0) | |
308 return; | |
309 | |
310 Thread injectStringThread = new Thread(new Runnable() { | |
311 public void run() { | |
312 try { | |
313 transport.write(string.getBytes(host.getEncoding())); | |
314 } | |
315 catch (Exception e) { | |
316 Log.e(TAG, "Couldn't inject string to remote host: ", e); | |
317 } | |
318 } | |
319 }); | |
320 injectStringThread.setName("InjectString"); | |
321 injectStringThread.start(); | |
322 } | |
323 | |
324 /** | |
325 * Internal method to request actual PTY terminal once we've finished | |
326 * authentication. If called before authenticated, it will just fail. | |
327 */ | |
328 public void onConnected() { | |
329 disconnected = false; | |
330 buffer.reset(); | |
331 buffer.setAnswerBack(emulation); | |
42
7ac846a07ed4
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
40
diff
changeset
|
332 localOutput.clear(); // We no longer need our local output. |
0 | 333 |
334 if (HostDatabase.DELKEY_BACKSPACE.equals(host.getDelKey())) | |
335 buffer.setBackspace(vt320.DELETE_IS_BACKSPACE); | |
336 else | |
337 buffer.setBackspace(vt320.DELETE_IS_DEL); | |
338 | |
339 // 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
|
340 // only if needed by the transport |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
341 if (transport.needsRelay()) { |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
342 relay = new Relay(this, transport, buffer, host.getEncoding()); |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
343 Thread relayThread = new Thread(relay); |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
344 relayThread.setDaemon(true); |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
345 relayThread.setName("Relay"); |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
346 relayThread.start(); |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
347 } |
017eeed8332c
start tn5250 integration
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
348 |
0 | 349 // force font-size to make sure we resizePTY as needed |
350 setFontSize(fontSize); | |
351 // finally send any post-login string, if requested | |
352 injectString(host.getPostLogin()); | |
353 } | |
354 | |
355 /** | |
356 * @return whether a session is open or not | |
357 */ | |
358 public boolean isSessionOpen() { | |
359 if (transport != null) return transport.isSessionOpen(); | |
360 | |
361 return false; | |
362 } | |
363 | |
364 public void setOnDisconnectedListener(BridgeDisconnectedListener disconnectListener) { | |
365 this.disconnectListener = disconnectListener; | |
366 } | |
367 | |
368 /** | |
369 * Force disconnection of this terminal bridge. | |
370 */ | |
371 public void dispatchDisconnect(boolean immediate) { | |
372 // We don't need to do this multiple times. | |
373 synchronized (this) { | |
374 if (disconnected && !immediate) return; | |
375 | |
376 disconnected = true; | |
377 } | |
378 | |
379 // Cancel any pending prompts. | |
380 promptHelper.cancelPrompt(); | |
381 // disconnection request hangs if we havent really connected to a host yet | |
382 // temporary fix is to just spawn disconnection into a thread | |
383 Thread disconnectThread = new Thread(new Runnable() { | |
384 public void run() { | |
385 if (transport != null && transport.isConnected()) | |
386 transport.close(); | |
387 } | |
388 }); | |
389 disconnectThread.setName("Disconnect"); | |
390 disconnectThread.start(); | |
391 | |
392 if (immediate) { | |
393 awaitingClose = true; | |
394 | |
395 if (disconnectListener != null) | |
396 disconnectListener.onDisconnected(TerminalBridge.this); | |
397 } | |
398 else { | |
399 final String line = manager.res.getString(R.string.alert_disconnect_msg); | |
400 buffer.putString("\r\n" + line + "\r\n"); | |
401 | |
402 if (host.getStayConnected()) { | |
403 manager.requestReconnect(this); | |
404 return; | |
405 } | |
406 | |
407 Thread disconnectPromptThread = new Thread(new Runnable() { | |
408 public void run() { | |
409 Boolean result = promptHelper.requestBooleanPrompt(null, | |
410 manager.res.getString(R.string.prompt_host_disconnected)); | |
411 | |
412 if (result == null || result.booleanValue()) { | |
413 awaitingClose = true; | |
414 | |
415 // Tell the TerminalManager that we can be destroyed now. | |
416 if (disconnectListener != null) | |
417 disconnectListener.onDisconnected(TerminalBridge.this); | |
418 } | |
419 } | |
420 }); | |
421 disconnectPromptThread.setName("DisconnectPrompt"); | |
422 disconnectPromptThread.setDaemon(true); | |
423 disconnectPromptThread.start(); | |
424 } | |
425 | |
426 // close the monitor | |
427 if (monitor != null) monitor.Disconnect(); | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
95
diff
changeset
|
428 |
0 | 429 monitor = null; |
430 } | |
431 | |
432 public void setSelectingForCopy(boolean selectingForCopy) { | |
433 this.selectingForCopy = selectingForCopy; | |
434 } | |
435 | |
436 public boolean isSelectingForCopy() { | |
437 return selectingForCopy; | |
438 } | |
439 | |
440 public SelectionArea getSelectionArea() { | |
441 return selectionArea; | |
442 } | |
443 | |
444 public synchronized void tryKeyVibrate() { | |
445 manager.tryKeyVibrate(); | |
446 } | |
447 | |
448 /** | |
449 * Request a different font size. Will make call to parentChanged() to make | |
450 * sure we resize PTY if needed. | |
451 */ | |
191
2e4ab8c33851
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
190
diff
changeset
|
452 final void setFontSize(float size) { |
2e4ab8c33851
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
190
diff
changeset
|
453 if (size <= 0.0) return; |
2e4ab8c33851
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
190
diff
changeset
|
454 size = (float)(int)((size * 10.0f) + 0.5f) / 10.0f; |
0 | 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 | |
188
cf677a6f586d
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
182
diff
changeset
|
471 host.setFontSize(fontSize); |
0 | 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() { | |
188
cf677a6f586d
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
182
diff
changeset
|
996 setFontSize(fontSize * FONT_SIZE_FACTOR); |
0 | 997 } |
998 | |
999 /** | |
1000 * | |
1001 */ | |
1002 public void decreaseFontSize() { | |
188
cf677a6f586d
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
182
diff
changeset
|
1003 setFontSize(fontSize / FONT_SIZE_FACTOR); |
0 | 1004 } |
1005 | |
1006 /** | |
1007 * Auto-size window back to default | |
1008 */ | |
1009 public void resetSize(TerminalView parent) { | |
1010 this.forcedSize = false; | |
188
cf677a6f586d
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
182
diff
changeset
|
1011 float defaultFontSize = Float.parseFloat(manager.prefs.getString(PreferenceConstants.DEFAULT_FONT_SIZE, "-1")); |
0 | 1012 |
188
cf677a6f586d
use floating point font size, change size by scaling factor rather than linear addition
Carl Byington <carl@five-ten-sg.com>
parents:
182
diff
changeset
|
1013 if (defaultFontSize > 0) |
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() { | |
230
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1132 final String []pickerStrings = {"←", "→", "↑", "↓", "tab", "ins", "del", "ret"; |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1133 final HashMap<String,Integer> keymap = new HashMap<String,Integer>(); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1134 keymap.put("←", vt320.KEY_LEFT); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1135 keymap.put("→", vt320.KEY_RIGHT); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1136 keymap.put("↑", vt320.KEY_UP); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1137 keymap.put("↓", vt320.KEY_DOWN); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1138 keymap.put("tab", vt320.KEY_TAB); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1139 keymap.put("ins", vt320.KEY_INSERT); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1140 keymap.put("del", vt320.KEY_DELETE); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1141 keymap.put("ret", vt320.KEY_ENTER); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1142 |
0 | 1143 CharSequence str = ""; |
1144 Editable content = Editable.Factory.getInstance().newEditable(str); | |
1145 | |
230
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1146 if (parent == null) return false; |
0 | 1147 |
230
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1148 StringPickerDialog cpd = new StringPickerDialog(parent.getContext(), |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1149 parent, content, |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1150 pickerStrings, true) { |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1151 private void buttonPressed(String s) { |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1152 if (keymap.containsKey(s)) buffer.keyPressed(keymap.get(s), ' ', 0); |
0 | 1153 } |
1154 @Override | |
1155 public void onItemClick(AdapterView p, View v, int pos, long id) { | |
230
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1156 buttonPressed(pickerStrings[pos]); |
0 | 1157 } |
1158 @Override | |
1159 public void onClick(View v) { | |
1160 if (v instanceof Button) { | |
230
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1161 final String s = ((Button) v).getText().toString(); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1162 if (s.equals("")) dismiss(); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1163 else buttonPressed(s); |
0 | 1164 } |
1165 } | |
1166 @Override | |
1167 public boolean dispatchKeyEvent(KeyEvent event) { | |
1168 if (event.getAction() == KeyEvent.ACTION_DOWN) { | |
1169 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) | |
1170 dismiss(); | |
1171 | |
1172 return keyListener.onKey(parent, event.getKeyCode(), event); | |
1173 } | |
1174 | |
1175 return true; | |
1176 } | |
1177 }; | |
1178 cpd.show(); | |
1179 return true; | |
1180 } | |
1181 | |
1182 | |
1183 /** | |
1184 * CTRL dialog | |
1185 */ | |
1186 private String getCtrlString() { | |
1187 final String defaultSet = "ABCDEKLOQRWSTUXZ"; | |
1188 String set = manager.prefs.getString(PreferenceConstants.CTRL_STRING, defaultSet); | |
1189 | |
1190 if (set == null || set.equals("")) { | |
1191 set = defaultSet; | |
1192 } | |
1193 | |
1194 return set; | |
1195 } | |
1196 | |
1197 public boolean showCtrlDialog() { | |
1198 CharSequence str = ""; | |
1199 Editable content = Editable.Factory.getInstance().newEditable(str); | |
1200 | |
1201 if (parent == null) | |
1202 return false; | |
1203 | |
1204 CharacterPickerDialog cpd = new CharacterPickerDialog(parent.getContext(), | |
1205 parent, content, getCtrlString(), true) { | |
1206 private void buttonPressed(CharSequence result) { | |
1207 int code = result.toString().toUpperCase().charAt(0) - 64; | |
1208 | |
1209 if (code > 0 && code < 80) { | |
1210 try { | |
1211 transport.write(code); | |
1212 } | |
1213 catch (IOException e) { | |
1214 Log.d(TAG, "Error writing CTRL+" + result.toString().toUpperCase().charAt(0)); | |
1215 } | |
1216 } | |
1217 | |
1218 dismiss(); | |
1219 } | |
1220 @Override | |
1221 public void onItemClick(AdapterView p, View v, int pos, long id) { | |
1222 final String result = String.valueOf(getCtrlString().charAt(pos)); | |
1223 buttonPressed(result); | |
1224 } | |
1225 @Override | |
1226 public void onClick(View v) { | |
1227 if (v instanceof Button) { | |
1228 final CharSequence result = ((Button) v).getText(); | |
1229 | |
1230 if (result.equals("")) | |
1231 dismiss(); | |
1232 else | |
1233 buttonPressed(result); | |
1234 } | |
1235 } | |
1236 @Override | |
1237 public boolean dispatchKeyEvent(KeyEvent event) { | |
1238 if (event.getAction() == KeyEvent.ACTION_DOWN) { | |
1239 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) | |
1240 dismiss(); | |
1241 | |
1242 return keyListener.onKey(parent, event.getKeyCode(), event); | |
1243 } | |
1244 | |
1245 return true; | |
1246 } | |
1247 }; | |
1248 cpd.show(); | |
1249 return true; | |
1250 } | |
1251 | |
1252 /** | |
1253 * Function keys dialog | |
1254 */ | |
1255 public boolean showFKeysDialog() { | |
230
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1256 final String []pickerStrings = {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24"}; |
179 | 1257 final HashMap<String,Integer> keymap = new HashMap<String,Integer>(); |
223
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1258 keymap.put("F1", vt320.KEY_F1); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1259 keymap.put("F2", vt320.KEY_F2); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1260 keymap.put("F3", vt320.KEY_F3); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1261 keymap.put("F4", vt320.KEY_F4); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1262 keymap.put("F5", vt320.KEY_F5); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1263 keymap.put("F6", vt320.KEY_F6); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1264 keymap.put("F7", vt320.KEY_F7); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1265 keymap.put("F8", vt320.KEY_F8); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1266 keymap.put("F9", vt320.KEY_F9); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1267 keymap.put("F10", vt320.KEY_F10); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1268 keymap.put("F11", vt320.KEY_F11); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1269 keymap.put("F12", vt320.KEY_F12); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1270 keymap.put("F13", vt320.KEY_F13); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1271 keymap.put("F14", vt320.KEY_F14); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1272 keymap.put("F15", vt320.KEY_F15); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1273 keymap.put("F16", vt320.KEY_F16); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1274 keymap.put("F17", vt320.KEY_F17); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1275 keymap.put("F18", vt320.KEY_F18); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1276 keymap.put("F19", vt320.KEY_F19); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1277 keymap.put("F20", vt320.KEY_F20); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1278 keymap.put("F21", vt320.KEY_F21); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1279 keymap.put("F22", vt320.KEY_F22); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1280 keymap.put("F23", vt320.KEY_F23); |
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1281 keymap.put("F24", vt320.KEY_F24); |
176 | 1282 |
0 | 1283 CharSequence str = ""; |
1284 Editable content = Editable.Factory.getInstance().newEditable(str); | |
1285 | |
230
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1286 if (parent == null) return false; |
0 | 1287 |
223
61ed3984fc1d
proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
191
diff
changeset
|
1288 StringPickerDialog cpd = new StringPickerDialog(parent.getContext(), |
230
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1289 parent, content, |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1290 pickerStrings, true) { |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1291 private void buttonPressed(String s) { |
176 | 1292 if (keymap.containsKey(s)) buffer.keyPressed(keymap.get(s), ' ', 0); |
0 | 1293 dismiss(); |
1294 } | |
1295 @Override | |
1296 public void onItemClick(AdapterView p, View v, int pos, long id) { | |
230
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1297 buttonPressed(pickerStrings[pos]); |
0 | 1298 } |
1299 @Override | |
1300 public void onClick(View v) { | |
1301 if (v instanceof Button) { | |
230
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1302 final String s = ((Button) v).getText().toString(); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1303 if (s.equals("")) dismiss(); |
bc40032ad1da
use StringPickerDialog for soft cursor keypad
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
1304 else buttonPressed(s); |
0 | 1305 } |
1306 } | |
1307 @Override | |
1308 public boolean dispatchKeyEvent(KeyEvent event) { | |
1309 if (event.getAction() == KeyEvent.ACTION_DOWN) { | |
1310 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) | |
1311 dismiss(); | |
1312 | |
1313 return keyListener.onKey(parent, event.getKeyCode(), event); | |
1314 } | |
1315 | |
1316 return true; | |
1317 } | |
1318 }; | |
1319 cpd.show(); | |
1320 return true; | |
1321 } | |
1322 | |
1323 private String getPickerString() { | |
1324 final String defaultSet = "~\\^()[]{}<>|/:_;,.!@#$%&*?\"'-+="; | |
1325 String set = manager.prefs.getString(PreferenceConstants.PICKER_STRING, defaultSet); | |
1326 | |
1327 if (set == null || set.equals("")) { | |
1328 set = defaultSet; | |
1329 } | |
1330 | |
1331 return set; | |
1332 } | |
1333 | |
1334 public boolean showCharPickerDialog() { | |
1335 CharSequence str = ""; | |
1336 Editable content = Editable.Factory.getInstance().newEditable(str); | |
1337 | |
1338 if (parent == null || !transport.isAuthenticated()) | |
1339 return false; | |
1340 | |
1341 CharacterPickerDialog cpd = new CharacterPickerDialog(parent.getContext(), | |
1342 parent, content, getPickerString(), true) { | |
1343 private void writeChar(CharSequence result) { | |
1344 try { | |
1345 if (transport.isAuthenticated()) | |
1346 transport.write(result.toString().getBytes(getCharset().name())); | |
1347 } | |
1348 catch (IOException e) { | |
1349 Log.e(TAG, "Problem with the CharacterPickerDialog", e); | |
1350 } | |
1351 | |
1352 if (!manager.prefs.getBoolean(PreferenceConstants.PICKER_KEEP_OPEN, false)) | |
1353 dismiss(); | |
1354 } | |
1355 @Override | |
1356 public void onItemClick(AdapterView p, View v, int pos, long id) { | |
1357 String result = String.valueOf(getPickerString().charAt(pos)); | |
1358 writeChar(result); | |
1359 } | |
1360 @Override | |
1361 public void onClick(View v) { | |
1362 if (v instanceof Button) { | |
1363 CharSequence result = ((Button) v).getText(); | |
1364 | |
1365 if (result.equals("")) | |
1366 dismiss(); | |
1367 else | |
1368 writeChar(result); | |
1369 } | |
1370 } | |
1371 @Override | |
1372 public boolean dispatchKeyEvent(KeyEvent event) { | |
1373 int keyCode = event.getKeyCode(); | |
1374 | |
1375 if (event.getAction() == KeyEvent.ACTION_DOWN) { | |
1376 // close window if SYM or BACK keys are pressed | |
1377 if (keyListener.isSymKey(keyCode) || | |
1378 keyCode == KeyEvent.KEYCODE_BACK) { | |
1379 dismiss(); | |
1380 return true; | |
1381 } | |
1382 } | |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
95
diff
changeset
|
1383 |
0 | 1384 return super.dispatchKeyEvent(event); |
1385 } | |
1386 }; | |
1387 cpd.show(); | |
1388 return true; | |
1389 } | |
1390 } |