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.View;
14 import android.view.inputmethod.InputMethodManager;
15 import android.widget.ImageButton;
17 import org.chromium.chromoting.jni.JniInterface;
20 * A simple screen that does nothing except display a DesktopView and notify it of rotations.
22 public class Desktop extends Activity {
23 /** The surface that displays the remote host's desktop feed. */
24 private DesktopView mRemoteHostDesktop;
26 /** The button used to show the action bar. */
27 private ImageButton mOverlayButton;
29 /** Called when the activity is first created. */
31 public void onCreate(Bundle savedInstanceState) {
32 super.onCreate(savedInstanceState);
33 setContentView(R.layout.desktop);
34 mRemoteHostDesktop = (DesktopView)findViewById(R.id.desktop_view);
35 mOverlayButton = (ImageButton)findViewById(R.id.desktop_overlay_button);
36 mRemoteHostDesktop.setDesktop(this);
38 // Ensure the button is initially hidden.
42 /** Called when the activity is finally finished. */
44 public void onDestroy() {
46 JniInterface.disconnectFromHost();
49 /** Called when the display is rotated (as registered in the manifest). */
51 public void onConfigurationChanged(Configuration newConfig) {
52 super.onConfigurationChanged(newConfig);
53 mRemoteHostDesktop.onScreenConfigurationChanged();
56 /** Called to initialize the action bar. */
58 public boolean onCreateOptionsMenu(Menu menu) {
59 getMenuInflater().inflate(R.menu.desktop_actionbar, menu);
60 return super.onCreateOptionsMenu(menu);
63 public void showActionBar() {
64 mOverlayButton.setVisibility(View.INVISIBLE);
65 getActionBar().show();
68 public void hideActionBar() {
69 mOverlayButton.setVisibility(View.VISIBLE);
70 getActionBar().hide();
73 /** The overlay button's onClick handler. */
74 public void onOverlayButtonPressed(View view) {
78 /** Called whenever an action bar button is pressed. */
80 public boolean onOptionsItemSelected(MenuItem item) {
81 switch (item.getItemId()) {
82 case R.id.actionbar_keyboard:
83 ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).toggleSoftInput(0, 0);
86 case R.id.actionbar_hide:
90 case R.id.actionbar_disconnect:
91 JniInterface.disconnectFromHost();
94 case R.id.actionbar_send_ctrl_alt_del:
97 KeyEvent.KEYCODE_CTRL_LEFT,
98 KeyEvent.KEYCODE_ALT_LEFT,
99 KeyEvent.KEYCODE_FORWARD_DEL,
101 for (int key : keys) {
102 JniInterface.keyboardAction(key, true);
104 for (int key : keys) {
105 JniInterface.keyboardAction(key, false);
111 return super.onOptionsItemSelected(item);
116 * Called once when a keyboard key is pressed, then again when that same key is released. This
117 * is not guaranteed to be notified of all soft keyboard events: certian keyboards might not
118 * call it at all, while others might skip it in certain situations (e.g. swipe input).
121 public boolean dispatchKeyEvent(KeyEvent event) {
122 boolean depressed = event.getAction() == KeyEvent.ACTION_DOWN;
124 switch (event.getKeyCode()) {
125 case KeyEvent.KEYCODE_AT:
126 JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
127 JniInterface.keyboardAction(KeyEvent.KEYCODE_2, depressed);
130 case KeyEvent.KEYCODE_POUND:
131 JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
132 JniInterface.keyboardAction(KeyEvent.KEYCODE_3, depressed);
135 case KeyEvent.KEYCODE_STAR:
136 JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
137 JniInterface.keyboardAction(KeyEvent.KEYCODE_8, depressed);
140 case KeyEvent.KEYCODE_PLUS:
141 JniInterface.keyboardAction(KeyEvent.KEYCODE_SHIFT_LEFT, depressed);
142 JniInterface.keyboardAction(KeyEvent.KEYCODE_EQUALS, depressed);
146 // We try to send all other key codes to the host directly.
147 JniInterface.keyboardAction(event.getKeyCode(), depressed);
150 return super.dispatchKeyEvent(event);