Mercurial > 510Connectbot
annotate src/com/five_ten_sg/connectbot/TerminalView.java @ 377:4226f87534f4
remove hmac-sha2-512 - fails interop w/ centos6
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 04 Aug 2014 14:51:38 -0700 |
parents | e3b83c4f02f1 |
children | 4dcc071e1feb |
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; | |
19 | |
20 import java.util.List; | |
21 import java.util.regex.Matcher; | |
22 import java.util.regex.Pattern; | |
23 | |
24 import com.five_ten_sg.connectbot.bean.SelectionArea; | |
25 import com.five_ten_sg.connectbot.service.FontSizeChangedListener; | |
26 import com.five_ten_sg.connectbot.service.TerminalBridge; | |
27 import com.five_ten_sg.connectbot.service.TerminalKeyListener; | |
28 import android.app.Activity; | |
29 import android.content.ContentResolver; | |
30 import android.content.Context; | |
31 import android.content.Intent; | |
32 import android.content.pm.ResolveInfo; | |
33 import android.database.Cursor; | |
34 import android.graphics.Canvas; | |
35 import android.graphics.Matrix; | |
36 import android.graphics.Paint; | |
37 import android.graphics.Path; | |
38 import android.graphics.PixelXorXfermode; | |
39 import android.graphics.RectF; | |
40 import android.net.Uri; | |
41 import android.os.AsyncTask; | |
42 import android.view.KeyEvent; | |
43 import android.view.View; | |
44 import android.view.ViewGroup.LayoutParams; | |
45 import android.view.accessibility.AccessibilityEvent; | |
46 import android.view.accessibility.AccessibilityManager; | |
47 import android.view.inputmethod.BaseInputConnection; | |
48 import android.view.inputmethod.EditorInfo; | |
49 import android.view.inputmethod.InputConnection; | |
50 import android.view.ScaleGestureDetector; | |
51 import android.widget.Toast; | |
52 import de.mud.terminal.VDUBuffer; | |
53 | |
54 /** | |
55 * User interface {@link View} for showing a TerminalBridge in an | |
56 * {@link Activity}. Handles drawing bitmap updates and passing keystrokes down | |
57 * to terminal. | |
58 * | |
59 * @author jsharkey | |
60 */ | |
61 public class TerminalView extends View implements FontSizeChangedListener { | |
62 | |
63 private final Context context; | |
91
33eb63352be5
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
64 public final TerminalBridge bridge; |
0 | 65 private final Paint paint; |
66 private final Paint cursorPaint; | |
67 private final Paint cursorStrokePaint; | |
68 | |
69 // Cursor paints to distinguish modes | |
70 private Path ctrlCursor, altCursor, shiftCursor; | |
71 private RectF tempSrc, tempDst; | |
72 private Matrix scaleMatrix; | |
73 private static final Matrix.ScaleToFit scaleType = Matrix.ScaleToFit.FILL; | |
74 | |
75 private Toast notification = null; | |
76 private String lastNotification = null; | |
77 private volatile boolean notifications = true; | |
78 | |
79 // Related to Accessibility Features | |
80 private boolean mAccessibilityInitialized = false; | |
81 private boolean mAccessibilityActive = true; | |
82 private Object[] mAccessibilityLock = new Object[0]; | |
83 private StringBuffer mAccessibilityBuffer; | |
84 private Pattern mControlCodes = null; | |
85 private Matcher mCodeMatcher = null; | |
86 private AccessibilityEventSender mEventSender = null; | |
87 | |
23 | 88 |
0 | 89 private static final String BACKSPACE_CODE = "\\x08\\x1b\\[K"; |
90 private static final String CONTROL_CODE_PATTERN = "\\x1b\\[K[^m]+[m|:]"; | |
91 | |
92 private static final int ACCESSIBILITY_EVENT_THRESHOLD = 1000; | |
93 private static final String SCREENREADER_INTENT_ACTION = "android.accessibilityservice.AccessibilityService"; | |
94 private static final String SCREENREADER_INTENT_CATEGORY = "android.accessibilityservice.category.FEEDBACK_SPOKEN"; | |
95 | |
96 public ScaleGestureDetector mScaleDetector; | |
97 | |
98 public TerminalView(Context context, TerminalBridge bridge) { | |
99 super(context); | |
91
33eb63352be5
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
100 this.context = context; |
33eb63352be5
remove 5250 configuration
Carl Byington <carl@five-ten-sg.com>
parents:
23
diff
changeset
|
101 this.bridge = bridge; |
0 | 102 paint = new Paint(); |
103 setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); | |
104 setFocusable(true); | |
105 setFocusableInTouchMode(true); | |
106 cursorPaint = new Paint(); | |
107 cursorPaint.setColor(bridge.color[bridge.defaultFg]); | |
108 cursorPaint.setXfermode(new PixelXorXfermode(bridge.color[bridge.defaultBg])); | |
109 cursorPaint.setAntiAlias(true); | |
110 cursorStrokePaint = new Paint(cursorPaint); | |
111 cursorStrokePaint.setStrokeWidth(0.1f); | |
112 cursorStrokePaint.setStyle(Paint.Style.STROKE); | |
113 /* | |
114 * Set up our cursor indicators on a 1x1 Path object which we can later | |
115 * transform to our character width and height | |
116 */ | |
117 // TODO make this into a resource somehow | |
118 shiftCursor = new Path(); | |
119 shiftCursor.lineTo(0.5f, 0.33f); | |
120 shiftCursor.lineTo(1.0f, 0.0f); | |
121 altCursor = new Path(); | |
122 altCursor.moveTo(0.0f, 1.0f); | |
123 altCursor.lineTo(0.5f, 0.66f); | |
124 altCursor.lineTo(1.0f, 1.0f); | |
125 ctrlCursor = new Path(); | |
126 ctrlCursor.moveTo(0.0f, 0.25f); | |
127 ctrlCursor.lineTo(1.0f, 0.5f); | |
128 ctrlCursor.lineTo(0.0f, 0.75f); | |
129 // For creating the transform when the terminal resizes | |
130 tempSrc = new RectF(); | |
131 tempSrc.set(0.0f, 0.0f, 1.0f, 1.0f); | |
132 tempDst = new RectF(); | |
133 scaleMatrix = new Matrix(); | |
134 bridge.addFontSizeChangedListener(this); | |
135 // connect our view up to the bridge | |
136 setOnKeyListener(bridge.getKeyHandler()); | |
137 mAccessibilityBuffer = new StringBuffer(); | |
138 // Enable accessibility features if a screen reader is active. | |
139 new AccessibilityStateTester().execute((Void) null); | |
140 mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); | |
141 } | |
142 | |
143 public void destroy() { | |
144 // tell bridge to destroy its bitmap | |
145 bridge.parentDestroyed(); | |
146 } | |
147 | |
148 @Override | |
149 protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
150 super.onSizeChanged(w, h, oldw, oldh); | |
151 bridge.parentChanged(this); | |
152 scaleCursors(); | |
153 } | |
154 | |
155 public void onFontSizeChanged(float size) { | |
156 scaleCursors(); | |
157 } | |
158 | |
159 private void scaleCursors() { | |
160 // Create a scale matrix to scale our 1x1 representation of the cursor | |
161 tempDst.set(0.0f, 0.0f, bridge.charWidth, bridge.charHeight); | |
162 scaleMatrix.setRectToRect(tempSrc, tempDst, scaleType); | |
163 } | |
164 | |
165 @Override | |
166 public void onDraw(Canvas canvas) { | |
167 if (bridge.bitmap != null) { | |
168 // draw the bitmap | |
169 bridge.onDraw(); | |
170 // draw the bridge bitmap if it exists | |
171 canvas.drawBitmap(bridge.bitmap, 0, 0, paint); | |
172 | |
173 // also draw cursor if visible | |
174 if (bridge.buffer.isCursorVisible()) { | |
175 int cursorColumn = bridge.buffer.getCursorColumn(); | |
176 final int cursorRow = bridge.buffer.getCursorRow(); | |
177 final int columns = bridge.buffer.getColumns(); | |
178 | |
179 if (cursorColumn == columns) | |
180 cursorColumn = columns - 1; | |
181 | |
182 if (cursorColumn < 0 || cursorRow < 0) | |
183 return; | |
184 | |
185 int currentAttribute = bridge.buffer.getAttributes( | |
186 cursorColumn, cursorRow); | |
187 boolean onWideCharacter = (currentAttribute & VDUBuffer.FULLWIDTH) != 0; | |
188 int x = cursorColumn * bridge.charWidth; | |
189 int y = (bridge.buffer.getCursorRow() | |
190 + bridge.buffer.screenBase - bridge.buffer.windowBase) | |
191 * bridge.charHeight; | |
192 // Save the current clip and translation | |
193 canvas.save(); | |
194 canvas.translate(x, y); | |
195 canvas.clipRect(0, 0, | |
196 bridge.charWidth * (onWideCharacter ? 2 : 1), | |
197 bridge.charHeight); | |
198 canvas.drawPaint(cursorPaint); | |
199 final int deadKey = bridge.getKeyHandler().getDeadKey(); | |
200 | |
201 if (deadKey != 0) { | |
202 canvas.drawText(new char[] { (char)deadKey }, 0, 1, 0, 0, cursorStrokePaint); | |
203 } | |
204 | |
205 // Make sure we scale our decorations to the correct size. | |
206 canvas.concat(scaleMatrix); | |
207 int metaState = bridge.getKeyHandler().getMetaState(); | |
208 | |
209 if ((metaState & TerminalKeyListener.META_SHIFT_ON) != 0) | |
210 canvas.drawPath(shiftCursor, cursorStrokePaint); | |
211 else if ((metaState & TerminalKeyListener.META_SHIFT_LOCK) != 0) | |
212 canvas.drawPath(shiftCursor, cursorPaint); | |
213 | |
214 if ((metaState & TerminalKeyListener.META_ALT_ON) != 0) | |
215 canvas.drawPath(altCursor, cursorStrokePaint); | |
216 else if ((metaState & TerminalKeyListener.META_ALT_LOCK) != 0) | |
217 canvas.drawPath(altCursor, cursorPaint); | |
218 | |
219 if ((metaState & TerminalKeyListener.META_CTRL_ON) != 0) | |
220 canvas.drawPath(ctrlCursor, cursorStrokePaint); | |
221 else if ((metaState & TerminalKeyListener.META_CTRL_LOCK) != 0) | |
222 canvas.drawPath(ctrlCursor, cursorPaint); | |
223 | |
224 // Restore previous clip region | |
225 canvas.restore(); | |
226 } | |
227 | |
228 // draw any highlighted area | |
229 if (bridge.isSelectingForCopy()) { | |
230 SelectionArea area = bridge.getSelectionArea(); | |
231 canvas.save(Canvas.CLIP_SAVE_FLAG); | |
232 canvas.clipRect( | |
233 area.getLeft() * bridge.charWidth, | |
234 area.getTop() * bridge.charHeight, | |
235 (area.getRight() + 1) * bridge.charWidth, | |
236 (area.getBottom() + 1) * bridge.charHeight | |
237 ); | |
238 canvas.drawPaint(cursorPaint); | |
239 canvas.restore(); | |
240 } | |
241 } | |
242 } | |
243 | |
244 public void notifyUser(String message) { | |
245 if (!notifications) | |
246 return; | |
247 | |
248 if (notification != null) { | |
249 // Don't keep telling the user the same thing. | |
250 if (lastNotification != null && lastNotification.equals(message)) | |
251 return; | |
252 | |
253 notification.setText(message); | |
254 notification.show(); | |
255 } | |
256 else { | |
257 notification = Toast.makeText(context, message, Toast.LENGTH_SHORT); | |
258 notification.show(); | |
259 } | |
260 | |
261 lastNotification = message; | |
262 } | |
263 | |
264 /** | |
265 * Ask the {@link TerminalBridge} we're connected to to resize to a specific size. | |
266 * @param width | |
267 * @param height | |
268 */ | |
269 public void forceSize(int width, int height) { | |
270 bridge.resizeComputed(width, height, getWidth(), getHeight()); | |
271 } | |
272 | |
273 /** | |
274 * Sets the ability for the TerminalView to display Toast notifications to the user. | |
275 * @param value whether to enable notifications or not | |
276 */ | |
277 public void setNotifications(boolean value) { | |
278 notifications = value; | |
279 } | |
280 | |
281 @Override | |
282 public boolean onCheckIsTextEditor() { | |
283 return true; | |
284 } | |
285 | |
286 @Override | |
287 public InputConnection onCreateInputConnection(EditorInfo outAttrs) { | |
288 outAttrs.imeOptions |= | |
289 EditorInfo.IME_FLAG_NO_EXTRACT_UI | | |
290 EditorInfo.IME_FLAG_NO_ENTER_ACTION | | |
291 EditorInfo.IME_ACTION_NONE; | |
292 outAttrs.inputType = EditorInfo.TYPE_NULL; | |
293 return new BaseInputConnection(this, false) { | |
294 @Override | |
295 public boolean deleteSurroundingText(int leftLength, int rightLength) { | |
296 if (rightLength == 0 && leftLength == 0) { | |
297 return this.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)); | |
298 } | |
299 | |
300 for (int i = 0; i < leftLength; i++) { | |
301 this.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)); | |
302 } | |
303 | |
304 // TODO: forward delete | |
305 return true; | |
306 } | |
307 }; | |
308 } | |
309 | |
310 public void propagateConsoleText(char[] rawText, int length) { | |
311 if (mAccessibilityActive) { | |
312 synchronized (mAccessibilityLock) { | |
313 mAccessibilityBuffer.append(rawText, 0, length); | |
314 } | |
315 | |
316 if (mAccessibilityInitialized) { | |
317 if (mEventSender != null) { | |
318 removeCallbacks(mEventSender); | |
319 } | |
320 else { | |
321 mEventSender = new AccessibilityEventSender(); | |
322 } | |
323 | |
324 postDelayed(mEventSender, ACCESSIBILITY_EVENT_THRESHOLD); | |
325 } | |
326 } | |
327 } | |
328 | |
329 private class AccessibilityEventSender implements Runnable { | |
330 public void run() { | |
331 synchronized (mAccessibilityLock) { | |
332 if (mCodeMatcher == null) { | |
333 mCodeMatcher = mControlCodes.matcher(mAccessibilityBuffer); | |
334 } | |
335 else { | |
336 mCodeMatcher.reset(mAccessibilityBuffer); | |
337 } | |
338 | |
339 // Strip all control codes out. | |
340 mAccessibilityBuffer = new StringBuffer(mCodeMatcher.replaceAll(" ")); | |
341 // Apply Backspaces using backspace character sequence | |
342 int i = mAccessibilityBuffer.indexOf(BACKSPACE_CODE); | |
343 | |
344 while (i != -1) { | |
345 mAccessibilityBuffer = mAccessibilityBuffer.replace(i == 0 ? 0 : i - 1, i | |
346 + BACKSPACE_CODE.length(), ""); | |
347 i = mAccessibilityBuffer.indexOf(BACKSPACE_CODE); | |
348 } | |
349 | |
350 if (mAccessibilityBuffer.length() > 0) { | |
351 AccessibilityEvent event = AccessibilityEvent.obtain( | |
352 AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED); | |
353 event.setFromIndex(0); | |
354 event.setAddedCount(mAccessibilityBuffer.length()); | |
355 event.getText().add(mAccessibilityBuffer); | |
356 sendAccessibilityEventUnchecked(event); | |
357 mAccessibilityBuffer.setLength(0); | |
358 } | |
359 } | |
360 } | |
361 } | |
362 | |
363 private class AccessibilityStateTester extends AsyncTask<Void, Void, Boolean> { | |
364 @Override | |
365 protected Boolean doInBackground(Void... params) { | |
366 /* | |
367 * Presumably if the accessibility manager is not enabled, we don't | |
368 * need to send accessibility events. | |
369 */ | |
370 final AccessibilityManager accessibility = (AccessibilityManager) context | |
371 .getSystemService(Context.ACCESSIBILITY_SERVICE); | |
372 | |
373 if (!accessibility.isEnabled()) { | |
374 return false; | |
375 } | |
376 | |
377 /* | |
378 * Restrict the set of intents to only accessibility services that | |
379 * have the category FEEDBACK_SPOKEN (aka, screen readers). | |
380 */ | |
381 final Intent screenReaderIntent = new Intent(SCREENREADER_INTENT_ACTION); | |
382 screenReaderIntent.addCategory(SCREENREADER_INTENT_CATEGORY); | |
383 final ContentResolver cr = context.getContentResolver(); | |
384 final List<ResolveInfo> screenReaders = context.getPackageManager().queryIntentServices( | |
385 screenReaderIntent, 0); | |
386 boolean foundScreenReader = false; | |
387 final int N = screenReaders.size(); | |
388 | |
389 for (int i = 0; i < N; i++) { | |
390 final ResolveInfo screenReader = screenReaders.get(i); | |
391 /* | |
392 * All screen readers are expected to implement a content | |
393 * provider that responds to: | |
394 * content://<nameofpackage>.providers.StatusProvider | |
395 */ | |
396 final Cursor cursor = cr.query( | |
397 Uri.parse("content://" + screenReader.serviceInfo.packageName | |
398 + ".providers.StatusProvider"), null, null, null, null); | |
399 | |
400 if (cursor != null && cursor.moveToFirst()) { | |
401 /* | |
402 * These content providers use a special cursor that only has | |
403 * one element, an integer that is 1 if the screen reader is | |
404 * running. | |
405 */ | |
406 final int status = cursor.getInt(0); | |
407 cursor.close(); | |
408 | |
409 if (status == 1) { | |
410 foundScreenReader = true; | |
411 break; | |
412 } | |
413 } | |
414 } | |
415 | |
416 if (foundScreenReader) { | |
417 mControlCodes = Pattern.compile(CONTROL_CODE_PATTERN); | |
418 } | |
419 | |
420 return foundScreenReader; | |
421 } | |
422 | |
423 @Override | |
424 protected void onPostExecute(Boolean result) { | |
425 mAccessibilityActive = result; | |
426 mAccessibilityInitialized = true; | |
427 | |
428 if (result) { | |
429 mEventSender = new AccessibilityEventSender(); | |
430 postDelayed(mEventSender, ACCESSIBILITY_EVENT_THRESHOLD); | |
431 } | |
432 else { | |
433 mAccessibilityBuffer = null; | |
434 } | |
435 } | |
436 } | |
437 | |
438 private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { | |
439 @Override | |
440 public boolean onScale(ScaleGestureDetector detector) { | |
441 float mScaleFactor = detector.getScaleFactor(); | |
442 | |
443 if (mScaleFactor > 1.1) { | |
444 bridge.increaseFontSize(); | |
445 return true; | |
446 } | |
447 else if (mScaleFactor < 0.9) { | |
448 bridge.decreaseFontSize(); | |
449 return true; | |
450 } | |
451 | |
452 return (false); | |
453 } | |
454 } | |
455 | |
456 } |