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