| 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.chromoting; |
| 6 | |
| 7 | import android.app.Activity; |
| 8 | import android.content.res.Configuration; |
| 9 | import android.os.Bundle; |
| 10 | import android.util.Log; |
| 11 | import android.view.KeyEvent; |
| 12 | import android.view.Menu; |
| 13 | import android.view.MenuItem; |
| 14 | import android.view.WindowManager; |
| 15 | import android.view.inputmethod.InputMethodManager; |
| 16 | |
| 17 | import org.chromium.chromoting.jni.JniInterface; |
| 18 | |
| 19 | /** |
| 20 | * A simple screen that does nothing except display a DesktopView and notify it of rotations. |
| 21 | */ |
| 22 | public class Desktop extends Activity { |
| 23 | /** The surface that displays the remote host's desktop feed. */ |
| 24 | private DesktopView remoteHostDesktop; |
| 25 | |
| 26 | /** Called when the activity is first created. */ |
| 27 | @Override |
| 28 | public void onCreate(Bundle savedInstanceState) { |
| 29 | super.onCreate(savedInstanceState); |
| 30 | remoteHostDesktop = new DesktopView(this); |
| 31 | setContentView(remoteHostDesktop); |
| 32 | } |
| 33 | |
| 34 | /** Called when the activity is finally finished. */ |
| 35 | @Override |
| 36 | public void onDestroy() { |
| 37 | super.onDestroy(); |
| 38 | JniInterface.disconnectFromHost(); |
| 39 | } |
| 40 | |
| 41 | /** Called when the display is rotated (as registered in the manifest). */ |
| 42 | @Override |
| 43 | public void onConfigurationChanged(Configuration newConfig) { |
| 44 | super.onConfigurationChanged(newConfig); |
| 45 | remoteHostDesktop.requestRecheckConstrainingDimension(); |
| 46 | } |
| 47 | |
| 48 | /** Called to initialize the action bar. */ |
| 49 | @Override |
| 50 | public boolean onCreateOptionsMenu(Menu menu) { |
| 51 | getMenuInflater().inflate(R.menu.desktop_actionbar, menu); |
| 52 | return super.onCreateOptionsMenu(menu); |
| 53 | } |
| 54 | |
| 55 | /** Called whenever an action bar button is pressed. */ |
| 56 | @Override |
| 57 | public boolean onOptionsItemSelected(MenuItem item) { |
| 58 | switch (item.getItemId()) { |
| 59 | case R.id.actionbar_keyboard: |
| 60 | ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0, 0); |
| 61 | return true; |
| 62 | case R.id.actionbar_hide: |
| 63 | getActionBar().hide(); |
| 64 | return true; |
| 65 | default: |
| 66 | return super.onOptionsItemSelected(item); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Called once when a keyboard key is pressed, then again when that same key is released. This |
| 72 | * is not guaranteed to be notified of all soft keyboard events: certian keyboards might not |
| 73 | * call it at all, while others might skip it in certain situations (e.g. swipe input). |
| 74 | */ |
| 75 | @Override |
| 76 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 77 | boolean depressed = event.getAction() == KeyEvent.ACTION_DOWN; |
| 78 | |
| 79 | switch (event.getKeyCode()) { |
| 80 | case KeyEvent.KEYCODE_AT: |
| 81 | JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed); |
| 82 | JniInterface.keyboardAction(KeyEvent.KEYCODE_2, depressed); |
| 83 | break; |
| 84 | case KeyEvent.KEYCODE_POUND: |
| 85 | JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed); |
| 86 | JniInterface.keyboardAction(KeyEvent.KEYCODE_3, depressed); |
| 87 | break; |
| 88 | case KeyEvent.KEYCODE_STAR: |
| 89 | JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed); |
| 90 | JniInterface.keyboardAction(KeyEvent.KEYCODE_8, depressed); |
| 91 | break; |
| 92 | case KeyEvent.KEYCODE_PLUS: |
| 93 | JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed); |
| 94 | JniInterface.keyboardAction(KeyEvent.KEYCODE_EQUALS, depressed); |
| 95 | break; |
| 96 | default: |
| 97 | // We try to send all other key codes to the host directly. |
| 98 | JniInterface.keyboardAction(event.getKeyCode(), depressed); |
| 99 | } |
| 100 | |
| 101 | return super.dispatchKeyEvent(event); |
| 102 | } |
| 103 | } |