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