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.
5 package org.chromium.chromoting;
7 import android.app.Activity;
8 import android.content.res.Configuration;
9 import android.os.Bundle;
10 import android.view.KeyEvent;
11 import android.view.Menu;
12 import android.view.MenuItem;
13 import android.view.inputmethod.InputMethodManager;
15 import org.chromium.chromoting.jni.JniInterface;
18 * A simple screen that does nothing except display a DesktopView and notify it of rotations.
20 public class Desktop extends Activity {
21 /** The surface that displays the remote host's desktop feed. */
22 private DesktopView remoteHostDesktop;
24 /** Called when the activity is first created. */
26 public void onCreate(Bundle savedInstanceState) {
27 super.onCreate(savedInstanceState);
28 remoteHostDesktop = new DesktopView(this);
29 setContentView(remoteHostDesktop);
32 /** Called when the activity is finally finished. */
34 public void onDestroy() {
36 JniInterface.disconnectFromHost();
39 /** Called when the display is rotated (as registered in the manifest). */
41 public void onConfigurationChanged(Configuration newConfig) {
42 super.onConfigurationChanged(newConfig);
43 remoteHostDesktop.onScreenConfigurationChanged();
46 /** Called to initialize the action bar. */
48 public boolean onCreateOptionsMenu(Menu menu) {
49 getMenuInflater().inflate(R.menu.desktop_actionbar, menu);
50 return super.onCreateOptionsMenu(menu);
53 /** Called whenever an action bar button is pressed. */
55 public boolean onOptionsItemSelected(MenuItem item) {
56 switch (item.getItemId()) {
57 case R.id.actionbar_keyboard:
58 ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0, 0);
60 case R.id.actionbar_hide:
61 getActionBar().hide();
63 case R.id.actionbar_disconnect:
64 JniInterface.disconnectFromHost();
66 case R.id.actionbar_send_ctrl_alt_del:
69 KeyEvent.KEYCODE_CTRL_LEFT,
70 KeyEvent.KEYCODE_ALT_LEFT,
71 KeyEvent.KEYCODE_FORWARD_DEL,
73 for (int key : keys) {
74 JniInterface.keyboardAction(key, true);
76 for (int key : keys) {
77 JniInterface.keyboardAction(key, false);
82 return super.onOptionsItemSelected(item);
87 * Called once when a keyboard key is pressed, then again when that same key is released. This
88 * is not guaranteed to be notified of all soft keyboard events: certian keyboards might not
89 * call it at all, while others might skip it in certain situations (e.g. swipe input).
92 public boolean dispatchKeyEvent(KeyEvent event) {
93 boolean depressed = event.getAction() == KeyEvent.ACTION_DOWN;
95 switch (event.getKeyCode()) {
96 case KeyEvent.KEYCODE_AT:
97 JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
98 JniInterface.keyboardAction(KeyEvent.KEYCODE_2, depressed);
100 case KeyEvent.KEYCODE_POUND:
101 JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
102 JniInterface.keyboardAction(KeyEvent.KEYCODE_3, depressed);
104 case KeyEvent.KEYCODE_STAR:
105 JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
106 JniInterface.keyboardAction(KeyEvent.KEYCODE_8, depressed);
108 case KeyEvent.KEYCODE_PLUS:
109 JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
110 JniInterface.keyboardAction(KeyEvent.KEYCODE_EQUALS, depressed);
113 // We try to send all other key codes to the host directly.
114 JniInterface.keyboardAction(event.getKeyCode(), depressed);
117 return super.dispatchKeyEvent(event);