| @@ -0,0 +1,437 @@ |
| 1 | +package org.echoline.drawterm; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.SharedPreferences; |
| 5 | +import android.content.pm.ActivityInfo; |
| 6 | +import android.content.res.Resources; |
| 7 | +import android.graphics.Point; |
| 8 | +import android.os.Bundle; |
| 9 | +import android.os.Environment; |
| 10 | + |
| 11 | +import android.app.Activity; |
| 12 | + |
| 13 | +import android.app.Notification; |
| 14 | +import android.app.NotificationManager; |
| 15 | + |
| 16 | +import android.util.DisplayMetrics; |
| 17 | +import android.util.Log; |
| 18 | +import android.view.Display; |
| 19 | +import android.view.MotionEvent; |
| 20 | +import android.view.View; |
| 21 | +import android.view.Menu; |
| 22 | +import android.view.MenuItem; |
| 23 | +import android.view.WindowManager; |
| 24 | +import android.view.Surface; |
| 25 | +import android.view.inputmethod.InputMethodManager; |
| 26 | +import android.view.KeyEvent; |
| 27 | +import android.widget.ArrayAdapter; |
| 28 | +import android.widget.Button; |
| 29 | +import android.widget.EditText; |
| 30 | +import android.widget.LinearLayout; |
| 31 | +import android.widget.ListView; |
| 32 | +import android.widget.TextView; |
| 33 | +import android.content.ClipData; |
| 34 | +import android.content.ClipboardManager; |
| 35 | +import android.hardware.camera2.CameraManager; |
| 36 | +import android.hardware.camera2.CameraAccessException; |
| 37 | +import android.hardware.camera2.CameraDevice; |
| 38 | +import android.hardware.camera2.CaptureRequest; |
| 39 | +import android.hardware.camera2.CameraMetadata; |
| 40 | +import android.hardware.camera2.CameraCaptureSession; |
| 41 | +import android.hardware.camera2.TotalCaptureResult; |
| 42 | +import android.media.Image; |
| 43 | +import android.media.ImageReader; |
| 44 | +import android.graphics.ImageFormat; |
| 45 | +import android.os.Handler; |
| 46 | +import android.os.HandlerThread; |
| 47 | + |
| 48 | +import java.io.File; |
| 49 | +import java.util.Map; |
| 50 | +import java.nio.ByteBuffer; |
| 51 | +import java.util.List; |
| 52 | +import java.util.ArrayList; |
| 53 | + |
| 54 | +public class MainActivity extends Activity { |
| 55 | + private Map<String, ?> map; |
| 56 | + private MainActivity mainActivity; |
| 57 | + private boolean dtrunning = false; |
| 58 | + private DrawTermThread dthread; |
| 59 | + private int notificationId; |
| 60 | + private CameraDevice cameraDevice = null; |
| 61 | + private byte []jpegBytes; |
| 62 | + |
| 63 | + static { |
| 64 | + System.loadLibrary("drawterm"); |
| 65 | + } |
| 66 | + |
| 67 | + public void showNotification(String text) { |
| 68 | + Notification.Builder builder = new Notification.Builder(this) |
| 69 | + .setDefaults(Notification.DEFAULT_SOUND) |
| 70 | + .setSmallIcon(R.drawable.ic_small) |
| 71 | + .setContentText(text) |
| 72 | + .setStyle(new Notification.BigTextStyle().bigText(text)) |
| 73 | + .setPriority(Notification.PRIORITY_DEFAULT); |
| 74 | + |
| 75 | + ((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(notificationId, builder.build()); |
| 76 | + notificationId++; |
| 77 | + } |
| 78 | + |
| 79 | + public int numCameras() { |
| 80 | + try { |
| 81 | + return ((CameraManager)getSystemService(Context.CAMERA_SERVICE)).getCameraIdList().length; |
| 82 | + } catch (CameraAccessException e) { |
| 83 | + Log.w("drawterm", e.toString()); |
| 84 | + return 0; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public void takePicture(int id) { |
| 89 | + try { |
| 90 | + HandlerThread mBackgroundThread = new HandlerThread("Camera Background"); |
| 91 | + mBackgroundThread.start(); |
| 92 | + Handler mBackgroundHandler = new Handler(mBackgroundThread.getLooper()); |
| 93 | + CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE); |
| 94 | + String []cameraIdList = manager.getCameraIdList(); |
| 95 | + manager.openCamera(cameraIdList[id], new CameraDevice.StateCallback() { |
| 96 | + public void onOpened(CameraDevice device) { |
| 97 | + cameraDevice = device; |
| 98 | + } |
| 99 | + public void onDisconnected(CameraDevice device) { |
| 100 | + if (cameraDevice != null) |
| 101 | + cameraDevice.close(); |
| 102 | + cameraDevice = null; |
| 103 | + } |
| 104 | + public void onError(CameraDevice device, int error) { |
| 105 | + if (cameraDevice != null) |
| 106 | + cameraDevice.close(); |
| 107 | + cameraDevice = null; |
| 108 | + } |
| 109 | + }, mBackgroundHandler); |
| 110 | + ImageReader reader = ImageReader.newInstance(640, 480, ImageFormat.JPEG, 1); |
| 111 | + CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG); |
| 112 | + captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); |
| 113 | + captureBuilder.set(CaptureRequest.CONTROL_AWB_MODE, CameraMetadata.CONTROL_AWB_MODE_AUTO); |
| 114 | + captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AE_MODE_ON); |
| 115 | + captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getWindowManager().getDefaultDisplay().getRotation()); |
| 116 | + captureBuilder.addTarget(reader.getSurface()); |
| 117 | + reader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() { |
| 118 | + public void onImageAvailable(ImageReader reader) { |
| 119 | + Image image = null; |
| 120 | + try { |
| 121 | + image = reader.acquireLatestImage(); |
| 122 | + ByteBuffer buffer = image.getPlanes()[0].getBuffer(); |
| 123 | + jpegBytes = new byte[buffer.capacity()]; |
| 124 | + buffer.get(jpegBytes); |
| 125 | + } catch (Exception e) { |
| 126 | + Log.w("drawterm", e.toString()); |
| 127 | + } finally { |
| 128 | + if (image != null) { |
| 129 | + image.close(); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + }, mBackgroundHandler); |
| 134 | + List<Surface> outputSurfaces = new ArrayList<Surface>(1); |
| 135 | + outputSurfaces.add(reader.getSurface()); |
| 136 | + cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() { |
| 137 | + public void onConfigured(CameraCaptureSession session) { |
| 138 | + try { |
| 139 | + List<CaptureRequest> captureRequests = new ArrayList<CaptureRequest>(10); |
| 140 | + for (int i = 0; i < 10; i++) |
| 141 | + captureRequests.add(captureBuilder.build()); |
| 142 | + session.captureBurst(captureRequests, new CameraCaptureSession.CaptureCallback() { |
| 143 | + public void onCaptureSequenceCompleted(CameraCaptureSession session, int sequenceId, long frameNumber) { |
| 144 | + try { |
| 145 | + sendPicture(jpegBytes); |
| 146 | + mBackgroundThread.quitSafely(); |
| 147 | + mBackgroundThread.join(); |
| 148 | + } catch (Exception e) { |
| 149 | + Log.w("drawterm", e.toString()); |
| 150 | + } |
| 151 | + } |
| 152 | + }, mBackgroundHandler); |
| 153 | + } catch (CameraAccessException e) { |
| 154 | + e.printStackTrace(); |
| 155 | + } |
| 156 | + } |
| 157 | + public void onConfigureFailed(CameraCaptureSession session) { |
| 158 | + } |
| 159 | + }, mBackgroundHandler); |
| 160 | + } catch (Exception e) { |
| 161 | + e.printStackTrace(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + public void serverView(View v) { |
| 166 | + setContentView(R.layout.server_main); |
| 167 | + serverButtons(); |
| 168 | + |
| 169 | + String s = (String)map.get(((TextView)v).getText().toString()); |
| 170 | + String []a = s.split("\007"); |
| 171 | + |
| 172 | + ((EditText)findViewById(R.id.cpuServer)).setText((String)a[0]); |
| 173 | + ((EditText)findViewById(R.id.authServer)).setText((String)a[1]); |
| 174 | + ((EditText)findViewById(R.id.userName)).setText((String)a[2]); |
| 175 | + if (a.length > 3) |
| 176 | + ((EditText)findViewById(R.id.passWord)).setText((String)a[3]); |
| 177 | + } |
| 178 | + |
| 179 | + public void populateServers(Context context) { |
| 180 | + ListView ll = (ListView)findViewById(R.id.servers); |
| 181 | + ArrayAdapter<String> la = new ArrayAdapter<String>(this, R.layout.item_main); |
| 182 | + SharedPreferences settings = getSharedPreferences("DrawtermPrefs", 0); |
| 183 | + map = (Map<String, ?>)settings.getAll(); |
| 184 | + String key; |
| 185 | + Object []keys = map.keySet().toArray(); |
| 186 | + for (int i = 0; i < keys.length; i++) { |
| 187 | + key = (String)keys[i]; |
| 188 | + la.add(key); |
| 189 | + } |
| 190 | + ll.setAdapter(la); |
| 191 | + |
| 192 | + setDTSurface(null); |
| 193 | + dtrunning = false; |
| 194 | + } |
| 195 | + |
| 196 | + public void runDrawterm(String []args, String pass) { |
| 197 | + Resources res = getResources(); |
| 198 | + DisplayMetrics dm = res.getDisplayMetrics(); |
| 199 | + |
| 200 | + int wp = dm.widthPixels; |
| 201 | + int hp = dm.heightPixels; |
| 202 | + |
| 203 | + setContentView(R.layout.drawterm_main); |
| 204 | + |
| 205 | + Button kbutton = (Button)findViewById(R.id.keyboardToggle); |
| 206 | + kbutton.setOnClickListener(new View.OnClickListener() { |
| 207 | + @Override |
| 208 | + public void onClick(final View view) { |
| 209 | + InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); |
| 210 | + imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0); |
| 211 | + } |
| 212 | + }); |
| 213 | + |
| 214 | + int rid = res.getIdentifier("navigation_bar_height", "dimen", "android"); |
| 215 | + if (rid > 0) { |
| 216 | + hp -= res.getDimensionPixelSize(rid); |
| 217 | + } |
| 218 | + LinearLayout ll = (LinearLayout)findViewById(R.id.dtButtons); |
| 219 | + hp -= ll.getHeight(); |
| 220 | + |
| 221 | + int w = (int)(wp * (160.0/dm.xdpi)); |
| 222 | + int h = (int)(hp * (160.0/dm.ydpi)); |
| 223 | + float ws = (float)wp/w; |
| 224 | + float hs = (float)hp/h; |
| 225 | + // only scale up |
| 226 | + if (ws < 1) { |
| 227 | + ws = 1; |
| 228 | + w = wp; |
| 229 | + } |
| 230 | + if (hs < 1) { |
| 231 | + hs = 1; |
| 232 | + h = hp; |
| 233 | + } |
| 234 | + |
| 235 | + MySurfaceView mView = new MySurfaceView(mainActivity, w, h, ws, hs); |
| 236 | + mView.getHolder().setFixedSize(w, h); |
| 237 | + |
| 238 | + LinearLayout l = (LinearLayout)findViewById(R.id.dlayout); |
| 239 | + l.addView(mView, 1, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); |
| 240 | + |
| 241 | + dthread = new DrawTermThread(args, pass, mainActivity); |
| 242 | + dthread.start(); |
| 243 | + |
| 244 | + dtrunning = true; |
| 245 | + } |
| 246 | + |
| 247 | + public void serverButtons() { |
| 248 | + Button button = (Button)findViewById(R.id.save); |
| 249 | + button.setOnClickListener(new View.OnClickListener() { |
| 250 | + @Override |
| 251 | + public void onClick(View v) { |
| 252 | + String cpu = ((EditText)findViewById(R.id.cpuServer)).getText().toString(); |
| 253 | + String auth = ((EditText)findViewById(R.id.authServer)).getText().toString(); |
| 254 | + String user = ((EditText)findViewById(R.id.userName)).getText().toString(); |
| 255 | + String pass = ((EditText)findViewById(R.id.passWord)).getText().toString(); |
| 256 | + |
| 257 | + SharedPreferences settings = getSharedPreferences("DrawtermPrefs", 0); |
| 258 | + SharedPreferences.Editor editor = settings.edit(); |
| 259 | + editor.putString(user + "@" + cpu + " (auth=" + auth + ")", cpu + "\007" + auth + "\007" + user + "\007" + pass); |
| 260 | + editor.commit(); |
| 261 | + } |
| 262 | + }); |
| 263 | + |
| 264 | + button = (Button) findViewById(R.id.connect); |
| 265 | + button.setOnClickListener(new View.OnClickListener() { |
| 266 | + @Override |
| 267 | + public void onClick(final View view) { |
| 268 | + String cpu = ((EditText)findViewById(R.id.cpuServer)).getText().toString(); |
| 269 | + String auth = ((EditText)findViewById(R.id.authServer)).getText().toString(); |
| 270 | + String user = ((EditText)findViewById(R.id.userName)).getText().toString(); |
| 271 | + String pass = ((EditText)findViewById(R.id.passWord)).getText().toString(); |
| 272 | + |
| 273 | + String args[] = {"drawterm", "-p", "-h", cpu, "-a", auth, "-u", user}; |
| 274 | + runDrawterm(args, pass); |
| 275 | + } |
| 276 | + }); |
| 277 | + } |
| 278 | + |
| 279 | + @Override |
| 280 | + protected void onCreate(Bundle savedInstanceState) { |
| 281 | + super.onCreate(savedInstanceState); |
| 282 | + setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); |
| 283 | + getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 284 | + |
| 285 | + mainActivity = this; |
| 286 | + setObject(); |
| 287 | + setContentView(R.layout.activity_main); |
| 288 | + populateServers(this); |
| 289 | + |
| 290 | + View fab = findViewById(R.id.fab); |
| 291 | + fab.setOnClickListener(new View.OnClickListener() { |
| 292 | + @Override |
| 293 | + public void onClick(View v) { |
| 294 | + setContentView(R.layout.server_main); |
| 295 | + serverButtons(); |
| 296 | + } |
| 297 | + }); |
| 298 | + } |
| 299 | + |
| 300 | + @Override |
| 301 | + public boolean dispatchKeyEvent(KeyEvent event) |
| 302 | + { |
| 303 | + if (!dtrunning) { |
| 304 | + return super.dispatchKeyEvent(event); |
| 305 | + } |
| 306 | + |
| 307 | + int k = event.getUnicodeChar(); |
| 308 | + if (k == 0) { |
| 309 | + k = event.getDisplayLabel(); |
| 310 | + if (k >= 'A' && k <= 'Z') |
| 311 | + k |= 0x20; |
| 312 | + } |
| 313 | + String chars = event.getCharacters(); |
| 314 | + if (k == 0 && chars != null) { |
| 315 | + for (int i = 0; i < chars.length(); i++) { |
| 316 | + k = chars.codePointAt(i); |
| 317 | + keyDown(k); |
| 318 | + keyUp(k); |
| 319 | + } |
| 320 | + return true; |
| 321 | + } |
| 322 | + |
| 323 | + if (k == 0) switch (event.getKeyCode()) { |
| 324 | + case KeyEvent.KEYCODE_DEL: |
| 325 | + k = 0x0008; |
| 326 | + break; |
| 327 | + case KeyEvent.KEYCODE_FORWARD_DEL: |
| 328 | + k = 0x007F; |
| 329 | + break; |
| 330 | + case KeyEvent.KEYCODE_ESCAPE: |
| 331 | + k = 0x001B; |
| 332 | + break; |
| 333 | + case KeyEvent.KEYCODE_MOVE_HOME: |
| 334 | + k = 0xF00D; |
| 335 | + break; |
| 336 | + case KeyEvent.KEYCODE_MOVE_END: |
| 337 | + k = 0xF018; |
| 338 | + break; |
| 339 | + case KeyEvent.KEYCODE_PAGE_UP: |
| 340 | + k = 0xF00F; |
| 341 | + break; |
| 342 | + case KeyEvent.KEYCODE_PAGE_DOWN: |
| 343 | + k = 0xF013; |
| 344 | + break; |
| 345 | + case KeyEvent.KEYCODE_INSERT: |
| 346 | + k = 0xF014; |
| 347 | + break; |
| 348 | + case KeyEvent.KEYCODE_SYSRQ: |
| 349 | + k = 0xF010; |
| 350 | + break; |
| 351 | + case KeyEvent.KEYCODE_DPAD_UP: |
| 352 | + k = 0xF00E; |
| 353 | + break; |
| 354 | + case KeyEvent.KEYCODE_DPAD_LEFT: |
| 355 | + k = 0xF011; |
| 356 | + break; |
| 357 | + case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 358 | + k = 0xF012; |
| 359 | + break; |
| 360 | + case KeyEvent.KEYCODE_DPAD_DOWN: |
| 361 | + k = 0xF800; |
| 362 | + break; |
| 363 | + } |
| 364 | + |
| 365 | + if (k == 0) |
| 366 | + return true; |
| 367 | + |
| 368 | + if (event.isCtrlPressed()) { |
| 369 | + keyDown(0xF017); |
| 370 | + } |
| 371 | + if (event.isAltPressed() && k < 128) { |
| 372 | + keyDown(0xF015); |
| 373 | + } |
| 374 | + |
| 375 | + if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| 376 | + keyDown(k); |
| 377 | + } |
| 378 | + else if (event.getAction() == KeyEvent.ACTION_UP) { |
| 379 | + keyUp(k); |
| 380 | + } |
| 381 | + |
| 382 | + if (event.isCtrlPressed()) { |
| 383 | + keyUp(0xF017); |
| 384 | + } |
| 385 | + if (event.isAltPressed() && k < 128) { |
| 386 | + keyUp(0xF015); |
| 387 | + } |
| 388 | + |
| 389 | + return true; |
| 390 | + } |
| 391 | + |
| 392 | + @Override |
| 393 | + public void onBackPressed() |
| 394 | + { |
| 395 | + } |
| 396 | + |
| 397 | + @Override |
| 398 | + public void onDestroy() |
| 399 | + { |
| 400 | + setDTSurface(null); |
| 401 | + dtrunning = false; |
| 402 | + exitDT(); |
| 403 | + super.onDestroy(); |
| 404 | + } |
| 405 | + |
| 406 | + public void setClipBoard(String str) { |
| 407 | + ClipboardManager cm = (ClipboardManager)getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE); |
| 408 | + if (cm != null) { |
| 409 | + ClipData cd = ClipData.newPlainText(null, str); |
| 410 | + cm.setPrimaryClip(cd); |
| 411 | + } |
| 412 | + } |
| 413 | + |
| 414 | + public String getClipBoard() { |
| 415 | + ClipboardManager cm = (ClipboardManager)getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE); |
| 416 | + if (cm != null) { |
| 417 | + ClipData cd = cm.getPrimaryClip(); |
| 418 | + if (cd != null) |
| 419 | + return (String)(cd.getItemAt(0).coerceToText(mainActivity.getApplicationContext()).toString()); |
| 420 | + } |
| 421 | + return ""; |
| 422 | + } |
| 423 | + |
| 424 | + public native void dtmain(Object[] args); |
| 425 | + public native void setPass(String arg); |
| 426 | + public native void setWidth(int arg); |
| 427 | + public native void setHeight(int arg); |
| 428 | + public native void setWidthScale(float arg); |
| 429 | + public native void setHeightScale(float arg); |
| 430 | + public native void setDTSurface(Surface surface); |
| 431 | + public native void setMouse(int[] args); |
| 432 | + public native void setObject(); |
| 433 | + public native void keyDown(int c); |
| 434 | + public native void keyUp(int c); |
| 435 | + public native void exitDT(); |
| 436 | + public native void sendPicture(byte[] array); |
| 437 | +} |