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