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